data_processor.ts 5.2 KB

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