utils.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import {
  2. AbsoluteTimeRange,
  3. DataFrame,
  4. FieldType,
  5. LegacyGraphHoverEventPayload,
  6. reduceField,
  7. ReducerID,
  8. } from '@grafana/data';
  9. /**
  10. * Find the min and max time that covers all data
  11. */
  12. export function getDataTimeRange(frames: DataFrame[]): AbsoluteTimeRange | undefined {
  13. const range: AbsoluteTimeRange = {
  14. from: Number.MAX_SAFE_INTEGER,
  15. to: Number.MIN_SAFE_INTEGER,
  16. };
  17. let found = false;
  18. const reducers = [ReducerID.min, ReducerID.max];
  19. for (const frame of frames) {
  20. for (const field of frame.fields) {
  21. if (field.type === FieldType.time) {
  22. const calcs = reduceField({ field, reducers });
  23. range.from = Math.min(range.from, calcs[ReducerID.min]);
  24. range.to = Math.max(range.to, calcs[ReducerID.max]);
  25. found = true;
  26. }
  27. }
  28. }
  29. return found ? range : undefined;
  30. }
  31. // Check wether event is LegacyGraphHoverEvent
  32. export function isLegacyGraphHoverEvent(event: any): event is LegacyGraphHoverEventPayload {
  33. return event.hasOwnProperty('pos');
  34. }