response_parser.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 (!results || results.data.length === 0 || results.data.results[refId].meta.rowCount === 0) { return []; }
  35. const columns = results.data.results[refId].tables[0].columns;
  36. const rows = results.data.results[refId].tables[0].rows;
  37. const textColIndex = this.findColIndex(columns, '__text');
  38. const valueColIndex = this.findColIndex(columns, '__value');
  39. if (columns.length === 2 && textColIndex !== -1 && valueColIndex !== -1){
  40. return this.transformToKeyValueList(rows, textColIndex, valueColIndex);
  41. }
  42. return this.transformToSimpleList(rows);
  43. }
  44. transformToKeyValueList(rows, textColIndex, valueColIndex) {
  45. const res = [];
  46. for (let i = 0; i < rows.length; i++) {
  47. if (!this.containsKey(res, rows[i][textColIndex])) {
  48. res.push({text: rows[i][textColIndex], value: rows[i][valueColIndex]});
  49. }
  50. }
  51. return res;
  52. }
  53. transformToSimpleList(rows) {
  54. const res = [];
  55. for (let i = 0; i < rows.length; i++) {
  56. for (let j = 0; j < rows[i].length; j++) {
  57. const value = rows[i][j];
  58. if ( res.indexOf( value ) === -1 ) {
  59. res.push(value);
  60. }
  61. }
  62. }
  63. return _.map(res, value => {
  64. return { text: value};
  65. });
  66. }
  67. findColIndex(columns, colName) {
  68. for (let i = 0; i < columns.length; i++) {
  69. if (columns[i].text === colName) {
  70. return i;
  71. }
  72. }
  73. return -1;
  74. }
  75. containsKey(res, key) {
  76. for (let i = 0; i < res.length; i++) {
  77. if (res[i].text === key) {
  78. return true;
  79. }
  80. }
  81. return false;
  82. }
  83. transformAnnotationResponse(options, data) {
  84. const table = data.data.results[options.annotation.name].tables[0];
  85. let timeColumnIndex = -1;
  86. let titleColumnIndex = -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_sec') {
  91. timeColumnIndex = i;
  92. } else if (table.columns[i].text === 'title') {
  93. titleColumnIndex = i;
  94. } else if (table.columns[i].text === 'text') {
  95. textColumnIndex = i;
  96. } else if (table.columns[i].text === 'tags') {
  97. tagsColumnIndex = i;
  98. }
  99. }
  100. if (timeColumnIndex === -1) {
  101. return this.$q.reject({message: 'Missing mandatory time column (with time_sec column alias) in annotation query.'});
  102. }
  103. const list = [];
  104. for (let i = 0; i < table.rows.length; i++) {
  105. const row = table.rows[i];
  106. list.push({
  107. annotation: options.annotation,
  108. time: Math.floor(row[timeColumnIndex]) * 1000,
  109. title: row[titleColumnIndex],
  110. text: row[textColumnIndex],
  111. tags: row[tagsColumnIndex] ? row[tagsColumnIndex].trim().split(/\s*,\s*/) : []
  112. });
  113. }
  114. return list;
  115. }
  116. }