From 7220fe7e5a255b692318c3153eaf6bf6aa054533 Mon Sep 17 00:00:00 2001 From: Daniel Bachler Date: Fri, 11 Oct 2024 17:59:22 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20incorporate=20PR=20feedback?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...dataForGrapher.ts => downloadFunctions.ts} | 4 +- functions/_common/env.ts | 1 + functions/_common/grapherRenderer.ts | 3 - functions/_common/metadataTools.ts | 57 ++++++++----------- functions/_common/reusableHandlers.ts | 4 +- functions/grapher/[slug].ts | 5 +- functions/grapher/by-uuid/[uuid].ts | 3 +- 7 files changed, 31 insertions(+), 46 deletions(-) rename functions/_common/{fetchMetadataForGrapher.ts => downloadFunctions.ts} (97%) diff --git a/functions/_common/fetchMetadataForGrapher.ts b/functions/_common/downloadFunctions.ts similarity index 97% rename from functions/_common/fetchMetadataForGrapher.ts rename to functions/_common/downloadFunctions.ts index 6052150d76..aa9f260434 100644 --- a/functions/_common/fetchMetadataForGrapher.ts +++ b/functions/_common/downloadFunctions.ts @@ -1,12 +1,12 @@ import { Grapher } from "@ourworldindata/grapher" import { OwidColumnDef } from "@ourworldindata/types" import { StatusError } from "itty-router" -import { createZip } from "littlezipper" +import { createZip, File } from "littlezipper" import { assembleMetadata, getColumnsForMetadata } from "./metadataTools.js" import { Env } from "./env.js" import { GrapherIdentifier, initGrapher } from "./grapherTools.js" import { TWITTER_OPTIONS } from "./imageOptions.js" -import { constructReadme } from "./readmeTools" +import { constructReadme } from "./readmeTools.js" export async function fetchMetadataForGrapher( identifier: GrapherIdentifier, diff --git a/functions/_common/env.ts b/functions/_common/env.ts index bee51a2624..a26d303d84 100644 --- a/functions/_common/env.ts +++ b/functions/_common/env.ts @@ -19,3 +19,4 @@ export const extensions = { readme: ".readme.md", zip: ".zip", } +export type Etag = string diff --git a/functions/_common/grapherRenderer.ts b/functions/_common/grapherRenderer.ts index 8523f2fbe6..c56b214755 100644 --- a/functions/_common/grapherRenderer.ts +++ b/functions/_common/grapherRenderer.ts @@ -1,7 +1,6 @@ import { svg2png, initialize as initializeSvg2Png } from "svg2png-wasm" import { TimeLogger } from "./timeLogger" import { png } from "itty-router" -import { File } from "littlezipper" import svg2png_wasm from "../../node_modules/svg2png-wasm/svg2png_wasm_bg.wasm" @@ -19,8 +18,6 @@ declare global { var window: any } -export type Etag = string - export const grapherBaseUrl = "https://ourworldindata.org/grapher" // Lots of defaults; these are mostly the same as they are in owid-grapher. diff --git a/functions/_common/metadataTools.ts b/functions/_common/metadataTools.ts index d67019a215..1d1508cecd 100644 --- a/functions/_common/metadataTools.ts +++ b/functions/_common/metadataTools.ts @@ -12,6 +12,27 @@ import { getCitationLong, } from "@ourworldindata/utils" +type MetadataColumn = { + titleShort: string + titleLong: string + descriptionShort: string + descriptionKey: string[] + descriptionProcessing: string + shortUnit: string + unit: string + timespan: string + tolerance: number + type: string + conversionFactor: number + owidVariableId: number + shortName: string + lastUpdated: string + nextUpdate: string + citationShort: string + citationLong: string + fullMetadata: string +} + export const getColumnsForMetadata = (grapher: Grapher) => { const columnsToIgnore = new Set( [ @@ -40,43 +61,12 @@ export function assembleMetadata( const metadataCols = getColumnsForMetadata(grapher) - 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 - }, - ][] = metadataCols.map((col) => { + const columns: [string, MetadataColumn][] = metadataCols.map((col) => { console.log("mapping col", col.name) const { descriptionShort, descriptionKey, descriptionProcessing, - additionalInfo, shortUnit, unit, timespan, @@ -177,13 +167,12 @@ export function assembleMetadata( conversionFactor: col.display?.conversionFactor, owidVariableId, shortName, - additionalInfo, lastUpdated, nextUpdate, citationShort, citationLong, fullMetadata: `https://api.ourworldindata.org/v1/indicators/${owidVariableId}.metadata.json`, - }, + } satisfies MetadataColumn, ] }) const dateDownloaded = new Date() diff --git a/functions/_common/reusableHandlers.ts b/functions/_common/reusableHandlers.ts index 9c0f311815..06d7da7733 100644 --- a/functions/_common/reusableHandlers.ts +++ b/functions/_common/reusableHandlers.ts @@ -1,5 +1,5 @@ -import { Env } from "./env.js" -import { Etag, fetchAndRenderGrapher } from "./grapherRenderer.js" +import { Env, Etag } from "./env.js" +import { fetchAndRenderGrapher } from "./grapherRenderer.js" import { GrapherIdentifier } from "./grapherTools.js" export async function handleThumbnailRequest( diff --git a/functions/grapher/[slug].ts b/functions/grapher/[slug].ts index cebee59635..60c2772b1d 100644 --- a/functions/grapher/[slug].ts +++ b/functions/grapher/[slug].ts @@ -1,11 +1,10 @@ -import { Env, extensions } from "../_common/env.js" -import { Etag } from "../_common/grapherRenderer.js" +import { Env, extensions, Etag } from "../_common/env.js" import { fetchCsvForGrapher, fetchMetadataForGrapher, fetchReadmeForGrapher, fetchZipForGrapher, -} from "../_common/fetchMetadataForGrapher.js" +} from "../_common/downloadFunctions.js" import { handleThumbnailRequest } from "../_common/reusableHandlers.js" import { handlePageNotFound, diff --git a/functions/grapher/by-uuid/[uuid].ts b/functions/grapher/by-uuid/[uuid].ts index c3c3e11d21..352813a0b7 100644 --- a/functions/grapher/by-uuid/[uuid].ts +++ b/functions/grapher/by-uuid/[uuid].ts @@ -1,5 +1,4 @@ -import { Env } from "../../_common/env.js" -import { extensions } from "../../_common/env.js" +import { Env, extensions } from "../../_common/env.js" import { fetchGrapherConfig } from "../../_common/grapherTools.js" import { IRequestStrict, Router, error, StatusError } from "itty-router" import { handleThumbnailRequest } from "../../_common/reusableHandlers.js"