data_processor.ts 4.5 KB

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