Skip to content
This repository was archived by the owner on Dec 11, 2022. It is now read-only.

Commit 2088739

Browse files
authored
Merge pull request #72 from doitintl/issue-38
Support BQ queries in variables
2 parents 857e928 + aa2f51b commit 2088739

File tree

5 files changed

+169
-7
lines changed

5 files changed

+169
-7
lines changed

dist/module.js

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51885,12 +51885,13 @@ var BigQueryDatasource =
5188551885
/** @class */
5188651886
function () {
5188751887
/** @ngInject */
51888-
function BigQueryDatasource(instanceSettings, backendSrv, $q, templateSrv) {
51888+
function BigQueryDatasource(instanceSettings, backendSrv, $q, templateSrv, timeSrv) {
5188951889
var _this = this;
5189051890

5189151891
this.backendSrv = backendSrv;
5189251892
this.$q = $q;
5189351893
this.templateSrv = templateSrv;
51894+
this.timeSrv = timeSrv;
5189451895

5189551896
this.interpolateVariable = function (value, variable) {
5189651897
if (typeof value === "string") {
@@ -52182,6 +52183,30 @@ function () {
5218252183
});
5218352184
};
5218452185

52186+
BigQueryDatasource.prototype.metricFindQuery = function (query, optionalOptions) {
52187+
var refId = "tempvar";
52188+
52189+
if (optionalOptions && optionalOptions.variable && optionalOptions.variable.name) {
52190+
refId = optionalOptions.variable.name;
52191+
}
52192+
52193+
var interpolatedQuery = {
52194+
datasourceId: this.id,
52195+
format: "table",
52196+
rawSql: this.templateSrv.replace(query, {}, this.interpolateVariable),
52197+
refId: refId
52198+
};
52199+
var range = this.timeSrv.timeRange();
52200+
var data = {
52201+
from: range.from.valueOf().toString(),
52202+
queries: [interpolatedQuery],
52203+
to: range.to.valueOf().toString()
52204+
};
52205+
return this.doQuery(query, refId).then(function (metricData) {
52206+
return _response_parser2.default.parseDataQuery(metricData, "var");
52207+
});
52208+
};
52209+
5218552210
BigQueryDatasource.prototype.testDatasource = function () {
5218652211
return tslib_1.__awaiter(this, void 0, void 0, function () {
5218752212
var status, message, defaultErrorMessage, path, response, error_1, path, response, error_2;
@@ -53814,9 +53839,17 @@ function () {
5381453839

5381553840
if (format === "time_series") {
5381653841
return ResponseParser._toTimeSeries(results);
53817-
} else {
53842+
}
53843+
53844+
if (format === "table") {
5381853845
return ResponseParser._toTable(results);
5381953846
}
53847+
53848+
if (format === "var") {
53849+
return ResponseParser._toVar(results);
53850+
}
53851+
53852+
return [];
5382053853
};
5382153854

5382253855
ResponseParser._convertValues = function (v, type) {
@@ -54003,6 +54036,21 @@ function () {
5400354036
};
5400454037
};
5400554038

54039+
ResponseParser._toVar = function (results) {
54040+
var res = [];
54041+
54042+
for (var _i = 0, _a = results.rows; _i < _a.length; _i++) {
54043+
var row = _a[_i];
54044+
res.push(row.f[0].v);
54045+
}
54046+
54047+
return _lodash2.default.map(res, function (value) {
54048+
return {
54049+
text: value
54050+
};
54051+
});
54052+
};
54053+
5400654054
ResponseParser.prototype.parseTabels = function (results) {
5400754055
return this._handelWildCardTables(ResponseParser.parseData(results, "tableReference.tableId", "tableReference.tableId"));
5400854056
};

dist/module.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/datasource.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import BigQueryQuery from "./bigquery_query";
44
import ResponseParser, { IResultFormat } from "./response_parser";
55
import {countBy, size} from "lodash-es";
66
import {sheets} from "googleapis/build/src/apis/sheets";
7+
import {validate} from "@babel/types";
78

89
const Shifted = "_shifted";
910
function sleep(ms) {
@@ -148,7 +149,8 @@ export class BigQueryDatasource {
148149
instanceSettings,
149150
private backendSrv,
150151
private $q,
151-
private templateSrv
152+
private templateSrv,
153+
private timeSrv
152154
) {
153155
this.name = instanceSettings.name;
154156
this.id = instanceSettings.id;
@@ -246,6 +248,33 @@ export class BigQueryDatasource {
246248
);
247249
}
248250

251+
public metricFindQuery(query, optionalOptions) {
252+
let refId = "tempvar";
253+
if (
254+
optionalOptions &&
255+
optionalOptions.variable &&
256+
optionalOptions.variable.name
257+
) {
258+
refId = optionalOptions.variable.name;
259+
}
260+
261+
const interpolatedQuery = {
262+
datasourceId: this.id,
263+
format: "table",
264+
rawSql: this.templateSrv.replace(query, {}, this.interpolateVariable),
265+
refId
266+
};
267+
268+
const range = this.timeSrv.timeRange();
269+
const data = {
270+
from: range.from.valueOf().toString(),
271+
queries: [interpolatedQuery],
272+
to: range.to.valueOf().toString()
273+
};
274+
return this.doQuery(query, refId).then(metricData =>
275+
ResponseParser.parseDataQuery(metricData, "var")
276+
);
277+
}
249278
public async testDatasource() {
250279
let status = "success";
251280
let message = "Successfully queried the BigQuery API.";

src/response_parser.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,14 @@ export default class ResponseParser {
6060
}
6161
if (format === "time_series") {
6262
return ResponseParser._toTimeSeries(results);
63-
} else {
63+
}
64+
if (format === "table") {
6465
return ResponseParser._toTable(results);
6566
}
67+
if (format === "var") {
68+
return ResponseParser._toVar(results);
69+
}
70+
return [];
6671
}
6772

6873
public static _convertValues(v, type) {
@@ -226,6 +231,17 @@ export default class ResponseParser {
226231
};
227232
}
228233

234+
private static _toVar(results) {
235+
const res = [];
236+
for (const row of results.rows) {
237+
res.push(row.f[0].v);
238+
}
239+
240+
return _.map(res, value => {
241+
return { text: value };
242+
});
243+
}
244+
229245
constructor(private $q) {}
230246

231247
public parseTabels(results): IResultFormat[] {

src/specs/datasource.test.ts

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ describe("BigQueryDatasource", () => {
1212
const templateSrv = {
1313
replace: jest.fn(text => text)
1414
};
15+
const timeSrv = {};
1516
const raw = {
1617
from: moment.utc("2018-04-25 10:00"),
1718
to: moment.utc("2018-04-25 11:00")
@@ -32,7 +33,8 @@ describe("BigQueryDatasource", () => {
3233
instanceSettings,
3334
backendSrv,
3435
{},
35-
templateSrv
36+
templateSrv,
37+
timeSrv
3638
);
3739
ctx.ds.projectName = "my project";
3840
});
@@ -970,14 +972,81 @@ describe("BigQueryDatasource", () => {
970972
};
971973

972974
results = ResponseParser.parseDataQuery(response, "time_series");
973-
it("should return a table", () => {
975+
it("should return a time_series", () => {
974976
expect(results[0].datapoints.length).toBe(3);
975977
expect(results[0].datapoints[0][0]).toBe(37.7753058);
976978
expect(results[0].datapoints[0][1]).toBe(1521578851000);
977979
expect(results[0].datapoints[2][0]).toBe(37.781752);
978980
expect(results[0].datapoints[2][1]).toBe(1521578927000);
979981
});
980982
});
983+
describe("When performing parseDataQuery for vars", () => {
984+
let results;
985+
const response = {
986+
kind: "bigquery#queryResponse",
987+
schema: {
988+
fields: [
989+
{
990+
name: "time",
991+
type: "TIMESTAMP",
992+
mode: "NULLABLE"
993+
},
994+
{
995+
name: "start_station_latitude",
996+
type: "FLOAT",
997+
mode: "NULLABLE"
998+
}
999+
]
1000+
},
1001+
jobReference: {
1002+
projectId: "proj-1",
1003+
jobId: "job_fB4qCDAO-TKg1Orc-OrkdIRxCGN5",
1004+
location: "US"
1005+
},
1006+
totalRows: "3",
1007+
rows: [
1008+
{
1009+
f: [
1010+
{
1011+
v: "1.521578851E9"
1012+
},
1013+
{
1014+
v: "37.7753058"
1015+
}
1016+
]
1017+
},
1018+
{
1019+
f: [
1020+
{
1021+
v: "1.521578916E9"
1022+
},
1023+
{
1024+
v: "37.3322326"
1025+
}
1026+
]
1027+
},
1028+
{
1029+
f: [
1030+
{
1031+
v: "1.521578927E9"
1032+
},
1033+
{
1034+
v: "37.781752"
1035+
}
1036+
]
1037+
}
1038+
],
1039+
totalBytesProcessed: "23289520",
1040+
jobComplete: true,
1041+
cacheHit: false
1042+
};
1043+
1044+
results = ResponseParser.parseDataQuery(response, "var");
1045+
it("should return a var", () => {
1046+
expect(results.length).toBe(3);
1047+
expect(results[0].text).toBe("1.521578851E9");
1048+
});
1049+
});
9811050

9821051
describe("When performing testDatasource", () => {
9831052
let results;

0 commit comments

Comments
 (0)