rangeutil.ts 5.2 KB

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