| 
									
										
										
										
											2018-09-07 23:18:15 +08:00
										 |  |  | import _ from 'lodash'; | 
					
						
							| 
									
										
										
										
											2018-09-04 19:21:02 +08:00
										 |  |  | import { QueryCtrl } from 'app/plugins/sdk'; | 
					
						
							| 
									
										
										
										
											2018-09-07 23:18:15 +08:00
										 |  |  | import appEvents from 'app/core/app_events'; | 
					
						
							| 
									
										
										
										
											2018-09-24 06:59:28 +08:00
										 |  |  | import { FilterSegments, DefaultRemoveFilterValue } from './filter_segments'; | 
					
						
							| 
									
										
										
										
											2018-09-25 20:42:47 +08:00
										 |  |  | import './query_aggregation_ctrl'; | 
					
						
							| 
									
										
										
										
											2018-09-04 19:21:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-12 04:41:24 +08:00
										 |  |  | export interface QueryMeta { | 
					
						
							|  |  |  |   rawQuery: string; | 
					
						
							|  |  |  |   rawQueryString: string; | 
					
						
							| 
									
										
										
										
											2018-09-14 05:51:45 +08:00
										 |  |  |   metricLabels: { [key: string]: string[] }; | 
					
						
							|  |  |  |   resourceLabels: { [key: string]: string[] }; | 
					
						
							| 
									
										
										
										
											2018-09-12 04:41:24 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-09-14 07:49:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 19:21:02 +08:00
										 |  |  | export class StackdriverQueryCtrl extends QueryCtrl { | 
					
						
							|  |  |  |   static templateUrl = 'partials/query.editor.html'; | 
					
						
							| 
									
										
										
										
											2018-09-10 04:53:35 +08:00
										 |  |  |   target: { | 
					
						
							|  |  |  |     project: { | 
					
						
							|  |  |  |       id: string; | 
					
						
							|  |  |  |       name: string; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     metricType: string; | 
					
						
							| 
									
										
										
										
											2018-09-26 17:17:24 +08:00
										 |  |  |     resourceType: string; | 
					
						
							| 
									
										
										
										
											2018-09-12 04:41:24 +08:00
										 |  |  |     refId: string; | 
					
						
							| 
									
										
										
										
											2018-09-13 17:02:31 +08:00
										 |  |  |     aggregation: { | 
					
						
							|  |  |  |       crossSeriesReducer: string; | 
					
						
							|  |  |  |       alignmentPeriod: string; | 
					
						
							|  |  |  |       perSeriesAligner: string; | 
					
						
							|  |  |  |       groupBys: string[]; | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2018-09-14 21:44:12 +08:00
										 |  |  |     filters: string[]; | 
					
						
							| 
									
										
										
										
											2018-09-20 17:44:17 +08:00
										 |  |  |     aliasBy: string; | 
					
						
							| 
									
										
										
										
											2018-09-25 15:34:14 +08:00
										 |  |  |     metricKind: any; | 
					
						
							|  |  |  |     valueType: any; | 
					
						
							| 
									
										
										
										
											2018-09-07 23:18:15 +08:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2018-09-14 21:44:12 +08:00
										 |  |  |   defaultDropdownValue = 'select metric'; | 
					
						
							| 
									
										
										
										
											2018-09-25 22:51:12 +08:00
										 |  |  |   defaultMetricResourcesValue = 'all'; | 
					
						
							| 
									
										
										
										
											2018-09-14 21:44:12 +08:00
										 |  |  |   defaultRemoveGroupByValue = '-- remove group by --'; | 
					
						
							| 
									
										
										
										
											2018-09-17 17:28:10 +08:00
										 |  |  |   loadLabelsPromise: Promise<any>; | 
					
						
							| 
									
										
										
										
											2018-09-17 23:16:14 +08:00
										 |  |  |   stackdriverConstants; | 
					
						
							| 
									
										
										
										
											2018-09-07 23:18:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 01:08:39 +08:00
										 |  |  |   defaults = { | 
					
						
							|  |  |  |     project: { | 
					
						
							|  |  |  |       id: 'default', | 
					
						
							|  |  |  |       name: 'loading project...', | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2018-09-12 06:24:59 +08:00
										 |  |  |     metricType: this.defaultDropdownValue, | 
					
						
							| 
									
										
										
										
											2018-09-26 17:17:24 +08:00
										 |  |  |     resourceType: this.defaultMetricResourcesValue, | 
					
						
							| 
									
										
										
										
											2018-09-25 22:51:12 +08:00
										 |  |  |     metric: '', | 
					
						
							| 
									
										
										
										
											2018-09-13 17:02:31 +08:00
										 |  |  |     aggregation: { | 
					
						
							|  |  |  |       crossSeriesReducer: 'REDUCE_MEAN', | 
					
						
							| 
									
										
										
										
											2018-09-17 23:16:14 +08:00
										 |  |  |       alignmentPeriod: 'auto', | 
					
						
							| 
									
										
										
										
											2018-09-17 20:32:49 +08:00
										 |  |  |       perSeriesAligner: 'ALIGN_MEAN', | 
					
						
							| 
									
										
										
										
											2018-09-13 17:02:31 +08:00
										 |  |  |       groupBys: [], | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2018-09-14 07:49:39 +08:00
										 |  |  |     filters: [], | 
					
						
							| 
									
										
										
										
											2018-09-17 20:32:49 +08:00
										 |  |  |     showAggregationOptions: false, | 
					
						
							| 
									
										
										
										
											2018-09-20 17:44:17 +08:00
										 |  |  |     aliasBy: '', | 
					
						
							| 
									
										
										
										
											2018-09-25 15:34:14 +08:00
										 |  |  |     metricKind: '', | 
					
						
							|  |  |  |     valueType: '', | 
					
						
							| 
									
										
										
										
											2018-09-11 01:08:39 +08:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-25 22:51:12 +08:00
										 |  |  |   metricDescriptors: any[]; | 
					
						
							|  |  |  |   metrics: any[]; | 
					
						
							| 
									
										
										
										
											2018-09-13 17:02:31 +08:00
										 |  |  |   groupBySegments: any[]; | 
					
						
							| 
									
										
										
										
											2018-09-14 07:49:39 +08:00
										 |  |  |   removeSegment: any; | 
					
						
							| 
									
										
										
										
											2018-09-12 04:41:24 +08:00
										 |  |  |   showHelp: boolean; | 
					
						
							|  |  |  |   showLastQuery: boolean; | 
					
						
							|  |  |  |   lastQueryMeta: QueryMeta; | 
					
						
							|  |  |  |   lastQueryError?: string; | 
					
						
							| 
									
										
										
										
											2018-09-14 07:49:39 +08:00
										 |  |  |   metricLabels: { [key: string]: string[] }; | 
					
						
							|  |  |  |   resourceLabels: { [key: string]: string[] }; | 
					
						
							| 
									
										
										
										
											2018-09-24 06:59:28 +08:00
										 |  |  |   filterSegments: any; | 
					
						
							| 
									
										
										
										
											2018-09-12 04:41:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 19:21:02 +08:00
										 |  |  |   /** @ngInject */ | 
					
						
							| 
									
										
										
										
											2018-09-24 21:26:49 +08:00
										 |  |  |   constructor($scope, $injector, private uiSegmentSrv, private templateSrv) { | 
					
						
							| 
									
										
										
										
											2018-09-04 19:21:02 +08:00
										 |  |  |     super($scope, $injector); | 
					
						
							| 
									
										
										
										
											2018-09-11 01:08:39 +08:00
										 |  |  |     _.defaultsDeep(this.target, this.defaults); | 
					
						
							| 
									
										
										
										
											2018-09-25 22:51:12 +08:00
										 |  |  |     this.metricDescriptors = []; | 
					
						
							|  |  |  |     this.metrics = []; | 
					
						
							| 
									
										
										
										
											2018-09-12 04:41:24 +08:00
										 |  |  |     this.panelCtrl.events.on('data-received', this.onDataReceived.bind(this), $scope); | 
					
						
							|  |  |  |     this.panelCtrl.events.on('data-error', this.onDataError.bind(this), $scope); | 
					
						
							| 
									
										
										
										
											2018-09-17 17:28:10 +08:00
										 |  |  |     this.getCurrentProject() | 
					
						
							| 
									
										
										
										
											2018-09-26 17:17:24 +08:00
										 |  |  |       .then(this.loadMetricDescriptors.bind(this)) | 
					
						
							| 
									
										
										
										
											2018-09-17 17:28:10 +08:00
										 |  |  |       .then(this.getLabels.bind(this)); | 
					
						
							| 
									
										
										
										
											2018-09-14 07:49:39 +08:00
										 |  |  |     this.initSegments(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   initSegments() { | 
					
						
							| 
									
										
										
										
											2018-09-14 00:22:48 +08:00
										 |  |  |     this.groupBySegments = this.target.aggregation.groupBys.map(groupBy => { | 
					
						
							| 
									
										
										
										
											2018-09-14 07:49:39 +08:00
										 |  |  |       return this.uiSegmentSrv.getSegmentForValue(groupBy); | 
					
						
							| 
									
										
										
										
											2018-09-13 17:02:31 +08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2018-09-14 07:49:39 +08:00
										 |  |  |     this.removeSegment = this.uiSegmentSrv.newSegment({ fake: true, value: '-- remove group by --' }); | 
					
						
							| 
									
										
										
										
											2018-09-13 17:02:31 +08:00
										 |  |  |     this.ensurePlusButton(this.groupBySegments); | 
					
						
							| 
									
										
										
										
											2018-09-14 07:49:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-24 06:59:28 +08:00
										 |  |  |     this.filterSegments = new FilterSegments( | 
					
						
							|  |  |  |       this.uiSegmentSrv, | 
					
						
							|  |  |  |       this.target, | 
					
						
							|  |  |  |       this.getGroupBys.bind(this, null, null, DefaultRemoveFilterValue, false), | 
					
						
							|  |  |  |       this.getFilterValues.bind(this) | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |     this.filterSegments.buildSegmentModel(); | 
					
						
							| 
									
										
										
										
											2018-09-07 23:18:15 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async getCurrentProject() { | 
					
						
							|  |  |  |     try { | 
					
						
							| 
									
										
										
										
											2018-09-24 16:17:06 +08:00
										 |  |  |       this.target.project = await this.datasource.getDefaultProject(); | 
					
						
							| 
									
										
										
										
											2018-09-07 23:18:15 +08:00
										 |  |  |     } catch (error) { | 
					
						
							|  |  |  |       let message = 'Projects cannot be fetched: '; | 
					
						
							|  |  |  |       message += error.statusText ? error.statusText + ': ' : ''; | 
					
						
							|  |  |  |       if (error && error.data && error.data.error && error.data.error.message) { | 
					
						
							|  |  |  |         if (error.data.error.code === 403) { | 
					
						
							|  |  |  |           message += `
 | 
					
						
							|  |  |  |             A list of projects could not be fetched from the Google Cloud Resource Manager API. | 
					
						
							|  |  |  |             You might need to enable it first: | 
					
						
							|  |  |  |             https://console.developers.google.com/apis/library/cloudresourcemanager.googleapis.com`;
 | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |           message += error.data.error.code + '. ' + error.data.error.message; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         message += 'Cannot connect to Stackdriver API'; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       appEvents.emit('ds-request-error', message); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-26 17:17:24 +08:00
										 |  |  |   async loadMetricDescriptors() { | 
					
						
							| 
									
										
										
										
											2018-09-10 04:53:35 +08:00
										 |  |  |     if (this.target.project.id !== 'default') { | 
					
						
							| 
									
										
										
										
											2018-09-26 17:17:24 +08:00
										 |  |  |       this.metricDescriptors = await this.datasource.getMetricTypes(this.target.project.id); | 
					
						
							|  |  |  |       this.metrics = this.getMetrics(); | 
					
						
							|  |  |  |       return this.metricDescriptors; | 
					
						
							| 
									
										
										
										
											2018-09-07 23:18:15 +08:00
										 |  |  |     } else { | 
					
						
							|  |  |  |       return []; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-09-04 19:21:02 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-09-12 04:41:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-26 17:17:24 +08:00
										 |  |  |   getResourceTypes() { | 
					
						
							| 
									
										
										
										
											2018-09-25 22:51:12 +08:00
										 |  |  |     const defaultValue = { value: this.defaultMetricResourcesValue, text: this.defaultMetricResourcesValue }; | 
					
						
							|  |  |  |     const resources = this.metricDescriptors.map(m => { | 
					
						
							|  |  |  |       const [resource] = m.type.split('/'); | 
					
						
							|  |  |  |       const [service] = resource.split('.'); | 
					
						
							|  |  |  |       return { | 
					
						
							|  |  |  |         value: resource, | 
					
						
							|  |  |  |         text: service, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     return resources.length > 0 ? [defaultValue, ..._.uniqBy(resources, 'value')] : []; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   getMetrics() { | 
					
						
							|  |  |  |     const metrics = this.metricDescriptors.map(m => { | 
					
						
							|  |  |  |       const [resource] = m.type.split('/'); | 
					
						
							|  |  |  |       const [service] = resource.split('.'); | 
					
						
							|  |  |  |       return { | 
					
						
							|  |  |  |         resource, | 
					
						
							|  |  |  |         value: m.type, | 
					
						
							|  |  |  |         service, | 
					
						
							|  |  |  |         text: m.displayName, | 
					
						
							|  |  |  |         title: m.description, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2018-09-26 17:17:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (this.target.resourceType === this.defaultMetricResourcesValue) { | 
					
						
							| 
									
										
										
										
											2018-09-25 22:51:12 +08:00
										 |  |  |       return metrics.map(m => ({ ...m, text: `${m.service} - ${m.text}` })); | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2018-09-26 17:17:24 +08:00
										 |  |  |       return metrics.filter(m => m.resource === this.target.resourceType); | 
					
						
							| 
									
										
										
										
											2018-09-25 22:51:12 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   onResourceTypeChange(resource) { | 
					
						
							|  |  |  |     this.metrics = this.getMetrics(); | 
					
						
							|  |  |  |     if (!this.metrics.find(m => m.value === this.target.metricType)) { | 
					
						
							|  |  |  |       this.target.metricType = this.defaultDropdownValue; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       this.refresh(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-14 00:22:48 +08:00
										 |  |  |   async getLabels() { | 
					
						
							| 
									
										
										
										
											2018-09-17 17:28:10 +08:00
										 |  |  |     this.loadLabelsPromise = new Promise(async resolve => { | 
					
						
							|  |  |  |       try { | 
					
						
							| 
									
										
										
										
											2018-09-24 21:26:49 +08:00
										 |  |  |         const data = await this.datasource.getLabels(this.target.metricType, this.target.refId); | 
					
						
							| 
									
										
										
										
											2018-09-17 17:28:10 +08:00
										 |  |  |         this.metricLabels = data.results[this.target.refId].meta.metricLabels; | 
					
						
							|  |  |  |         this.resourceLabels = data.results[this.target.refId].meta.resourceLabels; | 
					
						
							|  |  |  |         resolve(); | 
					
						
							|  |  |  |       } catch (error) { | 
					
						
							| 
									
										
										
										
											2018-09-24 21:40:10 +08:00
										 |  |  |         console.log(error.data.message); | 
					
						
							|  |  |  |         appEvents.emit('alert-error', ['Error', 'Error loading metric labels for ' + this.target.metricType]); | 
					
						
							| 
									
										
										
										
											2018-09-17 17:28:10 +08:00
										 |  |  |         resolve(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2018-09-14 00:22:48 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async onMetricTypeChange() { | 
					
						
							| 
									
										
										
										
											2018-09-26 17:17:24 +08:00
										 |  |  |     const { valueType, metricKind } = this.metricDescriptors.find(m => m.type === this.target.metricType); | 
					
						
							|  |  |  |     this.target.valueType = valueType; | 
					
						
							|  |  |  |     this.target.metricKind = metricKind; | 
					
						
							| 
									
										
										
										
											2018-09-14 00:22:48 +08:00
										 |  |  |     this.refresh(); | 
					
						
							|  |  |  |     this.getLabels(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-14 23:19:15 +08:00
										 |  |  |   async getGroupBys(segment, index, removeText?: string, removeUsed = true) { | 
					
						
							| 
									
										
										
										
											2018-09-17 17:28:10 +08:00
										 |  |  |     await this.loadLabelsPromise; | 
					
						
							| 
									
										
										
										
											2018-09-24 21:40:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const metricLabels = Object.keys(this.metricLabels || {}) | 
					
						
							| 
									
										
										
										
											2018-09-14 05:51:45 +08:00
										 |  |  |       .filter(ml => { | 
					
						
							| 
									
										
										
										
											2018-09-14 21:44:12 +08:00
										 |  |  |         if (!removeUsed) { | 
					
						
							|  |  |  |           return true; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-09-14 05:51:45 +08:00
										 |  |  |         return this.target.aggregation.groupBys.indexOf('metric.label.' + ml) === -1; | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       .map(l => { | 
					
						
							|  |  |  |         return this.uiSegmentSrv.newSegment({ | 
					
						
							|  |  |  |           value: `metric.label.${l}`, | 
					
						
							|  |  |  |           expandable: false, | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2018-09-14 00:22:48 +08:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-24 21:40:10 +08:00
										 |  |  |     const resourceLabels = Object.keys(this.resourceLabels || {}) | 
					
						
							| 
									
										
										
										
											2018-09-14 05:51:45 +08:00
										 |  |  |       .filter(ml => { | 
					
						
							| 
									
										
										
										
											2018-09-14 21:44:12 +08:00
										 |  |  |         if (!removeUsed) { | 
					
						
							|  |  |  |           return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-14 05:51:45 +08:00
										 |  |  |         return this.target.aggregation.groupBys.indexOf('resource.label.' + ml) === -1; | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       .map(l => { | 
					
						
							|  |  |  |         return this.uiSegmentSrv.newSegment({ | 
					
						
							|  |  |  |           value: `resource.label.${l}`, | 
					
						
							|  |  |  |           expandable: false, | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2018-09-14 00:22:48 +08:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2018-09-13 17:02:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-15 01:28:48 +08:00
										 |  |  |     const noValueOrPlusButton = !segment || segment.type === 'plus-button'; | 
					
						
							|  |  |  |     if (noValueOrPlusButton && metricLabels.length === 0 && resourceLabels.length === 0) { | 
					
						
							|  |  |  |       return Promise.resolve([]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-14 21:44:12 +08:00
										 |  |  |     this.removeSegment.value = removeText || this.defaultRemoveGroupByValue; | 
					
						
							| 
									
										
										
										
											2018-09-14 06:13:23 +08:00
										 |  |  |     return Promise.resolve([...metricLabels, ...resourceLabels, this.removeSegment]); | 
					
						
							| 
									
										
										
										
											2018-09-13 17:02:31 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-14 06:13:23 +08:00
										 |  |  |   groupByChanged(segment, index) { | 
					
						
							|  |  |  |     if (segment.value === this.removeSegment.value) { | 
					
						
							|  |  |  |       this.groupBySegments.splice(index, 1); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       segment.type = 'value'; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-09-14 05:51:45 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const reducer = (memo, seg) => { | 
					
						
							|  |  |  |       if (!seg.fake) { | 
					
						
							|  |  |  |         memo.push(seg.value); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       return memo; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     this.target.aggregation.groupBys = this.groupBySegments.reduce(reducer, []); | 
					
						
							| 
									
										
										
										
											2018-09-13 17:02:31 +08:00
										 |  |  |     this.ensurePlusButton(this.groupBySegments); | 
					
						
							| 
									
										
										
										
											2018-09-14 00:22:48 +08:00
										 |  |  |     this.refresh(); | 
					
						
							| 
									
										
										
										
											2018-09-13 17:02:31 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-14 21:44:12 +08:00
										 |  |  |   async getFilters(segment, index) { | 
					
						
							| 
									
										
										
										
											2018-09-24 06:59:28 +08:00
										 |  |  |     const hasNoFilterKeys = this.metricLabels && Object.keys(this.metricLabels).length === 0; | 
					
						
							|  |  |  |     return this.filterSegments.getFilters(segment, index, hasNoFilterKeys); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-09-14 07:49:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-24 06:59:28 +08:00
										 |  |  |   getFilterValues(index) { | 
					
						
							|  |  |  |     const filterKey = this.templateSrv.replace(this.filterSegments.filterSegments[index - 2].value); | 
					
						
							|  |  |  |     if (!filterKey || !this.metricLabels || Object.keys(this.metricLabels).length === 0) { | 
					
						
							|  |  |  |       return []; | 
					
						
							| 
									
										
										
										
											2018-09-14 07:49:39 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-24 06:59:28 +08:00
										 |  |  |     const shortKey = filterKey.substring(filterKey.indexOf('.label.') + 7); | 
					
						
							| 
									
										
										
										
											2018-09-14 07:49:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-24 06:59:28 +08:00
										 |  |  |     if (filterKey.startsWith('metric.label.') && this.metricLabels.hasOwnProperty(shortKey)) { | 
					
						
							|  |  |  |       return this.metricLabels[shortKey]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-09-14 07:49:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-24 06:59:28 +08:00
										 |  |  |     if (filterKey.startsWith('resource.label.') && this.resourceLabels.hasOwnProperty(shortKey)) { | 
					
						
							|  |  |  |       return this.resourceLabels[shortKey]; | 
					
						
							| 
									
										
										
										
											2018-09-14 07:49:39 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return []; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-14 21:44:12 +08:00
										 |  |  |   filterSegmentUpdated(segment, index) { | 
					
						
							| 
									
										
										
										
											2018-09-24 06:59:28 +08:00
										 |  |  |     this.target.filters = this.filterSegments.filterSegmentUpdated(segment, index); | 
					
						
							| 
									
										
										
										
											2018-09-14 21:44:12 +08:00
										 |  |  |     this.refresh(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-13 17:02:31 +08:00
										 |  |  |   ensurePlusButton(segments) { | 
					
						
							|  |  |  |     const count = segments.length; | 
					
						
							|  |  |  |     const lastSegment = segments[Math.max(count - 1, 0)]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!lastSegment || lastSegment.type !== 'plus-button') { | 
					
						
							|  |  |  |       segments.push(this.uiSegmentSrv.newPlusButton()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-12 04:41:24 +08:00
										 |  |  |   onDataReceived(dataList) { | 
					
						
							|  |  |  |     this.lastQueryError = null; | 
					
						
							|  |  |  |     this.lastQueryMeta = null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const anySeriesFromQuery: any = _.find(dataList, { refId: this.target.refId }); | 
					
						
							|  |  |  |     if (anySeriesFromQuery) { | 
					
						
							|  |  |  |       this.lastQueryMeta = anySeriesFromQuery.meta; | 
					
						
							|  |  |  |       this.lastQueryMeta.rawQueryString = decodeURIComponent(this.lastQueryMeta.rawQuery); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   onDataError(err) { | 
					
						
							|  |  |  |     if (err.data && err.data.results) { | 
					
						
							|  |  |  |       const queryRes = err.data.results[this.target.refId]; | 
					
						
							| 
									
										
										
										
											2018-09-14 23:47:39 +08:00
										 |  |  |       if (queryRes && queryRes.error) { | 
					
						
							| 
									
										
										
										
											2018-09-12 04:41:24 +08:00
										 |  |  |         this.lastQueryMeta = queryRes.meta; | 
					
						
							|  |  |  |         this.lastQueryMeta.rawQueryString = decodeURIComponent(this.lastQueryMeta.rawQuery); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         let jsonBody; | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |           jsonBody = JSON.parse(queryRes.error); | 
					
						
							|  |  |  |         } catch { | 
					
						
							|  |  |  |           this.lastQueryError = queryRes.error; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.lastQueryError = jsonBody.error.message; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-09-17 17:28:10 +08:00
										 |  |  |     console.error(err); | 
					
						
							| 
									
										
										
										
											2018-09-12 04:41:24 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-09-04 19:21:02 +08:00
										 |  |  | } |