response_parser.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import _ from 'lodash';
  2. import { IQService } from 'angular';
  3. export default class ResponseParser {
  4. constructor(private $q: IQService) {}
  5. processQueryResult(res: any) {
  6. const data: any[] = [];
  7. if (!res.data.results) {
  8. return { data };
  9. }
  10. for (const key in res.data.results) {
  11. const queryRes = res.data.results[key];
  12. if (queryRes.series) {
  13. for (const series of queryRes.series) {
  14. data.push({
  15. target: series.name,
  16. datapoints: series.points,
  17. refId: queryRes.refId,
  18. meta: queryRes.meta,
  19. });
  20. }
  21. }
  22. if (queryRes.tables) {
  23. for (const table of queryRes.tables) {
  24. table.type = 'table';
  25. table.refId = queryRes.refId;
  26. table.meta = queryRes.meta;
  27. data.push(table);
  28. }
  29. }
  30. }
  31. return { data: data };
  32. }
  33. parseMetricFindQueryResult(refId: string, results: any) {
  34. if (!results || results.data.length === 0 || results.data.results[refId].meta.rowCount === 0) {
  35. return [];
  36. }
  37. const columns = results.data.results[refId].tables[0].columns;
  38. const rows = results.data.results[refId].tables[0].rows;
  39. const textColIndex = this.findColIndex(columns, '__text');
  40. const valueColIndex = this.findColIndex(columns, '__value');
  41. if (columns.length === 2 && textColIndex !== -1 && valueColIndex !== -1) {
  42. return this.transformToKeyValueList(rows, textColIndex, valueColIndex);
  43. }
  44. return this.transformToSimpleList(rows);
  45. }
  46. transformToKeyValueList(rows: any, textColIndex: number, valueColIndex: number) {
  47. const res = [];
  48. for (let i = 0; i < rows.length; i++) {
  49. if (!this.containsKey(res, rows[i][textColIndex])) {
  50. res.push({ text: rows[i][textColIndex], value: rows[i][valueColIndex] });
  51. }
  52. }
  53. return res;
  54. }
  55. transformToSimpleList(rows: any) {
  56. const res = [];
  57. for (let i = 0; i < rows.length; i++) {
  58. for (let j = 0; j < rows[i].length; j++) {
  59. const value = rows[i][j];
  60. if (res.indexOf(value) === -1) {
  61. res.push(value);
  62. }
  63. }
  64. }
  65. return _.map(res, value => {
  66. return { text: value };
  67. });
  68. }
  69. findColIndex(columns: any[], colName: string) {
  70. for (let i = 0; i < columns.length; i++) {
  71. if (columns[i].text === colName) {
  72. return i;
  73. }
  74. }
  75. return -1;
  76. }
  77. containsKey(res: any[], key: any) {
  78. for (let i = 0; i < res.length; i++) {
  79. if (res[i].text === key) {
  80. return true;
  81. }
  82. }
  83. return false;
  84. }
  85. transformAnnotationResponse(options: any, data: any) {
  86. const table = data.data.results[options.annotation.name].tables[0];
  87. let timeColumnIndex = -1;
  88. let textColumnIndex = -1;
  89. let tagsColumnIndex = -1;
  90. for (let i = 0; i < table.columns.length; i++) {
  91. if (table.columns[i].text === 'time') {
  92. timeColumnIndex = i;
  93. } else if (table.columns[i].text === 'text') {
  94. textColumnIndex = i;
  95. } else if (table.columns[i].text === 'tags') {
  96. tagsColumnIndex = i;
  97. }
  98. }
  99. if (timeColumnIndex === -1) {
  100. return this.$q.reject({ message: 'Missing mandatory time column (with time column alias) in annotation query.' });
  101. }
  102. const list = [];
  103. for (let i = 0; i < table.rows.length; i++) {
  104. const row = table.rows[i];
  105. list.push({
  106. annotation: options.annotation,
  107. time: Math.floor(row[timeColumnIndex]),
  108. text: row[textColumnIndex],
  109. tags: row[tagsColumnIndex] ? row[tagsColumnIndex].trim().split(/\s*,\s*/) : [],
  110. });
  111. }
  112. return list;
  113. }
  114. }