Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Telemetry Collections] Respect "Latest" Strategy Option #5421

Merged
merged 11 commits into from
Jul 6, 2022
Prev Previous commit
Next Next commit
have reworked the process telemetry logic to keep the future bound ha…
…ndling and to only emit/keep latest datum
  • Loading branch information
jvigliotta committed Jul 6, 2022
commit 7646437a78e525abfca3294dac87c9815da9e51d
86 changes: 43 additions & 43 deletions src/api/telemetry/TelemetryCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,64 +174,64 @@ export class TelemetryCollection extends EventEmitter {
return;
}

let latestBoundedDatum = this.boundedTelemetry[this.boundedTelemetry.length - 1];
let data = Array.isArray(telemetryData) ? telemetryData : [telemetryData];
let parsedValue;
let beforeStartOfBounds;
let afterEndOfBounds;
let added = [];

if (!this.isStrategyLatest) {

// loop through, sort and dedupe
for (let datum of data) {
parsedValue = this.parseTime(datum);
beforeStartOfBounds = parsedValue < this.lastBounds.start;
afterEndOfBounds = parsedValue > this.lastBounds.end;

if (!afterEndOfBounds && !beforeStartOfBounds) {
let isDuplicate = false;
let startIndex = this._sortedIndex(datum);
let endIndex = undefined;

// dupe check
if (startIndex !== this.boundedTelemetry.length) {
endIndex = _.sortedLastIndexBy(
this.boundedTelemetry,
datum,
boundedDatum => this.parseTime(boundedDatum)
);

if (endIndex > startIndex) {
let potentialDupes = this.boundedTelemetry.slice(startIndex, endIndex);
isDuplicate = potentialDupes.some(_.isEqual.bind(undefined, datum));
}
} else if (startIndex === this.boundedTelemetry.length) {
isDuplicate = _.isEqual(datum, this.boundedTelemetry[this.boundedTelemetry.length - 1]);
}
// loop through, sort and dedupe
for (let datum of data) {
parsedValue = this.parseTime(datum);
beforeStartOfBounds = parsedValue < this.lastBounds.start;
afterEndOfBounds = parsedValue > this.lastBounds.end;

if (!afterEndOfBounds && !beforeStartOfBounds) {
let isDuplicate = false;
let startIndex = this._sortedIndex(datum);
let endIndex = undefined;

if (!isDuplicate) {
let index = endIndex || startIndex;
// dupe check
if (startIndex !== this.boundedTelemetry.length) {
endIndex = _.sortedLastIndexBy(
this.boundedTelemetry,
datum,
boundedDatum => this.parseTime(boundedDatum)
);

this.boundedTelemetry.splice(index, 0, datum);
added.push(datum);
if (endIndex > startIndex) {
let potentialDupes = this.boundedTelemetry.slice(startIndex, endIndex);
isDuplicate = potentialDupes.some(_.isEqual.bind(undefined, datum));
}
} else if (startIndex === this.boundedTelemetry.length) {
isDuplicate = _.isEqual(datum, this.boundedTelemetry[this.boundedTelemetry.length - 1]);
}

if (!isDuplicate) {
let index = endIndex || startIndex;

} else if (afterEndOfBounds) {
this.futureBuffer.push(datum);
this.boundedTelemetry.splice(index, 0, datum);
added.push(datum);
}
}

if (added.length) {
this.emit('add', added);
} else if (afterEndOfBounds) {
this.futureBuffer.push(datum);
}
} else {
// strategy latest, we only need one value
let latest = this._getLatestDatum(data);
}

added.push(latest);
if (added.length) {
// if latest strategy is requested, we need to check if the value is the latest unmitted value
if (this.isStrategyLatest) {
this.boundedTelemetry = [this.boundedTelemetry[this.boundedTelemetry.length - 1]];

this.boundedTelemetry = added;
this.emit('add', added);
// if true, then this value has yet to be emitted
if (this.boundedTelemetry[0] !== latestBoundedDatum) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, we can just do a direct comparison. Noice.

this.emit('add', this.boundedTelemetry);
}
} else {
this.emit('add', added);
}
}
}

Expand Down