From b8f1af4270f2a2df424a1c2375c38fd172f3d23d Mon Sep 17 00:00:00 2001 From: Daniel Bachler Date: Fri, 26 Jul 2024 19:34:32 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20first=20version=20of=20decent=20?= =?UTF-8?q?metadata?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- functions/_common/grapherRenderer.ts | 145 +++++++++++++++++- .../grapher/src/core/LegacyToOwidTable.ts | 2 + .../types/src/domainTypes/CoreTableTypes.ts | 1 + 3 files changed, 144 insertions(+), 4 deletions(-) diff --git a/functions/_common/grapherRenderer.ts b/functions/_common/grapherRenderer.ts index 26f83f60fd..00a05deb48 100644 --- a/functions/_common/grapherRenderer.ts +++ b/functions/_common/grapherRenderer.ts @@ -17,6 +17,7 @@ import { prepareSourcesForDisplay, uniqBy, } from "@ourworldindata/utils" +import { OwidOrigin } from "@ourworldindata/types" import { getAttribution, getCitationLines, @@ -43,6 +44,7 @@ import PlayfairSemiBold from "../_common/fonts/PlayfairDisplayLatin-SemiBold.ttf import { Env } from "../grapher/thumbnail/[slug].js" import { CoreColumn } from "@ourworldindata/core-table" import { toJS } from "mobx" +import { fromPairs } from "lodash" declare global { // eslint-disable-next-line no-var @@ -223,11 +225,146 @@ export async function fetchMetadataForGrapher( await grapher.downloadLegacyDataFromOwidVariableIds() - const defs = grapher.inputTable - .getColumns(grapher.inputTable.columnNames) - .map((col) => col.def) + const columnsToIgnore = new Set( + [ + OwidTableSlugs.entityId, + OwidTableSlugs.time, + OwidTableSlugs.entityColor, + OwidTableSlugs.entityName, + OwidTableSlugs.entityCode, + OwidTableSlugs.year, + ].map((slug) => slug.toString()) + ) + + const columnsToGet = grapher.inputTable.columnSlugs.filter( + (col) => !columnsToIgnore.has(col) + ) + + const columns: [ + string, + { + title: string + titleProducer: string + titleVariant: string + descriptionShort: string + descriptionFromProducer: string + descriptionKey: string[] + descriptionProcessing: string + shortUnit: string + unit: string + timespan: string + tolerance: number + type: string + conversionFactor: number + owidVariableId: number + catalogPath: string + sources: Partial< + Pick< + OwidOrigin, + | "attribution" + | "attributionShort" + | "description" + | "urlDownload" + | "urlMain" + > + >[] + shortName: string + }, + ][] = grapher.inputTable.getColumns(columnsToGet).map((col) => { + const { + descriptionShort, + descriptionFromProducer, + descriptionKey, + descriptionProcessing, + shortUnit, + unit, + timespan, + tolerance, + type, + display, + presentation, + origins, + sourceLink, + sourceName, + catalogPath, + owidVariableId, + shortName, + } = col.def as OwidColumnDef + + let consensedOrigins: Partial< + Pick< + OwidOrigin, + | "attribution" + | "attributionShort" + | "description" + | "urlDownload" + | "urlMain" + > + >[] = origins.map((origin) => { + const { + attribution, + attributionShort, + description, + urlDownload, + urlMain, + } = origin + return { + attribution, + attributionShort, + description, + urlDownload, + urlMain, + } + }) + + if (consensedOrigins.length === 0) { + consensedOrigins = [ + { + attribution: sourceName, + urlMain: sourceLink, + }, + ] + } + + return [ + col.name, + { + title: col.titlePublicOrDisplayName.title, + titleProducer: col.titlePublicOrDisplayName.attributionShort, + titleVariant: col.titlePublicOrDisplayName.titleVariant, + descriptionShort, + descriptionFromProducer, + descriptionKey, + descriptionProcessing, + shortUnit, + unit, + timespan, + tolerance, + type, + conversionFactor: col.display.conversionFactor, + owidVariableId, + catalogPath, + sources: consensedOrigins, + shortName, + }, + ] + }) + + const fullMetadata = { + chart: { + title: grapher.title, + subtitle: grapher.subtitle, + note: grapher.note, + xAxisLabel: grapher.xAxis.label, + yAxisLabel: grapher.yAxis.label, + citation: grapher.sourcesLine, + originalChartUrl: grapher.canonicalUrl, + selection: grapher.selectedEntityNames, + }, + columns: fromPairs(columns), + } - return new Response(JSON.stringify(defs), { + return new Response(JSON.stringify(fullMetadata), { headers: { "Content-Type": "application/json", }, diff --git a/packages/@ourworldindata/grapher/src/core/LegacyToOwidTable.ts b/packages/@ourworldindata/grapher/src/core/LegacyToOwidTable.ts index c1641eb5e2..320d52861c 100644 --- a/packages/@ourworldindata/grapher/src/core/LegacyToOwidTable.ts +++ b/packages/@ourworldindata/grapher/src/core/LegacyToOwidTable.ts @@ -609,6 +609,7 @@ const columnDefFromOwidVariable = ( presentation, catalogPath, updatePeriodDays, + shortName, } = variable // Without this the much used var 123 appears as "Countries Continent". We could rename in Grapher but not sure the effects of that. @@ -660,6 +661,7 @@ const columnDefFromOwidVariable = ( owidSchemaVersion: variable.schemaVersion, type, sort, + shortName, } } diff --git a/packages/@ourworldindata/types/src/domainTypes/CoreTableTypes.ts b/packages/@ourworldindata/types/src/domainTypes/CoreTableTypes.ts index 2df5e36c63..5fa5ba92b0 100644 --- a/packages/@ourworldindata/types/src/domainTypes/CoreTableTypes.ts +++ b/packages/@ourworldindata/types/src/domainTypes/CoreTableTypes.ts @@ -259,6 +259,7 @@ export interface OwidColumnDef extends CoreColumnDef { catalogPath?: string owidProcessingLevel?: OwidProcessingLevel owidSchemaVersion?: number + shortName?: string } export const OwidEntityNameColumnDef = {