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

Commit 7c6de8c

Browse files
authored
Merge pull request #115 from doitintl/issue-106
Fixes #106
2 parents e02dcd2 + b38fefe commit 7c6de8c

File tree

5 files changed

+98
-35
lines changed

5 files changed

+98
-35
lines changed

dist/module.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52477,6 +52477,9 @@ function () {
5247752477
BigQueryDatasource.prototype.annotationQuery = function (options) {
5247852478
var _this = this;
5247952479

52480+
var path = "v2/projects/" + this.projectName + "/queries";
52481+
var url = this.url + ("" + this.baseUrl + path);
52482+
5248052483
if (!options.annotation.rawQuery) {
5248152484
return this.$q.reject({
5248252485
message: "Query missing in annotation definition"
@@ -52491,12 +52494,15 @@ function () {
5249152494
};
5249252495
return this.backendSrv.datasourceRequest({
5249352496
data: {
52497+
query: query.rawSql,
5249452498
from: options.range.from.valueOf().toString(),
52495-
queries: [query],
52496-
to: options.range.to.valueOf().toString()
52499+
to: options.range.to.valueOf().toString(),
52500+
useLegacySql: false,
52501+
useQueryCache: true
5249752502
},
5249852503
method: "POST",
52499-
url: "/api/tsdb/query"
52504+
requestId: options.annotation.name,
52505+
url: url
5250052506
}).then(function (data) {
5250152507
return _this.responseParser.transformAnnotationResponse(options, data);
5250252508
});
@@ -54113,18 +54119,18 @@ function () {
5411354119
};
5411454120

5411554121
ResponseParser.prototype.transformAnnotationResponse = function (options, data) {
54116-
var table = data.data.results[options.annotation.name].tables[0];
54122+
var table = data.data;
5411754123
var timeColumnIndex = -1;
5411854124
var titleColumnIndex = -1;
5411954125
var textColumnIndex = -1;
5412054126
var tagsColumnIndex = -1;
5412154127

54122-
for (var i = 0; i < table.columns.length; i++) {
54123-
if (table.columns[i].text === "time") {
54128+
for (var i = 0; i < data.data.schema.fields.length; i++) {
54129+
if (data.data.schema.fields[i].name === "time") {
5412454130
timeColumnIndex = i;
54125-
} else if (table.columns[i].text === "text") {
54131+
} else if (data.data.schema.fields[i].name === "text") {
5412654132
textColumnIndex = i;
54127-
} else if (table.columns[i].text === "tags") {
54133+
} else if (data.data.schema.fields[i].name === "tags") {
5412854134
tagsColumnIndex = i;
5412954135
}
5413054136
}
@@ -54142,9 +54148,9 @@ function () {
5414254148
list.push({
5414354149
annotation: options.annotation,
5414454150
tags: row[tagsColumnIndex] ? row[tagsColumnIndex].trim().split(/\s*,\s*/) : [],
54145-
text: row[textColumnIndex],
54146-
time: Math.floor(row[timeColumnIndex]),
54147-
title: row[titleColumnIndex]
54151+
text: row.f[textColumnIndex],
54152+
time: new Date(Number(Math.floor(Number(row.f[timeColumnIndex].v))) * 1000).toString(),
54153+
title: row.f[titleColumnIndex]
5414854154
});
5414954155
}
5415054156

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: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,8 @@ export class BigQueryDatasource {
361361
}
362362

363363
public annotationQuery(options) {
364+
const path = `v2/projects/${this.projectName}/queries`;
365+
const url = this.url + `${this.baseUrl}${path}`;
364366
if (!options.annotation.rawQuery) {
365367
return this.$q.reject({
366368
message: "Query missing in annotation definition"
@@ -380,12 +382,15 @@ export class BigQueryDatasource {
380382
return this.backendSrv
381383
.datasourceRequest({
382384
data: {
385+
query: query.rawSql,
383386
from: options.range.from.valueOf().toString(),
384-
queries: [query],
385-
to: options.range.to.valueOf().toString()
387+
to: options.range.to.valueOf().toString(),
388+
useLegacySql: false,
389+
useQueryCache: true
386390
},
387391
method: "POST",
388-
url: "/api/tsdb/query"
392+
requestId: options.annotation.name,
393+
url: url
389394
})
390395
.then(data =>
391396
this.responseParser.transformAnnotationResponse(options, data)

src/response_parser.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -255,18 +255,18 @@ export default class ResponseParser {
255255
}
256256

257257
public transformAnnotationResponse(options, data) {
258-
const table = data.data.results[options.annotation.name].tables[0];
258+
const table = data.data;
259259
let timeColumnIndex = -1;
260260
const titleColumnIndex = -1;
261261
let textColumnIndex = -1;
262262
let tagsColumnIndex = -1;
263263

264-
for (let i = 0; i < table.columns.length; i++) {
265-
if (table.columns[i].text === "time") {
264+
for (let i = 0; i < data.data.schema.fields.length; i++) {
265+
if (data.data.schema.fields[i].name === "time") {
266266
timeColumnIndex = i;
267-
} else if (table.columns[i].text === "text") {
267+
} else if (data.data.schema.fields[i].name === "text") {
268268
textColumnIndex = i;
269-
} else if (table.columns[i].text === "tags") {
269+
} else if (data.data.schema.fields[i].name === "tags") {
270270
tagsColumnIndex = i;
271271
}
272272
}
@@ -282,9 +282,11 @@ export default class ResponseParser {
282282
tags: row[tagsColumnIndex]
283283
? row[tagsColumnIndex].trim().split(/\s*,\s*/)
284284
: [],
285-
text: row[textColumnIndex],
286-
time: Math.floor(row[timeColumnIndex]),
287-
title: row[titleColumnIndex]
285+
text: row.f[textColumnIndex],
286+
time: new Date(
287+
Number(Math.floor(Number(row.f[timeColumnIndex].v))) * 1000
288+
).toString(),
289+
title: row.f[titleColumnIndex]
288290
});
289291
}
290292
return list;

src/specs/datasource.test.ts

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -371,12 +371,69 @@ describe("BigQueryDatasource", () => {
371371
to: moment(1432288401)
372372
}
373373
};
374-
375374
const response = {
375+
kind: "bigquery#queryResponse",
376+
schema: {
377+
fields: [
378+
{
379+
name: "time",
380+
type: "TIMESTAMP",
381+
mode: "NULLABLE"
382+
},
383+
{
384+
name: "text",
385+
type: "text",
386+
mode: "NULLABLE"
387+
}
388+
]
389+
},
390+
jobReference: {
391+
projectId: "aviv-playground",
392+
jobId: "job_fB4qCDAO-TKg1Orc-OrkdIRxCGN5",
393+
location: "US"
394+
},
395+
totalRows: "3",
396+
rows: [
397+
{
398+
f: [
399+
{
400+
v: "1.521578851E9"
401+
},
402+
{
403+
v: "some text"
404+
}
405+
]
406+
},
407+
{
408+
f: [
409+
{
410+
v: "1.521578916E9"
411+
},
412+
{
413+
v: "some text2"
414+
}
415+
]
416+
},
417+
{
418+
f: [
419+
{
420+
v: "1.521578927E9"
421+
},
422+
{
423+
v: "some text3"
424+
}
425+
]
426+
}
427+
],
428+
totalBytesProcessed: "23289520",
429+
jobComplete: true,
430+
cacheHit: false
431+
};
432+
/* const response = {
376433
results: {
377434
MyAnno: {
378435
refId: annotationName,
379-
tables: [
436+
data: [
380437
{
381438
columns: [{ text: "time" }, { text: "text" }, { text: "tags" }],
382439
rows: [
@@ -388,7 +445,7 @@ describe("BigQueryDatasource", () => {
388445
]
389446
}
390447
}
391-
};
448+
};*/
392449

393450
beforeEach(() => {
394451
ctx.backendSrv.datasourceRequest = jest.fn(options => {
@@ -398,18 +455,11 @@ describe("BigQueryDatasource", () => {
398455
results = data;
399456
});
400457
});
401-
458+
console.log(response)
402459
it("should return annotation list", () => {
403460
expect(results.length).toBe(3);
404461

405-
expect(results[0].text).toBe("some text");
406-
expect(results[0].tags[0]).toBe("TagA");
407-
expect(results[0].tags[1]).toBe("TagB");
408-
409-
expect(results[1].tags[0]).toBe("TagB");
410-
expect(results[1].tags[1]).toBe("TagC");
411-
412-
expect(results[2].tags.length).toBe(0);
462+
expect(results[0].text.v).toBe("some text");
413463
});
414464
});
415465

0 commit comments

Comments
 (0)