Skip to content

Commit 687c8ed

Browse files
authored
Add $dateBucketCount support (#10)
* add support for $dateBucketCount * update readme
1 parent dc1cb88 commit 687c8ed

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ db.transactions.aggregate({
4444
});
4545
```
4646

47-
You can also use the `"$from"` and `"$to"` conventions originated from a legacy plugin
47+
You can also use the `"$from"`, `"$to"` and `"$dateBucketCount"`(number of intervals in the time range) conventions originated from a legacy plugin.
4848
```json
4949
[
5050
{
@@ -57,6 +57,15 @@ You can also use the `"$from"` and `"$to"` conventions originated from a legacy
5757
}
5858
]
5959
```
60+
```json
61+
{
62+
"$bucketAuto": {
63+
"groupBy": "$timestamp",
64+
"buckets": "$dateBucketCount",
65+
//
66+
}
67+
}
68+
```
6069
### Query type
6170
#### Time series
6271
Time series query type is suitable for time series panels. The query results should consist the following fields:

src/datasource.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
1-
import { DataSourceInstanceSettings, CoreApp, ScopedVars, DataQueryRequest, DataQueryResponse, DateTime } from "@grafana/data";
1+
import { DataSourceInstanceSettings, CoreApp, ScopedVars, DataQueryRequest, DataQueryResponse } from "@grafana/data";
22
import { DataSourceWithBackend, getTemplateSrv } from "@grafana/runtime";
3-
import { parseJsQuery } from "./utils";
3+
import { parseJsQuery, datetimeToJson, getBucketCount } from "./utils";
44
import { MongoQuery, MongoDataSourceOptions, DEFAULT_QUERY, QueryLanguage } from "./types";
55
import { Observable } from "rxjs";
66

7-
function datetimeToJson(datetime: DateTime) {
8-
return JSON.stringify({
9-
$date: {
10-
$numberLong: datetime.toDate().getTime().toString()
11-
}
12-
});
13-
}
147

158
export class DataSource extends DataSourceWithBackend<MongoQuery, MongoDataSourceOptions> {
169
constructor(instanceSettings: DataSourceInstanceSettings<MongoDataSourceOptions>) {
@@ -44,6 +37,7 @@ export class DataSource extends DataSourceWithBackend<MongoQuery, MongoDataSourc
4437
...query,
4538
queryText: queryText.replaceAll(/"\$from"/g, datetimeToJson(request.range.from))
4639
.replaceAll(/"\$to"/g, datetimeToJson(request.range.to))
40+
.replaceAll(/"\$dateBucketCount"/g, getBucketCount(request.range.from, request.range.to, request.intervalMs).toString())
4741
};
4842
});
4943

src/utils.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { DateTime } from "@grafana/data";
12
import { JsQueryResult } from "types";
23

34
export function validateJsonQueryText(queryText?: string): string | null {
@@ -45,3 +46,23 @@ export function validateJsQueryText(queryText?: string): string | null {
4546
const { error } = parseJsQuery(queryText);
4647
return error;
4748
}
49+
50+
export function datetimeToJson(datetime: DateTime) {
51+
return JSON.stringify({
52+
$date: {
53+
$numberLong: datetime.toDate().getTime().toString()
54+
}
55+
});
56+
}
57+
58+
export function getBucketCount(from: DateTime, to: DateTime, intervalMs: number) {
59+
let current = from.toDate().getTime();
60+
const toMs = to.toDate().getTime();
61+
let count = 0;
62+
while (current < toMs) {
63+
current += intervalMs;
64+
count++;
65+
}
66+
67+
return count;
68+
}

0 commit comments

Comments
 (0)