datasource.ts 3.1 KB

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