Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
applyTemplateVariables(query: AwsAthenaQuery, scopedVars: ScopedVars) {
// TODO: pass scopedVars to templateSrv.replace()
const templateSrv = getTemplateSrv();
query.region = templateSrv.replace(query.region, scopedVars);
query.maxRows = query.maxRows || '';
query.cacheDuration = query.cacheDuration || '';
if (typeof query.queryString === 'undefined' || query.queryString === '') {
query.queryExecutionId = templateSrv.replace(query.queryExecutionId, scopedVars);
query.inputs = query.queryExecutionId.split(/,/).map(id => {
return {
queryExecutionId: id,
};
});
} else {
query.queryExecutionId = '';
query.inputs = [];
}
query.queryString = templateSrv.replace(query.queryString, scopedVars) || '';
query.outputLocation = this.outputLocation;
query(options: DataQueryRequest): Observable {
for (const target of options.targets) {
target.target = getTemplateSrv().replace(target.target, options.scopedVars);
}
// store the targets in activeTargets so we can
// access the legendFormat later on via the refId
let activeTargets: { [key: string]: any } = {};
for (const target of options.targets) {
if (target.hide) {
continue;
}
activeTargets[target.refId] = target;
}
return super.query(options).pipe(
map((rsp: DataQueryResponse) => {
const finalRsp: DataQueryResponse = { data: [], state: LoadingState.Done };
async getUrl(entityName: string, spaceName: string, datasource: string) {
const datasourceId = await this.datasourceNameToId(datasource);
if (entityName === 'spaces') {
return `api/datasources/${datasourceId}/resources/spaces/nameid`;
}
/**
* Get space names mapped to IDs
*/
const spaces = await fetch(`api/datasources/${datasourceId}/resources/spaces/nameid`).then(response =>
response.json()
);
const spaceNameFixed = getTemplateSrv().replace(spaceName);
// treat an empty space as the default, which is identified as a single space
const spaceNameDealWithDefault = spaceNameFixed || ' ';
if (spaces[spaceNameDealWithDefault]) {
return `api/datasources/${datasourceId}/resources/${spaces[spaceNameDealWithDefault]}/nameid/${entityName}`;
}
throw 'Space could not be found';
}
async getSpaceId(spaceName: string, datasource: string): Promise {
const url = await this.getUrl('spaces', '', datasource);
const entities = await fetch(url).then(response => response.json());
const entityNameFixed = getTemplateSrv().replace(spaceName);
const entityNameOrDefault = entityNameFixed === '' ? ' ' : entityNameFixed;
return entities[entityNameOrDefault] || '';
}
constructor(instanceSettings: DataSourceInstanceSettings) {
super(instanceSettings);
this.templateSrv = getTemplateSrv();
}
async getEntityId(spaceName: string, entityType: string, entityName: string, datasource: string): Promise {
const entityNameFixed = getTemplateSrv().replace(entityName);
const url = await this.getUrl(entityType, spaceName, datasource);
const entities = await fetch(url).then(response => response.json());
return entities[entityNameFixed] || '';
}
applyTemplateVariables(query: MyQuery) {
const templateSrv = getTemplateSrv();
return {
...query,
spaceName: query.spaceName ? templateSrv.replace(query.spaceName) : '',
projectName: query.projectName ? templateSrv.replace(query.projectName) : '',
tenantName: query.tenantName ? templateSrv.replace(query.tenantName) : '',
environmentName: query.environmentName ? templateSrv.replace(query.environmentName) : '',
channelName: query.channelName ? templateSrv.replace(query.channelName) : '',
releaseVersion: query.releaseVersion ? templateSrv.replace(query.releaseVersion) : '',
taskState: query.taskState ? templateSrv.replace(query.taskState) : '',
};
}
applyTemplateVariables(query: RedisQuery, scopedVars: ScopedVars) {
const templateSrv = getTemplateSrv();
/**
* Replace variables
*/
return {
...query,
keyName: query.keyName ? templateSrv.replace(query.keyName, scopedVars) : '',
query: query.query ? templateSrv.replace(query.query, scopedVars) : '',
field: query.field ? templateSrv.replace(query.field, scopedVars) : '',
filter: query.filter ? templateSrv.replace(query.filter, scopedVars) : '',
legend: query.legend ? templateSrv.replace(query.legend, scopedVars) : '',
value: query.value ? templateSrv.replace(query.value, scopedVars) : '',
};
}