response_parser.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import _ from 'lodash';
  2. export default class ResponseParser {
  3. constructor(private $q) {}
  4. processQueryResult(res) {
  5. const data = [];
  6. if (!res.data.results) {
  7. return { data: data };
  8. }
  9. for (const key in res.data.results) {
  10. const queryRes = res.data.results[key];
  11. if (queryRes.series) {
  12. for (const 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 (const 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 textColumnIndex = -1;
  91. let tagsColumnIndex = -1;
  92. for (let i = 0; i < table.columns.length; i++) {
  93. if (table.columns[i].text === 'time_sec' || table.columns[i].text === 'time') {
  94. timeColumnIndex = i;
  95. } else if (table.columns[i].text === 'title') {
  96. return this.$q.reject({
  97. message: 'The title column for annotations is deprecated, now only a column named text is returned',
  98. });
  99. } else if (table.columns[i].text === 'text') {
  100. textColumnIndex = i;
  101. } else if (table.columns[i].text === 'tags') {
  102. tagsColumnIndex = i;
  103. }
  104. }
  105. if (timeColumnIndex === -1) {
  106. return this.$q.reject({
  107. message: 'Missing mandatory time column (with time_sec column alias) in annotation query.',
  108. });
  109. }
  110. const list = [];
  111. for (let i = 0; i < table.rows.length; i++) {
  112. const row = table.rows[i];
  113. list.push({
  114. annotation: options.annotation,
  115. time: Math.floor(row[timeColumnIndex]),
  116. text: row[textColumnIndex] ? row[textColumnIndex].toString() : '',
  117. tags: row[tagsColumnIndex] ? row[tagsColumnIndex].trim().split(/\s*,\s*/) : [],
  118. });
  119. }
  120. return list;
  121. }
  122. }