datasource.ts 3.1 KB

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