datasource.ts 3.8 KB

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