diff --git a/functions/_common/grapherRenderer.ts b/functions/_common/grapherRenderer.ts index 00a05deb48d..26d985ace7c 100644 --- a/functions/_common/grapherRenderer.ts +++ b/functions/_common/grapherRenderer.ts @@ -225,6 +225,8 @@ export async function fetchMetadataForGrapher( await grapher.downloadLegacyDataFromOwidVariableIds() + //const useShortNames = searchParams.get("useColumnShortNames") === "true" + const columnsToIgnore = new Set( [ OwidTableSlugs.entityId, @@ -233,12 +235,15 @@ export async function fetchMetadataForGrapher( OwidTableSlugs.entityName, OwidTableSlugs.entityCode, OwidTableSlugs.year, + OwidTableSlugs.day, ].map((slug) => slug.toString()) ) const columnsToGet = grapher.inputTable.columnSlugs.filter( (col) => !columnsToIgnore.has(col) ) + const useShortNames = searchParams.get("useColumnShortNames") === "true" + console.log("useShortNames", useShortNames) const columns: [ string, @@ -271,6 +276,7 @@ export async function fetchMetadataForGrapher( shortName: string }, ][] = grapher.inputTable.getColumns(columnsToGet).map((col) => { + console.log("mapping col", col.name) const { descriptionShort, descriptionFromProducer, @@ -291,16 +297,18 @@ export async function fetchMetadataForGrapher( shortName, } = col.def as OwidColumnDef - let consensedOrigins: Partial< - Pick< - OwidOrigin, - | "attribution" - | "attributionShort" - | "description" - | "urlDownload" - | "urlMain" - > - >[] = origins.map((origin) => { + let consensedOrigins: + | Partial< + Pick< + OwidOrigin, + | "attribution" + | "attributionShort" + | "description" + | "urlDownload" + | "urlMain" + > + >[] + | undefined = origins?.map((origin) => { const { attribution, attributionShort, @@ -317,7 +325,7 @@ export async function fetchMetadataForGrapher( } }) - if (consensedOrigins.length === 0) { + if (!consensedOrigins || consensedOrigins.length === 0) { consensedOrigins = [ { attribution: sourceName, @@ -327,7 +335,7 @@ export async function fetchMetadataForGrapher( } return [ - col.name, + useShortNames ? shortName : col.name, { title: col.titlePublicOrDisplayName.title, titleProducer: col.titlePublicOrDisplayName.attributionShort, @@ -341,7 +349,7 @@ export async function fetchMetadataForGrapher( timespan, tolerance, type, - conversionFactor: col.display.conversionFactor, + conversionFactor: col.display?.conversionFactor, owidVariableId, catalogPath, sources: consensedOrigins, @@ -422,11 +430,12 @@ export async function fetchCsvForGrapher( grapherLogger ) await grapher.downloadLegacyDataFromOwidVariableIds() + const useShortNames = searchParams.get("useColumnShortNames") === "true" const table = searchParams.get("csvType") === "filtered" ? grapher.transformedTable : grapher.inputTable - return new Response(table.toPrettyCsv(), { + return new Response(table.toPrettyCsv(useShortNames), { headers: { "Content-Type": "text/csv", }, @@ -459,6 +468,7 @@ export async function fetchReadmeForGrapher( OwidTableSlugs.entityName, OwidTableSlugs.entityCode, OwidTableSlugs.year, + OwidTableSlugs.day, ].map((slug) => slug.toString()) ) diff --git a/packages/@ourworldindata/core-table/src/CoreTable.ts b/packages/@ourworldindata/core-table/src/CoreTable.ts index 561db7565d6..86e601de8c6 100644 --- a/packages/@ourworldindata/core-table/src/CoreTable.ts +++ b/packages/@ourworldindata/core-table/src/CoreTable.ts @@ -37,6 +37,7 @@ import { CoreColumnDef, JsTypes, OwidTableSlugs, + OwidColumnDef, } from "@ourworldindata/types" import { AlignedTextTableOptions, @@ -904,11 +905,17 @@ export class CoreTable< return this.toDelimited("\t") } - toCsvWithColumnNames(): string { + toCsvWithColumnNames(useShortNames: boolean = false): string { const delimiter = "," const header = this.columnsAsArray - .map((col) => csvEscape(col.name)) + .map((col) => + csvEscape( + useShortNames && (col.def as OwidColumnDef).shortName + ? (col.def as OwidColumnDef).shortName + : col.name + ) + ) .join(delimiter) + "\n" const body = this.rows .map((row) => diff --git a/packages/@ourworldindata/core-table/src/OwidTable.ts b/packages/@ourworldindata/core-table/src/OwidTable.ts index 3c1568d6370..00420408f21 100644 --- a/packages/@ourworldindata/core-table/src/OwidTable.ts +++ b/packages/@ourworldindata/core-table/src/OwidTable.ts @@ -615,14 +615,14 @@ export class OwidTable extends CoreTable { } // Give our users a clean CSV of each Grapher. Assumes an Owid Table with entityName. - toPrettyCsv(): string { + toPrettyCsv(useShortNames: boolean = false): string { return this.dropColumns([ OwidTableSlugs.entityId, OwidTableSlugs.time, OwidTableSlugs.entityColor, ]) .sortBy([this.entityNameSlug]) - .toCsvWithColumnNames() + .toCsvWithColumnNames(useShortNames) } @imemo get entityNameColorIndex(): Map {