datasource.ts 3.7 KB

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