From 6f1ae8af02deefab880718fc2115e9810649b77e Mon Sep 17 00:00:00 2001 From: Don McCurdy Date: Wed, 4 Sep 2024 14:05:51 -0400 Subject: [PATCH] feat(widgets): Improve aggregation types and getTable params --- .../components/widgets/category-widget.ts | 10 ++------- examples/components/widgets/formula-widget.ts | 3 +-- .../components/widgets/histogram-widget.ts | 3 +-- examples/components/widgets/scatter-widget.ts | 5 ++--- src/constants.ts | 15 ------------- src/sources/types.ts | 21 +++++++++---------- src/sources/widget-base-source.ts | 6 +++--- src/types.ts | 20 +++++++++++++++++- test/constants.test.ts | 7 +------ test/filters.test.ts | 1 - test/sources/widget-base-source.test.ts | 6 ++++-- test/types.test.ts | 14 +++++++++++++ 12 files changed, 57 insertions(+), 54 deletions(-) diff --git a/examples/components/widgets/category-widget.ts b/examples/components/widgets/category-widget.ts index f81e180..089a4bd 100644 --- a/examples/components/widgets/category-widget.ts +++ b/examples/components/widgets/category-widget.ts @@ -3,13 +3,7 @@ import {Task, TaskStatus} from '@lit/task'; import {Ref, createRef, ref} from 'lit/directives/ref.js'; import {cache} from 'lit/directives/cache.js'; import * as echarts from 'echarts'; -import { - AggregationType, - Filter, - FilterType, - addFilter, - removeFilter, -} from '@carto/api-client'; +import {Filter, FilterType, addFilter, removeFilter} from '@carto/api-client'; import {DEFAULT_PALETTE, DEFAULT_TEXT_STYLE} from './styles.js'; import {DEBOUNCE_TIME_MS} from '../constants.js'; @@ -36,7 +30,7 @@ export class CategoryWidget extends BaseWidget { } declare column: string; - declare operation: AggregationType; + declare operation: 'count' | 'avg' | 'min' | 'max' | 'sum'; protected _chart: echarts.ECharts | null = null; protected _chartRef: Ref = createRef(); diff --git a/examples/components/widgets/formula-widget.ts b/examples/components/widgets/formula-widget.ts index 262e81a..a55af3e 100644 --- a/examples/components/widgets/formula-widget.ts +++ b/examples/components/widgets/formula-widget.ts @@ -1,6 +1,5 @@ import {html, css} from 'lit'; import {Task} from '@lit/task'; -import {AggregationType} from '@carto/api-client'; import {DEBOUNCE_TIME_MS} from '../constants.js'; import {sleep} from '../utils.js'; import {BaseWidget} from './base-widget.js'; @@ -39,7 +38,7 @@ export class FormulaWidget extends BaseWidget { }; } - declare operation: AggregationType; + declare operation: 'count' | 'avg' | 'min' | 'max' | 'sum'; declare column: string; constructor() { diff --git a/examples/components/widgets/histogram-widget.ts b/examples/components/widgets/histogram-widget.ts index 1f6955c..dc7af4b 100644 --- a/examples/components/widgets/histogram-widget.ts +++ b/examples/components/widgets/histogram-widget.ts @@ -4,7 +4,6 @@ import {Ref, createRef, ref} from 'lit/directives/ref.js'; import {cache} from 'lit/directives/cache.js'; import * as echarts from 'echarts'; import {TaskStatus} from '@lit/task'; -import {AggregationType} from '@carto/api-client'; import {DEBOUNCE_TIME_MS} from '../constants.js'; import {sleep} from '../utils.js'; @@ -31,7 +30,7 @@ export class HistogramWidget extends BaseWidget { } declare column: string; - declare operation: AggregationType; + declare operation: 'count' | 'avg' | 'min' | 'max' | 'sum'; declare ticks: number[]; protected _chart: echarts.ECharts | null = null; diff --git a/examples/components/widgets/scatter-widget.ts b/examples/components/widgets/scatter-widget.ts index ba5de21..674b04d 100644 --- a/examples/components/widgets/scatter-widget.ts +++ b/examples/components/widgets/scatter-widget.ts @@ -4,7 +4,6 @@ import {Ref, createRef, ref} from 'lit/directives/ref.js'; import {cache} from 'lit/directives/cache.js'; import * as echarts from 'echarts'; import {TaskStatus} from '@lit/task'; -import {AggregationType} from '@carto/api-client'; import {DEBOUNCE_TIME_MS} from '../constants.js'; import {sleep} from '../utils.js'; @@ -32,9 +31,9 @@ export class ScatterWidget extends BaseWidget { } declare xAxisColumn: string; - declare xAxisJoinOperation: AggregationType; + declare xAxisJoinOperation: 'count' | 'avg' | 'min' | 'max' | 'sum'; declare yAxisColumn: string; - declare yAxisJoinOperation: AggregationType; + declare yAxisJoinOperation: 'count' | 'avg' | 'min' | 'max' | 'sum'; protected _chart: echarts.ECharts | null = null; protected _chartRef: Ref = createRef(); diff --git a/src/constants.ts b/src/constants.ts index 93f8f54..2dee990 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,18 +1,3 @@ -/** - * Defines a step size increment for use with {@link TimeSeriesRequestOptions}. - * - * @internalRemarks Source: @carto/react-core - */ -export enum GroupDateType { - YEARS = 'year', - MONTHS = 'month', - WEEKS = 'week', - DAYS = 'day', - HOURS = 'hour', - MINUTES = 'minute', - SECONDS = 'second', -} - /** * Defines a comparator used when matching a column's values against given filter values. * diff --git a/src/sources/types.ts b/src/sources/types.ts index 2cb2562..05b035c 100644 --- a/src/sources/types.ts +++ b/src/sources/types.ts @@ -1,6 +1,5 @@ -import {GroupDateType} from '../constants'; import { - AggregationType, + GroupDateType, SortColumnType, SortDirection, SpatialFilter, @@ -20,14 +19,14 @@ interface BaseRequestOptions { /** Options for {@link WidgetBaseSource#getCategories}. */ export interface CategoryRequestOptions extends BaseRequestOptions { column: string; - operation?: AggregationType; + operation?: 'count' | 'avg' | 'min' | 'max' | 'sum'; operationColumn?: string; } /** Options for {@link WidgetBaseSource#getFormula}. */ export interface FormulaRequestOptions extends BaseRequestOptions { column: string; - operation?: AggregationType; + operation?: 'count' | 'avg' | 'min' | 'max' | 'sum'; operationExp?: string; } @@ -35,7 +34,7 @@ export interface FormulaRequestOptions extends BaseRequestOptions { export interface HistogramRequestOptions extends BaseRequestOptions { column: string; ticks: number[]; - operation?: AggregationType; + operation?: 'count' | 'avg' | 'min' | 'max' | 'sum'; } /** Options for {@link WidgetBaseSource#getRange}. */ @@ -46,9 +45,9 @@ export interface RangeRequestOptions extends BaseRequestOptions { /** Options for {@link WidgetBaseSource#getScatter}. */ export interface ScatterRequestOptions extends BaseRequestOptions { xAxisColumn: string; - xAxisJoinOperation?: AggregationType; + xAxisJoinOperation?: 'count' | 'avg' | 'min' | 'max' | 'sum'; yAxisColumn: string; - yAxisJoinOperation?: AggregationType; + yAxisJoinOperation?: 'count' | 'avg' | 'min' | 'max' | 'sum'; } /** Options for {@link WidgetBaseSource#getTable}. */ @@ -57,8 +56,8 @@ export interface TableRequestOptions extends BaseRequestOptions { sortBy?: string; sortDirection?: SortDirection; sortByColumnType?: SortColumnType; - page?: number; - rowsPerPage?: number; + offset?: number; + limit?: number; } /** Options for {@link WidgetBaseSource#getTimeSeries}. */ @@ -66,9 +65,9 @@ export interface TimeSeriesRequestOptions extends BaseRequestOptions { column: string; stepSize?: GroupDateType; stepMultiplier?: number; - operation?: AggregationType; + operation?: 'count' | 'avg' | 'min' | 'max' | 'sum'; operationColumn?: string; - joinOperation?: AggregationType; + joinOperation?: 'count' | 'avg' | 'min' | 'max' | 'sum'; splitByCategory?: string; splitByCategoryLimit?: number; splitByCategoryValues?: string[]; diff --git a/src/sources/widget-base-source.ts b/src/sources/widget-base-source.ts index f2c20c3..25b8bf9 100644 --- a/src/sources/widget-base-source.ts +++ b/src/sources/widget-base-source.ts @@ -239,7 +239,7 @@ export abstract class WidgetBaseSource { */ async getTable(options: TableRequestOptions): Promise { const {filterOwner, spatialFilter, abortController, ...params} = options; - const {columns, sortBy, sortDirection, page = 0, rowsPerPage = 10} = params; + const {columns, sortBy, sortDirection, offset = 0, limit = 10} = params; type TableModelResponse = { rows: Record[]; @@ -253,8 +253,8 @@ export abstract class WidgetBaseSource { column: columns, sortBy, sortDirection, - limit: rowsPerPage, - offset: page * rowsPerPage, + limit, + offset, }, opts: {abortController}, }).then((res: TableModelResponse) => ({ diff --git a/src/types.ts b/src/types.ts index e736b11..e905205 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,4 @@ -import type {FilterType} from './constants'; +import type {FilterType} from './constants.js'; /****************************************************************************** * AGGREGATION @@ -39,6 +39,24 @@ export interface Filter { /** @internalRemarks Source: @carto/react-core */ export type FilterLogicalOperator = 'and' | 'or'; +/****************************************************************************** + * GROUPING + */ + +/** + * Defines a step size increment for use with {@link TimeSeriesRequestOptions}. + * + * @internalRemarks Source: @carto/react-core + */ +export type GroupDateType = + | 'year' + | 'month' + | 'week' + | 'day' + | 'hour' + | 'minute' + | 'second'; + /****************************************************************************** * SORTING */ diff --git a/test/constants.test.ts b/test/constants.test.ts index 3f1b4dd..77015e6 100644 --- a/test/constants.test.ts +++ b/test/constants.test.ts @@ -1,10 +1,5 @@ import {expect, test} from 'vitest'; -import {GroupDateType, FilterType} from '@carto/api-client'; - -test('GroupDateType', () => { - expect(GroupDateType.DAYS).toBe('day'); - expect(GroupDateType.HOURS).toBe('hour'); -}); +import {FilterType} from '@carto/api-client'; test('FilterType', () => { expect(FilterType.IN).toBe('in'); diff --git a/test/filters.test.ts b/test/filters.test.ts index 5b4367d..0b25176 100644 --- a/test/filters.test.ts +++ b/test/filters.test.ts @@ -1,6 +1,5 @@ import {expect, test} from 'vitest'; import { - GroupDateType, FilterType, clearFilters, addFilter, diff --git a/test/sources/widget-base-source.test.ts b/test/sources/widget-base-source.test.ts index bc40b2e..d7ffc6f 100644 --- a/test/sources/widget-base-source.test.ts +++ b/test/sources/widget-base-source.test.ts @@ -321,6 +321,8 @@ test('getTable', async () => { const actualTable = await widgetSource.getTable({ columns: ['name', 'revenue'], + limit: 20, + offset: 10, }); expect(mockFetch).toHaveBeenCalledOnce(); @@ -335,8 +337,8 @@ test('getTable', async () => { source: 'test-data', params: JSON.stringify({ column: ['name', 'revenue'], - limit: 10, - offset: 0, + limit: 20, + offset: 10, }), queryParameters: '', filters: JSON.stringify({}), diff --git a/test/types.test.ts b/test/types.test.ts index d0545b5..a2fdf0a 100644 --- a/test/types.test.ts +++ b/test/types.test.ts @@ -4,6 +4,7 @@ import { Filter, FilterLogicalOperator, FilterType, + GroupDateType, SpatialFilter, } from '@carto/api-client'; @@ -77,3 +78,16 @@ test('FilterLogicalOperator', () => { // @ts-expect-error assertType('invalid'); }); + +test('GroupDateType', () => { + assertType('year'); + assertType('month'); + assertType('week'); + assertType('day'); + assertType('hour'); + assertType('minute'); + assertType('second'); + + // @ts-expect-error + assertType('invalid'); +});