From b5c18cd9074994cb4321e61d2502609fd7728923 Mon Sep 17 00:00:00 2001 From: Daniel Bachler Date: Tue, 13 Aug 2024 16:45:54 +0200 Subject: [PATCH] Add storing single charts to local R2 bucket to syncGraphersToR2 tool The syncGraphersToR2 tool let's you sync all needed entries from the chart_configs table to an actual R2 bucket. This is useful for production and staging servers but in local dev the R2 bindings currently can't connect to an actual R2 bucket and instead simulate one using a local folder. This PR adds a tool to store single chart configs by slug from the database into the local R2 bucket by invoking the `wrangler` cli tool for storing R2 objects. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/owid/owid-grapher?shareId=XXXX-XXXX-XXXX-XXXX). --- devTools/syncGraphersToR2/syncGraphersToR2.ts | 40 ++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/devTools/syncGraphersToR2/syncGraphersToR2.ts b/devTools/syncGraphersToR2/syncGraphersToR2.ts index 52df77a92f6..7a084d94b36 100644 --- a/devTools/syncGraphersToR2/syncGraphersToR2.ts +++ b/devTools/syncGraphersToR2/syncGraphersToR2.ts @@ -36,6 +36,8 @@ import { import { string } from "ts-pattern/dist/patterns.js" import { chunk, take } from "lodash" import ProgressBar from "progress" +import { getEnrichedChartBySlug } from "../../db/model/Chart.js" +import { exec } from "child_process" type HashAndId = Pick @@ -283,7 +285,43 @@ async function storeDevBySlug( parsedArgs: parseArgs.ParsedArgs, dryRun: boolean ) { - console.log("Dummy implementation for store-dev-by-slug") + const slug = parsedArgs._[1] + if (!slug) { + console.error("No slug provided") + return + } + + await knexReadonlyTransaction(async (trx) => { + const chart = await knexRawFirst( + trx, + `SELECT full FROM chart_configs WHERE slug = ?`, + [slug] + ) + if (!chart) { + console.error(`No chart found for slug ${slug}`) + return + } + + const fullConfig = JSON.stringify(chart.full) + const command = `npx wrangler r2 object put --local $GRAPHER_CONFIG_R2_BUCKET/$GRAPHER_CONFIG_R2_BUCKET_PATH/grapher/by-slug/${slug}.json --pipe --content-type application/json --persist-to ./cfstorage` + + const process = exec(command, (error, stdout, stderr) => { + if (error) { + console.error( + `Error executing wrangler command: ${error.message}` + ) + return + } + if (stderr) { + console.error(`Wrangler stderr: ${stderr}`) + return + } + console.log(`Wrangler stdout: ${stdout}`) + }) + + process.stdin.write(fullConfig) + process.stdin.end() + }) } async function main(parsedArgs: parseArgs.ParsedArgs) {