data_processor.ts 4.9 KB

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