data_processor.ts 5.0 KB

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