response_parser.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. export default class ResponseParser {
  4. constructor(private $q) {}
  5. processQueryResult(res) {
  6. var data = [];
  7. if (!res.data.results) {
  8. return { data: data };
  9. }
  10. for (let key in res.data.results) {
  11. let queryRes = res.data.results[key];
  12. if (queryRes.series) {
  13. for (let 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 (let 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, results) {
  34. if (
  35. !results ||
  36. results.data.length === 0 ||
  37. results.data.results[refId].meta.rowCount === 0
  38. ) {
  39. return [];
  40. }
  41. const columns = results.data.results[refId].tables[0].columns;
  42. const rows = results.data.results[refId].tables[0].rows;
  43. const textColIndex = this.findColIndex(columns, '__text');
  44. const valueColIndex = this.findColIndex(columns, '__value');
  45. if (columns.length === 2 && textColIndex !== -1 && valueColIndex !== -1) {
  46. return this.transformToKeyValueList(rows, textColIndex, valueColIndex);
  47. }
  48. return this.transformToSimpleList(rows);
  49. }
  50. transformToKeyValueList(rows, textColIndex, valueColIndex) {
  51. const res = [];
  52. for (let i = 0; i < rows.length; i++) {
  53. if (!this.containsKey(res, rows[i][textColIndex])) {
  54. res.push({
  55. text: rows[i][textColIndex],
  56. value: rows[i][valueColIndex],
  57. });
  58. }
  59. }
  60. return res;
  61. }
  62. transformToSimpleList(rows) {
  63. const res = [];
  64. for (let i = 0; i < rows.length; i++) {
  65. for (let j = 0; j < rows[i].length; j++) {
  66. const value = rows[i][j];
  67. if (res.indexOf(value) === -1) {
  68. res.push(value);
  69. }
  70. }
  71. }
  72. return _.map(res, value => {
  73. return { text: value };
  74. });
  75. }
  76. findColIndex(columns, colName) {
  77. for (let i = 0; i < columns.length; i++) {
  78. if (columns[i].text === colName) {
  79. return i;
  80. }
  81. }
  82. return -1;
  83. }
  84. containsKey(res, key) {
  85. for (let i = 0; i < res.length; i++) {
  86. if (res[i].text === key) {
  87. return true;
  88. }
  89. }
  90. return false;
  91. }
  92. transformAnnotationResponse(options, data) {
  93. const table = data.data.results[options.annotation.name].tables[0];
  94. let timeColumnIndex = -1;
  95. let textColumnIndex = -1;
  96. let tagsColumnIndex = -1;
  97. for (let i = 0; i < table.columns.length; i++) {
  98. if (table.columns[i].text === 'time_sec') {
  99. timeColumnIndex = i;
  100. } else if (table.columns[i].text === 'title') {
  101. return this.$q.reject({
  102. message:
  103. 'The title column for annotations is deprecated, now only a column named text is returned',
  104. });
  105. } else if (table.columns[i].text === 'text') {
  106. textColumnIndex = i;
  107. } else if (table.columns[i].text === 'tags') {
  108. tagsColumnIndex = i;
  109. }
  110. }
  111. if (timeColumnIndex === -1) {
  112. return this.$q.reject({
  113. message:
  114. 'Missing mandatory time column (with time_sec column alias) in annotation query.',
  115. });
  116. }
  117. const list = [];
  118. for (let i = 0; i < table.rows.length; i++) {
  119. const row = table.rows[i];
  120. list.push({
  121. annotation: options.annotation,
  122. time: Math.floor(row[timeColumnIndex]) * 1000,
  123. text: row[textColumnIndex],
  124. tags: row[tagsColumnIndex]
  125. ? row[tagsColumnIndex].trim().split(/\s*,\s*/)
  126. : [],
  127. });
  128. }
  129. return list;
  130. }
  131. }