rangeutil.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. ///<reference path="../../headers/common.d.ts" />
  2. import moment = require('moment');
  3. import _ = require('lodash');
  4. import dateMath = require('./datemath');
  5. import angular = require('angular');
  6. var spans = {
  7. 's': {display: 'second'},
  8. 'm': {display: 'minute'},
  9. 'h': {display: 'hour'},
  10. 'd': {display: 'day'},
  11. 'w': {display: 'week'},
  12. 'M': {display: 'month'},
  13. 'y': {display: 'year'},
  14. };
  15. var rangeOptions = [
  16. { from: 'now/d', to: 'now/d', display: 'Today', section: 2 },
  17. { from: 'now/d', to: 'now', display: 'The day so far', section: 2 },
  18. { from: 'now/w', to: 'now/w', display: 'This week', section: 2 },
  19. { from: 'now/w', to: 'now', display: 'Week to date', section: 2 },
  20. { from: 'now/M', to: 'now/M', display: 'This month', section: 2 },
  21. { from: 'now/y', to: 'now/y', display: 'This year', section: 2 },
  22. { from: 'now-1d/d', to: 'now-1d/d', display: 'Yesterday', section: 1 },
  23. { from: 'now-2d/d', to: 'now-2d/d', display: 'Day before yesterday', section: 1 },
  24. { from: 'now-7d/d', to: 'now-7d/d', display: 'This day last week', section: 1 },
  25. { from: 'now-1w/w', to: 'now-1w/w', display: 'Previous week', section: 1 },
  26. { from: 'now-1M/M', to: 'now-1M/M', display: 'Previous month', section: 1 },
  27. { from: 'now-1y/y', to: 'now-1y/y', display: 'Previous year', section: 1 },
  28. { from: 'now-5m', to: 'now', display: 'Last 5 minutes', section: 3 },
  29. { from: 'now-15m', to: 'now', display: 'Last 15 minutes', section: 3 },
  30. { from: 'now-30m', to: 'now', display: 'Last 30 minutes', section: 3 },
  31. { from: 'now-1h', to: 'now', display: 'Last 1 hour', section: 3 },
  32. { from: 'now-6h', to: 'now', display: 'Last 6 hours', section: 3 },
  33. { from: 'now-12h', to: 'now', display: 'Last 12 hours', section: 3 },
  34. { from: 'now-24h', to: 'now', display: 'Last 24 hours', section: 3 },
  35. { from: 'now-7d', to: 'now', display: 'Last 7 days', section: 3 },
  36. { from: 'now-30d', to: 'now', display: 'Last 30 days', section: 0 },
  37. { from: 'now-60d', to: 'now', display: 'Last 60 days', section: 0 },
  38. { from: 'now-90d', to: 'now', display: 'Last 90 days', section: 0 },
  39. { from: 'now-6M', to: 'now', display: 'Last 6 months', section: 0 },
  40. { from: 'now-1y', to: 'now', display: 'Last 1 year', section: 0 },
  41. { from: 'now-2y', to: 'now', display: 'Last 2 years', section: 0 },
  42. { from: 'now-5y', to: 'now', display: 'Last 5 years', section: 0 },
  43. ];
  44. var absoluteFormat = 'MMM D, YYYY HH:mm:ss';
  45. var rangeIndex = {};
  46. _.each(rangeOptions, function (frame) {
  47. rangeIndex[frame.from + ' to ' + frame.to] = frame;
  48. });
  49. function getRelativeTimesList(timepickerSettings, currentDisplay) {
  50. return _.groupBy(rangeOptions, (option: any) => {
  51. option.active = option.display === currentDisplay;
  52. return option.section;
  53. });
  54. // return _.map(timepickerSettings.time_options, function(duration: string) {
  55. // return describeTextRange(duration);
  56. // });
  57. }
  58. function formatDate(date) {
  59. return date.format(absoluteFormat);
  60. }
  61. // handles expressions like
  62. // 5m
  63. // 5m to now/d
  64. // now/d to now
  65. // now/d
  66. // if no to <expr> then to now is assumed
  67. function describeTextRange(expr: any) {
  68. if (expr.indexOf('now') === -1) {
  69. expr = 'now-' + expr;
  70. }
  71. let opt = rangeIndex[expr + ' to now'];
  72. if (opt) {
  73. return opt;
  74. }
  75. opt = {from: expr, to: 'now'};
  76. let parts = /^now-(\d+)(\w)/.exec(expr);
  77. if (parts) {
  78. let unit = parts[2];
  79. let amount = parseInt(parts[1]);
  80. let span = spans[unit];
  81. if (span) {
  82. opt.display = 'Last ' + amount + ' ' + span.display;
  83. if (amount > 1) {
  84. opt.display += 's';
  85. }
  86. }
  87. } else {
  88. opt.display = 'parse error';
  89. opt.invalid = true;
  90. }
  91. return opt;
  92. }
  93. function describeTimeRange(range) {
  94. var option = rangeIndex[range.from.toString() + ' to ' + range.to.toString()];
  95. if (option) {
  96. return option.display;
  97. }
  98. if (moment.isMoment(range.from)) {
  99. var toMoment = dateMath.parse(range.to, true);
  100. return formatDate(range.from) + ' to ' + toMoment.fromNow();
  101. }
  102. if (moment.isMoment(range.to)) {
  103. var from = dateMath.parse(range.from, false);
  104. return from.fromNow() + ' to ' + formatDate(range.to);
  105. }
  106. var res = describeTextRange(range.from);
  107. return res.display;
  108. }
  109. export = {
  110. getRelativeTimesList: getRelativeTimesList,
  111. describeTextRange: describeTextRange,
  112. describeTimeRange: describeTimeRange,
  113. }