From 1c1166239372d4aaed19502a58b95fbfacab6712 Mon Sep 17 00:00:00 2001 From: MGJamJam Date: Wed, 8 May 2024 14:50:08 -0300 Subject: [PATCH] add queryFilter type and renames AnalyticsDataQuery and AnalyticsRequestQuery type --- .../src/components/ConfigEditor.tsx | 4 +- .../src/components/QueryEditor.tsx | 4 +- .../src/datasource.ts | 41 ++++++------------- bitmovin-analytics-datasource/src/module.ts | 6 ++- bitmovin-analytics-datasource/src/types.ts | 19 ++++++++- .../src/types/queryFilter.ts | 37 +++++++++++++++++ 6 files changed, 76 insertions(+), 35 deletions(-) create mode 100644 bitmovin-analytics-datasource/src/types/queryFilter.ts diff --git a/bitmovin-analytics-datasource/src/components/ConfigEditor.tsx b/bitmovin-analytics-datasource/src/components/ConfigEditor.tsx index 60c8455..cdae81c 100644 --- a/bitmovin-analytics-datasource/src/components/ConfigEditor.tsx +++ b/bitmovin-analytics-datasource/src/components/ConfigEditor.tsx @@ -1,9 +1,9 @@ import React, { ChangeEvent, useEffect } from 'react'; import { DataSourceHttpSettings, FieldSet, InlineField, InlineSwitch, Input } from '@grafana/ui'; import { DataSourcePluginOptionsEditorProps } from '@grafana/data'; -import { MyDataSourceOptions } from '../types'; +import { BitmovinDataSourceOptions } from '../types'; -interface Props extends DataSourcePluginOptionsEditorProps {} +interface Props extends DataSourcePluginOptionsEditorProps {} export function ConfigEditor(props: Props) { const { onOptionsChange, options } = props; diff --git a/bitmovin-analytics-datasource/src/components/QueryEditor.tsx b/bitmovin-analytics-datasource/src/components/QueryEditor.tsx index d324b75..cdd2cab 100644 --- a/bitmovin-analytics-datasource/src/components/QueryEditor.tsx +++ b/bitmovin-analytics-datasource/src/components/QueryEditor.tsx @@ -3,7 +3,7 @@ import { FieldSet, InlineField, InlineSwitch, Select } from '@grafana/ui'; import { QueryEditorProps, SelectableValue } from '@grafana/data'; import { DataSource } from '../datasource'; -import { MyDataSourceOptions, BitmovinAnalyticsDataQuery } from '../types'; +import { BitmovinDataSourceOptions, BitmovinAnalyticsDataQuery } from '../types'; import { fetchLicenses } from '../utils/licenses'; import { DEFAULT_SELECTABLE_QUERY_INTERVAL, SELECTABLE_QUERY_INTERVALS } from '../utils/intervalUtils'; import { DEFAULT_SELECTABLE_AGGREGATION, SELECTABLE_AGGREGATIONS } from '../types/aggregations'; @@ -21,7 +21,7 @@ enum LoadingState { Error = 'ERROR', } -type Props = QueryEditorProps; +type Props = QueryEditorProps; export function QueryEditor({ query, onChange, onRunQuery, datasource }: Props) { const [selectableLicenses, setSelectableLicenses] = useState([]); diff --git a/bitmovin-analytics-datasource/src/datasource.ts b/bitmovin-analytics-datasource/src/datasource.ts index 6211340..5efc062 100644 --- a/bitmovin-analytics-datasource/src/datasource.ts +++ b/bitmovin-analytics-datasource/src/datasource.ts @@ -9,34 +9,25 @@ import { import { getBackendSrv } from '@grafana/runtime'; import { catchError, lastValueFrom, map, Observable, of } from 'rxjs'; -import { MixedDataRowList, MyDataSourceOptions, BitmovinAnalyticsDataQuery, NumberDataRowList } from './types'; +import { + MixedDataRowList, + BitmovinDataSourceOptions, + BitmovinAnalyticsDataQuery, + NumberDataRowList, + BitmovinAnalyticsRequestQuery, +} from './types'; import { transformGroupedTimeSeriesData, transformSimpleTimeSeries, transformTableData } from './utils/dataUtils'; -import { calculateQueryInterval, QueryInterval } from './utils/intervalUtils'; -import { QueryAttribute } from './types/queryAttributes'; -import { QueryAdAttribute } from './types/queryAdAttributes'; +import { calculateQueryInterval } from './utils/intervalUtils'; import { Metric } from './types/metric'; import { Aggregation } from './types/aggregations'; -import { QueryOrderBy } from './types/queryOrderBy'; - -type AnalyticsQuery = { - filters: Array<{ name: string; operator: string; value: number }>; - groupBy: QueryAttribute[] | QueryAdAttribute[]; - orderBy: QueryOrderBy[]; - dimension?: QueryAttribute | QueryAdAttribute; - metric?: Metric; - start: Date; - end: Date; - licenseKey: string; - interval?: QueryInterval; -}; - -export class DataSource extends DataSourceApi { + +export class DataSource extends DataSourceApi { baseUrl: string; apiKey: string; tenantOrgId?: string; adAnalytics?: boolean; - constructor(instanceSettings: DataSourceInstanceSettings) { + constructor(instanceSettings: DataSourceInstanceSettings) { super(instanceSettings); this.apiKey = instanceSettings.jsonData.apiKey; @@ -64,14 +55,8 @@ export class DataSource extends DataSourceApi(DataSource) +export const plugin = new DataSourcePlugin( + DataSource +) .setConfigEditor(ConfigEditor) .setQueryEditor(QueryEditor); diff --git a/bitmovin-analytics-datasource/src/types.ts b/bitmovin-analytics-datasource/src/types.ts index b7d5f82..b0016ea 100644 --- a/bitmovin-analytics-datasource/src/types.ts +++ b/bitmovin-analytics-datasource/src/types.ts @@ -6,7 +6,11 @@ import { QueryAttribute } from './types/queryAttributes'; import { QueryAdAttribute } from './types/queryAdAttributes'; import { Metric } from './types/metric'; import { QueryOrderBy } from './types/queryOrderBy'; +import { QueryFilter } from './types/queryFilter'; +/** + * These are the options configurable via the QueryEditor + * */ export interface BitmovinAnalyticsDataQuery extends DataQuery { licenseKey: string; interval?: QueryInterval | 'AUTO'; @@ -15,6 +19,7 @@ export interface BitmovinAnalyticsDataQuery extends DataQuery { dimension?: QueryAttribute | QueryAdAttribute; groupBy: QueryAttribute[] | QueryAdAttribute[]; orderBy: QueryOrderBy[]; + filters: QueryFilter[]; } export const DEFAULT_QUERY: Partial = {}; @@ -22,12 +27,24 @@ export const DEFAULT_QUERY: Partial = {}; /** * These are options configured for each DataSource instance */ -export interface MyDataSourceOptions extends DataSourceJsonData { +export interface BitmovinDataSourceOptions extends DataSourceJsonData { apiKey: string; tenantOrgId?: string; adAnalytics?: boolean; } +export type BitmovinAnalyticsRequestQuery = { + licenseKey: string; + start: Date; + end: Date; + filters: QueryFilter[]; + groupBy: QueryAttribute[] | QueryAdAttribute[]; + orderBy: QueryOrderBy[]; + dimension?: QueryAttribute | QueryAdAttribute; + metric?: Metric; + interval?: QueryInterval; +}; + export type MixedDataRow = Array; export type MixedDataRowList = MixedDataRow[]; diff --git a/bitmovin-analytics-datasource/src/types/queryFilter.ts b/bitmovin-analytics-datasource/src/types/queryFilter.ts new file mode 100644 index 0000000..f7566cb --- /dev/null +++ b/bitmovin-analytics-datasource/src/types/queryFilter.ts @@ -0,0 +1,37 @@ +import { SelectableValue } from '@grafana/data'; +import { QueryAdAttribute } from './queryAdAttributes'; +import { QueryAttribute } from './queryAttributes'; + +export enum QUERY_FILTER_OPERATORS { + GT = 'GT', + GTE = 'GTE', + LT = 'LT', + LTE = 'LTE', + EQ = 'EQ', + NE = 'NE', + CONTAINS = 'CONTAINS', + NOTCONTAINS = 'NOTCONTAINS', + IN = 'IN', +} + +export type QueryFilterOperator = keyof typeof QUERY_FILTER_OPERATORS; + +export const SELECTABLE_QUERY_FILTER_OPERATORS: SelectableValue[] = [ + { value: QUERY_FILTER_OPERATORS.GT, label: QUERY_FILTER_OPERATORS.GT }, + { value: QUERY_FILTER_OPERATORS.GTE, label: QUERY_FILTER_OPERATORS.GTE }, + { value: QUERY_FILTER_OPERATORS.LT, label: QUERY_FILTER_OPERATORS.LT }, + { value: QUERY_FILTER_OPERATORS.LTE, label: QUERY_FILTER_OPERATORS.LTE }, + { value: QUERY_FILTER_OPERATORS.EQ, label: QUERY_FILTER_OPERATORS.EQ }, + { value: QUERY_FILTER_OPERATORS.NE, label: QUERY_FILTER_OPERATORS.NE }, + { value: QUERY_FILTER_OPERATORS.CONTAINS, label: QUERY_FILTER_OPERATORS.CONTAINS }, + { value: QUERY_FILTER_OPERATORS.NOTCONTAINS, label: QUERY_FILTER_OPERATORS.NOTCONTAINS }, + { value: QUERY_FILTER_OPERATORS.IN, label: QUERY_FILTER_OPERATORS.IN }, +]; + +export type QueryFilter = { + name: QueryAdAttribute | QueryAttribute; + operator: QueryFilterOperator; + value: QueryFilterValue; +}; + +export type QueryFilterValue = boolean | number | string | string[] | null;