From b21dd4fe7f50769ffc1991bc474b97a7a34038e7 Mon Sep 17 00:00:00 2001 From: Haohan Yang Date: Mon, 23 Sep 2024 18:03:38 +0200 Subject: [PATCH] use built-in template variables $__from and $__to --- pkg/plugin/types.go | 7 +++---- src/components/QueryEditor.tsx | 13 +++---------- src/datasource.ts | 24 +----------------------- src/types.ts | 2 -- 4 files changed, 7 insertions(+), 39 deletions(-) diff --git a/pkg/plugin/types.go b/pkg/plugin/types.go index 8e30e30..0ad1a15 100644 --- a/pkg/plugin/types.go +++ b/pkg/plugin/types.go @@ -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 { diff --git a/src/components/QueryEditor.tsx b/src/components/QueryEditor.tsx index a5db46a..baf8c3c 100644 --- a/src/components/QueryEditor.tsx +++ b/src/components/QueryEditor.tsx @@ -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'; @@ -42,10 +42,6 @@ export function QueryEditor({ query, onChange }: Props) { } }; - const onApplyTimeRangeChange = (event: FormEvent) => { - onChange({ ...query, applyTimeRange: event.currentTarget.checked }); - }; - const onQueryTypeChange = (sv: SelectableValue) => { onChange({ ...query, queryType: sv.value }); }; @@ -64,7 +60,7 @@ export function QueryEditor({ query, onChange }: Props) { } }; - const { queryText, collection, applyTimeRange, queryType } = query; + const { queryText, collection, queryType } = query; return ( <> @@ -76,9 +72,6 @@ export function QueryEditor({ query, onChange }: Props) { error="Please enter the collection" invalid={!collection}> - - - { constructor(instanceSettings: DataSourceInstanceSettings) { super(instanceSettings); @@ -42,19 +35,4 @@ export class DataSource extends DataSourceWithBackend): Observable { - 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 }); - } } diff --git a/src/types.ts b/src/types.ts index 4b5b3f8..1d91ef5 100644 --- a/src/types.ts +++ b/src/types.ts @@ -4,7 +4,6 @@ import { DataQuery } from '@grafana/schema'; export interface MongoQuery extends DataQuery { queryText?: string; collection?: string; - applyTimeRange: boolean; queryType?: string; } @@ -15,7 +14,6 @@ export const QueryType = { export const DEFAULT_QUERY: Partial = { queryText: "[]", - applyTimeRange: true, queryType: QueryType.TIMESERIES };