[Tables] Maintain correct insertion order with duplicates. Fixes #1465

This commit is contained in:
Henry
2017-03-01 22:02:32 -08:00
parent 6fa5a31217
commit 06f4a955b5
5 changed files with 128 additions and 38 deletions

View File

@@ -112,22 +112,6 @@ define(
this.lastBounds = bounds;
};
/**
* Determines is a given telemetry datum is within the bounds currently
* defined for this telemetry collection.
* @private
* @param datum
* @returns {boolean}
*/
TelemetryCollection.prototype.inBounds = function (datum) {
var noBoundsDefined = !this.lastBounds || (this.lastBounds.start === undefined && this.lastBounds.end === undefined);
var withinBounds =
_.get(datum, this.sortField) >= this.lastBounds.start &&
_.get(datum, this.sortField) <= this.lastBounds.end;
return noBoundsDefined || withinBounds;
};
/**
* Adds an individual item to the collection. Used internally only
* @private
@@ -173,9 +157,10 @@ define(
// based on time stamp because the array is guaranteed ordered due
// to sorted insertion.
var startIx = _.sortedIndex(array, item, this.sortField);
var endIx;
if (startIx !== array.length) {
var endIx = _.sortedLastIndex(array, item, this.sortField);
endIx = _.sortedLastIndex(array, item, this.sortField);
// Create an array of potential dupes, based on having the
// same time stamp
@@ -185,7 +170,7 @@ define(
}
if (!isDuplicate) {
array.splice(startIx, 0, item);
array.splice(endIx || startIx, 0, item);
//Return true if it was added and in bounds
return array === this.telemetry;

View File

@@ -425,6 +425,38 @@ define(
}
};
/**
* Finds the correct insertion point for a new row, which takes into
* account duplicates to make sure new rows are inserted in a way that
* maintains arrival order.
*
* @private
* @param {Array} searchArray
* @param {Object} searchElement Object to find the insertion point for
*/
MCTTableController.prototype.findInsertionPoint = function (searchArray, searchElement) {
//First, use a binary search to find the correct insertion point
var index = this.binarySearch(searchArray, searchElement, 0, searchArray.length - 1);
var testIndex = index;
//It's possible that the insertion point is a duplicate of the element to be inserted
var isDupe = function () {
return this.sortComparator(searchElement,
searchArray[testIndex][this.$scope.sortColumn].text) === 0;
}.bind(this);
// In the event of a duplicate, scan left or right (depending on
// sort order) to find an insertion point that maintains order received
while (testIndex >= 0 && testIndex < searchArray.length && isDupe()) {
if (this.$scope.sortDirection === 'asc') {
index = ++testIndex;
} else {
index = testIndex--;
}
}
return index;
};
/**
* @private
*/
@@ -439,9 +471,9 @@ define(
case -1:
return this.binarySearch(searchArray, searchElement, min,
sampleAt - 1);
case 0 :
case 0:
return sampleAt;
case 1 :
case 1:
return this.binarySearch(searchArray, searchElement,
sampleAt + 1, max);
}
@@ -458,7 +490,7 @@ define(
index = array.length;
} else {
//Sort is enabled, perform binary search to find insertion point
index = this.binarySearch(array, element[this.$scope.sortColumn].text, 0, array.length - 1);
index = this.findInsertionPoint(array, element[this.$scope.sortColumn].text);
}
if (index === -1) {
array.unshift(element);

View File

@@ -364,11 +364,8 @@ define(
var telemetryApi = this.openmct.telemetry;
var telemetryCollection = this.telemetry;
//Set table max length to avoid unbounded growth.
//var maxRows = 100000;
var maxRows = Number.MAX_VALUE;
var limitEvaluator;
var added = false;
var scope = this.$scope;
var table = this.table;
this.subscriptions.forEach(function (subscription) {
@@ -379,16 +376,6 @@ define(
function newData(domainObject, datum) {
limitEvaluator = telemetryApi.limitEvaluator(domainObject);
added = telemetryCollection.add([table.getRowValues(limitEvaluator, datum)]);
//Inform table that a new row has been added
if (scope.rows.length > maxRows) {
scope.$broadcast('remove:rows', scope.rows[0]);
scope.rows.shift();
}
if (!scope.loading && added) {
scope.$broadcast('add:row',
scope.rows.length - 1);
}
}
objects.forEach(function (object) {