|
1 | 1 | import { stackdriverUnitMappings } from './constants';
|
2 |
| -/** @ngInject */ |
| 2 | +import appEvents from 'app/core/app_events'; |
| 3 | + |
3 | 4 | export default class StackdriverDatasource {
|
4 | 5 | id: number;
|
5 | 6 | url: string;
|
6 | 7 | baseUrl: string;
|
7 | 8 | projectName: string;
|
8 | 9 |
|
| 10 | + /** @ngInject */ |
9 | 11 | constructor(instanceSettings, private backendSrv, private templateSrv, private timeSrv) {
|
10 | 12 | this.baseUrl = `/stackdriver/`;
|
11 | 13 | this.url = instanceSettings.url;
|
@@ -121,6 +123,49 @@ export default class StackdriverDatasource {
|
121 | 123 | return { data: result };
|
122 | 124 | }
|
123 | 125 |
|
| 126 | + async annotationQuery(options) { |
| 127 | + const annotation = options.annotation; |
| 128 | + const queries = [ |
| 129 | + { |
| 130 | + refId: 'annotationQuery', |
| 131 | + datasourceId: this.id, |
| 132 | + metricType: this.templateSrv.replace(annotation.target.metricType, options.scopedVars || {}), |
| 133 | + primaryAggregation: 'REDUCE_NONE', |
| 134 | + perSeriesAligner: 'ALIGN_NONE', |
| 135 | + title: this.templateSrv.replace(annotation.target.title, options.scopedVars || {}), |
| 136 | + text: this.templateSrv.replace(annotation.target.text, options.scopedVars || {}), |
| 137 | + tags: this.templateSrv.replace(annotation.target.tags, options.scopedVars || {}), |
| 138 | + view: 'FULL', |
| 139 | + filters: (annotation.target.filters || []).map(f => { |
| 140 | + return this.templateSrv.replace(f, options.scopedVars || {}); |
| 141 | + }), |
| 142 | + type: 'annotationQuery', |
| 143 | + }, |
| 144 | + ]; |
| 145 | + |
| 146 | + const { data } = await this.backendSrv.datasourceRequest({ |
| 147 | + url: '/api/tsdb/query', |
| 148 | + method: 'POST', |
| 149 | + data: { |
| 150 | + from: options.range.from.valueOf().toString(), |
| 151 | + to: options.range.to.valueOf().toString(), |
| 152 | + queries, |
| 153 | + }, |
| 154 | + }); |
| 155 | + |
| 156 | + const results = data.results['annotationQuery'].tables[0].rows.map(v => { |
| 157 | + return { |
| 158 | + annotation: annotation, |
| 159 | + time: Date.parse(v[0]), |
| 160 | + title: v[1], |
| 161 | + tags: [v[2]], |
| 162 | + text: v[3], |
| 163 | + }; |
| 164 | + }); |
| 165 | + |
| 166 | + return results; |
| 167 | + } |
| 168 | + |
124 | 169 | testDatasource() {
|
125 | 170 | const path = `v3/projects/${this.projectName}/metricDescriptors`;
|
126 | 171 | return this.doRequest(`${this.baseUrl}${path}`)
|
@@ -161,12 +206,30 @@ export default class StackdriverDatasource {
|
161 | 206 | }
|
162 | 207 |
|
163 | 208 | async getDefaultProject() {
|
164 |
| - const projects = await this.getProjects(); |
165 |
| - if (projects && projects.length > 0) { |
166 |
| - const test = projects.filter(p => p.id === this.projectName)[0]; |
167 |
| - return test; |
168 |
| - } else { |
169 |
| - throw new Error('No projects found'); |
| 209 | + try { |
| 210 | + const projects = await this.getProjects(); |
| 211 | + if (projects && projects.length > 0) { |
| 212 | + const test = projects.filter(p => p.id === this.projectName)[0]; |
| 213 | + return test; |
| 214 | + } else { |
| 215 | + throw new Error('No projects found'); |
| 216 | + } |
| 217 | + } catch (error) { |
| 218 | + let message = 'Projects cannot be fetched: '; |
| 219 | + message += error.statusText ? error.statusText + ': ' : ''; |
| 220 | + if (error && error.data && error.data.error && error.data.error.message) { |
| 221 | + if (error.data.error.code === 403) { |
| 222 | + message += ` |
| 223 | + A list of projects could not be fetched from the Google Cloud Resource Manager API. |
| 224 | + You might need to enable it first: |
| 225 | + https://console.developers.google.com/apis/library/cloudresourcemanager.googleapis.com`; |
| 226 | + } else { |
| 227 | + message += error.data.error.code + '. ' + error.data.error.message; |
| 228 | + } |
| 229 | + } else { |
| 230 | + message += 'Cannot connect to Stackdriver API'; |
| 231 | + } |
| 232 | + appEvents.emit('ds-request-error', message); |
170 | 233 | }
|
171 | 234 | }
|
172 | 235 |
|
|
0 commit comments