Skip to content

Variable interpolation for SQL doesnt work in dashboard #21

Open
@kcrandall

Description

@kcrandall

I can fix after my last pr is approved the code to fix it is

All changes add in datasource.ts

import { getBackendSrv, getTemplateSrv } from '@grafana/runtime';
applyTemplateVariables(query: MyQuery, scopedVars: any): MyQuery {
    return {
      ...query,
      query: getTemplateSrv().replace(query.query || '', scopedVars),
    };
  }
async query(options: DataQueryRequest<MyQuery>): Promise<DataQueryResponse> {
    const timestamps = getConsumableTime(options.range);
    const interpolatedTargets = options.targets.map((target) => {
      return this.applyTemplateVariables(target, options.scopedVars);
    });
    const promises = interpolatedTargets.map((target) => {
      if (!this.cachedQuery.data) {
        this.cachedQuery.data = new Promise((resolve, reject) => {
          this.cachedQuery.promise = {
            resolve,
            reject,
          };
        });
      }

after this is deployed something like this will work

select * from "default"
WHERE account_id = ${AccountId}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions