query_def.js 4.8 KB

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