datasource.ts 3.5 KB

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