data_processor.ts 5.5 KB

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