timePicker.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { TimeRange, toUtc, AbsoluteTimeRange } from '@grafana/data';
  2. export const getShiftedTimeRange = (direction: number, origRange: TimeRange): AbsoluteTimeRange => {
  3. const range = {
  4. from: toUtc(origRange.from),
  5. to: toUtc(origRange.to),
  6. };
  7. const timespan = (range.to.valueOf() - range.from.valueOf()) / 2;
  8. let to: number, from: number;
  9. if (direction === -1) {
  10. to = range.to.valueOf() - timespan;
  11. from = range.from.valueOf() - timespan;
  12. } else if (direction === 1) {
  13. to = range.to.valueOf() + timespan;
  14. from = range.from.valueOf() + timespan;
  15. if (to > Date.now() && range.to.valueOf() < Date.now()) {
  16. to = Date.now();
  17. from = range.from.valueOf();
  18. }
  19. } else {
  20. to = range.to.valueOf();
  21. from = range.from.valueOf();
  22. }
  23. return { from, to };
  24. };
  25. export const getZoomedTimeRange = (range: TimeRange, factor: number): AbsoluteTimeRange => {
  26. const timespan = range.to.valueOf() - range.from.valueOf();
  27. const center = range.to.valueOf() - timespan / 2;
  28. // If the timepsan is 0, zooming out would do nothing, so we force a zoom out to 30s
  29. const newTimespan = timespan === 0 ? 30000 : timespan * factor;
  30. const to = center + newTimespan / 2;
  31. const from = center - newTimespan / 2;
  32. return { from, to };
  33. };