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

Commit 18f6fee

Browse files
authored
Merge pull request #173 from doitintl/issue-171
Fixes #171
2 parents 90af5af + c89ea88 commit 18f6fee

File tree

4 files changed

+61842
-48
lines changed

4 files changed

+61842
-48
lines changed

dist/module.js

Lines changed: 61801 additions & 22 deletions
Large diffs are not rendered by default.

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/bigquery_query.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -510,16 +510,16 @@ export default class BigQueryQuery {
510510
}
511511

512512
public replaceTimeFilters(q, options) {
513-
let fromD = this.templateSrv.timeRange.from._d;
514-
let toD = this.templateSrv.timeRange.to._d;
513+
let fromD = options.range.from;
514+
let toD = options.range.to;
515515
if (this.target.convertToUTC === true) {
516516
fromD = new Date(
517-
this.templateSrv.timeRange.from._d.getTime() +
518-
this.templateSrv.timeRange.from._d.getTimezoneOffset() * 60000
517+
options.range.from._d.getTime() +
518+
options.range.from._d.getTimezoneOffset() * 60000
519519
);
520520
toD = new Date(
521-
this.templateSrv.timeRange.to._d.getTime() +
522-
this.templateSrv.timeRange.to._d.getTimezoneOffset() * 60000
521+
options.range.to._d.getTime() +
522+
options.range.to._d.getTimezoneOffset() * 60000
523523
);
524524
}
525525
let to = "";

src/datasource.ts

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import { validate } from "@babel/types";
2+
import { sheets } from "googleapis/build/src/apis/sheets";
13
import _ from "lodash";
4+
import { countBy, size } from "lodash-es";
25
import moment from "moment";
36
import BigQueryQuery from "./bigquery_query";
47
import ResponseParser, { IResultFormat } from "./response_parser";
5-
import {countBy, size} from "lodash-es";
6-
import {sheets} from "googleapis/build/src/apis/sheets";
7-
import {validate} from "@babel/types";
88

99
const Shifted = "_shifted";
1010
function sleep(ms) {
@@ -134,6 +134,21 @@ export class BigQueryDatasource {
134134
q = q.replace(to, newTo) + "\n ";
135135
return q;
136136
}
137+
138+
private static _updateTableSuffix(q, options) {
139+
const ind = q.indexOf("AND _TABLE_SUFFIX BETWEEN ");
140+
if (ind < 1) {
141+
return q;
142+
}
143+
const from = q.substr(ind + 28, 8);
144+
145+
const newFrom = BigQueryQuery.formatDateToString(options.range.from._d);
146+
q = q.replace(from, newFrom);
147+
const to = q.substr(ind + 43, 8);
148+
const newTo = BigQueryQuery.formatDateToString(options.range.to._d);
149+
q = q.replace(to, newTo) + "\n ";
150+
return q;
151+
}
137152
public authenticationType: string;
138153
public projectName: string;
139154
private readonly id: any;
@@ -233,6 +248,7 @@ export class BigQueryDatasource {
233248
);
234249
let q = this.queryModel.expend_macros(modOptions);
235250
q = BigQueryDatasource._updatePartition(q, modOptions);
251+
q = BigQueryDatasource._updateTableSuffix(q, modOptions);
236252
if (query.refId.search(Shifted) > -1) {
237253
q = this._updateAlias(q, modOptions, query.refId);
238254
}
@@ -251,29 +267,28 @@ export class BigQueryDatasource {
251267
for (const response of responses) {
252268
if (response.type && response.type === "table") {
253269
data.push(response);
254-
} else {
255-
for (const dp of response) {
256-
data.push(dp);
257-
}
270+
} else {
271+
for (const dp of response) {
272+
data.push(dp);
273+
}
258274
}
259275
}
260-
for (const d of data) {
276+
for (const d of data) {
261277
if (typeof d.target !== "undefined" && d.target.search(Shifted) > -1) {
262-
const res = BigQueryDatasource._getShiftPeriod(
263-
d.target.substring(d.target.lastIndexOf("_") + 1, d.target.length)
264-
);
265-
const shiftPeriod = res[0];
266-
const shiftVal = res[1];
267-
for(let i = 0; i < d.datapoints.length; i++){
268-
d.datapoints[i][1] = moment(d.datapoints[i][1])
278+
const res = BigQueryDatasource._getShiftPeriod(
279+
d.target.substring(d.target.lastIndexOf("_") + 1, d.target.length)
280+
);
281+
const shiftPeriod = res[0];
282+
const shiftVal = res[1];
283+
for (let i = 0; i < d.datapoints.length; i++) {
284+
d.datapoints[i][1] = moment(d.datapoints[i][1])
269285
.subtract(shiftVal, shiftPeriod)
270-
.valueOf();
271-
}
286+
.valueOf();
272287
}
273288
}
274-
return { data };
275289
}
276-
);
290+
return { data };
291+
});
277292
}
278293

279294
public metricFindQuery(query, optionalOptions) {

0 commit comments

Comments
 (0)