Skip to content

Commit

Permalink
Update grapher configs in R2 when variable config changes
Browse files Browse the repository at this point in the history
* Fix missing updates of configs in R2 after we update the configs in
  the DB on variable grapher config upsert/deletion in the API
* Update mdim view grapher configs on variable grapher config
  upsert/deletion
* Trigger baking if a published mdim config is created/updated
  • Loading branch information
rakyi committed Oct 31, 2024
1 parent 67c71eb commit c98ee7e
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 39 deletions.
84 changes: 64 additions & 20 deletions adminSiteServer/apiRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
updateGrapherConfigAdminOfVariable,
updateGrapherConfigETLOfVariable,
updateAllChartsThatInheritFromIndicator,
updateAllMultiDimViewsThatInheritFromIndicator,
getAllChartsForIndicator,
} from "../db/model/Variable.js"
import { updateExistingFullConfig } from "../db/model/ChartConfigs.js"
Expand Down Expand Up @@ -102,6 +103,7 @@ import {
parseChartConfig,
MultiDimDataPageConfigRaw,
R2GrapherConfigDirectory,
ChartConfigsTableName,
} from "@ourworldindata/types"
import { uuidv7 } from "uuidv7"
import {
Expand Down Expand Up @@ -182,6 +184,7 @@ import {
} from "./chartConfigR2Helpers.js"
import { fetchImagesFromDriveAndSyncToS3 } from "../db/model/Image.js"
import { createMultiDimConfig } from "./multiDim.js"
import { isMultiDimDataPagePublished } from "../db/model/MultiDimDataPage.js"

const apiRouter = new FunctionalRouter()

Expand Down Expand Up @@ -722,6 +725,23 @@ const saveGrapher = async (
}
}

async function updateGrapherConfigsInR2(
knex: db.KnexReadonlyTransaction,
updatedCharts: { chartConfigId: string; isPublished: boolean }[],
updatedMultiDimViews: { chartConfigId: string; isPublished: boolean }[]
) {
const idsToUpdate = [
...updatedCharts.filter(({ isPublished }) => isPublished),
...updatedMultiDimViews,
].map(({ chartConfigId }) => chartConfigId)
const builder = knex<DbRawChartConfig>(ChartConfigsTableName)
.select("id", "full", "fullMd5")
.whereIn("id", idsToUpdate)
for await (const { id, full, fullMd5 } of builder.stream()) {
await saveGrapherConfigToR2ByUUID(id, full, fullMd5)
}
}

getRouteWithROTransaction(apiRouter, "/charts.json", async (req, res, trx) => {
const limit = parseIntOrUndefined(req.query.limit as string) ?? 10000
const charts = await db.knexRaw<OldChartFieldList>(
Expand Down Expand Up @@ -1151,6 +1171,12 @@ postRouteWithRWTransaction(apiRouter, "/multi-dim", async (req, res, trx) => {
config: MultiDimDataPageConfigRaw
}
const id = await createMultiDimConfig(trx, slug, rawConfig)
if (await isMultiDimDataPagePublished(trx, id)) {
await triggerStaticBuild(
res.locals.user,
`Publishing multidimensional chart ${slug}`
)
}
return { success: true, id }
})

Expand Down Expand Up @@ -1642,11 +1668,13 @@ putRouteWithRWTransaction(
throw new JsonError(`Variable with id ${variableId} not found`, 500)
}

const { savedPatch, updatedCharts } =
const { savedPatch, updatedCharts, updatedMultiDimViews } =
await updateGrapherConfigETLOfVariable(trx, variable, validConfig)

// trigger build if any published chart has been updated
if (updatedCharts.some((chart) => chart.isPublished)) {
await updateGrapherConfigsInR2(trx, updatedCharts, updatedMultiDimViews)
const allUpdatedConfigs = [...updatedCharts, ...updatedMultiDimViews]

if (allUpdatedConfigs.some(({ isPublished }) => isPublished)) {
await triggerStaticBuild(
res.locals.user,
`Updating ETL config for variable ${variableId}`
Expand Down Expand Up @@ -1703,18 +1731,25 @@ deleteRouteWithRWTransaction(
})
}

// update all charts that inherit from the indicator
const updates = {
patchConfigAdmin: variable.admin?.patchConfig,
updatedAt: now,
}
const updatedCharts = await updateAllChartsThatInheritFromIndicator(
trx,
variableId,
{
patchConfigAdmin: variable.admin?.patchConfig,
updatedAt: now,
}
updates
)
const updatedMultiDimViews =
await updateAllMultiDimViewsThatInheritFromIndicator(
trx,
variableId,
updates
)
await updateGrapherConfigsInR2(trx, updatedCharts, updatedMultiDimViews)
const allUpdatedConfigs = [...updatedCharts, ...updatedMultiDimViews]

// trigger build if any published chart has been updated
if (updatedCharts.some((chart) => chart.isPublished)) {
if (allUpdatedConfigs.some(({ isPublished }) => isPublished)) {
await triggerStaticBuild(
res.locals.user,
`Updating ETL config for variable ${variableId}`
Expand Down Expand Up @@ -1747,11 +1782,13 @@ putRouteWithRWTransaction(
throw new JsonError(`Variable with id ${variableId} not found`, 500)
}

const { savedPatch, updatedCharts } =
const { savedPatch, updatedCharts, updatedMultiDimViews } =
await updateGrapherConfigAdminOfVariable(trx, variable, validConfig)

// trigger build if any published chart has been updated
if (updatedCharts.some((chart) => chart.isPublished)) {
await updateGrapherConfigsInR2(trx, updatedCharts, updatedMultiDimViews)
const allUpdatedConfigs = [...updatedCharts, ...updatedMultiDimViews]

if (allUpdatedConfigs.some(({ isPublished }) => isPublished)) {
await triggerStaticBuild(
res.locals.user,
`Updating admin-authored config for variable ${variableId}`
Expand Down Expand Up @@ -1799,18 +1836,25 @@ deleteRouteWithRWTransaction(
[variable.admin.configId]
)

// update all charts that inherit from the indicator
const updates = {
patchConfigETL: variable.etl?.patchConfig,
updatedAt: now,
}
const updatedCharts = await updateAllChartsThatInheritFromIndicator(
trx,
variableId,
{
patchConfigETL: variable.etl?.patchConfig,
updatedAt: now,
}
updates
)
const updatedMultiDimViews =
await updateAllMultiDimViewsThatInheritFromIndicator(
trx,
variableId,
updates
)
await updateGrapherConfigsInR2(trx, updatedCharts, updatedMultiDimViews)
const allUpdatedConfigs = [...updatedCharts, ...updatedMultiDimViews]

// trigger build if any published chart has been updated
if (updatedCharts.some((chart) => chart.isPublished)) {
if (allUpdatedConfigs.some(({ isPublished }) => isPublished)) {
await triggerStaticBuild(
res.locals.user,
`Updating admin-authored config for variable ${variableId}`
Expand Down
11 changes: 11 additions & 0 deletions db/model/MultiDimDataPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ export async function upsertMultiDimDataPage(
return result[0]
}

export async function isMultiDimDataPagePublished(
knex: KnexReadonlyTransaction,
id: number
): Promise<boolean> {
const result = await knex(MultiDimDataPagesTableName)
.select(knex.raw("1"))
.where({ id, published: true })
.first()
return Boolean(result)
}

const createOnlyPublishedFilter = (
onlyPublished: boolean
): Record<string, any> => (onlyPublished ? { published: true } : {})
Expand Down
Loading

0 comments on commit c98ee7e

Please sign in to comment.