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

Commit

Permalink
Solves #153
Browse files Browse the repository at this point in the history
Since BQ can't handle aliases in the where clause you will need to use the having clause:
```
SELECT
  end_time AS time,
  name as text,
  type as tags
FROM
  <some table name> group by 1,2,3
HAVING
  $__timeFilter(time)
```
  • Loading branch information
avivl committed Nov 3, 2019
1 parent 744e7d4 commit e15bd64
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 126 deletions.
10 changes: 6 additions & 4 deletions dist/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -59479,7 +59479,6 @@ function () {
q = _this._updateAlias(q, modOptions, query.refId);
}

console.log(q);
var limit = q.match(/[^]+(\bLIMIT\b)/gi);

if (limit == null) {
Expand Down Expand Up @@ -59786,12 +59785,15 @@ function () {
});
}

var rawSql = this.templateSrv.replace(options.annotation.rawQuery, options.scopedVars, this.interpolateVariable);
var query = {
datasourceId: this.id,
format: "table",
rawSql: this.templateSrv.replace(options.annotation.rawQuery, options.scopedVars, this.interpolateVariable),
rawSql: rawSql,
refId: options.annotation.name
};
this.queryModel.target.rawSql = query.rawSql;
query.rawSql = this.queryModel.expend_macros(options);
return this.backendSrv.datasourceRequest({
data: {
query: query.rawSql,
Expand Down Expand Up @@ -61447,9 +61449,9 @@ function () {
var row = _a[_i];
list.push({
annotation: options.annotation,
tags: row[tagsColumnIndex] ? row[tagsColumnIndex].trim().split(/\s*,\s*/) : [],
tags: row.f[tagsColumnIndex].v ? row.f[tagsColumnIndex].v.trim().split(/\s*,\s*/) : [],
text: row.f[textColumnIndex],
time: new Date(Number(Math.floor(Number(row.f[timeColumnIndex].v))) * 1000).toString(),
time: Number(Math.floor(Number(row.f[timeColumnIndex].v))) * 1000,
title: row.f[titleColumnIndex]
});
}
Expand Down
2 changes: 1 addition & 1 deletion dist/module.js.map

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ export class BigQueryDatasource {
if (query.refId.search(Shifted) > -1) {
q = this._updateAlias(q, modOptions, query.refId);
}
console.log(q)
const limit = q.match(/[^]+(\bLIMIT\b)/gi);
if (limit == null) {
q += " LIMIT " + options.maxDataPoints;
Expand Down Expand Up @@ -396,17 +395,20 @@ export class BigQueryDatasource {
message: "Query missing in annotation definition"
});
}
const rawSql = this.templateSrv.replace(
options.annotation.rawQuery,
options.scopedVars,
this.interpolateVariable
);

const query = {
datasourceId: this.id,
format: "table",
rawSql: this.templateSrv.replace(
options.annotation.rawQuery,
options.scopedVars,
this.interpolateVariable
),
rawSql,
refId: options.annotation.name
};
this.queryModel.target.rawSql = query.rawSql;
query.rawSql = this.queryModel.expend_macros(options);
return this.backendSrv
.datasourceRequest({
data: {
Expand All @@ -418,7 +420,7 @@ export class BigQueryDatasource {
},
method: "POST",
requestId: options.annotation.name,
url: url
url
})
.then(data =>
this.responseParser.transformAnnotationResponse(options, data)
Expand Down
10 changes: 3 additions & 7 deletions src/response_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ export default class ResponseParser {
const titleColumnIndex = -1;
let textColumnIndex = -1;
let tagsColumnIndex = -1;

for (let i = 0; i < data.data.schema.fields.length; i++) {
if (data.data.schema.fields[i].name === "time") {
timeColumnIndex = i;
Expand All @@ -279,19 +278,16 @@ export default class ResponseParser {
for (const row of table.rows) {
list.push({
annotation: options.annotation,
tags: row[tagsColumnIndex]
? row[tagsColumnIndex].trim().split(/\s*,\s*/)
tags: row.f[tagsColumnIndex].v
? row.f[tagsColumnIndex].v.trim().split(/\s*,\s*/)
: [],
text: row.f[textColumnIndex],
time: new Date(
Number(Math.floor(Number(row.f[timeColumnIndex].v))) * 1000
).toString(),
time: Number(Math.floor(Number(row.f[timeColumnIndex].v))) * 1000,
title: row.f[titleColumnIndex]
});
}
return list;
}

private _handelWildCardTables(tables) {
let sorted = new Map();
let newTables = [];
Expand Down
107 changes: 0 additions & 107 deletions src/specs/datasource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,113 +358,6 @@ describe("BigQueryDatasource", () => {
expect(results.length).toBe(6);
});
});

describe("When performing annotationQuery", () => {
let results;

const annotationName = "MyAnno";

const options = {
annotation: {
name: annotationName,
rawQuery: "select time, title, text, tags from table;"
},
range: {
from: moment(1432288354),
to: moment(1432288401)
}
};
const response = {
kind: "bigquery#queryResponse",
schema: {
fields: [
{
name: "time",
type: "TIMESTAMP",
mode: "NULLABLE"
},
{
name: "text",
type: "text",
mode: "NULLABLE"
}
]
},
jobReference: {
projectId: "aviv-playground",
jobId: "job_fB4qCDAO-TKg1Orc-OrkdIRxCGN5",
location: "US"
},
totalRows: "3",
rows: [
{
f: [
{
v: "1.521578851E9"
},
{
v: "some text"
}
]
},
{
f: [
{
v: "1.521578916E9"
},
{
v: "some text2"
}
]
},
{
f: [
{
v: "1.521578927E9"
},
{
v: "some text3"
}
]
}
],
totalBytesProcessed: "23289520",
jobComplete: true,
cacheHit: false
};
/* const response = {
results: {
MyAnno: {
refId: annotationName,
data: [
{
columns: [{ text: "time" }, { text: "text" }, { text: "tags" }],
rows: [
[1432288355, "some text", "TagA,TagB"],
[1432288390, "some text2", " TagB , TagC"],
[1432288400, "some text3"]
]
}
]
}
}
};*/

beforeEach(() => {
ctx.backendSrv.datasourceRequest = jest.fn(options => {
return Promise.resolve({ data: response, status: 200 });
});
ctx.ds.annotationQuery(options).then(data => {
results = data;
});
});
it("should return annotation list", () => {
expect(results.length).toBe(3);

expect(results[0].text.v).toBe("some text");
});
});

describe("When performing getProjects", () => {
let queryResults;
const response = {
Expand Down

0 comments on commit e15bd64

Please sign in to comment.