From ba2a6030c2c5acc7a0c7faa7f7a982382303165a Mon Sep 17 00:00:00 2001 From: Scott Bell Date: Thu, 20 Jan 2022 19:23:40 +0100 Subject: [PATCH] Re-implement the event generator as a new-style plugin - Mct4552 (#4620) Updates Event Generator to ES6 classes and current API. Co-authored-by: Andrew Henry Co-authored-by: John Hill --- ...tTelemetry.js => EventMetadataProvider.js} | 80 ++++++------ .../eventGenerator/EventTelemetryProvider.js | 96 ++++++++++++++ example/eventGenerator/bundle.js | 80 ------------ example/eventGenerator/plugin.js | 42 +++++++ example/eventGenerator/pluginSpec.js | 69 ++++++++++ .../src/EventTelemetryProvider.js | 118 ------------------ .../eventGenerator/{data => }/transcript.json | 0 index.html | 1 + indexTest.js | 2 +- .../legacySupport/installDefaultBundles.js | 1 - src/plugins/plugins.js | 3 + 11 files changed, 253 insertions(+), 239 deletions(-) rename example/eventGenerator/{src/EventTelemetry.js => EventMetadataProvider.js} (51%) create mode 100644 example/eventGenerator/EventTelemetryProvider.js delete mode 100644 example/eventGenerator/bundle.js create mode 100644 example/eventGenerator/plugin.js create mode 100644 example/eventGenerator/pluginSpec.js delete mode 100644 example/eventGenerator/src/EventTelemetryProvider.js rename example/eventGenerator/{data => }/transcript.json (100%) diff --git a/example/eventGenerator/src/EventTelemetry.js b/example/eventGenerator/EventMetadataProvider.js similarity index 51% rename from example/eventGenerator/src/EventTelemetry.js rename to example/eventGenerator/EventMetadataProvider.js index 0accbfe68b..bf566f61ba 100644 --- a/example/eventGenerator/src/EventTelemetry.js +++ b/example/eventGenerator/EventMetadataProvider.js @@ -20,43 +20,45 @@ * at runtime from the About dialog for additional information. *****************************************************************************/ -/** - * Module defining EventTelemetry. - * Created by chacskaylo on 06/18/2015. - * Modified by shale on 06/23/2015. - */ -define( - ['../data/transcript.json'], - function (messages) { - "use strict"; - - var firstObservedTime = Date.now(); - - function EventTelemetry(request, interval) { - - var latestObservedTime = Date.now(), - count = Math.floor((latestObservedTime - firstObservedTime) / interval), - generatorData = {}; - - generatorData.getPointCount = function () { - return count; - }; - - generatorData.getDomainValue = function (i, domain) { - return i * interval - + (domain !== 'delta' ? firstObservedTime : 0); - }; - - generatorData.getRangeValue = function (i, range) { - var domainDelta = this.getDomainValue(i) - firstObservedTime, - ind = i % messages.length; - - return messages[ind] + " - [" + domainDelta.toString() + "]"; - }; - - return generatorData; - } - - return EventTelemetry; +class EventMetadataProvider { + constructor() { + this.METADATA_BY_TYPE = { + 'eventGenerator': { + values: [ + { + key: "name", + name: "Name", + format: "string" + }, + { + key: "utc", + name: "Time", + format: "utc", + hints: { + domain: 1 + } + }, + { + key: "message", + name: "Message", + format: "string" + } + ] + } + }; } -); + + supportsMetadata(domainObject) { + return Object.prototype.hasOwnProperty.call(this.METADATA_BY_TYPE, domainObject.type); + } + + getMetadata(domainObject) { + return Object.assign( + {}, + domainObject.telemetry, + this.METADATA_BY_TYPE[domainObject.type] + ); + } +} + +export default EventMetadataProvider; diff --git a/example/eventGenerator/EventTelemetryProvider.js b/example/eventGenerator/EventTelemetryProvider.js new file mode 100644 index 0000000000..05be474f9b --- /dev/null +++ b/example/eventGenerator/EventTelemetryProvider.js @@ -0,0 +1,96 @@ +/***************************************************************************** + * Open MCT, Copyright (c) 2014-2022, United States Government + * as represented by the Administrator of the National Aeronautics and Space + * Administration. All rights reserved. + * + * Open MCT is licensed under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * Open MCT includes source code licensed under additional open source + * licenses. See the Open Source Licenses file (LICENSES.md) included with + * this source code distribution or the Licensing information page available + * at runtime from the About dialog for additional information. + *****************************************************************************/ + +/** + * Module defining EventTelemetryProvider. Created by chacskaylo on 06/18/2015. + */ + +import messages from './transcript.json'; + +class EventTelemetryProvider { + constructor() { + this.defaultSize = 25; + } + + generateData(firstObservedTime, count, startTime, duration, name) { + const millisecondsSinceStart = startTime - firstObservedTime; + const utc = Math.floor(startTime / duration) * duration; + const ind = count % messages.length; + const message = messages[ind] + " - [" + millisecondsSinceStart + "]"; + + return { + name, + utc, + message + }; + } + + supportsRequest(domainObject) { + return domainObject.type === 'eventGenerator'; + } + + supportsSubscribe(domainObject) { + return domainObject.type === 'eventGenerator'; + } + + subscribe(domainObject, callback) { + const duration = domainObject.telemetry.duration * 1000; + const firstObservedTime = Date.now(); + let count = 0; + + const interval = setInterval(() => { + const startTime = Date.now(); + const datum = this.generateData(firstObservedTime, count, startTime, duration, domainObject.name); + count += 1; + callback(datum); + }, duration); + + return function () { + clearInterval(interval); + }; + } + + request(domainObject, options) { + let start = options.start; + const end = Math.min(Date.now(), options.end); // no future values + const duration = domainObject.telemetry.duration * 1000; + const size = options.size ? options.size : this.defaultSize; + const data = []; + const firstObservedTime = Date.now(); + let count = 0; + + if (options.strategy === 'latest' || options.size === 1) { + start = end; + } + + while (start <= end && data.length < size) { + const startTime = Date.now() + count; + data.push(this.generateData(firstObservedTime, count, startTime, duration, domainObject.name)); + start += duration; + count += 1; + } + + return Promise.resolve(data); + } +} + +export default EventTelemetryProvider; diff --git a/example/eventGenerator/bundle.js b/example/eventGenerator/bundle.js deleted file mode 100644 index 598f24d2b3..0000000000 --- a/example/eventGenerator/bundle.js +++ /dev/null @@ -1,80 +0,0 @@ -/***************************************************************************** - * Open MCT, Copyright (c) 2014-2022, United States Government - * as represented by the Administrator of the National Aeronautics and Space - * Administration. All rights reserved. - * - * Open MCT is licensed under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * http://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * Open MCT includes source code licensed under additional open source - * licenses. See the Open Source Licenses file (LICENSES.md) included with - * this source code distribution or the Licensing information page available - * at runtime from the About dialog for additional information. - *****************************************************************************/ - -define([ - "./src/EventTelemetryProvider" -], function ( - EventTelemetryProvider -) { - "use strict"; - - return { - name: "example/eventGenerator", - definition: { - "name": "Event Message Generator", - "description": "For development use. Creates sample event message data that mimics a live data stream.", - "extensions": { - "components": [ - { - "implementation": EventTelemetryProvider, - "type": "provider", - "provides": "telemetryService", - "depends": [ - "$q", - "$timeout" - ] - } - ], - "types": [ - { - "key": "eventGenerator", - "name": "Event Message Generator", - "cssClass": "icon-generator-events", - "description": "For development use. Creates sample event message data that mimics a live data stream.", - "priority": 10, - "features": "creation", - "model": { - "telemetry": {} - }, - "telemetry": { - "source": "eventGenerator", - "domains": [ - { - "key": "utc", - "name": "Timestamp", - "format": "utc" - } - ], - "ranges": [ - { - "key": "message", - "name": "Message", - "format": "string" - } - ] - } - } - ] - } - } - }; -}); diff --git a/example/eventGenerator/plugin.js b/example/eventGenerator/plugin.js new file mode 100644 index 0000000000..513406dc1a --- /dev/null +++ b/example/eventGenerator/plugin.js @@ -0,0 +1,42 @@ +/***************************************************************************** + * Open MCT, Copyright (c) 2014-2022, United States Government + * as represented by the Administrator of the National Aeronautics and Space + * Administration. All rights reserved. + * + * Open MCT is licensed under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * Open MCT includes source code licensed under additional open source + * licenses. See the Open Source Licenses file (LICENSES.md) included with + * this source code distribution or the Licensing information page available + * at runtime from the About dialog for additional information. + *****************************************************************************/ +import EventTelmetryProvider from './EventTelemetryProvider'; +import EventMetadataProvider from './EventMetadataProvider'; + +export default function EventGeneratorPlugin(options) { + return function install(openmct) { + openmct.types.addType("eventGenerator", { + name: "Event Message Generator", + description: "For development use. Creates sample event message data that mimics a live data stream.", + cssClass: "icon-generator-events", + creatable: true, + initialize: function (object) { + object.telemetry = { + duration: 5 + }; + } + }); + openmct.telemetry.addProvider(new EventTelmetryProvider()); + openmct.telemetry.addProvider(new EventMetadataProvider()); + + }; +} diff --git a/example/eventGenerator/pluginSpec.js b/example/eventGenerator/pluginSpec.js new file mode 100644 index 0000000000..f9621a450d --- /dev/null +++ b/example/eventGenerator/pluginSpec.js @@ -0,0 +1,69 @@ +/***************************************************************************** + * Open MCT, Copyright (c) 2014-2022, United States Government + * as represented by the Administrator of the National Aeronautics and Space + * Administration. All rights reserved. + * + * Open MCT is licensed under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * Open MCT includes source code licensed under additional open source + * licenses. See the Open Source Licenses file (LICENSES.md) included with + * this source code distribution or the Licensing information page available + * at runtime from the About dialog for additional information. + *****************************************************************************/ +import EventMessageGeneratorPlugin from './plugin.js'; +import { + createOpenMct, + resetApplicationState +} from '../../src/utils/testing'; + +describe('the plugin', () => { + let openmct; + const mockDomainObject = { + identifier: { + namespace: '', + key: 'some-value' + }, + telemetry: { + duration: 0 + }, + type: 'eventGenerator' + }; + + beforeEach((done) => { + const options = {}; + openmct = createOpenMct(); + openmct.install(new EventMessageGeneratorPlugin(options)); + openmct.on('start', done); + openmct.startHeadless(); + }); + + afterEach(async () => { + await resetApplicationState(openmct); + }); + + describe('the plugin', () => { + it("supports subscription", (done) => { + const unsubscribe = openmct.telemetry.subscribe(mockDomainObject, (telemetry) => { + expect(telemetry).not.toEqual(null); + expect(telemetry.message).toContain('CC: Eagle, Houston'); + expect(unsubscribe).not.toEqual(null); + unsubscribe(); + done(); + }); + }); + + it("supports requests", async () => { + const telemetry = await openmct.telemetry.request(mockDomainObject); + expect(telemetry[0].message).toContain('CC: Eagle, Houston'); + }); + }); +}); diff --git a/example/eventGenerator/src/EventTelemetryProvider.js b/example/eventGenerator/src/EventTelemetryProvider.js deleted file mode 100644 index 6655aaf58d..0000000000 --- a/example/eventGenerator/src/EventTelemetryProvider.js +++ /dev/null @@ -1,118 +0,0 @@ -/***************************************************************************** - * Open MCT, Copyright (c) 2014-2022, United States Government - * as represented by the Administrator of the National Aeronautics and Space - * Administration. All rights reserved. - * - * Open MCT is licensed under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * http://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * Open MCT includes source code licensed under additional open source - * licenses. See the Open Source Licenses file (LICENSES.md) included with - * this source code distribution or the Licensing information page available - * at runtime from the About dialog for additional information. - *****************************************************************************/ - -/** - * Module defining EventTelemetryProvider. Created by chacskaylo on 06/18/2015. - */ -define( - ["./EventTelemetry"], - function (EventTelemetry) { - "use strict"; - - /** - * - * @constructor - */ - function EventTelemetryProvider($q, $timeout) { - var subscriptions = [], - genInterval = 1000, - generating = false; - - // - function matchesSource(request) { - return request.source === "eventGenerator"; - } - - // Used internally; this will be repacked by doPackage - function generateData(request) { - return { - key: request.key, - telemetry: new EventTelemetry(request, genInterval) - }; - } - - // - function doPackage(results) { - var packaged = {}; - results.forEach(function (result) { - packaged[result.key] = result.telemetry; - }); - - // Format as expected (sources -> keys -> telemetry) - return { eventGenerator: packaged }; - } - - function requestTelemetry(requests) { - return $timeout(function () { - return doPackage(requests.filter(matchesSource).map(generateData)); - }, 0); - } - - function handleSubscriptions(timeout) { - subscriptions.forEach(function (subscription) { - var requests = subscription.requests; - subscription.callback(doPackage( - requests.filter(matchesSource).map(generateData) - )); - }); - } - - function startGenerating() { - generating = true; - $timeout(function () { - handleSubscriptions(); - if (generating && subscriptions.length > 0) { - startGenerating(); - } else { - generating = false; - } - }, genInterval); - } - - function subscribe(callback, requests) { - var subscription = { - callback: callback, - requests: requests - }; - function unsubscribe() { - subscriptions = subscriptions.filter(function (s) { - return s !== subscription; - }); - } - - subscriptions.push(subscription); - if (!generating) { - startGenerating(); - } - - return unsubscribe; - } - - return { - requestTelemetry: requestTelemetry, - subscribe: subscribe - }; - } - - return EventTelemetryProvider; - } -); diff --git a/example/eventGenerator/data/transcript.json b/example/eventGenerator/transcript.json similarity index 100% rename from example/eventGenerator/data/transcript.json rename to example/eventGenerator/transcript.json diff --git a/index.html b/index.html index 2d0d548e2b..fdad41c667 100644 --- a/index.html +++ b/index.html @@ -81,6 +81,7 @@ openmct.install(openmct.plugins.Espresso()); openmct.install(openmct.plugins.MyItems()); openmct.install(openmct.plugins.Generator()); + openmct.install(openmct.plugins.EventGeneratorPlugin()); openmct.install(openmct.plugins.ExampleImagery()); openmct.install(openmct.plugins.PlanLayout()); openmct.install(openmct.plugins.Timeline()); diff --git a/indexTest.js b/indexTest.js index 6c35491865..ab14013109 100644 --- a/indexTest.js +++ b/indexTest.js @@ -1,3 +1,3 @@ -const testsContext = require.context('.', true, /\/(src|platform)\/.*Spec.js$/); +const testsContext = require.context('.', true, /\/(src|platform|\.\/example)\/.*Spec.js$/); testsContext.keys().forEach(testsContext); diff --git a/src/plugins/legacySupport/installDefaultBundles.js b/src/plugins/legacySupport/installDefaultBundles.js index 88dc75587a..36875adefb 100644 --- a/src/plugins/legacySupport/installDefaultBundles.js +++ b/src/plugins/legacySupport/installDefaultBundles.js @@ -44,7 +44,6 @@ const DEFAULTS = [ define([ '../../adapter/bundle', - '../../../example/eventGenerator/bundle', '../../../example/export/bundle', '../../../example/forms/bundle', '../../../example/identity/bundle', diff --git a/src/plugins/plugins.js b/src/plugins/plugins.js index 332e5c3c93..a9cca07336 100644 --- a/src/plugins/plugins.js +++ b/src/plugins/plugins.js @@ -28,6 +28,7 @@ define([ './ISOTimeFormat/plugin', './myItems/plugin', '../../example/generator/plugin', + '../../example/eventGenerator/plugin', './autoflow/AutoflowTabularPlugin', './timeConductor/plugin', '../../example/imagery/plugin', @@ -85,6 +86,7 @@ define([ ISOTimeFormat, MyItems, GeneratorPlugin, + EventGeneratorPlugin, AutoflowPlugin, TimeConductorPlugin, ExampleImagery, @@ -196,6 +198,7 @@ define([ return GeneratorPlugin; }; + plugins.EventGeneratorPlugin = EventGeneratorPlugin.default; plugins.ExampleImagery = ExampleImagery.default; plugins.ImageryPlugin = ImageryPlugin; plugins.Plot = PlotPlugin.default;