time.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { describeInterval } from '@grafana/data/src/datetime/rangeutil';
  2. import { TimeOptions } from '../types/time';
  3. export function parseInterval(value: string): [number, string] {
  4. const match = value.match(/(\d+)(\w+)/);
  5. if (match) {
  6. return [Number(match[1]), match[2]];
  7. }
  8. throw new Error(`Invalid interval description: ${value}`);
  9. }
  10. export function intervalToSeconds(interval: string): number {
  11. const { sec, count } = describeInterval(interval);
  12. return sec * count;
  13. }
  14. export const timeOptions = Object.entries(TimeOptions).map(([key, value]) => ({
  15. label: key[0].toUpperCase() + key.slice(1),
  16. value: value,
  17. }));
  18. // 1h, 10m and such
  19. export const positiveDurationValidationPattern = {
  20. value: new RegExp(`^\\d+(${Object.values(TimeOptions).join('|')})$`),
  21. message: `Must be of format "(number)(unit)" , for example "1m". Available units: ${Object.values(TimeOptions).join(
  22. ', '
  23. )}`,
  24. };
  25. // 1h, 10m or 0 (without units)
  26. export const durationValidationPattern = {
  27. value: new RegExp(`^\\d+(${Object.values(TimeOptions).join('|')})|0$`),
  28. message: `Must be of format "(number)(unit)", for example "1m", or just "0". Available units: ${Object.values(
  29. TimeOptions
  30. ).join(', ')}`,
  31. };