response_parser.ts 3.5 KB

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