From ba95bfafe2a676d921a5207672b38ec07d4b60ac Mon Sep 17 00:00:00 2001 From: YANG QIA <2013xile@gmail.com> Date: Tue, 20 Aug 2024 16:47:03 +0800 Subject: [PATCH] feat: allow to set distinct for query measures (#5091) --- .../src/client/configure/schemas/configure.ts | 6 ++++++ .../plugin-data-visualization/src/locale/en-US.json | 3 ++- .../plugin-data-visualization/src/locale/zh-CN.json | 3 ++- .../plugin-data-visualization/src/server/actions/query.ts | 5 +++-- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/plugins/@nocobase/plugin-data-visualization/src/client/configure/schemas/configure.ts b/packages/plugins/@nocobase/plugin-data-visualization/src/client/configure/schemas/configure.ts index eab34e309..2be946786 100644 --- a/packages/plugins/@nocobase/plugin-data-visualization/src/client/configure/schemas/configure.ts +++ b/packages/plugins/@nocobase/plugin-data-visualization/src/client/configure/schemas/configure.ts @@ -289,6 +289,12 @@ export const querySchema: ISchema = { }, }, }, + distinct: { + type: 'boolean', + 'x-decorator': 'FormItem', + 'x-component': 'Checkbox', + 'x-content': '{{t("Distinct")}}', + }, }, { required: true, diff --git a/packages/plugins/@nocobase/plugin-data-visualization/src/locale/en-US.json b/packages/plugins/@nocobase/plugin-data-visualization/src/locale/en-US.json index 88902e283..7a6b12777 100644 --- a/packages/plugins/@nocobase/plugin-data-visualization/src/locale/en-US.json +++ b/packages/plugins/@nocobase/plugin-data-visualization/src/locale/en-US.json @@ -94,5 +94,6 @@ "Aspect ratio": "Aspect ratio", "Fixed height": "Fixed height", "Show background": "Show background", - "Show padding": "Show padding" + "Show padding": "Show padding", + "Distinct": "Distinct" } diff --git a/packages/plugins/@nocobase/plugin-data-visualization/src/locale/zh-CN.json b/packages/plugins/@nocobase/plugin-data-visualization/src/locale/zh-CN.json index c1c9d272a..563357e91 100644 --- a/packages/plugins/@nocobase/plugin-data-visualization/src/locale/zh-CN.json +++ b/packages/plugins/@nocobase/plugin-data-visualization/src/locale/zh-CN.json @@ -95,5 +95,6 @@ "Aspect ratio": "宽高比", "Fixed height": "固定高度", "Show background": "显示背景", - "Show padding": "显示内边距" + "Show padding": "显示内边距", + "Distinct": "去重" } diff --git a/packages/plugins/@nocobase/plugin-data-visualization/src/server/actions/query.ts b/packages/plugins/@nocobase/plugin-data-visualization/src/server/actions/query.ts index bbf9eaef9..747213664 100644 --- a/packages/plugins/@nocobase/plugin-data-visualization/src/server/actions/query.ts +++ b/packages/plugins/@nocobase/plugin-data-visualization/src/server/actions/query.ts @@ -19,6 +19,7 @@ type MeasureProps = { type?: string; aggregation?: string; alias?: string; + distinct?: boolean; }; type DimensionProps = { @@ -117,7 +118,7 @@ export const parseBuilder = async (ctx: Context, next: Next) => { let hasAgg = false; measures.forEach((measure: MeasureProps & { field: string }) => { - const { field, aggregation, alias } = measure; + const { field, aggregation, alias, distinct } = measure; const attribute = []; const col = sequelize.col(field); if (aggregation) { @@ -125,7 +126,7 @@ export const parseBuilder = async (ctx: Context, next: Next) => { throw new Error(`Invalid aggregation function: ${aggregation}`); } hasAgg = true; - attribute.push(sequelize.fn(aggregation, col)); + attribute.push(sequelize.fn(aggregation, distinct ? sequelize.fn('DISTINCT', col) : col)); } else { attribute.push(col); }