-
-
Notifications
You must be signed in to change notification settings - Fork 229
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
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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).