Second rewrite

This commit is contained in:
Henry 2016-05-27 15:07:21 -07:00
parent d8a097a95a
commit d8d0f22889
3 changed files with 80 additions and 41 deletions

View File

@ -109,7 +109,7 @@ define([
{ {
"key": "HistoricalTableController", "key": "HistoricalTableController",
"implementation": HistoricalTableController, "implementation": HistoricalTableController,
"depends": ["$scope", "telemetryHandler", "telemetryFormatter", "$timeout", "$q"] "depends": ["$scope", "telemetryHandler", "telemetryFormatter", "$timeout"]
}, },
{ {
"key": "RealtimeTableController", "key": "RealtimeTableController",

View File

@ -25,6 +25,7 @@ define(
'./TelemetryTableController' './TelemetryTableController'
], ],
function (TableController) { function (TableController) {
var BATCH_SIZE = 1000;
/** /**
* Extends TelemetryTableController and adds real-time streaming * Extends TelemetryTableController and adds real-time streaming
@ -35,11 +36,18 @@ define(
* @param telemetryFormatter * @param telemetryFormatter
* @constructor * @constructor
*/ */
function HistoricalTableController($scope, telemetryHandler, telemetryFormatter, $timeout, $q) { function HistoricalTableController($scope, telemetryHandler, telemetryFormatter, $timeout) {
var self = this;
this.$timeout = $timeout; this.$timeout = $timeout;
this.$q = $q; this.timeouts = [];
this.batchSize = BATCH_SIZE;
$scope.$on("$destroy", function () {
self.cancelAllTimeouts();
});
TableController.call(this, $scope, telemetryHandler, telemetryFormatter); TableController.call(this, $scope, telemetryHandler, telemetryFormatter);
$scope.loading = true;
} }
HistoricalTableController.prototype = Object.create(TableController.prototype); HistoricalTableController.prototype = Object.create(TableController.prototype);
@ -57,54 +65,77 @@ define(
} }
/** /**
* Cancels outstanding processing
* @private * @private
*/ */
HistoricalTableController.prototype.yieldingLoop = function (index, max, forEach, yieldAfter) { HistoricalTableController.prototype.cancelAllTimeouts = function() {
var self = this; this.timeouts.forEach(function (timeout) {
clearTimeout(timeout);
if (index < max) { });
forEach(index); this.timeouts = [];
if (index % yieldAfter === 0) {
return this.$timeout(function () {
return self.yieldingLoop(++index, max, forEach, yieldAfter);
});
} else {
return self.yieldingLoop(++index, max, forEach, yieldAfter);
}
} else {
return this.$q.when(undefined);
}
}; };
/** /**
* Populates historical data on scope when it becomes available from * Will yield execution of a long running process, allowing
* the telemetry API * execution of UI and other activities
* @private
* @param callback the function to execute after yielding
* @returns {number}
*/ */
HistoricalTableController.prototype.yield = function(callback) {
return setTimeout(callback, 0);
};
/**
* Populates historical data on scope when it becomes available from
* the telemetry API
*/
HistoricalTableController.prototype.addHistoricalData = function () { HistoricalTableController.prototype.addHistoricalData = function () {
var rowData = [], var rowData = [],
self = this; self = this,
telemetryObjects = this.handle.getTelemetryObjects();
this.$scope.loading = true; this.cancelAllTimeouts();
function processTelemetryObject(telemetryObject) { function processTelemetryObject(offset) {
var series = self.handle.getSeries(telemetryObject) || {}, var telemetryObject = telemetryObjects[offset],
pointCount = series.getPointCount ? series.getPointCount() : 0, series = self.handle.getSeries(telemetryObject) || {},
i = 0; pointCount = series.getPointCount ? series.getPointCount() : 0;
return self.yieldingLoop(i, pointCount, function (index) { function processBatch(start, end, done) {
rowData.push(self.table.getRowValues(telemetryObject, var i;
self.handle.makeDatum(telemetryObject, series, index)));
}, 1000); if (start < pointCount) {
for (i = start; i < end; i++) {
rowData.push(self.table.getRowValues(telemetryObject,
self.handle.makeDatum(telemetryObject, series, i)));
}
self.timeouts.push(self.yield(function () {
processBatch(end, end + self.batchSize, done);
}));
} else {
offset++;
if (offset < telemetryObjects.length) {
processTelemetryObject(offset);
} else {
// Apply digest. Digest may not be necessary here, so
// using $timeout instead of $scope.$apply to avoid
// in progress error
self.$timeout(function () {
self.$scope.loading = false;
self.$scope.rows = rowData;
});
}
}
}
processBatch(0, self.batchSize);
}
if (telemetryObjects.length > 0) {
this.$scope.loading = true;
processTelemetryObject(0);
} }
this.handle.getTelemetryObjects().reduce(function (promise, telemetryObject) {
return promise.then(function () {
return processTelemetryObject(telemetryObject);
});
}, self.$q.when(undefined)).then(function () {
self.$scope.rows = rowData;
self.$scope.loading = false;
});
}; };
return HistoricalTableController; return HistoricalTableController;

View File

@ -83,16 +83,24 @@ define(
* @private * @private
*/ */
TelemetryTableController.prototype.registerChangeListeners = function () { TelemetryTableController.prototype.registerChangeListeners = function () {
var self=this;
this.unregisterChangeListeners(); this.unregisterChangeListeners();
// When composition changes, re-subscribe to the various // When composition changes, re-subscribe to the various
// telemetry subscriptions // telemetry subscriptions
this.changeListeners.push(this.$scope.$watchCollection( this.changeListeners.push(this.$scope.$watchCollection(
'domainObject.getModel().composition', this.subscribe.bind(this))); 'domainObject.getModel().composition',
function (newVal, oldVal) {
if (newVal !== oldVal) {
self.subscribe();
}
})
);
//Change of bounds in time conductor //Change of bounds in time conductor
this.changeListeners.push(this.$scope.$on('telemetry:display:bounds', this.changeListeners.push(this.$scope.$on('telemetry:display:bounds',
this.subscribe.bind(this))); this.subscribe.bind(this))
);
}; };
/** /**