| 
									
										
										
										
											2019-01-18 01:51:07 +08:00
										 |  |  | // Libraries
 | 
					
						
							| 
									
										
										
										
											2017-12-23 03:52:57 +08:00
										 |  |  | import coreModule from 'app/core/core_module'; | 
					
						
							| 
									
										
										
										
											2019-01-18 01:51:07 +08:00
										 |  |  | // Services & Utils
 | 
					
						
							| 
									
										
										
										
											2019-04-05 00:30:15 +08:00
										 |  |  | import { importDataSourcePlugin } from './plugin_loader'; | 
					
						
							| 
									
										
										
										
											2020-10-02 01:51:23 +08:00
										 |  |  | import { | 
					
						
							| 
									
										
										
										
											2020-12-04 21:24:55 +08:00
										 |  |  |   GetDataSourceListFilters, | 
					
						
							| 
									
										
										
										
											2020-10-02 01:51:23 +08:00
										 |  |  |   DataSourceSrv as DataSourceService, | 
					
						
							|  |  |  |   getDataSourceSrv as getDataSourceService, | 
					
						
							|  |  |  |   TemplateSrv, | 
					
						
							|  |  |  | } from '@grafana/runtime'; | 
					
						
							| 
									
										
										
										
											2019-01-18 01:51:07 +08:00
										 |  |  | // Types
 | 
					
						
							| 
									
										
										
										
											2021-10-30 01:57:24 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   AppEvents, | 
					
						
							|  |  |  |   DataSourceApi, | 
					
						
							|  |  |  |   DataSourceInstanceSettings, | 
					
						
							|  |  |  |   DataSourceRef, | 
					
						
							|  |  |  |   DataSourceSelectItem, | 
					
						
							|  |  |  |   ScopedVars, | 
					
						
							|  |  |  | } from '@grafana/data'; | 
					
						
							| 
									
										
										
										
											2019-07-18 14:03:04 +08:00
										 |  |  | import { auto } from 'angular'; | 
					
						
							| 
									
										
										
										
											2019-10-14 16:27:47 +08:00
										 |  |  | import { GrafanaRootScope } from 'app/routes/GrafanaCtrl'; | 
					
						
							| 
									
										
										
										
											2019-10-31 02:38:28 +08:00
										 |  |  | // Pretend Datasource
 | 
					
						
							| 
									
										
										
										
											2021-04-29 21:10:14 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   dataSource as expressionDatasource, | 
					
						
							|  |  |  |   ExpressionDatasourceID, | 
					
						
							|  |  |  |   ExpressionDatasourceUID, | 
					
						
							|  |  |  |   instanceSettings as expressionInstanceSettings, | 
					
						
							|  |  |  | } from 'app/features/expressions/ExpressionDatasource'; | 
					
						
							| 
									
										
										
										
											2020-06-04 19:44:48 +08:00
										 |  |  | import { DataSourceVariableModel } from '../variables/types'; | 
					
						
							| 
									
										
										
										
											2020-12-04 21:24:55 +08:00
										 |  |  | import { cloneDeep } from 'lodash'; | 
					
						
							| 
									
										
										
										
											2019-10-31 02:38:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-03 23:55:59 +08:00
										 |  |  | export class DatasourceSrv implements DataSourceService { | 
					
						
							| 
									
										
										
										
											2021-10-30 01:57:24 +08:00
										 |  |  |   private datasources: Record<string, DataSourceApi> = {}; // UID
 | 
					
						
							| 
									
										
										
										
											2020-12-01 22:17:33 +08:00
										 |  |  |   private settingsMapByName: Record<string, DataSourceInstanceSettings> = {}; | 
					
						
							|  |  |  |   private settingsMapByUid: Record<string, DataSourceInstanceSettings> = {}; | 
					
						
							| 
									
										
										
										
											2021-04-10 03:17:22 +08:00
										 |  |  |   private settingsMapById: Record<string, DataSourceInstanceSettings> = {}; | 
					
						
							| 
									
										
										
										
											2021-10-30 01:57:24 +08:00
										 |  |  |   private defaultName = ''; // actually UID
 | 
					
						
							| 
									
										
										
										
											2017-12-23 03:52:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 15:34:48 +08:00
										 |  |  |   /** @ngInject */ | 
					
						
							| 
									
										
										
										
											2019-07-18 14:03:04 +08:00
										 |  |  |   constructor( | 
					
						
							|  |  |  |     private $injector: auto.IInjectorService, | 
					
						
							| 
									
										
										
										
											2019-10-14 16:27:47 +08:00
										 |  |  |     private $rootScope: GrafanaRootScope, | 
					
						
							| 
									
										
										
										
											2019-07-18 14:03:04 +08:00
										 |  |  |     private templateSrv: TemplateSrv | 
					
						
							| 
									
										
										
										
											2020-12-01 22:17:33 +08:00
										 |  |  |   ) {} | 
					
						
							| 
									
										
										
										
											2017-12-23 03:52:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-01 22:17:33 +08:00
										 |  |  |   init(settingsMapByName: Record<string, DataSourceInstanceSettings>, defaultName: string) { | 
					
						
							| 
									
										
										
										
											2017-12-23 03:52:57 +08:00
										 |  |  |     this.datasources = {}; | 
					
						
							| 
									
										
										
										
											2020-12-01 22:17:33 +08:00
										 |  |  |     this.settingsMapByUid = {}; | 
					
						
							|  |  |  |     this.settingsMapByName = settingsMapByName; | 
					
						
							|  |  |  |     this.defaultName = defaultName; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const dsSettings of Object.values(settingsMapByName)) { | 
					
						
							| 
									
										
										
										
											2021-10-30 01:57:24 +08:00
										 |  |  |       if (!dsSettings.uid) { | 
					
						
							|  |  |  |         dsSettings.uid = dsSettings.name; // -- Grafana --, -- Mixed etc
 | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-01 22:17:33 +08:00
										 |  |  |       this.settingsMapByUid[dsSettings.uid] = dsSettings; | 
					
						
							| 
									
										
										
										
											2021-04-10 03:17:22 +08:00
										 |  |  |       this.settingsMapById[dsSettings.id] = dsSettings; | 
					
						
							| 
									
										
										
										
											2020-12-01 22:17:33 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-10-30 01:57:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Preload expressions
 | 
					
						
							|  |  |  |     this.datasources[ExpressionDatasourceID] = expressionDatasource as any; | 
					
						
							|  |  |  |     this.datasources[ExpressionDatasourceUID] = expressionDatasource as any; | 
					
						
							|  |  |  |     this.settingsMapByUid[ExpressionDatasourceID] = expressionInstanceSettings; | 
					
						
							|  |  |  |     this.settingsMapByUid[ExpressionDatasourceUID] = expressionInstanceSettings; | 
					
						
							| 
									
										
										
										
											2017-12-23 03:52:57 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-20 21:48:38 +08:00
										 |  |  |   getDataSourceSettingsByUid(uid: string): DataSourceInstanceSettings | undefined { | 
					
						
							| 
									
										
										
										
											2020-12-01 22:17:33 +08:00
										 |  |  |     return this.settingsMapByUid[uid]; | 
					
						
							| 
									
										
										
										
											2020-04-20 21:48:38 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-30 01:57:24 +08:00
										 |  |  |   getInstanceSettings(ref: string | null | undefined | DataSourceRef): DataSourceInstanceSettings | undefined { | 
					
						
							|  |  |  |     const isstring = typeof ref === 'string'; | 
					
						
							|  |  |  |     let nameOrUid = isstring ? (ref as string) : ((ref as any)?.uid as string | undefined); | 
					
						
							| 
									
										
										
										
											2020-12-01 22:17:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-30 01:57:24 +08:00
										 |  |  |     if (nameOrUid === 'default' || nameOrUid === null || nameOrUid === undefined) { | 
					
						
							|  |  |  |       if (!isstring && ref) { | 
					
						
							|  |  |  |         const type = (ref as any)?.type as string; | 
					
						
							|  |  |  |         if (type === ExpressionDatasourceID) { | 
					
						
							|  |  |  |           return expressionDatasource.instanceSettings; | 
					
						
							|  |  |  |         } else if (type) { | 
					
						
							|  |  |  |           console.log('FIND Default instance for datasource type?', ref); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       return this.settingsMapByUid[this.defaultName] ?? this.settingsMapByName[this.defaultName]; | 
					
						
							| 
									
										
										
										
											2021-04-29 21:10:14 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 21:24:55 +08:00
										 |  |  |     // Complex logic to support template variable data source names
 | 
					
						
							|  |  |  |     // For this we just pick the current or first data source in the variable
 | 
					
						
							|  |  |  |     if (nameOrUid[0] === '$') { | 
					
						
							|  |  |  |       const interpolatedName = this.templateSrv.replace(nameOrUid, {}, variableInterpolation); | 
					
						
							| 
									
										
										
										
											2021-02-19 19:31:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       let dsSettings; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (interpolatedName === 'default') { | 
					
						
							|  |  |  |         dsSettings = this.settingsMapByName[this.defaultName]; | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         dsSettings = this.settingsMapByUid[interpolatedName] ?? this.settingsMapByName[interpolatedName]; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 21:24:55 +08:00
										 |  |  |       if (!dsSettings) { | 
					
						
							|  |  |  |         return undefined; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       // The return name or uid needs preservet string containing the variable
 | 
					
						
							|  |  |  |       const clone = cloneDeep(dsSettings); | 
					
						
							|  |  |  |       clone.name = nameOrUid; | 
					
						
							| 
									
										
										
										
											2021-03-29 20:10:40 +08:00
										 |  |  |       // A data source being looked up using a variable should not be considered default
 | 
					
						
							|  |  |  |       clone.isDefault = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 21:24:55 +08:00
										 |  |  |       return clone; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-01 22:17:33 +08:00
										 |  |  |     return this.settingsMapByUid[nameOrUid] ?? this.settingsMapByName[nameOrUid]; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-30 01:57:24 +08:00
										 |  |  |   get(ref?: string | DataSourceRef | null, scopedVars?: ScopedVars): Promise<DataSourceApi> { | 
					
						
							|  |  |  |     let nameOrUid = typeof ref === 'string' ? (ref as string) : ((ref as any)?.uid as string | undefined); | 
					
						
							| 
									
										
										
										
											2020-12-01 22:17:33 +08:00
										 |  |  |     if (!nameOrUid) { | 
					
						
							|  |  |  |       return this.get(this.defaultName); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Check if nameOrUid matches a uid and then get the name
 | 
					
						
							| 
									
										
										
										
											2021-10-30 01:57:24 +08:00
										 |  |  |     const byName = this.settingsMapByName[nameOrUid]; | 
					
						
							|  |  |  |     if (byName) { | 
					
						
							|  |  |  |       nameOrUid = byName.uid; | 
					
						
							| 
									
										
										
										
											2020-12-01 22:17:33 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // This check is duplicated below, this is here mainly as performance optimization to skip interpolation
 | 
					
						
							|  |  |  |     if (this.datasources[nameOrUid]) { | 
					
						
							|  |  |  |       return Promise.resolve(this.datasources[nameOrUid]); | 
					
						
							| 
									
										
										
										
											2017-12-23 03:52:57 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-12 01:36:49 +08:00
										 |  |  |     // Interpolation here is to support template variable in data source selection
 | 
					
						
							| 
									
										
										
										
											2020-12-04 21:24:55 +08:00
										 |  |  |     nameOrUid = this.templateSrv.replace(nameOrUid, scopedVars, variableInterpolation); | 
					
						
							| 
									
										
										
										
											2017-12-23 03:52:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-14 15:38:53 +08:00
										 |  |  |     if (nameOrUid === 'default' && this.defaultName !== 'default') { | 
					
						
							| 
									
										
										
										
											2020-12-01 22:17:33 +08:00
										 |  |  |       return this.get(this.defaultName); | 
					
						
							| 
									
										
										
										
											2017-12-23 03:52:57 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-01 22:17:33 +08:00
										 |  |  |     if (this.datasources[nameOrUid]) { | 
					
						
							|  |  |  |       return Promise.resolve(this.datasources[nameOrUid]); | 
					
						
							| 
									
										
										
										
											2017-12-23 03:52:57 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-01 22:17:33 +08:00
										 |  |  |     return this.loadDatasource(nameOrUid); | 
					
						
							| 
									
										
										
										
											2017-12-23 03:52:57 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-30 01:57:24 +08:00
										 |  |  |   async loadDatasource(key: string): Promise<DataSourceApi<any, any>> { | 
					
						
							|  |  |  |     if (this.datasources[key]) { | 
					
						
							|  |  |  |       return Promise.resolve(this.datasources[key]); | 
					
						
							| 
									
										
										
										
											2019-10-31 02:38:28 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-30 01:57:24 +08:00
										 |  |  |     // find the metadata
 | 
					
						
							|  |  |  |     const dsConfig = this.settingsMapByUid[key] ?? this.settingsMapByName[key] ?? this.settingsMapById[key]; | 
					
						
							| 
									
										
										
										
											2017-12-23 03:52:57 +08:00
										 |  |  |     if (!dsConfig) { | 
					
						
							| 
									
										
										
										
											2021-10-30 01:57:24 +08:00
										 |  |  |       return Promise.reject({ message: `Datasource ${key} was not found` }); | 
					
						
							| 
									
										
										
										
											2017-12-23 03:52:57 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-21 17:08:07 +08:00
										 |  |  |     try { | 
					
						
							|  |  |  |       const dsPlugin = await importDataSourcePlugin(dsConfig.meta); | 
					
						
							|  |  |  |       // check if its in cache now
 | 
					
						
							| 
									
										
										
										
											2021-10-30 01:57:24 +08:00
										 |  |  |       if (this.datasources[key]) { | 
					
						
							|  |  |  |         return this.datasources[key]; | 
					
						
							| 
									
										
										
										
											2020-01-21 17:08:07 +08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2017-12-23 03:52:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-21 17:08:07 +08:00
										 |  |  |       // If there is only one constructor argument it is instanceSettings
 | 
					
						
							|  |  |  |       const useAngular = dsPlugin.DataSourceClass.length !== 1; | 
					
						
							|  |  |  |       const instance: DataSourceApi = useAngular | 
					
						
							|  |  |  |         ? this.$injector.instantiate(dsPlugin.DataSourceClass, { | 
					
						
							|  |  |  |             instanceSettings: dsConfig, | 
					
						
							|  |  |  |           }) | 
					
						
							|  |  |  |         : new dsPlugin.DataSourceClass(dsConfig); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       instance.components = dsPlugin.components; | 
					
						
							|  |  |  |       instance.meta = dsConfig.meta; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // store in instance cache
 | 
					
						
							| 
									
										
										
										
											2021-10-30 01:57:24 +08:00
										 |  |  |       this.datasources[key] = instance; | 
					
						
							|  |  |  |       this.datasources[instance.uid] = instance; | 
					
						
							| 
									
										
										
										
											2020-01-21 17:08:07 +08:00
										 |  |  |       return instance; | 
					
						
							|  |  |  |     } catch (err) { | 
					
						
							| 
									
										
										
										
											2021-10-30 01:57:24 +08:00
										 |  |  |       if (this.$rootScope) { | 
					
						
							|  |  |  |         this.$rootScope.appEvent(AppEvents.alertError, [dsConfig.name + ' plugin failed', err.toString()]); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       return Promise.reject({ message: `Datasource: ${key} was not found` }); | 
					
						
							| 
									
										
										
										
											2020-01-21 17:08:07 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-12-23 03:52:57 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-20 21:48:38 +08:00
										 |  |  |   getAll(): DataSourceInstanceSettings[] { | 
					
						
							| 
									
										
										
										
											2020-12-01 22:17:33 +08:00
										 |  |  |     return Object.values(this.settingsMapByName); | 
					
						
							| 
									
										
										
										
											2017-12-23 03:52:57 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 21:24:55 +08:00
										 |  |  |   getList(filters: GetDataSourceListFilters = {}): DataSourceInstanceSettings[] { | 
					
						
							| 
									
										
										
										
											2021-01-20 14:59:48 +08:00
										 |  |  |     const base = Object.values(this.settingsMapByName).filter((x) => { | 
					
						
							| 
									
										
										
										
											2020-12-04 21:24:55 +08:00
										 |  |  |       if (x.meta.id === 'grafana' || x.meta.id === 'mixed' || x.meta.id === 'dashboard') { | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       if (filters.metrics && !x.meta.metrics) { | 
					
						
							|  |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2017-12-23 03:52:57 +08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2021-04-14 20:57:36 +08:00
										 |  |  |       if (filters.alerting && !x.meta.alerting) { | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2020-12-04 21:24:55 +08:00
										 |  |  |       if (filters.tracing && !x.meta.tracing) { | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       if (filters.annotations && !x.meta.annotations) { | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2021-04-15 19:53:40 +08:00
										 |  |  |       if (filters.alerting && !x.meta.alerting) { | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2020-12-04 21:24:55 +08:00
										 |  |  |       if (filters.pluginId && x.meta.id !== filters.pluginId) { | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2021-04-14 20:57:36 +08:00
										 |  |  |       if (filters.filter && !filters.filter(x)) { | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2021-04-15 19:53:40 +08:00
										 |  |  |       if (filters.type && (Array.isArray(filters.type) ? !filters.type.includes(x.type) : filters.type !== x.type)) { | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2021-02-15 18:27:16 +08:00
										 |  |  |       if ( | 
					
						
							|  |  |  |         !filters.all && | 
					
						
							|  |  |  |         x.meta.metrics !== true && | 
					
						
							|  |  |  |         x.meta.annotations !== true && | 
					
						
							|  |  |  |         x.meta.tracing !== true && | 
					
						
							| 
									
										
										
										
											2021-04-15 19:53:40 +08:00
										 |  |  |         x.meta.logs !== true && | 
					
						
							|  |  |  |         x.meta.alerting !== true | 
					
						
							| 
									
										
										
										
											2021-02-15 18:27:16 +08:00
										 |  |  |       ) { | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2020-12-04 21:24:55 +08:00
										 |  |  |       return true; | 
					
						
							| 
									
										
										
										
											2017-12-23 03:52:57 +08:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 21:24:55 +08:00
										 |  |  |     if (filters.variables) { | 
					
						
							| 
									
										
										
										
											2021-01-20 14:59:48 +08:00
										 |  |  |       for (const variable of this.templateSrv.getVariables().filter((variable) => variable.type === 'datasource')) { | 
					
						
							| 
									
										
										
										
											2020-12-04 21:24:55 +08:00
										 |  |  |         const dsVar = variable as DataSourceVariableModel; | 
					
						
							|  |  |  |         const first = dsVar.current.value === 'default' ? this.defaultName : dsVar.current.value; | 
					
						
							|  |  |  |         const dsName = (first as unknown) as string; | 
					
						
							|  |  |  |         const dsSettings = this.settingsMapByName[dsName]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (dsSettings) { | 
					
						
							|  |  |  |           const key = `$\{${variable.name}\}`; | 
					
						
							|  |  |  |           base.push({ | 
					
						
							|  |  |  |             ...dsSettings, | 
					
						
							|  |  |  |             name: key, | 
					
						
							|  |  |  |           }); | 
					
						
							| 
									
										
										
										
											2017-12-23 03:52:57 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 21:24:55 +08:00
										 |  |  |     const sorted = base.sort((a, b) => { | 
					
						
							|  |  |  |       if (a.name.toLowerCase() > b.name.toLowerCase()) { | 
					
						
							| 
									
										
										
										
											2017-12-23 03:52:57 +08:00
										 |  |  |         return 1; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2020-12-04 21:24:55 +08:00
										 |  |  |       if (a.name.toLowerCase() < b.name.toLowerCase()) { | 
					
						
							| 
									
										
										
										
											2017-12-23 03:52:57 +08:00
										 |  |  |         return -1; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       return 0; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-14 20:57:36 +08:00
										 |  |  |     if (!filters.pluginId && !filters.alerting) { | 
					
						
							| 
									
										
										
										
											2020-12-04 21:24:55 +08:00
										 |  |  |       if (filters.mixed) { | 
					
						
							|  |  |  |         base.push(this.getInstanceSettings('-- Mixed --')!); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (filters.dashboard) { | 
					
						
							|  |  |  |         base.push(this.getInstanceSettings('-- Dashboard --')!); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (!filters.tracing) { | 
					
						
							|  |  |  |         base.push(this.getInstanceSettings('-- Grafana --')!); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return sorted; | 
					
						
							| 
									
										
										
										
											2017-12-23 03:52:57 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 21:24:55 +08:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * @deprecated use getList | 
					
						
							|  |  |  |    * */ | 
					
						
							|  |  |  |   getExternal(): DataSourceInstanceSettings[] { | 
					
						
							|  |  |  |     return this.getList(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * @deprecated use getList | 
					
						
							|  |  |  |    * */ | 
					
						
							|  |  |  |   getAnnotationSources() { | 
					
						
							| 
									
										
										
										
											2021-01-20 14:59:48 +08:00
										 |  |  |     return this.getList({ annotations: true, variables: true }).map((x) => { | 
					
						
							| 
									
										
										
										
											2020-12-04 21:24:55 +08:00
										 |  |  |       return { | 
					
						
							|  |  |  |         name: x.name, | 
					
						
							|  |  |  |         value: x.isDefault ? null : x.name, | 
					
						
							|  |  |  |         meta: x.meta, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * @deprecated use getList | 
					
						
							|  |  |  |    * */ | 
					
						
							|  |  |  |   getMetricSources(options?: { skipVariables?: boolean }): DataSourceSelectItem[] { | 
					
						
							| 
									
										
										
										
											2021-01-20 14:59:48 +08:00
										 |  |  |     return this.getList({ metrics: true, variables: !options?.skipVariables }).map((x) => { | 
					
						
							| 
									
										
										
										
											2020-12-04 21:24:55 +08:00
										 |  |  |       return { | 
					
						
							|  |  |  |         name: x.name, | 
					
						
							|  |  |  |         value: x.isDefault ? null : x.name, | 
					
						
							|  |  |  |         meta: x.meta, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function variableInterpolation(value: any[]) { | 
					
						
							|  |  |  |   if (Array.isArray(value)) { | 
					
						
							|  |  |  |     return value[0]; | 
					
						
							| 
									
										
										
										
											2017-12-23 03:52:57 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-12-04 21:24:55 +08:00
										 |  |  |   return value; | 
					
						
							| 
									
										
										
										
											2017-12-23 03:52:57 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-07 00:00:05 +08:00
										 |  |  | export const getDatasourceSrv = (): DatasourceSrv => { | 
					
						
							| 
									
										
										
										
											2019-06-03 23:55:59 +08:00
										 |  |  |   return getDataSourceService() as DatasourceSrv; | 
					
						
							| 
									
										
										
										
											2019-12-07 00:00:05 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2018-10-14 21:39:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 03:52:57 +08:00
										 |  |  | coreModule.service('datasourceSrv', DatasourceSrv); | 
					
						
							| 
									
										
										
										
											2018-07-13 15:09:36 +08:00
										 |  |  | export default DatasourceSrv; |