Skip to content

Commit 989cd34

Browse files
committed
Refactor applyTemplateVariables
1 parent c5b9345 commit 989cd34

File tree

3 files changed

+17
-42
lines changed

3 files changed

+17
-42
lines changed

src/components/QueryEditor.css

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/components/QueryEditorRaw.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useRef, useCallback } from 'react';
2-
import { CodeEditor, type monacoTypes } from '@grafana/ui';
2+
import { CodeEditor, type MonacoEditor } from '@grafana/ui';
33
import { useAutocomplete } from 'autocomplete';
44

55
interface QueryEditorRawProps {
@@ -13,7 +13,7 @@ interface QueryEditorRawProps {
1313
}
1414

1515
export function QueryEditorRaw({ query, onBlur, language, width, height, fontSize, children }: QueryEditorRawProps) {
16-
const monacoRef = useRef<monacoTypes.editor.IStandaloneCodeEditor | null>(null);
16+
const monacoRef = useRef<MonacoEditor | null>(null);
1717
const setupAutocompleteFn = useAutocomplete();
1818

1919
const formatQuery = useCallback(() => {

src/datasource.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { DataSourceInstanceSettings, CoreApp, ScopedVars, DataQueryRequest, LiveChannelScope, DataQueryResponse, LoadingState, rangeUtil } from '@grafana/data';
22
import { DataSourceWithBackend, getGrafanaLiveSrv, getTemplateSrv, TemplateSrv } from '@grafana/runtime';
3-
import { parseJsQuery, getBucketCount, parseJsQueryLegacy, datetimeToJson, base64UrlEncode, unixTsToMongoID } from './utils';
3+
import { parseJsQuery, getBucketCount, parseJsQueryLegacy, base64UrlEncode, datetimeToJson, 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,17 +17,8 @@ export class MongoDBDataSource extends DataSourceWithBackend<MongoDBQuery, Mongo
1717
}
1818

1919
applyTemplateVariables(query: MongoDBQuery, scopedVars: ScopedVars) {
20-
console.log("scopedVars", scopedVars);
21-
console.log("Vars", this.templateSrv.getVariables());
2220
const variables = { ...scopedVars };
2321

24-
25-
const from = this.templateSrv.replace('$__from', variables);
26-
const to = this.templateSrv.replace('$__to', variables);
27-
28-
variables.from = { value: datetimeToJson(from) }
29-
variables.to = { value: datetimeToJson(to) }
30-
3122
let queryText = query.queryText!;
3223
if (query.queryLanguage === QueryLanguage.JAVASCRIPT || query.queryLanguage === QueryLanguage.JAVASCRIPT_SHADOW) {
3324
const { jsonQuery } =
@@ -37,16 +28,24 @@ export class MongoDBDataSource extends DataSourceWithBackend<MongoDBQuery, Mongo
3728
queryText = jsonQuery!;
3829
}
3930

31+
// Get time range
32+
const from = this.templateSrv.replace('$__from', variables);
33+
const to = this.templateSrv.replace('$__to', variables);
4034

41-
queryText = queryText
42-
.replaceAll(/"\$__from_oid"/g, `"${unixTsToMongoID(from, '0')}"`)
43-
.replaceAll(/"\$__to_oid"/g, `"${unixTsToMongoID(to, 'f')}"`);
35+
if (from && to) {
36+
variables.__from_oid = { value: unixTsToMongoID(from, '0') }
37+
variables.__to_oid = { value: unixTsToMongoID(to, 'f') }
4438

45-
const interval = scopedVars['__interval_ms']?.value;
39+
queryText = queryText
40+
.replaceAll(/"\$from"/g, datetimeToJson(from))
41+
.replaceAll(/"\$to"/g, datetimeToJson(to));
42+
}
4643

47-
// Compatible with legacy plugin $dateBucketCount
44+
45+
const interval = scopedVars['__interval_ms']?.value;
46+
// $dateBucketCount
4847
if (interval && from && to) {
49-
queryText = queryText.replaceAll(/"\$dateBucketCount"/g, getBucketCount(from, to, interval).toString());
48+
variables.dateBucketCount = { value: getBucketCount(from, to, interval) }
5049
}
5150

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

0 commit comments

Comments
 (0)