response_parser.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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: 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({
  51. text: rows[i][textColIndex],
  52. value: rows[i][valueColIndex],
  53. });
  54. }
  55. }
  56. return res;
  57. }
  58. transformToSimpleList(rows: any) {
  59. const res = [];
  60. for (let i = 0; i < rows.length; i++) {
  61. for (let j = 0; j < rows[i].length; j++) {
  62. const value = rows[i][j];
  63. if (res.indexOf(value) === -1) {
  64. res.push(value);
  65. }
  66. }
  67. }
  68. return _.map(res, value => {
  69. return { text: value };
  70. });
  71. }
  72. findColIndex(columns: any[], colName: string) {
  73. for (let i = 0; i < columns.length; i++) {
  74. if (columns[i].text === colName) {
  75. return i;
  76. }
  77. }
  78. return -1;
  79. }
  80. containsKey(res: any[], key: any) {
  81. for (let i = 0; i < res.length; i++) {
  82. if (res[i].text === key) {
  83. return true;
  84. }
  85. }
  86. return false;
  87. }
  88. transformAnnotationResponse(options: any, data: any) {
  89. const table = data.data.results[options.annotation.name].tables[0];
  90. let timeColumnIndex = -1;
  91. let textColumnIndex = -1;
  92. let tagsColumnIndex = -1;
  93. for (let i = 0; i < table.columns.length; i++) {
  94. if (table.columns[i].text === 'time_sec' || table.columns[i].text === 'time') {
  95. timeColumnIndex = i;
  96. } else if (table.columns[i].text === 'title') {
  97. return this.$q.reject({
  98. message: 'The title column for annotations is deprecated, now only a column named text is returned',
  99. });
  100. } else if (table.columns[i].text === 'text') {
  101. textColumnIndex = i;
  102. } else if (table.columns[i].text === 'tags') {
  103. tagsColumnIndex = i;
  104. }
  105. }
  106. if (timeColumnIndex === -1) {
  107. return this.$q.reject({
  108. message: 'Missing mandatory time column (with time_sec column alias) in annotation query.',
  109. });
  110. }
  111. const list = [];
  112. for (let i = 0; i < table.rows.length; i++) {
  113. const row = table.rows[i];
  114. list.push({
  115. annotation: options.annotation,
  116. time: Math.floor(row[timeColumnIndex]),
  117. text: row[textColumnIndex] ? row[textColumnIndex].toString() : '',
  118. tags: row[tagsColumnIndex] ? row[tagsColumnIndex].trim().split(/\s*,\s*/) : [],
  119. });
  120. }
  121. return list;
  122. }
  123. }