datasource.ts 3.3 KB

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