data_processor.ts 5.7 KB

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