Skip to content

Commit

Permalink
Add epoch time macros
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusolsson committed Dec 4, 2020
1 parent 27a9877 commit d72a502
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,21 @@ export class DataSource extends DataSourceApi<JsonApiQuery, JsonApiDataSourceOpt
async query(request: DataQueryRequest<JsonApiQuery>): Promise<DataQueryResponse> {
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 });
Expand Down

0 comments on commit d72a502

Please sign in to comment.