data_processor.ts 4.5 KB

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