Skip to content

Commit

Permalink
updating export data action
Browse files Browse the repository at this point in the history
  • Loading branch information
jvigliotta committed Apr 24, 2024
1 parent 9de99c1 commit 53a37ca
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 188 deletions.
15 changes: 13 additions & 2 deletions src/AMMOSPlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ define([
'./mcwsIndicator/plugin',
'./multipleHistoricalSessions/plugin',
'./realtimeSessions/plugin',
'./globalFilters/plugin'
'./globalFilters/plugin',
'./exportDataAction/plugin'
], function (
DatasetCache,
SessionService,
Expand Down Expand Up @@ -65,7 +66,8 @@ define([
MCWSIndicatorPlugin,
MultipleHistoricalSessions,
RealtimeSessions,
GlobalFilters
GlobalFilters,
ExportDataAction
) {

function AMMOSPlugins(options) {
Expand Down Expand Up @@ -131,6 +133,15 @@ define([
openmct.install(CustomFormsPlugin.default());

openmct.install(openmct.plugins.DefaultRootName('VISTA'));
openmct.install(new ExportDataAction.default([
'telemetry.plot.overlay',
'telemetry.plot.stacked',
'vista.channel',
'vista.channelGroup',
'vista.chanTableGroup',
'vista.evr',
'vista.evrView'
]));
openmct.install(ActionModifiersPlugin.default());
openmct.install(new PacketQueryPlugin.default());
if (options.globalFilters) {
Expand Down
66 changes: 66 additions & 0 deletions src/exportDataAction/ExportDataAction.js
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;
63 changes: 63 additions & 0 deletions src/exportDataAction/ExportDataTask.js
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.
8 changes: 8 additions & 0 deletions src/exportDataAction/plugin.js
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));
};
}

33 changes: 0 additions & 33 deletions src/legacy/export/bundle.js

This file was deleted.

80 changes: 0 additions & 80 deletions src/legacy/export/src/ExportDataAction.js

This file was deleted.

72 changes: 0 additions & 72 deletions src/legacy/export/src/ExportDataTask.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/types/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function TypePlugin() {
openmct.types.addType(type.key, pickedType);
});

openmct.composition.addPolicy(TypeCompositionPolicy);
openmct.composition.addPolicy(new TypeCompositionPolicy().allow);

openmct.telemetry.addFormat({
key: 'vista.invert-realtime-flag',
Expand Down

0 comments on commit 53a37ca

Please sign in to comment.