From 771358d35b38270ec03e69b390f58b8b81c5620a Mon Sep 17 00:00:00 2001 From: Ivan Polomani Date: Thu, 20 Apr 2023 15:56:12 +0200 Subject: [PATCH 1/7] event sort by property --- src/cdf/types.ts | 9 +++ src/components/eventsTab.tsx | 88 ++++++++++++++++++++++++++++- src/constants.ts | 17 +++++- src/datasources/EventsDatasource.ts | 78 ++++++++++++------------- src/types.ts | 21 ++++--- 5 files changed, 164 insertions(+), 49 deletions(-) diff --git a/src/cdf/types.ts b/src/cdf/types.ts index 40c67570..cf1de771 100644 --- a/src/cdf/types.ts +++ b/src/cdf/types.ts @@ -115,6 +115,15 @@ export interface FilterRequest extends Limit, Cursor { advancedFilter?: any; } +export interface AggregateRequest extends Limit, Cursor { + filter?: Filter; + aggregate: string; + properties: { + property?: string; + }[]; + advancedFilter?: any; +} + export interface Resource { id: number; externalId?: string; diff --git a/src/components/eventsTab.tsx b/src/components/eventsTab.tsx index 4faf5b4c..361a3c12 100644 --- a/src/components/eventsTab.tsx +++ b/src/components/eventsTab.tsx @@ -7,10 +7,11 @@ import { Switch, Segment, Tooltip, + Select, } from '@grafana/ui'; import jsonlint from 'jsonlint-mod'; import { EventQuery, SelectedProps, EditorProps } from '../types'; -import { EventFields } from '../constants'; +import { EventFields, EventSortByFields } from '../constants'; import CogniteDatasource from '../datasource'; import { EventQueryHelp, EventAdvancedFilterHelp } from './queryHelp'; import { InlineButton } from './inlineButton'; @@ -45,6 +46,7 @@ const ActiveAtTimeRangeCheckbox = (props: SelectedProps) => { const ColumnsPicker = ({ query, onQueryChange }: SelectedProps) => { const options = EventFields.map((value) => ({ value, label: value })); const { columns, aggregate } = query.eventQuery; + useEffect(() => { if (aggregate?.withAggregate) { ['count', 'map'].map((v) => !columns.includes(v) && columns.push(v)); @@ -103,6 +105,89 @@ const ColumnsPicker = ({ query, onQueryChange }: SelectedProps) => { ); }; + +const OrderDirectionEditor = ( + { onChange, direction = "asc" }: + { direction: "desc" | "asc", onChange: (val: 'asc' | 'desc') => void } +) => { + + return ( +
+ Order + onChange(value as 'asc' | 'desc')} + onChange={({ value }) => onChange(value as EventsOrderDirection)} options={[{ label: "ascending", value: "asc" }, { label: "descending", value: "desc" }]} menuPosition="fixed" value={direction} diff --git a/src/datasources/EventsDatasource.ts b/src/datasources/EventsDatasource.ts index c359ebc2..b3e68b3d 100644 --- a/src/datasources/EventsDatasource.ts +++ b/src/datasources/EventsDatasource.ts @@ -7,6 +7,7 @@ import { parse as parseQuery } from '../parser/events-assets'; import { AggregateRequest, CogniteEvent, + EventSortRequestParam, EventsFilterRequestParams, EventsFilterTimeParams, FilterRequest, @@ -59,10 +60,15 @@ export class EventsDatasource { advancedFilterQuery: any, timeRange: EventsFilterTimeParams, params: any - }): FilterRequest | AggregateRequest => { + }): FilterRequest | AggregateRequest => { const filter = { ...timeRange, ...params }; - const sortParams = sort?.length ? { sort } : {}; - let body: FilterRequest | AggregateRequest = { + const sortParams = sort?.length ? { + sort: sort.map(item => ({ + property: item.property.split("."), + order: item.order, + })) + } : {}; + let body: FilterRequest | AggregateRequest = { limit: EVENTS_PAGE_LIMIT, filter, } diff --git a/src/types.ts b/src/types.ts index 9b8be6c4..0f15f010 100644 --- a/src/types.ts +++ b/src/types.ts @@ -229,8 +229,12 @@ export interface EventQueryAggregate { withAggregate: boolean; } +export type EventsOrderDirection = 'desc' | 'asc' + +export type EventsOrderNulls = 'first' | 'last' | 'auto' + export interface EventQuerySortProp { - property: string, order?: 'asc' | 'desc', nulls?: 'first' | 'last' | 'auto' + property: string, order?: EventsOrderDirection, nulls?: EventsOrderNulls } export interface EventQuery { From 75bb4ab14ee343f4397d8c662b86ce1abe31f946 Mon Sep 17 00:00:00 2001 From: Ivan Polomani Date: Mon, 8 May 2023 16:52:51 +0200 Subject: [PATCH 5/7] lint --- .gitignore | 1 + src/cdf/types.ts | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index c5d71c30..acf6ccca 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,4 @@ yarn-error.log* .idea .ionide/ +.eslintcache diff --git a/src/cdf/types.ts b/src/cdf/types.ts index d071bbbc..77730944 100644 --- a/src/cdf/types.ts +++ b/src/cdf/types.ts @@ -124,9 +124,9 @@ export interface FilterRequest extends Limit, Cursor { export interface AggregateRequest extends Limit, Cursor { filter?: Filter; aggregate: string; - properties: { + properties: Array<{ property?: string; - }[]; + }>; advancedFilter?: any; } From c18e434d8131774f7183f482b8923908740b5f53 Mon Sep 17 00:00:00 2001 From: Ivan Polomani Date: Tue, 9 May 2023 09:51:22 +0200 Subject: [PATCH 6/7] fix aggregate type --- src/cdf/types.ts | 2 +- src/components/eventsTab.tsx | 5 +++-- src/datasources/EventsDatasource.ts | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/cdf/types.ts b/src/cdf/types.ts index 77730944..2aa84143 100644 --- a/src/cdf/types.ts +++ b/src/cdf/types.ts @@ -125,7 +125,7 @@ export interface AggregateRequest extends Limit, Cursor { filter?: Filter; aggregate: string; properties: Array<{ - property?: string; + property?: string[]; }>; advancedFilter?: any; } diff --git a/src/components/eventsTab.tsx b/src/components/eventsTab.tsx index 17696a25..70a61cdb 100644 --- a/src/components/eventsTab.tsx +++ b/src/components/eventsTab.tsx @@ -110,13 +110,14 @@ const OrderDirectionEditor = ( { onChange, direction = "asc" }: { direction: EventsOrderDirection, onChange: (val: EventsOrderDirection) => void } ) => { - + const options = [{ label: "ascending", value: "asc" }, { label: "descending", value: "desc" }] + return (
Order