[OpenMCT Tutorial] Using only Realtime (no historical data) telemetry issue #3641 (#4346)

* Reject promise if telemetryService is undefined
* added error and reformatted
* added notification and error if provider is missing
This commit is contained in:
Nikhil 2021-12-17 17:08:37 -08:00 committed by GitHub
parent 70f2fad243
commit b1ea6efd45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 74 additions and 40 deletions

View File

@ -255,8 +255,11 @@ define(
// If a telemetryService is not available, // If a telemetryService is not available,
// getTelemetryService() should reject, and this should // getTelemetryService() should reject, and this should
// bubble through subsequent then calls. // bubble through subsequent then calls.
return telemetryService if (!telemetryService) {
&& requestTelemetryFromService().then(getRelevantResponse); return Promise.reject(new Error('TelemetryService is not available'));
}
return requestTelemetryFromService().then(getRelevantResponse);
} else { } else {
return telemetryAPI.request(domainObject, fullRequest).then(function (telemetry) { return telemetryAPI.request(domainObject, fullRequest).then(function (telemetry) {
return asSeries(telemetry, defaultDomain, defaultRange, sourceMap); return asSeries(telemetry, defaultDomain, defaultRange, sourceMap);

View File

@ -41,6 +41,9 @@ define(
return { return {
then: function (callback) { then: function (callback) {
return mockPromise(callback(value)); return mockPromise(callback(value));
},
catch: (rejected) => {
return Promise.reject(rejected);
} }
}; };
} }
@ -225,19 +228,6 @@ define(
}); });
}); });
it("warns if no telemetry service can be injected", function () {
mockInjector.get.and.callFake(function () {
throw "";
});
// Verify precondition
expect(mockLog.warn).not.toHaveBeenCalled();
telemetry.requestData();
expect(mockLog.info).toHaveBeenCalled();
});
it("if a new style telemetry source is available, use it", function () { it("if a new style telemetry source is available, use it", function () {
var mockProvider = {}; var mockProvider = {};
mockTelemetryAPI.findSubscriptionProvider.and.returnValue(mockProvider); mockTelemetryAPI.findSubscriptionProvider.and.returnValue(mockProvider);

View File

@ -137,15 +137,17 @@ define([
*/ */
function TelemetryAPI(openmct) { function TelemetryAPI(openmct) {
this.openmct = openmct; this.openmct = openmct;
this.requestProviders = [];
this.subscriptionProviders = []; this.formatMapCache = new WeakMap();
this.metadataProviders = [new DefaultMetadataProvider(this.openmct)]; this.formatters = new Map();
this.limitProviders = []; this.limitProviders = [];
this.metadataCache = new WeakMap(); this.metadataCache = new WeakMap();
this.formatMapCache = new WeakMap(); this.metadataProviders = [new DefaultMetadataProvider(this.openmct)];
this.valueFormatterCache = new WeakMap(); this.noRequestProviderForAllObjects = false;
this.formatters = new Map();
this.requestAbortControllers = new Set(); this.requestAbortControllers = new Set();
this.requestProviders = [];
this.subscriptionProviders = [];
this.valueFormatterCache = new WeakMap();
} }
TelemetryAPI.prototype.abortAllRequests = function () { TelemetryAPI.prototype.abortAllRequests = function () {
@ -314,6 +316,10 @@ define([
* telemetry data * telemetry data
*/ */
TelemetryAPI.prototype.request = function (domainObject) { TelemetryAPI.prototype.request = function (domainObject) {
if (this.noRequestProviderForAllObjects) {
return Promise.resolve([]);
}
if (arguments.length === 1) { if (arguments.length === 1) {
arguments.length = 2; arguments.length = 2;
arguments[1] = {}; arguments[1] = {};
@ -326,19 +332,22 @@ define([
this.standardizeRequestOptions(arguments[1]); this.standardizeRequestOptions(arguments[1]);
const provider = this.findRequestProvider.apply(this, arguments); const provider = this.findRequestProvider.apply(this, arguments);
if (!provider) { if (!provider) {
return Promise.reject('No provider found'); this.requestAbortControllers.delete(abortController);
return this.handleMissingRequestProvider(domainObject);
} }
return provider.request.apply(provider, arguments).catch((rejected) => { return provider.request.apply(provider, arguments)
if (rejected.name !== 'AbortError') { .catch((rejected) => {
this.openmct.notifications.error('Error requesting telemetry data, see console for details'); if (rejected.name !== 'AbortError') {
console.error(rejected); this.openmct.notifications.error('Error requesting telemetry data, see console for details');
} console.error(rejected);
}
return Promise.reject(rejected); return Promise.reject(rejected);
}).finally(() => { }).finally(() => {
this.requestAbortControllers.delete(abortController); this.requestAbortControllers.delete(abortController);
}); });
}; };
/** /**
@ -495,6 +504,36 @@ define([
return this.formatMapCache.get(metadata); return this.formatMapCache.get(metadata);
}; };
/**
* Error Handling: Missing Request provider
*
* @returns Promise
*/
TelemetryAPI.prototype.handleMissingRequestProvider = function (domainObject) {
this.noRequestProviderForAllObjects = this.requestProviders.every(requestProvider => {
const supportsRequest = requestProvider.supportsRequest.apply(requestProvider, arguments);
const hasRequestProvider = Object.hasOwn(requestProvider, 'request');
return supportsRequest && hasRequestProvider;
});
let message = '';
let detailMessage = '';
if (this.noRequestProviderForAllObjects) {
message = 'Missing request providers, see console for details';
detailMessage = 'Missing request provider for all request providers';
} else {
message = 'Missing request provider, see console for details';
const { name, identifier } = domainObject;
detailMessage = `Missing request provider for domainObject, name: ${name}, identifier: ${JSON.stringify(identifier)}`;
}
this.openmct.notifications.error(message);
console.error(detailMessage);
return Promise.resolve([]);
};
/** /**
* Register a new telemetry data formatter. * Register a new telemetry data formatter.
* @param {Format} format the * @param {Format} format the

View File

@ -24,7 +24,6 @@ import TelemetryAPI from './TelemetryAPI';
const { TelemetryCollection } = require("./TelemetryCollection"); const { TelemetryCollection } = require("./TelemetryCollection");
describe('Telemetry API', function () { describe('Telemetry API', function () {
const NO_PROVIDER = 'No provider found';
let openmct; let openmct;
let telemetryAPI; let telemetryAPI;
@ -66,6 +65,12 @@ describe('Telemetry API', function () {
}, },
type: 'sample-type' type: 'sample-type'
}; };
openmct.notifications = {
error: () => {
console.log('sample error notification');
}
};
}); });
it('provides consistent results without providers', function (done) { it('provides consistent results without providers', function (done) {
@ -73,12 +78,11 @@ describe('Telemetry API', function () {
expect(unsubscribe).toEqual(jasmine.any(Function)); expect(unsubscribe).toEqual(jasmine.any(Function));
telemetryAPI.request(domainObject).then( telemetryAPI.request(domainObject)
() => {}, .then((data) => {
(error) => { expect(data).toEqual([]);
expect(error).toBe(NO_PROVIDER); })
} .finally(done);
).finally(done);
}); });
it('skips providers that do not match', function (done) { it('skips providers that do not match', function (done) {
@ -98,8 +102,6 @@ describe('Telemetry API', function () {
expect(telemetryProvider.supportsRequest) expect(telemetryProvider.supportsRequest)
.toHaveBeenCalledWith(domainObject, jasmine.any(Object)); .toHaveBeenCalledWith(domainObject, jasmine.any(Object));
expect(telemetryProvider.request).not.toHaveBeenCalled(); expect(telemetryProvider.request).not.toHaveBeenCalled();
}, (error) => {
expect(error).toBe(NO_PROVIDER);
}).finally(done); }).finally(done);
}); });