datasource.ts 3.0 KB

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