diff --git a/bitmovin-analytics-datasource/src/components/QueryEditor.tsx b/bitmovin-analytics-datasource/src/components/QueryEditor.tsx index f9b0647..3f4c22f 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 { MyDataSourceOptions, BitmovinAnalyticsDataQuery } from '../types'; +import { fetchLicenses } from '../utils/licenses'; +import { DEFAULT_SELECTABLE_QUERY_INTERVAL, SELECTABLE_QUERY_INTERVALS } from '../utils/intervalUtils'; + +enum LoadingState { + Default = 'DEFAULT', + Loading = 'LOADING', + Success = 'SUCCESS', + Error = 'ERROR', +} + +type Props = QueryEditorProps; + +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); -type Props = QueryEditorProps; + 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); + }); + }, [datasource.apiKey, datasource.baseUrl]); -export function QueryEditor({ query, onChange, onRunQuery }: Props) { - const onQueryTextChange = (event: ChangeEvent) => { - onChange({ ...query, queryText: event.target.value }); + 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 ( + <> + + - - - - +
+ +