transformers.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import _ from 'lodash';
  2. import flatten from 'app/core/utils/flatten';
  3. import TimeSeries from 'app/core/time_series2';
  4. import TableModel, { mergeTablesIntoModel } from 'app/core/table_model';
  5. import { TableTransform } from './types';
  6. import { Column, TableData } from '@grafana/data';
  7. const transformers: { [key: string]: TableTransform } = {};
  8. transformers['timeseries_to_rows'] = {
  9. description: 'Time series to rows',
  10. getColumns: () => {
  11. return [];
  12. },
  13. transform: (data, panel, model) => {
  14. model.columns = [{ text: 'Time', type: 'date' }, { text: 'Metric' }, { text: 'Value' }];
  15. for (let i = 0; i < data.length; i++) {
  16. const series = data[i];
  17. for (let y = 0; y < series.datapoints.length; y++) {
  18. const dp = series.datapoints[y];
  19. model.rows.push([dp[1], series.target, dp[0]]);
  20. }
  21. }
  22. },
  23. };
  24. transformers['timeseries_to_columns'] = {
  25. description: 'Time series to columns',
  26. getColumns: () => {
  27. return [];
  28. },
  29. transform: (data, panel, model) => {
  30. model.columns.push({ text: 'Time', type: 'date' });
  31. // group by time
  32. const points: any = {};
  33. for (let i = 0; i < data.length; i++) {
  34. const series = data[i];
  35. model.columns.push({ text: series.target });
  36. for (let y = 0; y < series.datapoints.length; y++) {
  37. const dp = series.datapoints[y];
  38. const timeKey = dp[1].toString();
  39. if (!points[timeKey]) {
  40. points[timeKey] = { time: dp[1] };
  41. points[timeKey][i] = dp[0];
  42. } else {
  43. points[timeKey][i] = dp[0];
  44. }
  45. }
  46. }
  47. for (const time in points) {
  48. const point = points[time];
  49. const values = [point.time];
  50. for (let i = 0; i < data.length; i++) {
  51. const value = point[i];
  52. values.push(value);
  53. }
  54. model.rows.push(values);
  55. }
  56. },
  57. };
  58. transformers['timeseries_aggregations'] = {
  59. description: 'Time series aggregations',
  60. getColumns: () => {
  61. return [
  62. { text: 'Avg', value: 'avg' },
  63. { text: 'Min', value: 'min' },
  64. { text: 'Max', value: 'max' },
  65. { text: 'Total', value: 'total' },
  66. { text: 'Current', value: 'current' },
  67. { text: 'Count', value: 'count' },
  68. ];
  69. },
  70. transform: (data, panel, model) => {
  71. let i, y;
  72. model.columns.push({ text: 'Metric' });
  73. for (i = 0; i < panel.columns.length; i++) {
  74. model.columns.push({ text: panel.columns[i].text });
  75. }
  76. for (i = 0; i < data.length; i++) {
  77. const series = new TimeSeries({
  78. datapoints: data[i].datapoints,
  79. alias: data[i].target,
  80. });
  81. series.getFlotPairs('connected');
  82. const cells = [series.alias];
  83. for (y = 0; y < panel.columns.length; y++) {
  84. cells.push(series.stats[panel.columns[y].value]);
  85. }
  86. model.rows.push(cells);
  87. }
  88. },
  89. };
  90. transformers['annotations'] = {
  91. description: 'Annotations',
  92. getColumns: () => {
  93. return [];
  94. },
  95. transform: (data, panel, model) => {
  96. model.columns.push({ text: 'Time', type: 'date' });
  97. model.columns.push({ text: 'Title' });
  98. model.columns.push({ text: 'Text' });
  99. model.columns.push({ text: 'Tags' });
  100. if (!data || !data.annotations || data.annotations.length === 0) {
  101. return;
  102. }
  103. for (let i = 0; i < data.annotations.length; i++) {
  104. const evt = data.annotations[i];
  105. model.rows.push([evt.time, evt.title, evt.text, evt.tags]);
  106. }
  107. },
  108. };
  109. transformers['table'] = {
  110. description: 'Table',
  111. getColumns: data => {
  112. if (!data || data.length === 0) {
  113. return [];
  114. }
  115. // Single query returns data columns as is
  116. if (data.length === 1) {
  117. return [...data[0].columns];
  118. }
  119. // Track column indexes: name -> index
  120. const columnNames: any = {};
  121. // Union of all columns
  122. const columns = data.reduce((acc: Column[], series: TableData) => {
  123. series.columns.forEach(col => {
  124. const { text } = col;
  125. if (columnNames[text] === undefined) {
  126. columnNames[text] = acc.length;
  127. acc.push(col);
  128. }
  129. });
  130. return acc;
  131. }, []);
  132. return columns;
  133. },
  134. transform: (data: any[], panel, model) => {
  135. if (!data || data.length === 0) {
  136. return;
  137. }
  138. const noTableIndex = _.findIndex(data, d => 'columns' in d && 'rows' in d);
  139. if (noTableIndex < 0) {
  140. throw {
  141. message: `Result of query #${String.fromCharCode(
  142. 65 + noTableIndex
  143. )} is not in table format, try using another transform.`,
  144. };
  145. }
  146. mergeTablesIntoModel(model, ...data);
  147. },
  148. };
  149. transformers['json'] = {
  150. description: 'JSON Data',
  151. getColumns: data => {
  152. if (!data || data.length === 0) {
  153. return [];
  154. }
  155. const names: any = {};
  156. for (let i = 0; i < data.length; i++) {
  157. const series = data[i];
  158. if (series.type !== 'docs') {
  159. continue;
  160. }
  161. // only look at 100 docs
  162. const maxDocs = Math.min(series.datapoints.length, 100);
  163. for (let y = 0; y < maxDocs; y++) {
  164. const doc = series.datapoints[y];
  165. const flattened = flatten(doc, {});
  166. for (const propName in flattened) {
  167. names[propName] = true;
  168. }
  169. }
  170. }
  171. return _.map(names, (value, key) => {
  172. return { text: key, value: key };
  173. });
  174. },
  175. transform: (data, panel, model) => {
  176. let i, y, z;
  177. for (const column of panel.columns) {
  178. const tableCol: any = { text: column.text };
  179. // if filterable data then set columns to filterable
  180. if (data.length > 0 && data[0].filterable) {
  181. tableCol.filterable = true;
  182. }
  183. model.columns.push(tableCol);
  184. }
  185. if (model.columns.length === 0) {
  186. model.columns.push({ text: 'JSON' });
  187. }
  188. for (i = 0; i < data.length; i++) {
  189. const series = data[i];
  190. for (y = 0; y < series.datapoints.length; y++) {
  191. const dp = series.datapoints[y];
  192. const values = [];
  193. if (_.isObject(dp) && panel.columns.length > 0) {
  194. const flattened = flatten(dp);
  195. for (z = 0; z < panel.columns.length; z++) {
  196. values.push(flattened[panel.columns[z].value]);
  197. }
  198. } else {
  199. values.push(JSON.stringify(dp));
  200. }
  201. model.rows.push(values);
  202. }
  203. }
  204. },
  205. };
  206. function transformDataToTable(data: any, panel: any) {
  207. const model = new TableModel();
  208. if (!data || data.length === 0) {
  209. return model;
  210. }
  211. const transformer = transformers[panel.transform];
  212. if (!transformer) {
  213. throw { message: 'Transformer ' + panel.transform + ' not found' };
  214. }
  215. transformer.transform(data, panel, model);
  216. return model;
  217. }
  218. export { transformers, transformDataToTable };