From d72a5024e601f8b31b0fcaa45bb497247ee6e389 Mon Sep 17 00:00:00 2001 From: Marcus Olsson Date: Thu, 3 Dec 2020 18:38:19 +0100 Subject: [PATCH] Add epoch time macros --- src/datasource.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/datasource.ts b/src/datasource.ts index beaa22a..99a3920 100644 --- a/src/datasource.ts +++ b/src/datasource.ts @@ -27,15 +27,21 @@ export class DataSource extends DataSourceApi): Promise { const templateSrv = getTemplateSrv(); + const replaceMacros = (str: string) => { + return str + .replace(/\$__unixEpochFrom\(\)/g, request.range.from.unix().toString()) + .replace(/\$__unixEpochTo\(\)/g, request.range.to.unix().toString()); + }; + const promises = request.targets.map(async query => { - const queryParamsTreated = templateSrv.replace(query.queryParams, request.scopedVars); + const queryParamsTreated = replaceMacros(templateSrv.replace(query.queryParams, request.scopedVars)); const response = await this.api.cachedGet(query.cacheDurationSeconds, queryParamsTreated); const fields = query.fields .filter(field => field.jsonPath) .map(field => { - const jsonPathTreated = templateSrv.replace(field.jsonPath, request.scopedVars); + const jsonPathTreated = replaceMacros(templateSrv.replace(field.jsonPath, request.scopedVars)); const nameTreated = templateSrv.replace(field.name, request.scopedVars); const values = JSONPath({ path: jsonPathTreated, json: response });