data_processor.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import _ from 'lodash';
  2. import TimeSeries from 'app/core/time_series2';
  3. import colors from 'app/core/utils/colors';
  4. export class DataProcessor {
  5. constructor(private panel) {}
  6. getSeriesList(options) {
  7. if (!options.dataList || options.dataList.length === 0) {
  8. return [];
  9. }
  10. // auto detect xaxis mode
  11. var firstItem;
  12. if (options.dataList && options.dataList.length > 0) {
  13. firstItem = options.dataList[0];
  14. let autoDetectMode = this.getAutoDetectXAxisMode(firstItem);
  15. if (this.panel.xaxis.mode !== autoDetectMode) {
  16. this.panel.xaxis.mode = autoDetectMode;
  17. this.setPanelDefaultsForNewXAxisMode();
  18. }
  19. }
  20. switch (this.panel.xaxis.mode) {
  21. case 'series':
  22. case 'time': {
  23. return options.dataList.map((item, index) => {
  24. return this.timeSeriesHandler(item, index, options);
  25. });
  26. }
  27. case 'histogram': {
  28. let histogramDataList = [
  29. {
  30. target: 'count',
  31. datapoints: _.concat([], _.flatten(_.map(options.dataList, 'datapoints'))),
  32. },
  33. ];
  34. return histogramDataList.map((item, index) => {
  35. return this.timeSeriesHandler(item, index, options);
  36. });
  37. }
  38. case 'field': {
  39. return this.customHandler(firstItem);
  40. }
  41. }
  42. }
  43. getAutoDetectXAxisMode(firstItem) {
  44. switch (firstItem.type) {
  45. case 'docs':
  46. return 'field';
  47. case 'table':
  48. return 'field';
  49. default: {
  50. if (this.panel.xaxis.mode === 'series') {
  51. return 'series';
  52. }
  53. if (this.panel.xaxis.mode === 'histogram') {
  54. return 'histogram';
  55. }
  56. return 'time';
  57. }
  58. }
  59. }
  60. setPanelDefaultsForNewXAxisMode() {
  61. switch (this.panel.xaxis.mode) {
  62. case 'time': {
  63. this.panel.bars = false;
  64. this.panel.lines = true;
  65. this.panel.points = false;
  66. this.panel.legend.show = true;
  67. this.panel.tooltip.shared = true;
  68. this.panel.xaxis.values = [];
  69. break;
  70. }
  71. case 'series': {
  72. this.panel.bars = true;
  73. this.panel.lines = false;
  74. this.panel.points = false;
  75. this.panel.stack = false;
  76. this.panel.legend.show = false;
  77. this.panel.tooltip.shared = false;
  78. this.panel.xaxis.values = ['total'];
  79. break;
  80. }
  81. case 'histogram': {
  82. this.panel.bars = true;
  83. this.panel.lines = false;
  84. this.panel.points = false;
  85. this.panel.stack = false;
  86. this.panel.legend.show = false;
  87. this.panel.tooltip.shared = false;
  88. break;
  89. }
  90. }
  91. }
  92. timeSeriesHandler(seriesData, index, options) {
  93. var datapoints = seriesData.datapoints || [];
  94. var alias = seriesData.target;
  95. var colorIndex = index % colors.length;
  96. var color = this.panel.aliasColors[alias] || colors[colorIndex];
  97. var series = new TimeSeries({
  98. datapoints: datapoints,
  99. alias: alias,
  100. color: color,
  101. unit: seriesData.unit,
  102. });
  103. if (datapoints && datapoints.length > 0) {
  104. var last = datapoints[datapoints.length - 1][1];
  105. var from = options.range.from;
  106. if (last - from < -10000) {
  107. series.isOutsideRange = true;
  108. }
  109. }
  110. return series;
  111. }
  112. customHandler(dataItem) {
  113. let nameField = this.panel.xaxis.name;
  114. if (!nameField) {
  115. throw {
  116. message: 'No field name specified to use for x-axis, check your axes settings',
  117. };
  118. }
  119. return [];
  120. }
  121. validateXAxisSeriesValue() {
  122. switch (this.panel.xaxis.mode) {
  123. case 'series': {
  124. if (this.panel.xaxis.values.length === 0) {
  125. this.panel.xaxis.values = ['total'];
  126. return;
  127. }
  128. var validOptions = this.getXAxisValueOptions({});
  129. var found = _.find(validOptions, { value: this.panel.xaxis.values[0] });
  130. if (!found) {
  131. this.panel.xaxis.values = ['total'];
  132. }
  133. return;
  134. }
  135. }
  136. }
  137. getDataFieldNames(dataList, onlyNumbers) {
  138. if (dataList.length === 0) {
  139. return [];
  140. }
  141. let fields = [];
  142. var firstItem = dataList[0];
  143. let fieldParts = [];
  144. function getPropertiesRecursive(obj) {
  145. _.forEach(obj, (value, key) => {
  146. if (_.isObject(value)) {
  147. fieldParts.push(key);
  148. getPropertiesRecursive(value);
  149. } else {
  150. if (!onlyNumbers || _.isNumber(value)) {
  151. let field = fieldParts.concat(key).join('.');
  152. fields.push(field);
  153. }
  154. }
  155. });
  156. fieldParts.pop();
  157. }
  158. if (firstItem.type === 'docs') {
  159. if (firstItem.datapoints.length === 0) {
  160. return [];
  161. }
  162. getPropertiesRecursive(firstItem.datapoints[0]);
  163. }
  164. return fields;
  165. }
  166. getXAxisValueOptions(options) {
  167. switch (this.panel.xaxis.mode) {
  168. case 'series': {
  169. return [
  170. { text: 'Avg', value: 'avg' },
  171. { text: 'Min', value: 'min' },
  172. { text: 'Max', value: 'max' },
  173. { text: 'Total', value: 'total' },
  174. { text: 'Count', value: 'count' },
  175. ];
  176. }
  177. }
  178. return [];
  179. }
  180. pluckDeep(obj: any, property: string) {
  181. let propertyParts = property.split('.');
  182. let value = obj;
  183. for (let i = 0; i < propertyParts.length; ++i) {
  184. if (value[propertyParts[i]]) {
  185. value = value[propertyParts[i]];
  186. } else {
  187. return undefined;
  188. }
  189. }
  190. return value;
  191. }
  192. }