response_parser.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 titleColumnIndex = -1;
  96. let textColumnIndex = -1;
  97. let tagsColumnIndex = -1;
  98. for (let i = 0; i < table.columns.length; i++) {
  99. if (table.columns[i].text === 'time') {
  100. timeColumnIndex = i;
  101. } else if (table.columns[i].text === 'text') {
  102. textColumnIndex = i;
  103. } else if (table.columns[i].text === 'tags') {
  104. tagsColumnIndex = i;
  105. }
  106. }
  107. if (timeColumnIndex === -1) {
  108. return this.$q.reject({
  109. message: 'Missing mandatory time column in annotation query.',
  110. });
  111. }
  112. const list = [];
  113. for (let i = 0; i < table.rows.length; i++) {
  114. const row = table.rows[i];
  115. list.push({
  116. annotation: options.annotation,
  117. time: Math.floor(row[timeColumnIndex]) * 1000,
  118. title: row[titleColumnIndex],
  119. text: row[textColumnIndex],
  120. tags: row[tagsColumnIndex]
  121. ? row[tagsColumnIndex].trim().split(/\s*,\s*/)
  122. : [],
  123. });
  124. }
  125. return list;
  126. }
  127. }