datasource.ts 3.3 KB

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