-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9de99c1
commit 53a37ca
Showing
10 changed files
with
151 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import ExportDataTask from './ExportDataTask'; | ||
|
||
/** | ||
* Implements the "Export Data" action, allowing data for Channels, EVRs, | ||
* or grouped containers of these to be exported as CSV using ES6 class syntax. | ||
* | ||
* @param {openmct} openmct instance | ||
* @memberof vista/export | ||
*/ | ||
class ExportDataAction { | ||
constructor(openmct, validTypes) { | ||
this.name = 'Export Historical Data'; | ||
this.key = 'vista.export'; | ||
this.description = 'Export channel or EVR data as CSV'; | ||
this.cssClass = 'icon-download'; | ||
this.group = 'view'; | ||
this.priority = 1; | ||
this.validTypes = validTypes; | ||
|
||
this._openmct = openmct; | ||
} | ||
|
||
appliesTo(objectPath) { | ||
const domainObject = objectPath[0]; | ||
const isValidType = this.validTypes.includes(domainObject.type); | ||
if (!isValidType) { | ||
return false; | ||
} | ||
|
||
const hasComposition = this._openmct.composition.get(domainObject) !== undefined; | ||
const hasHistoricalTelemetry = this._openmct.telemetry.isTelemetryObject(domainObject) && | ||
!domainObject.telemetry.realtimeOnly; | ||
|
||
return hasHistoricalTelemetry || !hasHistoricalTelemetry && hasComposition; | ||
} | ||
|
||
invoke(objectPath) { | ||
const domainObject = objectPath[0]; | ||
const progressDialog = this._openmct.notifications.progress('Exporting CSV', 'unknown'); | ||
const runTask = (domainObjects) => new ExportDataTask(this._openmct, domainObject.name, domainObjects).invoke(); | ||
const exportData = async (object) => { | ||
if (this._openmct.telemetry.isTelemetryObject(object)) { | ||
runTask([object]); | ||
} else { | ||
const compositionCollection = this._openmct.composition.get(object); | ||
const composition = await compositionCollection.load(); | ||
runTask(composition); | ||
} | ||
} | ||
|
||
const success = (value) => { | ||
progressDialog.dismiss(); | ||
return value; | ||
}; | ||
|
||
const failure = (error) => { | ||
progressDialog.dismiss(); | ||
console.error(error); | ||
this._openmct.notifications.error('Error exporting CSV'); | ||
}; | ||
|
||
return exportData(domainObject).then(success, failure); | ||
} | ||
} | ||
|
||
export default ExportDataAction; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import CSV from 'comma-separated-values'; | ||
import {saveAs} from 'saveAs'; | ||
|
||
export default class ExportDataTask { | ||
/** | ||
* Exports telemetry data to CSV for a group of domain objects. | ||
* Used to support the "Export Data" action. | ||
* @see {vista/export.ExportDataAction} | ||
* @param {DomainObject[]} domainObjects the domain object for which | ||
* telemetry data should be exported | ||
*/ | ||
constructor(openmct, filename, domainObjects) { | ||
this._openmct = openmct; | ||
this.filename = filename; | ||
this.domainObjects = domainObjects; | ||
} | ||
|
||
/** | ||
* Query for telemetry data and export it to CSV. | ||
* @returns {Promise} a promise which will resolve when the export is | ||
* successfully completed, or be rejected if an error occurs | ||
*/ | ||
async invoke() { | ||
const headers = []; | ||
const headerSet = {}; | ||
const requestTelemetry = async (domainObject) => { | ||
const telemetry = await this._openmct.telemetry.request(domainObject); | ||
|
||
return telemetry; | ||
}; | ||
const pullHeaders = (dataArray) => { | ||
dataArray.forEach((data) => { | ||
const datum = data[0] || {}; | ||
Object.keys(datum).forEach((key) => { | ||
if (!headerSet[key]) { | ||
headerSet[key] = true; | ||
headers.push(key); | ||
} | ||
}); | ||
}); | ||
|
||
return dataArray; | ||
}; | ||
const exportAsCSV = (rows) => this.exportCSV(rows, { | ||
headers, | ||
filename: this.filename | ||
}); | ||
const telemetry = await Promise.all(this.domainObjects.map(requestTelemetry)); | ||
pullHeaders(telemetry); | ||
const allTelemetry = telemetry.flat(); | ||
|
||
return exportAsCSV(allTelemetry); | ||
} | ||
|
||
exportCSV(rows, options) { | ||
let headers = (options && options.headers) | ||
|| (Object.keys((rows[0] || {})).sort()); | ||
let filename = `${(options && options.filename) || 'export'}.csv`; | ||
let csvText = new CSV(rows, { header: headers }).encode(); | ||
let blob = new Blob([csvText], { type: "text/csv" }); | ||
saveAs(blob, filename); | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import ExportDataAction from './ExportDataAction'; | ||
|
||
export default function (validTypes) { | ||
return function (openmct) { | ||
openmct.actions.register(new ExportDataAction(openmct, validTypes)); | ||
}; | ||
} | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters