response_parser.ts 3.4 KB

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