Skip to content

Commit

Permalink
Fix multidimensional data pages baking logic
Browse files Browse the repository at this point in the history
* Don't delete mdims in GrapherBaker
* Delete old mdims in MultiDimBaker
* Improve the console output
* Change TSConfig to get Set.prototype.intersection() types
  • Loading branch information
rakyi committed Jan 23, 2025
1 parent 61a40a8 commit 6e05aa2
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 14 deletions.
16 changes: 11 additions & 5 deletions baker/GrapherBaker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import { logErrorAndMaybeCaptureInSentry } from "../serverUtils/errorLog.js"
import { getTagToSlugMap } from "./GrapherBakingUtils.js"
import { knexRaw } from "../db/db.js"
import { getRelatedChartsForVariable } from "../db/model/Chart.js"
import { getAllMultiDimDataPageSlugs } from "../db/model/MultiDimDataPage.js"
import pMap from "p-map"

const renderDatapageIfApplicable = async (
Expand Down Expand Up @@ -308,7 +309,6 @@ const bakeGrapherPageAndVariablesPngAndSVGIfChanged = async (
imageMetadataDictionary
)
)
console.log(outPath)

const variableIds = lodash.uniq(
grapher.dimensions?.map((d) => d.variableId)
Expand Down Expand Up @@ -411,7 +411,6 @@ export const bakeAllChangedGrapherPagesVariablesPngSvgAndDeleteRemovedGraphers =
`
)

const newSlugs = chartsToBake.map((row) => row.slug)
await fs.mkdirp(bakedSiteDir + "/grapher")

// Prefetch imageMetadata instead of each grapher page fetching
Expand All @@ -432,7 +431,7 @@ export const bakeAllChangedGrapherPagesVariablesPngSvgAndDeleteRemovedGraphers =
)

const progressBar = new ProgressBar(
"bake grapher page [:bar] :current/:total :elapseds :rate/s :etas :name\n",
"bake grapher page [:bar] :current/:total :elapseds :rate/s :name\n",
{
width: 20,
total: chartsToBake.length + 1,
Expand All @@ -451,11 +450,18 @@ export const bakeAllChangedGrapherPagesVariablesPngSvgAndDeleteRemovedGraphers =
async (knex) => await bakeSingleGrapherChart(job, knex),
db.TransactionCloseMode.KeepOpen
)
progressBar.tick({ name: `slug ${job.slug}` })
progressBar.tick({ name: job.slug })
},
{ concurrency: 10 }
)

await deleteOldGraphers(bakedSiteDir, excludeUndefined(newSlugs))
// Multi-dim data pages are baked into the same directory as graphers
// and they are handled separately.
const multiDimSlugs = await getAllMultiDimDataPageSlugs(knex)
const newSlugs = excludeUndefined([
...chartsToBake.map((row) => row.slug),
...multiDimSlugs,
])
await deleteOldGraphers(bakedSiteDir, newSlugs)
progressBar.tick({ name: `✅ Deleted old graphers` })
}
4 changes: 1 addition & 3 deletions baker/GrapherImageBaker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ export async function bakeGrapherToSvgAndPng(
if (optimizeSvgs) svgCode = await optimizeSvg(svgCode)

return Promise.all([
fs
.writeFile(`${outPath}.svg`, svgCode)
.then(() => console.log(`${outPath}.svg`)),
fs.writeFile(`${outPath}.svg`, svgCode),
sharp(Buffer.from(grapher.staticSVG), { density: 144 })
.png()
.resize(grapher.defaultBounds.width, grapher.defaultBounds.height)
Expand Down
33 changes: 33 additions & 0 deletions baker/MultiDimBaker.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { glob } from "glob"
import fs from "fs-extra"
import path from "path"
import ProgressBar from "progress"
import {
ImageMetadata,
MultiDimDataPageConfigPreProcessed,
Expand Down Expand Up @@ -34,6 +36,7 @@ import {
import { logErrorAndMaybeCaptureInSentry } from "../serverUtils/errorLog.js"
import {
getAllMultiDimDataPages,
getAllUnpublishedMultiDimDataPageSlugs,
getMultiDimDataPageBySlug,
} from "../db/model/MultiDimDataPage.js"

Expand Down Expand Up @@ -219,12 +222,38 @@ export const bakeMultiDimDataPage = async (
await fs.writeFile(outPath, renderedHtml)
}

async function deleteOldMultiDimPages(
bakedSiteDir: string,
unpublishedSlugs: Set<string>
) {
const oldSlugs = glob
.sync(`${bakedSiteDir}/grapher/*.html`)
.map((slug) =>
slug.replace(`${bakedSiteDir}/grapher/`, "").replace(".html", "")
)
const toRemove = unpublishedSlugs.intersection(new Set(oldSlugs))
for (const slug of toRemove) {
console.log(`DELETING ${slug}`)
const path = `${bakedSiteDir}/grapher/${slug}.html`
await fs.unlink(path)
console.log(path)
}
}

export const bakeAllMultiDimDataPages = async (
knex: db.KnexReadonlyTransaction,
bakedSiteDir: string,
imageMetadata: Record<string, ImageMetadata>
) => {
const multiDimsBySlug = await getAllMultiDimDataPages(knex)
const progressBar = new ProgressBar(
"bake multi-dim page [:bar] :current/:total :elapseds :rate/s :name\n",
{
width: 20,
total: multiDimsBySlug.size + 1,
renderThrottle: 0,
}
)
for (const [slug, row] of multiDimsBySlug.entries()) {
await bakeMultiDimDataPage(
knex,
Expand All @@ -233,5 +262,9 @@ export const bakeAllMultiDimDataPages = async (
row.config,
imageMetadata
)
progressBar.tick({ name: slug })
}
const unpublishedSlugs = await getAllUnpublishedMultiDimDataPageSlugs(knex)
await deleteOldMultiDimPages(bakedSiteDir, new Set(unpublishedSlugs))
progressBar.tick({ name: `✅ Deleted old multi-dim pages` })
}
18 changes: 18 additions & 0 deletions db/model/MultiDimDataPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ export async function getAllLinkedPublishedMultiDimDataPages(
return rows.map(enrichRow)
}

export async function getAllMultiDimDataPageSlugs(
knex: KnexReadonlyTransaction
): Promise<string[]> {
const rows = await knex<DbPlainMultiDimDataPage>(
MultiDimDataPagesTableName
).select("slug")
return rows.map((row) => row.slug)
}

export async function getAllUnpublishedMultiDimDataPageSlugs(
knex: KnexReadonlyTransaction
): Promise<string[]> {
const rows = await knex<DbPlainMultiDimDataPage>(MultiDimDataPagesTableName)
.select("slug")
.where({ published: false })
return rows.map((row) => row.slug)
}

export const getMultiDimDataPageBySlug = async (
knex: KnexReadonlyTransaction,
slug: string,
Expand Down
8 changes: 2 additions & 6 deletions devTools/tsconfigs/tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@
"declaration": true,
"declarationMap": true,

// We start with https://github.com/tsconfig/bases/blob/master/bases/node14.json then drop:
// "lib": ["es2020"],
// "target": "es2020",

"lib": ["dom", "dom.iterable", "es2020", "es2021", "es2022", "es2023"],
"target": "es2019",
"lib": ["dom", "dom.iterable", "ESNext"],
"target": "ES2022",

"alwaysStrict": true,
"noImplicitReturns": true,
Expand Down

0 comments on commit 6e05aa2

Please sign in to comment.