UNPKG

diffusion

Version:

Diffusion JavaScript client

104 lines (103 loc) 3.56 kB
"use strict"; /** * @module Timeseries */ Object.defineProperty(exports, "__esModule", { value: true }); exports.QueryResultImpl = void 0; var range_query_parameters_1 = require("./../../features/time-series/range-query-parameters"); var Long = require("long"); /** * Check is a value is not `null` or `undefined` * * @param e the value to check * @return `true` if the value is truthy */ function nonNull(e) { return !!e; } /** * Compare two {@link Event}s by their sequence numbers * * Sorts first by `originalEvent.sequence`. Events with equal * `originalEvent.sequence` are sorted according to their `sequence` field. * * @param e1 the first event to compare * @param e2 the second event to compare * @return `+1` if the first event has a larger sequence number than the * second event, `-1` otherwise */ function comparingSequenceNumbers(e1, e2) { return e1.originalEvent.sequence !== e2.originalEvent.sequence ? e1.originalEvent.sequence - e2.originalEvent.sequence : e1.sequence - e2.sequence; } /** * Get a function that replaces an {@link Event} with the latest event in the * same sequence. * * @param allEvents an array of events to act as original events in the sequence * @return a function that replaces events */ function replaceWithLatest(allEvents) { var latestByOriginalSequence = {}; allEvents.forEach(function (e) { var k = e.originalEvent.sequence.toString(10); if (!latestByOriginalSequence[k] || (latestByOriginalSequence[k].sequence < e.sequence)) { latestByOriginalSequence[k] = e; } }); return function (e) { var originalSequence = e.originalEvent.sequence.toString(10); var latest = latestByOriginalSequence[originalSequence]; if (!latest) { return null; } if (!e.isEditEvent) { delete latestByOriginalSequence[originalSequence]; return latest; } else if (e.sequence.equals(latest.sequence)) { delete latestByOriginalSequence[originalSequence]; return e; } else { return null; } }; } /** * A timeseries query result */ var QueryResultImpl = /** @class */ (function () { /** * Create a new QueryResult instance * * @param selectedCount the number of selected events * @param eventCount the number of events * @param streamStructure the stream structure of the result * @param events the timeseries events returned */ function QueryResultImpl(selectedCount, eventCount, streamStructure, events) { this.events = events; this.selectedCount = selectedCount.toNumber(); this.isComplete = (selectedCount.equals(eventCount)); this.streamStructure = streamStructure; } /** * Merge a QueryResult with another result * * @param other another query result * @return a merged query result */ QueryResultImpl.prototype.merge = function (other) { var merged = this.events.concat(other.events) .sort(comparingSequenceNumbers) .map(replaceWithLatest(this.events.concat(other.events))) .filter(nonNull); var count = Long.fromNumber(merged.length); return new QueryResultImpl(count, count, range_query_parameters_1.StreamStructures.VALUE_EVENT_STREAM, merged); }; return QueryResultImpl; }()); exports.QueryResultImpl = QueryResultImpl;