@grafana/data#DataQueryRequest TypeScript Examples

The following examples show how to use @grafana/data#DataQueryRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: datasource.test.ts    From grafana-chinese with Apache License 2.0 6 votes vote down vote up
getPrepareTargetsContext = (target: PromQuery, app?: CoreApp) => {
  const instanceSettings = ({
    url: 'proxied',
    directUrl: 'direct',
    user: 'test',
    password: 'mupp',
    jsonData: { httpMethod: 'POST' },
  } as unknown) as DataSourceInstanceSettings<PromOptions>;
  const start = 0;
  const end = 1;
  const panelId = '2';
  const options = ({ targets: [target], interval: '1s', panelId, app } as any) as DataQueryRequest<PromQuery>;

  const ds = new PrometheusDatasource(instanceSettings);
  const { queries, activeTargets } = ds.prepareTargets(options, start, end);

  return {
    queries,
    activeTargets,
    start,
    end,
    panelId,
  };
}
Example #2
Source File: datasource.ts    From grafana-kdb-datasource-ws with Apache License 2.0 5 votes vote down vote up
query(options: DataQueryRequest<MyQuery>): Promise<DataQueryResponse> {
    var prefilterResultCount = options.targets.length;

    if (prefilterResultCount == 0) {
      return new Promise((resolve) => {
        resolve({ data: [] });
      });
    }

    var allRefIDs = [];
    var blankRefIDs = [];
    var validRequestList = [];
    var errorList = [];

    for (var i = 0; i < prefilterResultCount; i++) {
      //Inject variables into target
      this.injectVariables(options.targets[i], options.scopedVars, options.range);

      // for some reason randomWalk is defaulted
      if (options.targets[i].queryType == 'randomWalk') {
        options.targets[i].queryType = 'selectQuery';
      }
      allRefIDs.push(options.targets[i].refId);
      options.targets[i].range = options.range;
      if (
        (!options.targets[i].table && options.targets[i].queryType === 'selectQuery') ||
        (options.targets[i].queryType === 'functionQuery' && options.targets[i].kdbFunction === '') ||
        options.targets[i].hide === true
      ) {
        blankRefIDs.push(options.targets[i].refId);
      } else if (!options.targets[i].queryError) {
        blankRefIDs.push(options.targets[i].refId);
      } else if (options.targets[i].queryError.error.indexOf(true) !== -1) {
        errorList.push({
          refId: options.targets[i].refId,
          errorMessage: options.targets[i].queryError.message[options.targets[i].queryError.error.indexOf(true)],
        });
      } else validRequestList.push(options.targets[i]);
    }

    var nrBlankRequests = blankRefIDs.length;
    var requestList = validRequestList.map((target) => {
      return this.buildKdbRequest(target);
    });

    var nrRequests: number = requestList.length;
    if (!this.ws || this.ws.readyState > 1)
      return this.connectWS().then((connectStatus) => {
        if (connectStatus === true && nrRequests > 0)
          return this.sendQueries(nrRequests, requestList, nrBlankRequests, blankRefIDs, errorList).then(
            (series: any) => {
              return this.buildDataFrames(series);
            }
          );
        else if (connectStatus === true && nrRequests === 0)
          return this.emptyQueries(nrBlankRequests, blankRefIDs, errorList).then(() => {
            return { data: [] };
          });
        else
          return this.connectFail(prefilterResultCount, allRefIDs).then(() => {
            return { data: [] };
          });
      });
    else {
      return this.webSocketWait().then(() => {
        if (nrRequests > 0)
          return this.sendQueries(nrRequests, requestList, nrBlankRequests, blankRefIDs, errorList).then(
            (series: any) => {
              return this.buildDataFrames(series);
            }
          );
        else
          return this.emptyQueries(nrBlankRequests, blankRefIDs, errorList).then(() => {
            return { data: [] };
          });
      });
    }
  }
Example #3
Source File: datasource.ts    From grafana-chinese with Apache License 2.0 5 votes vote down vote up
annotationQuery(options: any) {
    // Graphite metric as annotation
    if (options.annotation.target) {
      const target = this.templateSrv.replace(options.annotation.target, {}, 'glob');
      const graphiteQuery = ({
        rangeRaw: options.rangeRaw,
        targets: [{ target: target }],
        format: 'json',
        maxDataPoints: 100,
      } as unknown) as DataQueryRequest<GraphiteQuery>;

      return this.query(graphiteQuery).then(result => {
        const list = [];

        for (let i = 0; i < result.data.length; i++) {
          const target = result.data[i];

          for (let y = 0; y < target.length; y++) {
            const time = target.fields[1].values.get(y);
            const value = target.fields[0].values.get(y);

            if (!value) {
              continue;
            }

            list.push({
              annotation: options.annotation,
              time,
              title: target.name,
            });
          }
        }

        return list;
      });
    } else {
      // Graphite event as annotation
      const tags = this.templateSrv.replace(options.annotation.tags);
      return this.events({ range: options.rangeRaw, tags: tags }).then((results: any) => {
        const list = [];
        for (let i = 0; i < results.data.length; i++) {
          const e = results.data[i];

          let tags = e.tags;
          if (_.isString(e.tags)) {
            tags = this.parseTags(e.tags);
          }

          list.push({
            annotation: options.annotation,
            time: e.when * 1000,
            title: e.what,
            tags: tags,
            text: e.data,
          });
        }

        return list;
      });
    }
  }