Skip to content

Commit

Permalink
add queryFilter type and renames AnalyticsDataQuery and AnalyticsRequ…
Browse files Browse the repository at this point in the history
…estQuery type
  • Loading branch information
MGJamJam committed May 8, 2024
1 parent 00dd1a5 commit 1c11662
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 35 deletions.
4 changes: 2 additions & 2 deletions bitmovin-analytics-datasource/src/components/ConfigEditor.tsx
Original file line number Diff line number Diff line change
@@ -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<MyDataSourceOptions> {}
interface Props extends DataSourcePluginOptionsEditorProps<BitmovinDataSourceOptions> {}

export function ConfigEditor(props: Props) {
const { onOptionsChange, options } = props;
Expand Down
4 changes: 2 additions & 2 deletions bitmovin-analytics-datasource/src/components/QueryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -21,7 +21,7 @@ enum LoadingState {
Error = 'ERROR',
}

type Props = QueryEditorProps<DataSource, BitmovinAnalyticsDataQuery, MyDataSourceOptions>;
type Props = QueryEditorProps<DataSource, BitmovinAnalyticsDataQuery, BitmovinDataSourceOptions>;

export function QueryEditor({ query, onChange, onRunQuery, datasource }: Props) {
const [selectableLicenses, setSelectableLicenses] = useState<SelectableValue[]>([]);
Expand Down
41 changes: 13 additions & 28 deletions bitmovin-analytics-datasource/src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<BitmovinAnalyticsDataQuery, MyDataSourceOptions> {

export class DataSource extends DataSourceApi<BitmovinAnalyticsDataQuery, BitmovinDataSourceOptions> {
baseUrl: string;
apiKey: string;
tenantOrgId?: string;
adAnalytics?: boolean;

constructor(instanceSettings: DataSourceInstanceSettings<MyDataSourceOptions>) {
constructor(instanceSettings: DataSourceInstanceSettings<BitmovinDataSourceOptions>) {
super(instanceSettings);

this.apiKey = instanceSettings.jsonData.apiKey;
Expand Down Expand Up @@ -64,14 +55,8 @@ export class DataSource extends DataSourceApi<BitmovinAnalyticsDataQuery, MyData
? calculateQueryInterval(target.interval!, from.getTime(), to.getTime())
: undefined;

const query: AnalyticsQuery = {
filters: [
{
name: 'VIDEO_STARTUPTIME',
operator: 'GT',
value: 0,
},
],
const query: BitmovinAnalyticsRequestQuery = {
filters: target.filters,
groupBy: target.groupBy,
orderBy: target.orderBy,
dimension: target.dimension,
Expand Down
6 changes: 4 additions & 2 deletions bitmovin-analytics-datasource/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { DataSourcePlugin } from '@grafana/data';
import { DataSource } from './datasource';
import { ConfigEditor } from './components/ConfigEditor';
import { QueryEditor } from './components/QueryEditor';
import { BitmovinAnalyticsDataQuery, MyDataSourceOptions } from './types';
import { BitmovinAnalyticsDataQuery, BitmovinDataSourceOptions } from './types';

export const plugin = new DataSourcePlugin<DataSource, BitmovinAnalyticsDataQuery, MyDataSourceOptions>(DataSource)
export const plugin = new DataSourcePlugin<DataSource, BitmovinAnalyticsDataQuery, BitmovinDataSourceOptions>(
DataSource
)
.setConfigEditor(ConfigEditor)
.setQueryEditor(QueryEditor);
19 changes: 18 additions & 1 deletion bitmovin-analytics-datasource/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -15,19 +19,32 @@ export interface BitmovinAnalyticsDataQuery extends DataQuery {
dimension?: QueryAttribute | QueryAdAttribute;
groupBy: QueryAttribute[] | QueryAdAttribute[];
orderBy: QueryOrderBy[];
filters: QueryFilter[];
}

export const DEFAULT_QUERY: Partial<BitmovinAnalyticsDataQuery> = {};

/**
* 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<string | number>;
export type MixedDataRowList = MixedDataRow[];

Expand Down
37 changes: 37 additions & 0 deletions bitmovin-analytics-datasource/src/types/queryFilter.ts
Original file line number Diff line number Diff line change
@@ -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<QueryFilterOperator>[] = [
{ 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;

0 comments on commit 1c11662

Please sign in to comment.