rangeutil.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. ///<reference path="../../headers/common.d.ts" />
  2. import moment = require('moment');
  3. import _ = require('lodash');
  4. import angular = require('angular');
  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: 0 },
  16. { from: 'now/w', to: 'now/w', display: 'This week', section: 0 },
  17. { from: 'now/d', to: 'now', display: 'The day so far', section: 0 },
  18. { from: 'now/w', to: 'now', display: 'Week to date', section: 0 },
  19. { from: 'now/M', to: 'now/M', display: 'This month', section: 0 },
  20. { from: 'now/y', to: 'now/y', display: 'This year', section: 0 },
  21. { from: 'now-1d/d', to: 'now-1d/d', display: 'Yesterday', section: 1 },
  22. { from: 'now-2d/d', to: 'now-2d/d', display: 'Day before yesterday', section: 1 },
  23. { from: 'now-7d/d', to: 'now-7d/d', display: 'This day last week', section: 1 },
  24. { from: 'now-1w/w', to: 'now-1w/w', display: 'Previous week', section: 1 },
  25. { from: 'now-1M/M', to: 'now-1M/M', display: 'Previous month', section: 1 },
  26. { from: 'now-1y/y', to: 'now-1y/y', display: 'Previous year', section: 1 },
  27. { from: 'now-5m', to: 'now', display: 'Last 5 minutes', section: 2 },
  28. { from: 'now-15m', to: 'now', display: 'Last 15 minutes', section: 2 },
  29. { from: 'now-30m', to: 'now', display: 'Last 30 minutes', section: 2 },
  30. { from: 'now-1h', to: 'now', display: 'Last 1 hour', section: 2 },
  31. { from: 'now-4h', to: 'now', display: 'Last 4 hours', section: 2 },
  32. { from: 'now-12h', to: 'now', display: 'Last 12 hours', section: 2 },
  33. { from: 'now-24h', to: 'now', display: 'Last 24 hours', section: 2 },
  34. { from: 'now-7d', to: 'now', display: 'Last 7 days', section: 2 },
  35. { from: 'now-30d', to: 'now', display: 'Last 30 days', section: 3 },
  36. { from: 'now-60d', to: 'now', display: 'Last 60 days', section: 3 },
  37. { from: 'now-90d', to: 'now', display: 'Last 90 days', section: 3 },
  38. { from: 'now-6M', to: 'now', display: 'Last 6 months', section: 3 },
  39. { from: 'now-1y', to: 'now', display: 'Last 1 year', section: 3 },
  40. { from: 'now-2y', to: 'now', display: 'Last 2 years', section: 3 },
  41. { from: 'now-5y', to: 'now', display: 'Last 5 years', section: 3 },
  42. ];
  43. var rangeIndex = {};
  44. _.each(rangeOptions, function (frame) {
  45. rangeIndex[frame.from + ' to ' + frame.to] = frame;
  46. });
  47. function getRelativeTimesList(timepickerSettings) {
  48. return _.map(timepickerSettings.time_options, function(duration: string) {
  49. return describeTextRange(duration);
  50. });
  51. }
  52. // handles expressions like
  53. // 5m
  54. // 5m to now/d
  55. // now/d to now
  56. // now/d
  57. // if no to <expr> then to now is assumed
  58. function describeTextRange(expr: string) {
  59. let rangeExpr = 'now-' + expr + ' to now';
  60. if (expr.indexOf('now') === 0) {
  61. rangeExpr = expr + ' to now';
  62. }
  63. let opt = rangeIndex[rangeExpr];
  64. if (opt) {
  65. return opt;
  66. }
  67. opt = {from: 'now-' + expr, to: 'now', display: 'Parse error'};
  68. if (/^\d+\w$/.test(expr)) {
  69. let unit = expr[expr.length - 1];
  70. let amount = parseInt(expr.substring(0, expr.length - 1));
  71. let span = spans[unit];
  72. if (span) {
  73. opt.display = 'Last ' + amount + ' ' + span.display;
  74. if (amount > 1) {
  75. opt.display += 's';
  76. }
  77. }
  78. }
  79. return opt;
  80. }
  81. function describeTimeRange(range) {
  82. var option = rangeIndex[range.from.toString() + ' to ' + range.to.toString()];
  83. if (option) {
  84. return option.display;
  85. }
  86. return "NA";
  87. }
  88. export = {
  89. getRelativeTimesList: getRelativeTimesList,
  90. describeTextRange: describeTextRange,
  91. describeTimeRange: describeTimeRange,
  92. }