markDelayedData.tsx 1008 B

123456789101112131415161718192021222324252627282930313233343536
  1. import type {TimeseriesData} from '../common/types';
  2. /**
  3. * Given a timeseries and a delay in seconds, goes through the timeseries data, and marks each point as either delayed (data bucket ended before the delay threshold) or not
  4. */
  5. export function markDelayedData(timeserie: TimeseriesData, delay: number) {
  6. if (delay === 0) {
  7. return timeserie;
  8. }
  9. const penultimateDatum = timeserie.data.at(-2);
  10. const finalDatum = timeserie.data.at(-1);
  11. let bucketSize: number = 0;
  12. if (penultimateDatum && finalDatum) {
  13. bucketSize =
  14. new Date(finalDatum.timestamp).getTime() -
  15. new Date(penultimateDatum.timestamp).getTime();
  16. }
  17. const ingestionDelayTimestamp = Date.now() - delay * 1000;
  18. return {
  19. ...timeserie,
  20. data: timeserie.data.map(datum => {
  21. const bucketEndTimestamp = new Date(datum.timestamp).getTime() + bucketSize;
  22. const delayed = bucketEndTimestamp >= ingestionDelayTimestamp;
  23. return {
  24. ...datum,
  25. delayed,
  26. };
  27. }),
  28. };
  29. }