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

Commit 3145602

Browse files
committed
Fixes #220
1 parent 9f9faba commit 3145602

File tree

7 files changed

+38
-23
lines changed

7 files changed

+38
-23
lines changed

dist/module.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58744,7 +58744,11 @@ function () {
5874458744
return q.replace(/(\$__timeShifting\().*?(?=\))./g, "");
5874558745
};
5874658746

58747-
BigQueryQuery.prototype.getIntervalStr = function (interval, mininterval) {
58747+
BigQueryQuery.prototype.getIntervalStr = function (interval, mininterval, options) {
58748+
if (interval === "auto") {
58749+
interval = this._calcAutoInterval(options);
58750+
}
58751+
5874858752
var res = _datasource.BigQueryDatasource._getShiftPeriod(interval);
5874958753

5875058754
var groupPeriod = res[0];
@@ -59221,8 +59225,8 @@ function () {
5922159225
var q = this.target.rawSql;
5922259226
q = BigQueryQuery.replaceTimeShift(q);
5922359227
q = this.replaceTimeFilters(q, options);
59224-
q = this.replacetimeGroupAlias(q, true);
59225-
q = this.replacetimeGroupAlias(q, false);
59228+
q = this.replacetimeGroupAlias(q, true, options);
59229+
q = this.replacetimeGroupAlias(q, false, options);
5922659230
return q;
5922759231
}
5922859232
};
@@ -59270,7 +59274,7 @@ function () {
5927059274
return q;
5927159275
};
5927259276

59273-
BigQueryQuery.prototype.replacetimeGroupAlias = function (q, alias) {
59277+
BigQueryQuery.prototype.replacetimeGroupAlias = function (q, alias, options) {
5927459278
var res = BigQueryQuery._getInterval(q, alias);
5927559279

5927659280
var interval = res[0];
@@ -59280,7 +59284,7 @@ function () {
5928059284
return q;
5928159285
}
5928259286

59283-
var intervalStr = this.getIntervalStr(interval, mininterval);
59287+
var intervalStr = this.getIntervalStr(interval, mininterval, options);
5928459288

5928559289
if (alias) {
5928659290
return q.replace(/\$__timeGroupAlias\(([\w_.]+,+[a-zA-Z0-9_ ]+.*\))/g, intervalStr);
@@ -59297,6 +59301,11 @@ function () {
5929759301
return BigQueryQuery.quoteFiledName(this.target.timeColumn);
5929859302
};
5929959303

59304+
BigQueryQuery.prototype._calcAutoInterval = function (options) {
59305+
var seconds = (this.templateSrv.timeRange.to._d - this.templateSrv.timeRange.from._d) / 1000;
59306+
return Math.round(seconds / options.maxDataPoints) + "s";
59307+
};
59308+
5930059309
return BigQueryQuery;
5930159310
}();
5930259311

@@ -60228,7 +60237,6 @@ function () {
6022860237
};
6022960238

6023060239
BigQueryDatasource.prototype.setUpQ = function (modOptions, options, query) {
60231-
console.log("setUpQ this.queryModel.target.rawSql", this.queryModel.target.rawSql);
6023260240
var q = this.queryModel.expend_macros(modOptions);
6023360241
q = BigQueryDatasource._updatePartition(q, modOptions);
6023460242
q = BigQueryDatasource._updateTableSuffix(q, modOptions);
@@ -62228,7 +62236,7 @@ register({
6222862236
label: 'time',
6222962237
params: [{
6223062238
name: "interval",
62231-
options: ["$__interval", "1s", "1min", "1h", "1d", "1w", "1m", "1y"],
62239+
options: ["$__interval", "1s", "1min", "1h", "1d", "1w", "1m", "1y", "auto"],
6223262240
type: "interval"
6223362241
}, {
6223462242
name: 'mininterval',

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: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,10 @@ export default class BigQueryQuery {
135135
this.interpolateQueryStr = this.interpolateQueryStr.bind(this);
136136
}
137137

138-
public getIntervalStr(interval: string, mininterval: string) {
138+
public getIntervalStr(interval: string, mininterval: string, options) {
139+
if (interval === "auto") {
140+
interval = this._calcAutoInterval(options);
141+
}
139142
const res = BigQueryDatasource._getShiftPeriod(interval);
140143
const groupPeriod = res[0];
141144
let IntervalStr =
@@ -618,8 +621,8 @@ export default class BigQueryQuery {
618621
let q = this.target.rawSql;
619622
q = BigQueryQuery.replaceTimeShift(q);
620623
q = this.replaceTimeFilters(q, options);
621-
q = this.replacetimeGroupAlias(q, true);
622-
q = this.replacetimeGroupAlias(q, false);
624+
q = this.replacetimeGroupAlias(q, true, options);
625+
q = this.replacetimeGroupAlias(q, false, options);
623626
return q;
624627
}
625628
}
@@ -673,14 +676,14 @@ export default class BigQueryQuery {
673676
return q;
674677
}
675678

676-
public replacetimeGroupAlias(q, alias: boolean) {
679+
public replacetimeGroupAlias(q, alias: boolean, options) {
677680
const res = BigQueryQuery._getInterval(q, alias);
678681
const interval = res[0];
679682
const mininterval = res[1];
680683
if (!interval) {
681684
return q;
682685
}
683-
const intervalStr = this.getIntervalStr(interval, mininterval);
686+
const intervalStr = this.getIntervalStr(interval, mininterval, options);
684687
if (alias) {
685688
return q.replace(
686689
/\$__timeGroupAlias\(([\w_.]+,+[a-zA-Z0-9_ ]+.*\))/g,
@@ -704,4 +707,10 @@ export default class BigQueryQuery {
704707
}
705708
return BigQueryQuery.quoteFiledName(this.target.timeColumn);
706709
}
710+
private _calcAutoInterval(options) {
711+
const seconds =
712+
(this.templateSrv.timeRange.to._d - this.templateSrv.timeRange.from._d) /
713+
1000;
714+
return Math.round(seconds / options.maxDataPoints) + "s";
715+
}
707716
}

src/datasource.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,6 @@ export class BigQueryDatasource {
520520
);
521521
}
522522
private setUpQ(modOptions, options, query) {
523-
console.log("setUpQ this.queryModel.target.rawSql", this.queryModel.target.rawSql)
524523
let q = this.queryModel.expend_macros(modOptions);
525524
q = BigQueryDatasource._updatePartition(q, modOptions);
526525
q = BigQueryDatasource._updateTableSuffix(q, modOptions);

src/query_ctrl.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,6 @@ export class BigQueryQueryCtrl extends QueryCtrl {
844844
params = ["$__interval", "none"];
845845
}
846846
const partModel = sqlPart.create({ type: partType, params });
847-
848847
if (partType === "time") {
849848
// put timeGroup at start
850849
this.groupParts.splice(0, 0, partModel);

src/specs/bigquery_query.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -331,25 +331,25 @@ describe("BigQueryQuery", () => {
331331
where: []
332332
};
333333
const query = new BigQueryQuery(target, templateSrv);
334-
expect(query.getIntervalStr("1s", "0")).toBe(
334+
expect(query.getIntervalStr("1s", "0", null)).toBe(
335335
"TIMESTAMP_SECONDS(DIV(UNIX_SECONDS(`my_data`), 1) * 1) AS time"
336336
);
337-
expect(query.getIntervalStr("1min", "0")).toBe(
337+
expect(query.getIntervalStr("1min", "0", null)).toBe(
338338
"TIMESTAMP_SECONDS(DIV(UNIX_SECONDS(`my_data`), 60) * 60) AS time"
339339
);
340-
expect(query.getIntervalStr("1h", "1s")).toBe(
340+
expect(query.getIntervalStr("1h", "1s", null)).toBe(
341341
"TIMESTAMP_SECONDS(DIV(UNIX_SECONDS(`my_data`), 3600) * 3600) AS time"
342342
);
343-
expect(query.getIntervalStr("1d", "1s")).toBe(
343+
expect(query.getIntervalStr("1d", "1s", null)).toBe(
344344
"TIMESTAMP_SECONDS(DIV(UNIX_SECONDS(`my_data`), 86400) * 86400) AS time"
345345
);
346-
expect(query.getIntervalStr("1w", "1d")).toBe(
346+
expect(query.getIntervalStr("1w", "1d", null)).toBe(
347347
"TIMESTAMP_SECONDS(DIV(UNIX_SECONDS(`my_data`), 604800) * 604800) AS time"
348348
);
349-
expect(query.getIntervalStr("1m", "1d")).toBe(
349+
expect(query.getIntervalStr("1m", "1d", null)).toBe(
350350
"TIMESTAMP( (PARSE_DATE( \"%Y-%m-%d\",CONCAT( CAST((EXTRACT(YEAR FROM `my_data`)) AS STRING),'-',CAST((EXTRACT(MONTH FROM `my_data`)) AS STRING),'-','01')))) AS time"
351351
);
352-
expect(query.getIntervalStr("1y", "2d")).toBe(
352+
expect(query.getIntervalStr("1y", "2d", null)).toBe(
353353
"TIMESTAMP_SECONDS(DIV(UNIX_SECONDS(`my_data`), 31536000) * 31536000) AS time"
354354
);
355355
});

src/sql_part.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ register({
155155
params: [
156156
{
157157
name: "interval",
158-
options: ["$__interval", "1s", "1min", "1h", "1d", "1w", "1m", "1y"],
158+
options: ["$__interval", "1s", "1min", "1h", "1d", "1w", "1m", "1y", "auto"],
159159
type: "interval"
160160
},
161161
{

0 commit comments

Comments
 (0)