From 6f425183ce798d2a0b9c9a8cfeef3e305cc1e58b Mon Sep 17 00:00:00 2001 From: Daniel Bachler Date: Mon, 2 Dec 2024 17:06:27 +0100 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8=20Filter=20for=20only=20active=20?= =?UTF-8?q?columsn=20in=20grapher=20data=20download?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core-table/src/OwidTable.ts | 21 ++++++++++++++++++- .../grapher/src/modal/DownloadModal.tsx | 11 ++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/packages/@ourworldindata/core-table/src/OwidTable.ts b/packages/@ourworldindata/core-table/src/OwidTable.ts index c17d1e7b540..7e2dbb3d851 100644 --- a/packages/@ourworldindata/core-table/src/OwidTable.ts +++ b/packages/@ourworldindata/core-table/src/OwidTable.ts @@ -606,11 +606,30 @@ export class OwidTable extends CoreTable { } // Give our users a clean CSV of each Grapher. Assumes an Owid Table with entityName. - toPrettyCsv(useShortNames: boolean = false): string { + toPrettyCsv( + // + useShortNames: boolean = false, + activeColumnSlugs: string[] | undefined = undefined + ): string { + const activeColumnsDiff: Set = activeColumnSlugs + ? differenceOfSets([ + new Set(this.columnSlugs), + new Set(activeColumnSlugs), + ]) + : new Set() + const columnSlugsToRemove = differenceOfSets([ + activeColumnsDiff, + new Set([ + OwidTableSlugs.year, + OwidTableSlugs.day, + this.entityNameSlug, + ]), + ]) return this.dropColumns([ OwidTableSlugs.entityId, OwidTableSlugs.time, OwidTableSlugs.entityColor, + ...columnSlugsToRemove, ]) .sortBy([this.entityNameSlug]) .toCsvWithColumnNames(useShortNames) diff --git a/packages/@ourworldindata/grapher/src/modal/DownloadModal.tsx b/packages/@ourworldindata/grapher/src/modal/DownloadModal.tsx index b5a45c73b77..39d896a763f 100644 --- a/packages/@ourworldindata/grapher/src/modal/DownloadModal.tsx +++ b/packages/@ourworldindata/grapher/src/modal/DownloadModal.tsx @@ -65,6 +65,7 @@ export interface DownloadModalManager { showAdminControls?: boolean isSocialMediaExport?: boolean isPublished?: boolean + activeColumnSlugs?: string[] } interface DownloadModalProps { @@ -432,13 +433,17 @@ interface DataDownloadContextClientSide extends DataDownloadContextBase { // Only needed for local CSV generation table: OwidTable transformedTable: OwidTable + activeColumnSlugs: string[] | undefined } const createCsvBlobLocally = async (ctx: DataDownloadContextClientSide) => { const csv = ctx.csvDownloadType === CsvDownloadType.Full - ? ctx.table.toPrettyCsv(ctx.shortColNames) - : ctx.transformedTable.toPrettyCsv(ctx.shortColNames) + ? ctx.table.toPrettyCsv(ctx.shortColNames, ctx.activeColumnSlugs) + : ctx.transformedTable.toPrettyCsv( + ctx.shortColNames, + ctx.activeColumnSlugs + ) return new Blob([csv], { type: "text/csv;charset=utf-8" }) } @@ -765,6 +770,7 @@ export const DownloadModalDataTab = (props: DownloadModalProps) => { table: props.manager.table ?? BlankOwidTable(), transformedTable: props.manager.transformedTable ?? BlankOwidTable(), + activeColumnSlugs: props.manager.activeColumnSlugs, }), [ props.manager.baseUrl, @@ -772,6 +778,7 @@ export const DownloadModalDataTab = (props: DownloadModalProps) => { props.manager.queryStr, props.manager.table, props.manager.transformedTable, + props.manager.activeColumnSlugs, ] ) From ff311eee804af2e815b3eea0e761eb7ecb4de92f Mon Sep 17 00:00:00 2001 From: Daniel Bachler Date: Tue, 3 Dec 2024 10:06:36 +0100 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=94=A8=20incorporate=20PR=20feedback?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core-table/src/OwidTable.ts | 31 ++++++++----------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/packages/@ourworldindata/core-table/src/OwidTable.ts b/packages/@ourworldindata/core-table/src/OwidTable.ts index 7e2dbb3d851..529aff908b1 100644 --- a/packages/@ourworldindata/core-table/src/OwidTable.ts +++ b/packages/@ourworldindata/core-table/src/OwidTable.ts @@ -607,30 +607,25 @@ export class OwidTable extends CoreTable { // Give our users a clean CSV of each Grapher. Assumes an Owid Table with entityName. toPrettyCsv( - // useShortNames: boolean = false, activeColumnSlugs: string[] | undefined = undefined ): string { - const activeColumnsDiff: Set = activeColumnSlugs - ? differenceOfSets([ - new Set(this.columnSlugs), - new Set(activeColumnSlugs), - ]) - : new Set() - const columnSlugsToRemove = differenceOfSets([ - activeColumnsDiff, - new Set([ + let table + if (activeColumnSlugs?.length) { + table = this.select([ OwidTableSlugs.year, OwidTableSlugs.day, this.entityNameSlug, - ]), - ]) - return this.dropColumns([ - OwidTableSlugs.entityId, - OwidTableSlugs.time, - OwidTableSlugs.entityColor, - ...columnSlugsToRemove, - ]) + ...activeColumnSlugs, + ]) + } else { + table = this.dropColumns([ + OwidTableSlugs.entityId, + OwidTableSlugs.time, + OwidTableSlugs.entityColor, + ]) + } + return table .sortBy([this.entityNameSlug]) .toCsvWithColumnNames(useShortNames) }