data_processor.ts 5.1 KB

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