datasource.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /** @ngInject */
  2. export default class StackdriverDatasource {
  3. id: number;
  4. url: string;
  5. baseUrl: string;
  6. projectName: string;
  7. constructor(instanceSettings, private backendSrv) {
  8. this.baseUrl = `/stackdriver/`;
  9. this.url = instanceSettings.url;
  10. this.doRequest = this.doRequest;
  11. this.id = instanceSettings.id;
  12. this.projectName = instanceSettings.jsonData.defaultProject || '';
  13. }
  14. async getTimeSeries(options) {
  15. const queries = options.targets
  16. .filter(target => {
  17. return !target.hide && target.metricType;
  18. })
  19. .map(t => {
  20. if (!t.hasOwnProperty('aggregation')) {
  21. t.aggregation = {
  22. crossSeriesReducer: 'REDUCE_MEAN',
  23. secondaryCrossSeriesReducer: 'REDUCE_NONE',
  24. groupBys: [],
  25. };
  26. }
  27. return {
  28. refId: t.refId,
  29. datasourceId: this.id,
  30. metricType: t.metricType,
  31. primaryAggregation: t.aggregation.crossSeriesReducer,
  32. secondaryAggregation: t.aggregation.secondaryCrossSeriesReducer,
  33. perSeriesAligner: t.aggregation.perSeriesAligner,
  34. alignmentPeriod: t.aggregation.alignmentPeriod,
  35. groupBys: t.aggregation.groupBys,
  36. view: t.view || 'FULL',
  37. filters: t.filters,
  38. };
  39. });
  40. const { data } = await this.backendSrv.datasourceRequest({
  41. url: '/api/tsdb/query',
  42. method: 'POST',
  43. data: {
  44. from: options.range.from.valueOf().toString(),
  45. to: options.range.to.valueOf().toString(),
  46. queries,
  47. },
  48. });
  49. return data;
  50. }
  51. async query(options) {
  52. const result = [];
  53. const data = await this.getTimeSeries(options);
  54. if (data.results) {
  55. Object['values'](data.results).forEach(queryRes => {
  56. if (!queryRes.series) {
  57. return;
  58. }
  59. queryRes.series.forEach(series => {
  60. result.push({
  61. target: series.name,
  62. datapoints: series.points,
  63. refId: queryRes.refId,
  64. meta: queryRes.meta,
  65. });
  66. });
  67. });
  68. }
  69. return { data: result };
  70. }
  71. testDatasource() {
  72. const path = `v3/projects/${this.projectName}/metricDescriptors`;
  73. return this.doRequest(`${this.baseUrl}${path}`)
  74. .then(response => {
  75. if (response.status === 200) {
  76. return {
  77. status: 'success',
  78. message: 'Successfully queried the Stackdriver API.',
  79. title: 'Success',
  80. };
  81. }
  82. return {
  83. status: 'error',
  84. message: 'Returned http status code ' + response.status,
  85. };
  86. })
  87. .catch(error => {
  88. let message = 'Stackdriver: ';
  89. message += error.statusText ? error.statusText + ': ' : '';
  90. if (error.data && error.data.error && error.data.error.code) {
  91. // 400, 401
  92. message += error.data.error.code + '. ' + error.data.error.message;
  93. } else {
  94. message += 'Cannot connect to Stackdriver API';
  95. }
  96. return {
  97. status: 'error',
  98. message: message,
  99. };
  100. });
  101. }
  102. async getProjects() {
  103. const response = await this.doRequest(`/cloudresourcemanager/v1/projects`);
  104. return response.data.projects.map(p => ({ id: p.projectId, name: p.name }));
  105. }
  106. async getMetricTypes(projectId: string) {
  107. try {
  108. const metricsApiPath = `v3/projects/${projectId}/metricDescriptors`;
  109. const { data } = await this.doRequest(`${this.baseUrl}${metricsApiPath}`);
  110. return data.metricDescriptors.map(m => ({ id: m.type, name: m.displayName }));
  111. } catch (error) {
  112. console.log(error);
  113. }
  114. }
  115. async doRequest(url, maxRetries = 1) {
  116. return this.backendSrv
  117. .datasourceRequest({
  118. url: this.url + url,
  119. method: 'GET',
  120. })
  121. .catch(error => {
  122. if (maxRetries > 0) {
  123. return this.doRequest(url, maxRetries - 1);
  124. }
  125. throw error;
  126. });
  127. }
  128. }