data_processor.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import _ from 'lodash';
  2. import { colors, getColorFromHexRgbOrName } from '@grafana/ui';
  3. import { TimeRange, FieldType, Field, DataFrame, getTimeField } from '@grafana/data';
  4. import TimeSeries from 'app/core/time_series2';
  5. import config from 'app/core/config';
  6. type Options = {
  7. dataList: DataFrame[];
  8. range?: TimeRange;
  9. };
  10. export class DataProcessor {
  11. constructor(private panel: any) {}
  12. getSeriesList(options: Options): TimeSeries[] {
  13. const list: TimeSeries[] = [];
  14. const { dataList, range } = options;
  15. if (!dataList || !dataList.length) {
  16. return list;
  17. }
  18. for (let i = 0; i < dataList.length; i++) {
  19. const series = dataList[i];
  20. const { timeField } = getTimeField(series);
  21. if (!timeField) {
  22. continue;
  23. }
  24. const seriesName = series.name ? series.name : series.refId;
  25. for (let j = 0; j < series.fields.length; j++) {
  26. const field = series.fields[j];
  27. if (field.type !== FieldType.number) {
  28. continue;
  29. }
  30. let name = field.config && field.config.title ? field.config.title : field.name;
  31. if (seriesName && dataList.length > 0 && name !== seriesName) {
  32. name = seriesName + ' ' + name;
  33. }
  34. const datapoints = [];
  35. for (let r = 0; r < series.length; r++) {
  36. datapoints.push([field.values.get(r), timeField.values.get(r)]);
  37. }
  38. list.push(this.toTimeSeries(field, name, i, j, datapoints, list.length, range));
  39. }
  40. }
  41. // Merge all the rows if we want to show a histogram
  42. if (this.panel.xaxis.mode === 'histogram' && !this.panel.stack && list.length > 1) {
  43. const first = list[0];
  44. first.alias = first.aliasEscaped = 'Count';
  45. for (let i = 1; i < list.length; i++) {
  46. first.datapoints = first.datapoints.concat(list[i].datapoints);
  47. }
  48. return [first];
  49. }
  50. return list;
  51. }
  52. private toTimeSeries(
  53. field: Field,
  54. alias: string,
  55. dataFrameIndex: number,
  56. fieldIndex: number,
  57. datapoints: any[][],
  58. index: number,
  59. range?: TimeRange
  60. ) {
  61. const colorIndex = index % colors.length;
  62. const color = this.panel.aliasColors[alias] || colors[colorIndex];
  63. const series = new TimeSeries({
  64. datapoints: datapoints || [],
  65. alias: alias,
  66. color: getColorFromHexRgbOrName(color, config.theme.type),
  67. unit: field.config ? field.config.unit : undefined,
  68. dataFrameIndex,
  69. fieldIndex,
  70. });
  71. if (datapoints && datapoints.length > 0 && range) {
  72. const last = datapoints[datapoints.length - 1][1];
  73. const from = range.from;
  74. if (last - from.valueOf() < -10000) {
  75. series.isOutsideRange = true;
  76. }
  77. }
  78. return series;
  79. }
  80. setPanelDefaultsForNewXAxisMode() {
  81. switch (this.panel.xaxis.mode) {
  82. case 'time': {
  83. this.panel.bars = false;
  84. this.panel.lines = true;
  85. this.panel.points = false;
  86. this.panel.legend.show = true;
  87. this.panel.tooltip.shared = true;
  88. this.panel.xaxis.values = [];
  89. break;
  90. }
  91. case 'series': {
  92. this.panel.bars = true;
  93. this.panel.lines = false;
  94. this.panel.points = false;
  95. this.panel.stack = false;
  96. this.panel.legend.show = false;
  97. this.panel.tooltip.shared = false;
  98. this.panel.xaxis.values = ['total'];
  99. break;
  100. }
  101. case 'histogram': {
  102. this.panel.bars = true;
  103. this.panel.lines = false;
  104. this.panel.points = false;
  105. this.panel.stack = false;
  106. this.panel.legend.show = false;
  107. this.panel.tooltip.shared = false;
  108. break;
  109. }
  110. }
  111. }
  112. validateXAxisSeriesValue() {
  113. switch (this.panel.xaxis.mode) {
  114. case 'series': {
  115. if (this.panel.xaxis.values.length === 0) {
  116. this.panel.xaxis.values = ['total'];
  117. return;
  118. }
  119. const validOptions = this.getXAxisValueOptions({});
  120. const found: any = _.find(validOptions, { value: this.panel.xaxis.values[0] });
  121. if (!found) {
  122. this.panel.xaxis.values = ['total'];
  123. }
  124. return;
  125. }
  126. }
  127. }
  128. getXAxisValueOptions(options: any) {
  129. switch (this.panel.xaxis.mode) {
  130. case 'series': {
  131. return [
  132. { text: 'Avg', value: 'avg' },
  133. { text: 'Min', value: 'min' },
  134. { text: 'Max', value: 'max' },
  135. { text: 'Total', value: 'total' },
  136. { text: 'Count', value: 'count' },
  137. ];
  138. }
  139. }
  140. return [];
  141. }
  142. pluckDeep(obj: any, property: string) {
  143. const propertyParts = property.split('.');
  144. let value = obj;
  145. for (let i = 0; i < propertyParts.length; ++i) {
  146. if (value[propertyParts[i]]) {
  147. value = value[propertyParts[i]];
  148. } else {
  149. return undefined;
  150. }
  151. }
  152. return value;
  153. }
  154. }