Skip to content

Commit 8912b2a

Browse files
committed
Add variable $__from_oid and $__to_oid
1 parent 384a32f commit 8912b2a

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/datasource.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
getMetricValues,
2020
datetimeToJson,
2121
base64UrlEncode,
22+
unixTsToMongoID
2223
} from './utils';
2324
import { MongoQuery, MongoDataSourceOptions, DEFAULT_QUERY, QueryLanguage, VariableQuery } from './types';
2425
import { firstValueFrom, merge, Observable, of } from 'rxjs';
@@ -46,6 +47,10 @@ export class DataSource extends DataSourceWithBackend<MongoQuery, MongoDataSourc
4647
const from = getTemplateSrv().replace('$__from', {});
4748
const to = getTemplateSrv().replace('$__to', {});
4849

50+
queryText = queryText
51+
.replaceAll(/"\$__from_oid"/g, `"${unixTsToMongoID(from, '0')}"`)
52+
.replaceAll(/"\$__to_oid"/g, `"${unixTsToMongoID(to, 'f')}"`);
53+
4954
// Compatible with legacy plugin $from
5055
if (from !== '$__from') {
5156
queryText = queryText.replaceAll(/"\$from"/g, datetimeToJson(from));

src/utils.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,23 @@ export function base64UrlEncode(input: string | undefined) {
141141
let base64Url = base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
142142
return base64Url;
143143
}
144+
145+
export function unixTsToMongoID(utc: string, rightPadding: string) {
146+
const val = Math.trunc(parseInt(utc, 10) / 1000)
147+
148+
if (val < 0 || val > 0xFFFFFFFF) {
149+
return '';
150+
}
151+
152+
let hexString = val.toString(16)
153+
154+
while (hexString.length < 8) {
155+
hexString = '0' + hexString;
156+
}
157+
158+
while (hexString.length < 24) {
159+
hexString = hexString + rightPadding;
160+
}
161+
162+
return hexString;
163+
}

0 commit comments

Comments
 (0)