data_processor.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import kbn from 'app/core/utils/kbn';
  3. import _ from 'lodash';
  4. import moment from 'moment';
  5. import TimeSeries from 'app/core/time_series2';
  6. import {colors} from 'app/core/core';
  7. export class DataProcessor {
  8. constructor(private panel) {
  9. }
  10. getSeriesList(options) {
  11. if (!options.dataList || options.dataList.length === 0) {
  12. return [];
  13. }
  14. // auto detect xaxis mode
  15. var firstItem;
  16. if (options.dataList && options.dataList.length > 0) {
  17. firstItem = options.dataList[0];
  18. let autoDetectMode = this.getAutoDetectXAxisMode(firstItem);
  19. if (this.panel.xaxis.mode !== autoDetectMode) {
  20. this.panel.xaxis.mode = autoDetectMode;
  21. this.setPanelDefaultsForNewXAxisMode();
  22. }
  23. }
  24. switch (this.panel.xaxis.mode) {
  25. case 'series':
  26. case 'time': {
  27. return options.dataList.map((item, index) => {
  28. return this.timeSeriesHandler(item, index, options);
  29. });
  30. }
  31. case 'field': {
  32. return this.customHandler(firstItem);
  33. }
  34. }
  35. }
  36. getAutoDetectXAxisMode(firstItem) {
  37. switch (firstItem.type) {
  38. case 'docs': return 'field';
  39. case 'table': return 'field';
  40. default: {
  41. if (this.panel.xaxis.mode === 'series') {
  42. return 'series';
  43. }
  44. return 'time';
  45. }
  46. }
  47. }
  48. setPanelDefaultsForNewXAxisMode() {
  49. switch (this.panel.xaxis.mode) {
  50. case 'time': {
  51. this.panel.bars = false;
  52. this.panel.lines = true;
  53. this.panel.points = false;
  54. this.panel.legend.show = true;
  55. this.panel.tooltip.shared = true;
  56. this.panel.xaxis.values = [];
  57. break;
  58. }
  59. case 'series': {
  60. this.panel.bars = true;
  61. this.panel.lines = false;
  62. this.panel.points = false;
  63. this.panel.stack = false;
  64. this.panel.legend.show = false;
  65. this.panel.tooltip.shared = false;
  66. this.panel.xaxis.values = ['total'];
  67. break;
  68. }
  69. }
  70. }
  71. timeSeriesHandler(seriesData, index, options) {
  72. var datapoints = seriesData.datapoints || [];
  73. var alias = seriesData.target;
  74. var colorIndex = index % colors.length;
  75. var color = this.panel.aliasColors[alias] || colors[colorIndex];
  76. var series = new TimeSeries({datapoints: datapoints, alias: alias, color: color, unit: seriesData.unit});
  77. if (datapoints && datapoints.length > 0) {
  78. var last = datapoints[datapoints.length - 1][1];
  79. var from = options.range.from;
  80. if (last - from < -10000) {
  81. series.isOutsideRange = true;
  82. }
  83. }
  84. return series;
  85. }
  86. customHandler(dataItem) {
  87. let nameField = this.panel.xaxis.name;
  88. if (!nameField) {
  89. throw {message: 'No field name specified to use for x-axis, check your axes settings'};
  90. }
  91. return [];
  92. }
  93. validateXAxisSeriesValue() {
  94. switch (this.panel.xaxis.mode) {
  95. case 'series': {
  96. if (this.panel.xaxis.values.length === 0) {
  97. this.panel.xaxis.values = ['total'];
  98. return;
  99. }
  100. var validOptions = this.getXAxisValueOptions({});
  101. var found = _.find(validOptions, {value: this.panel.xaxis.values[0]});
  102. if (!found) {
  103. this.panel.xaxis.values = ['total'];
  104. }
  105. return;
  106. }
  107. }
  108. }
  109. getDataFieldNames(dataList, onlyNumbers) {
  110. if (dataList.length === 0) {
  111. return [];
  112. }
  113. let fields = [];
  114. var firstItem = dataList[0];
  115. let fieldParts = [];
  116. function getPropertiesRecursive(obj) {
  117. _.forEach(obj, (value, key) => {
  118. if (_.isObject(value)) {
  119. fieldParts.push(key);
  120. getPropertiesRecursive(value);
  121. } else {
  122. if (!onlyNumbers || _.isNumber(value)) {
  123. let field = fieldParts.concat(key).join('.');
  124. fields.push(field);
  125. }
  126. }
  127. });
  128. fieldParts.pop();
  129. }
  130. if (firstItem.type === 'docs'){
  131. if (firstItem.datapoints.length === 0) {
  132. return [];
  133. }
  134. getPropertiesRecursive(firstItem.datapoints[0]);
  135. return fields;
  136. }
  137. }
  138. getXAxisValueOptions(options) {
  139. switch (this.panel.xaxis.mode) {
  140. case 'time': {
  141. return [];
  142. }
  143. case 'series': {
  144. return [
  145. {text: 'Avg', value: 'avg'},
  146. {text: 'Min', value: 'min'},
  147. {text: 'Max', value: 'min'},
  148. {text: 'Total', value: 'total'},
  149. {text: 'Count', value: 'count'},
  150. ];
  151. }
  152. }
  153. }
  154. pluckDeep(obj: any, property: string) {
  155. let propertyParts = property.split('.');
  156. let value = obj;
  157. for (let i = 0; i < propertyParts.length; ++i) {
  158. if (value[propertyParts[i]]) {
  159. value = value[propertyParts[i]];
  160. } else {
  161. return undefined;
  162. }
  163. }
  164. return value;
  165. }
  166. }