splitSeriesIntoCompleteAndIncomplete.tsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import partition from 'lodash/partition';
  2. import type {TimeseriesData} from '../common/types';
  3. export function splitSeriesIntoCompleteAndIncomplete(
  4. serie: TimeseriesData,
  5. delay: number
  6. ): (TimeseriesData | undefined)[] {
  7. const penultimateDatum = serie.data.at(-2);
  8. const finalDatum = serie.data.at(-1);
  9. let bucketSize: number = 0;
  10. if (penultimateDatum && finalDatum) {
  11. bucketSize =
  12. new Date(finalDatum.timestamp).getTime() -
  13. new Date(penultimateDatum.timestamp).getTime();
  14. }
  15. const ingestionDelayTimestamp = Date.now() - delay * 1000;
  16. const [completeData, incompleteData] = partition(serie.data, datum => {
  17. const bucketEndTimestamp = new Date(datum.timestamp).getTime() + bucketSize;
  18. return bucketEndTimestamp < ingestionDelayTimestamp;
  19. });
  20. // If there is both complete and incomplete data, prepend the incomplete data
  21. // with the final point from the complete data. This way, when the series are
  22. // plotted, there's a connecting line between them
  23. const finalCompletePoint = completeData.at(-1);
  24. if (incompleteData.length > 0 && finalCompletePoint) {
  25. incompleteData.unshift({...finalCompletePoint});
  26. }
  27. return [
  28. completeData.length > 0
  29. ? {
  30. ...serie,
  31. data: completeData,
  32. }
  33. : undefined,
  34. incompleteData.length > 0
  35. ? {
  36. ...serie,
  37. data: incompleteData,
  38. }
  39. : undefined,
  40. ];
  41. }