datasource.ts 3.0 KB

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