Skip to content

Commit

Permalink
use built-in template variables $__from and $__to
Browse files Browse the repository at this point in the history
  • Loading branch information
haohanyang committed Sep 23, 2024
1 parent 27f2c6c commit b21dd4f
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 39 deletions.
7 changes: 3 additions & 4 deletions pkg/plugin/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ type Datasource struct {
}

type queryModel struct {
QueryText string `json:"queryText"`
Collection string `json:"collection"`
QueryType string `json:"queryType"`
ApplyTimeRange bool `json:"applyTimeRange"`
QueryText string `json:"queryText"`
Collection string `json:"collection"`
QueryType string `json:"queryType"`
}

type timeSeriesRow[T any] struct {
Expand Down
13 changes: 3 additions & 10 deletions src/components/QueryEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { ChangeEvent, FormEvent, useRef, useState } from 'react';
import { Button, CodeEditor, Divider, Field, InlineField, InlineFieldRow, InlineSwitch, Input, Select } from '@grafana/ui';
import React, { ChangeEvent, useRef, useState } from 'react';
import { Button, CodeEditor, Divider, Field, InlineField, InlineFieldRow, Input, Select } from '@grafana/ui';
import { QueryEditorProps, SelectableValue } from '@grafana/data';
import { DataSource } from '../datasource';
import { MongoDataSourceOptions, MongoQuery, QueryType } from '../types';
Expand Down Expand Up @@ -42,10 +42,6 @@ export function QueryEditor({ query, onChange }: Props) {
}
};

const onApplyTimeRangeChange = (event: FormEvent<HTMLInputElement>) => {
onChange({ ...query, applyTimeRange: event.currentTarget.checked });
};

const onQueryTypeChange = (sv: SelectableValue<string>) => {
onChange({ ...query, queryType: sv.value });
};
Expand All @@ -64,7 +60,7 @@ export function QueryEditor({ query, onChange }: Props) {
}
};

const { queryText, collection, applyTimeRange, queryType } = query;
const { queryText, collection, queryType } = query;

return (
<>
Expand All @@ -76,9 +72,6 @@ export function QueryEditor({ query, onChange }: Props) {
error="Please enter the collection" invalid={!collection}>
<Input id="query-editor-collection" onChange={onCollectionChange} value={collection} required />
</InlineField>
<InlineField label="Apply time range" tooltip="Apply time range">
<InlineSwitch id="query-editor-apply-time-range" value={applyTimeRange} onChange={onApplyTimeRangeChange} />
</InlineField>
</InlineFieldRow>
<Divider />
<Field label="Query Text" description="Enter the Mongo Aggregation Pipeline"
Expand Down
24 changes: 1 addition & 23 deletions src/datasource.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { DataSourceInstanceSettings, CoreApp, ScopedVars, DataQueryRequest, DataQueryResponse, DateTime } from '@grafana/data';
import { DataSourceInstanceSettings, CoreApp, ScopedVars } from '@grafana/data';
import { DataSourceWithBackend, getTemplateSrv } from '@grafana/runtime';

import { MongoQuery, MongoDataSourceOptions, DEFAULT_QUERY } from './types';
import { Observable } from 'rxjs';

function isJsonStringValid(jsonString: string) {
try {
Expand All @@ -17,12 +16,6 @@ function isJsonStringValid(jsonString: string) {
return true;
}

function datetimeToJson(datetime: DateTime) {
return JSON.stringify({
$date: datetime.toISOString()
});
}

export class DataSource extends DataSourceWithBackend<MongoQuery, MongoDataSourceOptions> {
constructor(instanceSettings: DataSourceInstanceSettings<MongoDataSourceOptions>) {
super(instanceSettings);
Expand All @@ -42,19 +35,4 @@ export class DataSource extends DataSourceWithBackend<MongoQuery, MongoDataSourc
filterQuery(query: MongoQuery): boolean {
return !!query.queryText && !!query.collection && isJsonStringValid(query.queryText!);
}

query(request: DataQueryRequest<MongoQuery>): Observable<DataQueryResponse> {
const queries = request.targets.map(query => {
if (query.applyTimeRange) {
return {
...query,
queryText: query.queryText?.replaceAll(/"\$from"/g, datetimeToJson(request.range.from))
.replaceAll(/"\$to"/g, datetimeToJson(request.range.to))
};
}
return query;
});

return super.query({ ...request, targets: queries });
}
}
2 changes: 0 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { DataQuery } from '@grafana/schema';
export interface MongoQuery extends DataQuery {
queryText?: string;
collection?: string;
applyTimeRange: boolean;
queryType?: string;
}

Expand All @@ -15,7 +14,6 @@ export const QueryType = {

export const DEFAULT_QUERY: Partial<MongoQuery> = {
queryText: "[]",
applyTimeRange: true,
queryType: QueryType.TIMESERIES
};

Expand Down

0 comments on commit b21dd4f

Please sign in to comment.