data_processor.ts 5.8 KB

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