data_processor.ts 4.6 KB

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