rangeutil.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import _ from 'lodash';
  2. import moment from 'moment';
  3. import { RawTimeRange } from 'app/types/series';
  4. import * as dateMath from './datemath';
  5. const spans = {
  6. s: { display: 'second' },
  7. m: { display: 'minute' },
  8. h: { display: 'hour' },
  9. d: { display: 'day' },
  10. w: { display: 'week' },
  11. M: { display: 'month' },
  12. y: { display: 'year' },
  13. };
  14. const rangeOptions = [
  15. { from: 'now/d', to: 'now/d', display: 'Today', section: 2 },
  16. { from: 'now/d', to: 'now', display: 'Today so far', section: 2 },
  17. { from: 'now/w', to: 'now/w', display: 'This week', section: 2 },
  18. { from: 'now/w', to: 'now', display: 'This week so far', section: 2 },
  19. { from: 'now/M', to: 'now/M', display: 'This month', section: 2 },
  20. { from: 'now/M', to: 'now', display: 'This month so far', section: 2 },
  21. { from: 'now/y', to: 'now/y', display: 'This year', section: 2 },
  22. { from: 'now/y', to: 'now', display: 'This year so far', section: 2 },
  23. { from: 'now-1d/d', to: 'now-1d/d', display: 'Yesterday', section: 1 },
  24. {
  25. from: 'now-2d/d',
  26. to: 'now-2d/d',
  27. display: 'Day before yesterday',
  28. section: 1,
  29. },
  30. {
  31. from: 'now-7d/d',
  32. to: 'now-7d/d',
  33. display: 'This day last week',
  34. section: 1,
  35. },
  36. { from: 'now-1w/w', to: 'now-1w/w', display: 'Previous week', section: 1 },
  37. { from: 'now-1M/M', to: 'now-1M/M', display: 'Previous month', section: 1 },
  38. { from: 'now-1y/y', to: 'now-1y/y', display: 'Previous year', section: 1 },
  39. { from: 'now-5m', to: 'now', display: 'Last 5 minutes', section: 3 },
  40. { from: 'now-15m', to: 'now', display: 'Last 15 minutes', section: 3 },
  41. { from: 'now-30m', to: 'now', display: 'Last 30 minutes', section: 3 },
  42. { from: 'now-1h', to: 'now', display: 'Last 1 hour', section: 3 },
  43. { from: 'now-3h', to: 'now', display: 'Last 3 hours', section: 3 },
  44. { from: 'now-6h', to: 'now', display: 'Last 6 hours', section: 3 },
  45. { from: 'now-12h', to: 'now', display: 'Last 12 hours', section: 3 },
  46. { from: 'now-24h', to: 'now', display: 'Last 24 hours', section: 3 },
  47. { from: 'now-2d', to: 'now', display: 'Last 2 days', section: 0 },
  48. { from: 'now-7d', to: 'now', display: 'Last 7 days', section: 0 },
  49. { from: 'now-30d', to: 'now', display: 'Last 30 days', section: 0 },
  50. { from: 'now-90d', to: 'now', display: 'Last 90 days', section: 0 },
  51. { from: 'now-6M', to: 'now', display: 'Last 6 months', section: 0 },
  52. { from: 'now-1y', to: 'now', display: 'Last 1 year', section: 0 },
  53. { from: 'now-2y', to: 'now', display: 'Last 2 years', section: 0 },
  54. { from: 'now-5y', to: 'now', display: 'Last 5 years', section: 0 },
  55. ];
  56. const absoluteFormat = 'MMM D, YYYY HH:mm:ss';
  57. const rangeIndex = {};
  58. _.each(rangeOptions, frame => {
  59. rangeIndex[frame.from + ' to ' + frame.to] = frame;
  60. });
  61. export function getRelativeTimesList(timepickerSettings, currentDisplay) {
  62. const groups = _.groupBy(rangeOptions, (option: any) => {
  63. option.active = option.display === currentDisplay;
  64. return option.section;
  65. });
  66. // _.each(timepickerSettings.time_options, (duration: string) => {
  67. // let info = describeTextRange(duration);
  68. // if (info.section) {
  69. // groups[info.section].push(info);
  70. // }
  71. // });
  72. return groups;
  73. }
  74. function formatDate(date) {
  75. return date.format(absoluteFormat);
  76. }
  77. // handles expressions like
  78. // 5m
  79. // 5m to now/d
  80. // now/d to now
  81. // now/d
  82. // if no to <expr> then to now is assumed
  83. export function describeTextRange(expr: any) {
  84. const isLast = expr.indexOf('+') !== 0;
  85. if (expr.indexOf('now') === -1) {
  86. expr = (isLast ? 'now-' : 'now') + expr;
  87. }
  88. let opt = rangeIndex[expr + ' to now'];
  89. if (opt) {
  90. return opt;
  91. }
  92. if (isLast) {
  93. opt = { from: expr, to: 'now' };
  94. } else {
  95. opt = { from: 'now', to: expr };
  96. }
  97. const parts = /^now([-+])(\d+)(\w)/.exec(expr);
  98. if (parts) {
  99. const unit = parts[3];
  100. const amount = parseInt(parts[2], 10);
  101. const span = spans[unit];
  102. if (span) {
  103. opt.display = isLast ? 'Last ' : 'Next ';
  104. opt.display += amount + ' ' + span.display;
  105. opt.section = span.section;
  106. if (amount > 1) {
  107. opt.display += 's';
  108. }
  109. }
  110. } else {
  111. opt.display = opt.from + ' to ' + opt.to;
  112. opt.invalid = true;
  113. }
  114. return opt;
  115. }
  116. export function describeTimeRange(range: RawTimeRange): string {
  117. const option = rangeIndex[range.from.toString() + ' to ' + range.to.toString()];
  118. if (option) {
  119. return option.display;
  120. }
  121. if (moment.isMoment(range.from) && moment.isMoment(range.to)) {
  122. return formatDate(range.from) + ' to ' + formatDate(range.to);
  123. }
  124. if (moment.isMoment(range.from)) {
  125. const toMoment = dateMath.parse(range.to, true);
  126. return formatDate(range.from) + ' to ' + toMoment.fromNow();
  127. }
  128. if (moment.isMoment(range.to)) {
  129. const from = dateMath.parse(range.from, false);
  130. return from.fromNow() + ' to ' + formatDate(range.to);
  131. }
  132. if (range.to.toString() === 'now') {
  133. const res = describeTextRange(range.from);
  134. return res.display;
  135. }
  136. return range.from.toString() + ' to ' + range.to.toString();
  137. }
  138. export const isValidTimeSpan = (value: string) => {
  139. if (value.indexOf('$') === 0 || value.indexOf('+$') === 0) {
  140. return true;
  141. }
  142. const info = describeTextRange(value);
  143. return info.invalid !== true;
  144. };