datasource.ts 3.5 KB

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