From 3a8aa0b031b9fcc7d16f1ce1665000c98c4188c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ra=C4=8D=C3=A1k?= Date: Thu, 23 Jan 2025 11:47:37 +0100 Subject: [PATCH] Fix multidimensional data pages baking logic * Don't delete mdims in GrapherBaker * Delete old mdims in MultiDimBaker * Improve the console output * Change TSConfig to get Set.prototype.intersection() types --- baker/GrapherBaker.tsx | 16 +++++++++---- baker/GrapherImageBaker.tsx | 4 +--- baker/MultiDimBaker.tsx | 33 +++++++++++++++++++++++++++ db/model/MultiDimDataPage.ts | 18 +++++++++++++++ devTools/tsconfigs/tsconfig.base.json | 8 ++----- 5 files changed, 65 insertions(+), 14 deletions(-) diff --git a/baker/GrapherBaker.tsx b/baker/GrapherBaker.tsx index 74f3fa4eb84..86897b067f9 100644 --- a/baker/GrapherBaker.tsx +++ b/baker/GrapherBaker.tsx @@ -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 ( @@ -308,7 +309,6 @@ const bakeGrapherPageAndVariablesPngAndSVGIfChanged = async ( imageMetadataDictionary ) ) - console.log(outPath) const variableIds = lodash.uniq( grapher.dimensions?.map((d) => d.variableId) @@ -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 @@ -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, @@ -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` }) } diff --git a/baker/GrapherImageBaker.tsx b/baker/GrapherImageBaker.tsx index 51362d1cafa..59694a128c4 100644 --- a/baker/GrapherImageBaker.tsx +++ b/baker/GrapherImageBaker.tsx @@ -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) diff --git a/baker/MultiDimBaker.tsx b/baker/MultiDimBaker.tsx index 1756c725058..04a06250f78 100644 --- a/baker/MultiDimBaker.tsx +++ b/baker/MultiDimBaker.tsx @@ -1,5 +1,7 @@ +import { glob } from "glob" import fs from "fs-extra" import path from "path" +import ProgressBar from "progress" import { ImageMetadata, MultiDimDataPageConfigPreProcessed, @@ -34,6 +36,7 @@ import { import { logErrorAndMaybeCaptureInSentry } from "../serverUtils/errorLog.js" import { getAllMultiDimDataPages, + getAllUnpublishedMultiDimDataPageSlugs, getMultiDimDataPageBySlug, } from "../db/model/MultiDimDataPage.js" @@ -219,12 +222,38 @@ export const bakeMultiDimDataPage = async ( await fs.writeFile(outPath, renderedHtml) } +async function deleteOldMultiDimPages( + bakedSiteDir: string, + unpublishedSlugs: Set +) { + 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 ) => { 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, @@ -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` }) } diff --git a/db/model/MultiDimDataPage.ts b/db/model/MultiDimDataPage.ts index 50fc52e0c2c..f94d6a230fa 100644 --- a/db/model/MultiDimDataPage.ts +++ b/db/model/MultiDimDataPage.ts @@ -84,6 +84,24 @@ export async function getAllLinkedPublishedMultiDimDataPages( return rows.map(enrichRow) } +export async function getAllMultiDimDataPageSlugs( + knex: KnexReadonlyTransaction +): Promise { + const rows = await knex( + MultiDimDataPagesTableName + ).select("slug") + return rows.map((row) => row.slug) +} + +export async function getAllUnpublishedMultiDimDataPageSlugs( + knex: KnexReadonlyTransaction +): Promise { + const rows = await knex(MultiDimDataPagesTableName) + .select("slug") + .where({ published: false }) + return rows.map((row) => row.slug) +} + export const getMultiDimDataPageBySlug = async ( knex: KnexReadonlyTransaction, slug: string, diff --git a/devTools/tsconfigs/tsconfig.base.json b/devTools/tsconfigs/tsconfig.base.json index 9b13020b1fa..99609ed3182 100644 --- a/devTools/tsconfigs/tsconfig.base.json +++ b/devTools/tsconfigs/tsconfig.base.json @@ -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": "ESNext", "alwaysStrict": true, "noImplicitReturns": true,