From d26b0f0b21dc8b69c2c6dcd9fa81d3117b8a5e0e Mon Sep 17 00:00:00 2001 From: Sophia Mersmann Date: Tue, 21 Jan 2025 09:03:34 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20bake=20chart=20view=20map?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _routes.json | 2 +- adminSiteServer/apiRoutes/chartViews.ts | 8 +++++++- baker/SiteBaker.tsx | 19 ++++++++++++++++++- db/model/ChartView.ts | 17 ++++++++++++++++- 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/_routes.json b/_routes.json index d8dfb830c38..c4a5a3d7b6d 100644 --- a/_routes.json +++ b/_routes.json @@ -1,5 +1,5 @@ { "version": 1, "include": ["/grapher/*", "/deleted/*", "/donation/*", "/explorers/*"], - "exclude": ["/grapher/_grapherRedirects.json"] + "exclude": ["/grapher/_grapherRedirects.json", "grapher/_chartViews.json"] } diff --git a/adminSiteServer/apiRoutes/chartViews.ts b/adminSiteServer/apiRoutes/chartViews.ts index 8dd6224384f..a747d370428 100644 --- a/adminSiteServer/apiRoutes/chartViews.ts +++ b/adminSiteServer/apiRoutes/chartViews.ts @@ -35,6 +35,8 @@ import { expectChartById } from "./charts.js" import { Request } from "../authentication.js" import e from "express" import { getPublishedLinksTo } from "../../db/model/Link.js" +import { triggerStaticBuild } from "./routeUtils.js" + const createPatchConfigAndQueryParamsForChartView = async ( knex: db.KnexReadonlyTransaction, parentChartId: number, @@ -222,6 +224,8 @@ export async function createChartView( const result = await trx.table(ChartViewsTableName).insert(insertRow) const [resultId] = result + await triggerStaticBuild(res.locals.user, `Creating chart view ${name}`) + return { chartViewId: resultId, success: true } } @@ -276,7 +280,7 @@ export async function updateChartView( export async function deleteChartView( req: Request, - _res: e.Response>, + res: e.Response>, trx: db.KnexReadWriteTransaction ) { const id = expectInt(req.params.id) @@ -316,6 +320,8 @@ export async function deleteChartView( await trx.table(ChartConfigsTableName).where({ id: chartConfigId }).delete() + await triggerStaticBuild(res.locals.user, `Creating chart view ${name}`) + return { success: true } } diff --git a/baker/SiteBaker.tsx b/baker/SiteBaker.tsx index bb395c4affc..c729a44d0bb 100644 --- a/baker/SiteBaker.tsx +++ b/baker/SiteBaker.tsx @@ -114,7 +114,10 @@ import { getTombstones } from "../db/model/GdocTombstone.js" import { bakeAllMultiDimDataPages } from "./MultiDimBaker.js" import { getAllLinkedPublishedMultiDimDataPages } from "../db/model/MultiDimDataPage.js" import { getPublicDonorNames } from "../db/model/Donor.js" -import { getChartViewsInfo } from "../db/model/ChartView.js" +import { + getChartViewNameConfigMap, + getChartViewsInfo, +} from "../db/model/ChartView.js" type PrefetchedAttachments = { donors: string[] @@ -152,6 +155,7 @@ const nonWordpressSteps = [ "dods", "dataInsights", "authors", + "chartViewMap", ] as const const otherSteps = ["removeDeletedPosts"] as const @@ -1116,6 +1120,18 @@ export class SiteBaker { this.progressBar.tick({ name: "✅ baked redirects" }) } + async bakeChartViewMap(knex: db.KnexReadonlyTransaction) { + if (!this.bakeSteps.has("chartViewMap")) return + + const chartViewMap = await getChartViewNameConfigMap(knex) + await this.stageWrite( + path.join(this.bakedSiteDir, `grapher/_chartViews.json`), + JSON.stringify(chartViewMap, null, 2) + ) + + this.progressBar.tick({ name: "✅ baked chart views" }) + } + async bakeWordpressPages(knex: db.KnexReadonlyTransaction) { await this.bakeRedirects(knex) await this.bakeEmbeds(knex) @@ -1148,6 +1164,7 @@ export class SiteBaker { await this.bakeGDocTombstones(knex) await this.bakeDataInsights(knex) await this.bakeAuthors(knex) + await this.bakeChartViewMap(knex) } async bakeNonWordpressPages(knex: db.KnexReadonlyTransaction) { diff --git a/db/model/ChartView.ts b/db/model/ChartView.ts index 2bfd5ad8ed3..ac380e12239 100644 --- a/db/model/ChartView.ts +++ b/db/model/ChartView.ts @@ -1,4 +1,8 @@ -import { ChartViewInfo, JsonString } from "@ourworldindata/types" +import { + ChartViewInfo, + DbPlainChartView, + JsonString, +} from "@ourworldindata/types" import * as db from "../db.js" export const getChartViewsInfo = async ( @@ -32,3 +36,14 @@ JOIN chart_configs pcc on pc.configId = pcc.id queryParamsForParentChart: JSON.parse(row.queryParamsForParentChart), })) } + +export const getChartViewNameConfigMap = async ( + knex: db.KnexReadonlyTransaction +): Promise< + Record +> => { + const rows = await db.knexRaw< + Pick + >(knex, `SELECT name, chartConfigId FROM chart_views`) + return Object.fromEntries(rows.map((row) => [row.name, row.chartConfigId])) +}