Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Filter for only active columns in grapher data download #4242

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
//
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: empty comment

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,
]),
])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this all would be a bit cleaner and easier to follow if it was using this.select() instead. The two diffs after one another are hard to reason about, after all.

I.e. something like:

if (activeColumnSlugs?.length) {
	this.select([year, day, entityName, ...activeColumnSlugs])
} else {
	this.dropColumns(...)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah very nice! I looked a bit to see if we had the inverse of drop and was looking for pick or keep but didn't think of select. Much nicer, thanks!

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
Loading