Skip to content

Commit

Permalink
✨ Filter for only active columsn in grapher data download
Browse files Browse the repository at this point in the history
  • Loading branch information
danyx23 committed Dec 2, 2024
1 parent 7cb2d4f commit 6f42518
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
21 changes: 20 additions & 1 deletion packages/@ourworldindata/core-table/src/OwidTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,11 +606,30 @@ export class OwidTable extends CoreTable<OwidRow, OwidColumnDef> {
}

// 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<string> = 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)
Expand Down
11 changes: 9 additions & 2 deletions packages/@ourworldindata/grapher/src/modal/DownloadModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export interface DownloadModalManager {
showAdminControls?: boolean
isSocialMediaExport?: boolean
isPublished?: boolean
activeColumnSlugs?: string[]
}

interface DownloadModalProps {
Expand Down Expand Up @@ -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" })
}
Expand Down Expand Up @@ -765,13 +770,15 @@ export const DownloadModalDataTab = (props: DownloadModalProps) => {
table: props.manager.table ?? BlankOwidTable(),
transformedTable:
props.manager.transformedTable ?? BlankOwidTable(),
activeColumnSlugs: props.manager.activeColumnSlugs,
}),
[
props.manager.baseUrl,
props.manager.displaySlug,
props.manager.queryStr,
props.manager.table,
props.manager.transformedTable,
props.manager.activeColumnSlugs,
]
)

Expand Down

0 comments on commit 6f42518

Please sign in to comment.