Skip to content

Commit 91513dc

Browse files
committed
Refactor temp var interop
1 parent 989cd34 commit 91513dc

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed

src/datasource.ts

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { DataSourceInstanceSettings, CoreApp, ScopedVars, DataQueryRequest, LiveChannelScope, DataQueryResponse, LoadingState, rangeUtil } from '@grafana/data';
1+
import { DataSourceInstanceSettings, CoreApp, ScopedVars, DataQueryRequest, LiveChannelScope, DataQueryResponse, LoadingState } from '@grafana/data';
22
import { DataSourceWithBackend, getGrafanaLiveSrv, getTemplateSrv, TemplateSrv } from '@grafana/runtime';
3-
import { parseJsQuery, getBucketCount, parseJsQueryLegacy, base64UrlEncode, datetimeToJson, unixTsToMongoID } from './utils';
3+
import { parseJsQuery, parseJsQueryLegacy, base64UrlEncode, unixTsToMongoID } from './utils';
44
import { MongoDBQuery, MongoDataSourceOptions, DEFAULT_QUERY, QueryLanguage } from './types';
55
import { merge, Observable, of } from 'rxjs';
66
import { MongoDBVariableSupport } from 'variables'
@@ -17,6 +17,7 @@ export class MongoDBDataSource extends DataSourceWithBackend<MongoDBQuery, Mongo
1717
}
1818

1919
applyTemplateVariables(query: MongoDBQuery, scopedVars: ScopedVars) {
20+
console.log("scopedVars", scopedVars);
2021
const variables = { ...scopedVars };
2122

2223
let queryText = query.queryText!;
@@ -29,23 +30,42 @@ export class MongoDBDataSource extends DataSourceWithBackend<MongoDBQuery, Mongo
2930
}
3031

3132
// Get time range
33+
let from_ms: number | undefined = undefined;
34+
let to_ms: number | undefined = undefined;
35+
3236
const from = this.templateSrv.replace('$__from', variables);
3337
const to = this.templateSrv.replace('$__to', variables);
3438

35-
if (from && to) {
36-
variables.__from_oid = { value: unixTsToMongoID(from, '0') }
37-
variables.__to_oid = { value: unixTsToMongoID(to, 'f') }
38-
39+
if (from !== '$__from') {
40+
from_ms = parseInt(from, 10);
41+
// $__from_oid
42+
variables.__from_oid = { value: unixTsToMongoID(from_ms, '0') }
43+
// $from
3944
queryText = queryText
40-
.replaceAll(/"\$from"/g, datetimeToJson(from))
41-
.replaceAll(/"\$to"/g, datetimeToJson(to));
45+
.replaceAll(/"\$from"/g, JSON.stringify({
46+
$date: {
47+
$numberLong: from,
48+
},
49+
}))
4250
}
4351

52+
if (to !== '$__to') {
53+
to_ms = parseInt(to, 10);
54+
// $__to_oid
55+
variables.__to_oid = { value: unixTsToMongoID(to_ms, 'f') }
56+
// $to
57+
queryText = queryText
58+
.replaceAll(/"\$to"/g, JSON.stringify({
59+
$date: {
60+
$numberLong: to,
61+
},
62+
}));
63+
}
4464

45-
const interval = scopedVars['__interval_ms']?.value;
65+
const interval_ms: number | undefined = scopedVars['__interval_ms']?.value;
4666
// $dateBucketCount
47-
if (interval && from && to) {
48-
variables.dateBucketCount = { value: getBucketCount(from, to, interval) }
67+
if (interval_ms && from_ms && to_ms) {
68+
variables.dateBucketCount = { value: Math.ceil((parseInt(to, 10) - parseInt(from, 10)) / interval_ms) }
4969
}
5070

5171
const text = this.templateSrv.replace(queryText, variables);

src/utils.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,6 @@ export function parseJsQuery(queryText: string): JsQueryResult {
5050
}
5151
}
5252

53-
export function datetimeToJson(datetime: string) {
54-
return JSON.stringify({
55-
$date: {
56-
$numberLong: datetime,
57-
},
58-
})
59-
}
60-
61-
export function getBucketCount(from: string, to: string, intervalMs: number) {
62-
let current = parseInt(from, 10);
63-
const toMs = parseInt(to, 10);
64-
let count = 0;
65-
while (current < toMs) {
66-
current += intervalMs;
67-
count++;
68-
}
69-
70-
return count;
71-
}
72-
7353
export function randomId(length: number) {
7454
let result = '';
7555
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
@@ -93,8 +73,8 @@ export function base64UrlEncode(input: string | undefined) {
9373
return base64Url;
9474
}
9575

96-
export function unixTsToMongoID(utc: string, rightPadding: string) {
97-
const val = Math.trunc(parseInt(utc, 10) / 1000);
76+
export function unixTsToMongoID(utc_ms: number, rightPadding: string) {
77+
const val = Math.trunc(utc_ms / 1000);
9878

9979
if (val < 0 || val > 0xffffffff) {
10080
return '';

0 commit comments

Comments
 (0)