getRefreshFromUrl.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { defaultIntervals } from '@grafana/ui';
  2. interface Args {
  3. urlRefresh: string | null;
  4. currentRefresh: string | boolean | undefined;
  5. isAllowedIntervalFn: (interval: string) => boolean;
  6. minRefreshInterval: string;
  7. refreshIntervals?: string[];
  8. }
  9. // getRefreshFromUrl function returns the value from the supplied &refresh= param in url.
  10. // If the supplied interval is not allowed or does not exist in the refresh intervals for the dashboard then we
  11. // try to find the first refresh interval that matches the minRefreshInterval (min_refresh_interval in ini)
  12. // or just take the first interval.
  13. export function getRefreshFromUrl({
  14. urlRefresh,
  15. currentRefresh,
  16. isAllowedIntervalFn,
  17. minRefreshInterval,
  18. refreshIntervals = defaultIntervals,
  19. }: Args): string | boolean | undefined {
  20. if (!urlRefresh) {
  21. return currentRefresh;
  22. }
  23. const isAllowedInterval = isAllowedIntervalFn(urlRefresh);
  24. const isExistingInterval = refreshIntervals.find((interval) => interval === urlRefresh);
  25. if (!isAllowedInterval || !isExistingInterval) {
  26. const minRefreshIntervalInIntervals = minRefreshInterval
  27. ? refreshIntervals.find((interval) => interval === minRefreshInterval)
  28. : undefined;
  29. const lowestRefreshInterval = refreshIntervals?.length ? refreshIntervals[0] : undefined;
  30. return minRefreshIntervalInIntervals ?? lowestRefreshInterval ?? currentRefresh;
  31. }
  32. return urlRefresh || currentRefresh;
  33. }