query_def.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. define([
  2. 'lodash'
  3. ],
  4. function (_) {
  5. 'use strict';
  6. return {
  7. metricAggTypes: [
  8. {text: "Count", value: 'count', requiresField: false},
  9. {text: "Average", value: 'avg', requiresField: true, supportsInlineScript: true, supportsMissing: true},
  10. {text: "Sum", value: 'sum', requiresField: true, supportsInlineScript: true, supportsMissing: true},
  11. {text: "Max", value: 'max', requiresField: true, supportsInlineScript: true, supportsMissing: true},
  12. {text: "Min", value: 'min', requiresField: true, supportsInlineScript: true, supportsMissing: true},
  13. {text: "Extended Stats", value: 'extended_stats', requiresField: true, supportsMissing: true, supportsInlineScript: true},
  14. {text: "Percentiles", value: 'percentiles', requiresField: true, supportsMissing: true, supportsInlineScript: true},
  15. {text: "Unique Count", value: "cardinality", requiresField: true, supportsMissing: true},
  16. {text: "Moving Average", value: 'moving_avg', requiresField: false, isPipelineAgg: true },
  17. {text: "Derivative", value: 'derivative', requiresField: false, isPipelineAgg: true },
  18. {text: "Raw Document", value: "raw_document", requiresField: false}
  19. ],
  20. bucketAggTypes: [
  21. {text: "Terms", value: 'terms' },
  22. {text: "Filters", value: 'filters' },
  23. {text: "Date Histogram", value: 'date_histogram' },
  24. ],
  25. orderByOptions: [
  26. {text: "Doc Count", value: '_count' },
  27. {text: "Term value", value: '_term' },
  28. ],
  29. orderOptions: [
  30. {text: "Top", value: 'desc' },
  31. {text: "Bottom", value: 'asc' },
  32. ],
  33. sizeOptions: [
  34. {text: "No limit", value: '0' },
  35. {text: "1", value: '1' },
  36. {text: "2", value: '2' },
  37. {text: "3", value: '3' },
  38. {text: "5", value: '5' },
  39. {text: "10", value: '10' },
  40. {text: "15", value: '15' },
  41. {text: "20", value: '20' },
  42. ],
  43. extendedStats: [
  44. {text: 'Avg', value: 'avg'},
  45. {text: 'Min', value: 'min'},
  46. {text: 'Max', value: 'max'},
  47. {text: 'Sum', value: 'sum'},
  48. {text: 'Count', value: 'count'},
  49. {text: 'Std Dev', value: 'std_deviation'},
  50. {text: 'Std Dev Upper', value: 'std_deviation_bounds_upper'},
  51. {text: 'Std Dev Lower', value: 'std_deviation_bounds_lower'},
  52. ],
  53. intervalOptions: [
  54. {text: 'auto', value: 'auto'},
  55. {text: '10s', value: '10s'},
  56. {text: '1m', value: '1m'},
  57. {text: '5m', value: '5m'},
  58. {text: '10m', value: '10m'},
  59. {text: '20m', value: '20m'},
  60. {text: '1h', value: '1h'},
  61. {text: '1d', value: '1d'},
  62. ],
  63. pipelineOptions: {
  64. 'moving_avg' : [
  65. {text: 'window', default: 5},
  66. {text: 'model', default: 'simple'}
  67. ],
  68. 'derivative': []
  69. },
  70. getPipelineOptions: function(metric) {
  71. if (!this.isPipelineAgg(metric.type)) {
  72. return [];
  73. }
  74. return this.pipelineOptions[metric.type];
  75. },
  76. isPipelineAgg: function(metricType) {
  77. if (metricType) {
  78. var po = this.pipelineOptions[metricType];
  79. return po !== null && po !== undefined;
  80. }
  81. return false;
  82. },
  83. getPipelineAggOptions: function(targets) {
  84. var self = this;
  85. var result = [];
  86. _.each(targets.metrics, function(metric) {
  87. if (!self.isPipelineAgg(metric.type)) {
  88. result.push({text: self.describeMetric(metric), value: metric.id });
  89. }
  90. });
  91. return result;
  92. },
  93. getOrderByOptions: function(target) {
  94. var self = this;
  95. var metricRefs = [];
  96. _.each(target.metrics, function(metric) {
  97. if (metric.type !== 'count') {
  98. metricRefs.push({text: self.describeMetric(metric), value: metric.id});
  99. }
  100. });
  101. return this.orderByOptions.concat(metricRefs);
  102. },
  103. describeOrder: function(order) {
  104. var def = _.findWhere(this.orderOptions, {value: order});
  105. return def.text;
  106. },
  107. describeMetric: function(metric) {
  108. var def = _.findWhere(this.metricAggTypes, {value: metric.type});
  109. return def.text + ' ' + metric.field;
  110. },
  111. describeOrderBy: function(orderBy, target) {
  112. var def = _.findWhere(this.orderByOptions, {value: orderBy});
  113. if (def) {
  114. return def.text;
  115. }
  116. var metric = _.findWhere(target.metrics, {id: orderBy});
  117. if (metric) {
  118. return this.describeMetric(metric);
  119. } else {
  120. return "metric not found";
  121. }
  122. },
  123. };
  124. });