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

🧹 Restructure CF functions code into smaller files #4054

Merged
merged 3 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
138 changes: 138 additions & 0 deletions functions/_common/downloadFunctions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import { Grapher } from "@ourworldindata/grapher"
import { OwidColumnDef } from "@ourworldindata/types"
import { StatusError } from "itty-router"
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.js"

export async function fetchMetadataForGrapher(
identifier: GrapherIdentifier,
env: Env,
searchParams?: URLSearchParams
) {
console.log("Initializing grapher")
const grapher = await initGrapher(
identifier,
TWITTER_OPTIONS,
searchParams ?? new URLSearchParams(""),
env
)

await grapher.downloadLegacyDataFromOwidVariableIds()

const fullMetadata = assembleMetadata(
grapher,
searchParams ?? new URLSearchParams("")
)

return Response.json(fullMetadata)
}

export async function fetchZipForGrapher(
Copy link
Member

Choose a reason for hiding this comment

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

Not related to this change as it also happens in master, but in my random testing with URLs in my address bar history, I discovered that http://localhost:8788/grapher/human-trafficking-victims-under-18-years-old-male-vs-female.zip 404s:

Handling error TypeError: Cannot read properties of undefined (reading 'map') at getCitationShort

Should I make a ticket?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A good catch. I fixed this (wrong handling of origins in one place).

identifier: GrapherIdentifier,
env: Env,
searchParams?: URLSearchParams
) {
console.log("preparing to generate zip file")
const grapher = await initGrapher(
identifier,
TWITTER_OPTIONS,
searchParams ?? new URLSearchParams(""),
env
)
await grapher.downloadLegacyDataFromOwidVariableIds()
ensureDownloadOfDataAllowed(grapher)
const metadata = assembleMetadata(grapher, searchParams)
const readme = assembleReadme(grapher)
const csv = assembleCsv(grapher, searchParams)
console.log("Fetched the parts, creating zip file")

const zipContent: File[] = [
{
path: `${identifier.id}.metadata.json`,
data: JSON.stringify(metadata, undefined, 2),
},
{ path: `${identifier.id}.csv`, data: csv },
{ path: "readme.md", data: readme },
]
const content = await createZip(zipContent)
console.log("Generated content, returning response")
return new Response(content, {
headers: {
"Content-Type": "application/zip",
},
})
}
function assembleCsv(grapher: Grapher, searchParams: URLSearchParams): string {
const useShortNames = searchParams.get("useColumnShortNames") === "true"
const table =
searchParams.get("csvType") === "filtered"
? grapher.transformedTable
: grapher.inputTable
return table.toPrettyCsv(useShortNames)
}

export async function fetchCsvForGrapher(
identifier: GrapherIdentifier,
env: Env,
searchParams?: URLSearchParams
) {
const grapher = await initGrapher(
identifier,
TWITTER_OPTIONS,
searchParams ?? new URLSearchParams(""),
env
)
await grapher.downloadLegacyDataFromOwidVariableIds()
console.log("checking if download is allowed")
ensureDownloadOfDataAllowed(grapher)
console.log("data download is allowed")
const csv = assembleCsv(grapher, searchParams ?? new URLSearchParams(""))
return new Response(csv, {
headers: {
"Content-Type": "text/csv",
},
})
}
function ensureDownloadOfDataAllowed(grapher: Grapher) {
if (
grapher.inputTable.columnsAsArray.some(
(col) => (col.def as OwidColumnDef).nonRedistributable
)
) {
throw new StatusError(
403,
"This chart contains non-redistributable data that we are not allowed to re-share and it therefore cannot be downloaded as a CSV."
)
}
}

export async function fetchReadmeForGrapher(
identifier: GrapherIdentifier,
env: Env,
searchParams?: URLSearchParams
) {
console.log("Initializing grapher")
const grapher = await initGrapher(
identifier,
TWITTER_OPTIONS,
searchParams ?? new URLSearchParams(""),
env
)

await grapher.downloadLegacyDataFromOwidVariableIds()

const readme = assembleReadme(grapher)
return new Response(readme, {
headers: {
"Content-Type": "text/markdown; charset=utf-8",
},
})
}
function assembleReadme(grapher: Grapher): string {
const metadataCols = getColumnsForMetadata(grapher)
return constructReadme(grapher, metadataCols)
}
12 changes: 12 additions & 0 deletions functions/_common/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,15 @@ export interface Env {
CF_PAGES_BRANCH: string
ENV: string
}
// We collect the possible extensions here so we can easily take them into account
// when handling redirects
export const extensions = {
configJson: ".config.json",
png: ".png",
svg: ".svg",
csv: ".csv",
metadata: ".metadata.json",
readme: ".readme.md",
zip: ".zip",
}
export type Etag = string
Loading