Skip to content

Commit

Permalink
🔨 improve metadata, enable using column short names
Browse files Browse the repository at this point in the history
  • Loading branch information
danyx23 committed Jul 26, 2024
1 parent e03cc92 commit dfca6dc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
38 changes: 24 additions & 14 deletions functions/_common/grapherRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ export async function fetchMetadataForGrapher(

await grapher.downloadLegacyDataFromOwidVariableIds()

//const useShortNames = searchParams.get("useColumnShortNames") === "true"

const columnsToIgnore = new Set(
[
OwidTableSlugs.entityId,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -317,7 +325,7 @@ export async function fetchMetadataForGrapher(
}
})

if (consensedOrigins.length === 0) {
if (!consensedOrigins || consensedOrigins.length === 0) {
consensedOrigins = [
{
attribution: sourceName,
Expand All @@ -327,7 +335,7 @@ export async function fetchMetadataForGrapher(
}

return [
col.name,
useShortNames ? shortName : col.name,
{
title: col.titlePublicOrDisplayName.title,
titleProducer: col.titlePublicOrDisplayName.attributionShort,
Expand All @@ -341,7 +349,7 @@ export async function fetchMetadataForGrapher(
timespan,
tolerance,
type,
conversionFactor: col.display.conversionFactor,
conversionFactor: col.display?.conversionFactor,
owidVariableId,
catalogPath,
sources: consensedOrigins,
Expand Down Expand Up @@ -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",
},
Expand Down Expand Up @@ -459,6 +468,7 @@ export async function fetchReadmeForGrapher(
OwidTableSlugs.entityName,
OwidTableSlugs.entityCode,
OwidTableSlugs.year,
OwidTableSlugs.day,
].map((slug) => slug.toString())
)

Expand Down
11 changes: 9 additions & 2 deletions packages/@ourworldindata/core-table/src/CoreTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
CoreColumnDef,
JsTypes,
OwidTableSlugs,
OwidColumnDef,
} from "@ourworldindata/types"
import {
AlignedTextTableOptions,
Expand Down Expand Up @@ -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) =>
Expand Down
4 changes: 2 additions & 2 deletions packages/@ourworldindata/core-table/src/OwidTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,14 +615,14 @@ export class OwidTable extends CoreTable<OwidRow, OwidColumnDef> {
}

// 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<EntityName, Color> {
Expand Down

0 comments on commit dfca6dc

Please sign in to comment.