From 33a6c7d999c3abba284b763c25b480495e9c6a7d Mon Sep 17 00:00:00 2001 From: MGJamJam Date: Thu, 2 May 2024 07:20:52 -0300 Subject: [PATCH 1/8] implements license fetching and interval selection --- .../src/components/QueryEditor.tsx | 99 ++++++++++++++++--- .../src/datasource.ts | 54 +++++----- bitmovin-analytics-datasource/src/types.ts | 9 +- .../src/utils/dataUtils.ts | 16 ++- .../src/utils/intervalUtils.ts | 72 ++++++++++++++ .../src/utils/licenses.ts | 61 ++++++++++++ 6 files changed, 263 insertions(+), 48 deletions(-) create mode 100644 bitmovin-analytics-datasource/src/utils/licenses.ts diff --git a/bitmovin-analytics-datasource/src/components/QueryEditor.tsx b/bitmovin-analytics-datasource/src/components/QueryEditor.tsx index f9b0647..1baf383 100644 --- a/bitmovin-analytics-datasource/src/components/QueryEditor.tsx +++ b/bitmovin-analytics-datasource/src/components/QueryEditor.tsx @@ -1,32 +1,99 @@ -import React, { ChangeEvent } from 'react'; -import { InlineField, Input } from '@grafana/ui'; -import { QueryEditorProps } from '@grafana/data'; +import React, { ChangeEvent, useEffect, useState } from 'react'; +import { FieldSet, InlineField, InlineSwitch, Select } from '@grafana/ui'; +import { QueryEditorProps, SelectableValue } from '@grafana/data'; + import { DataSource } from '../datasource'; import { MyDataSourceOptions, MyQuery } from '../types'; +import { fetchLicenses } from '../utils/licenses'; +import { DEFAULT_SELECTABLE_QUERY_INTERVAL, SELECTABLE_QUERY_INTERVALS } from '../utils/intervalUtils'; type Props = QueryEditorProps; -export function QueryEditor({ query, onChange, onRunQuery }: Props) { - const onQueryTextChange = (event: ChangeEvent) => { - onChange({ ...query, queryText: event.target.value }); +export enum LoadingState { + Default = 'DEFAULT', + Loading = 'LOADING', + Success = 'SUCCESS', + Error = 'ERROR', +} + +export function QueryEditor({ query, onChange, onRunQuery, datasource }: Props) { + const [selectableLicenses, setSelectableLicenses] = useState([]); + const [licenseLoadingState, setLicenseLoadingState] = useState(LoadingState.Default); + const [licenseErrorMessage, setLicenseErrorMessage] = useState(''); + const [isTimeSeries, setIsTimeSeries] = useState(true); + + useEffect(() => { + setLicenseLoadingState(LoadingState.Loading); + fetchLicenses(datasource.apiKey, datasource.baseUrl) + .then((licenses) => { + setSelectableLicenses(licenses); + setLicenseLoadingState(LoadingState.Success); + }) + .catch((e) => { + setLicenseLoadingState(LoadingState.Error); + setLicenseErrorMessage(e.status + ' ' + e.statusText); + }); + }, []); + + const onLicenseChange = (item: SelectableValue) => { + onChange({ ...query, licenseKey: item.value }); + onRunQuery(); }; - const onConstantChange = (event: ChangeEvent) => { - onChange({ ...query, constant: parseFloat(event.target.value) }); - // executes the query + const onFormatAsTimeSeriesChange = (event: ChangeEvent) => { + setIsTimeSeries(event.currentTarget.checked); + if (event.currentTarget.checked) { + onChange({ ...query, interval: 'AUTO' }); + } else { + onChange({ ...query, interval: undefined }); + } onRunQuery(); }; - const { queryText, constant } = query; + const onIntervalChange = (item: SelectableValue) => { + onChange({ ...query, interval: item.value }); + onRunQuery(); + }; + + const renderTimeSeriesOption = () => { + return ( + <> + + - - - - +
+ +