data_processor.ts 5.0 KB

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