Skip to content

Commit

Permalink
🔨 bake chart view map
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiamersmann committed Jan 22, 2025
1 parent 4d6453e commit da86b74
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
2 changes: 1 addition & 1 deletion _routes.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": 1,
"include": ["/grapher/*", "/deleted/*", "/donation/*", "/explorers/*"],
"exclude": ["/grapher/_grapherRedirects.json"]
"exclude": ["/grapher/_grapherRedirects.json", "/grapher/_chartViews.json"]
}
8 changes: 7 additions & 1 deletion adminSiteServer/apiRoutes/chartViews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 }
}

Expand Down Expand Up @@ -276,7 +280,7 @@ export async function updateChartView(

export async function deleteChartView(
req: Request,
_res: e.Response<any, Record<string, any>>,
res: e.Response<any, Record<string, any>>,
trx: db.KnexReadWriteTransaction
) {
const id = expectInt(req.params.id)
Expand Down Expand Up @@ -316,6 +320,8 @@ export async function deleteChartView(

await trx.table(ChartConfigsTableName).where({ id: chartConfigId }).delete()

await triggerStaticBuild(res.locals.user, `Deleting chart view ${name}`)

return { success: true }
}

Expand Down
19 changes: 18 additions & 1 deletion baker/SiteBaker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down Expand Up @@ -152,6 +155,7 @@ const nonWordpressSteps = [
"dods",
"dataInsights",
"authors",
"chartViewMap",
] as const

const otherSteps = ["removeDeletedPosts"] as const
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
17 changes: 16 additions & 1 deletion db/model/ChartView.ts
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -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<DbPlainChartView["name"], DbPlainChartView["chartConfigId"]>
> => {
const rows = await db.knexRaw<
Pick<DbPlainChartView, "name" | "chartConfigId">
>(knex, `SELECT name, chartConfigId FROM chart_views`)
return Object.fromEntries(rows.map((row) => [row.name, row.chartConfigId]))
}

0 comments on commit da86b74

Please sign in to comment.