data_processor.ts 5.7 KB

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