-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathExplorerBaker.tsx
190 lines (177 loc) · 6.6 KB
/
ExplorerBaker.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import fs from "fs-extra"
import path from "path"
import {
ExplorerProgram,
ExplorerGrammar,
ColumnGrammar,
explorerUrlMigrationsById,
} from "@ourworldindata/explorer"
import { ExplorerAdminServer } from "../explorerAdminServer/ExplorerAdminServer.js"
import { explorerRedirectTable } from "../explorerAdminServer/ExplorerRedirects.js"
import { renderExplorerPage } from "./siteRenderers.js"
import * as db from "../db/db.js"
import { getVariableIdsByCatalogPath } from "../db/model/Variable.js"
import {
CoreTable,
ErrorValueTypes,
isNotErrorValueOrEmptyCell,
} from "@ourworldindata/core-table"
import { ColumnTypeNames } from "@ourworldindata/types"
export const transformExplorerProgramToResolveCatalogPaths = async (
program: ExplorerProgram,
knex: db.KnexReadonlyTransaction
): Promise<{
program: ExplorerProgram
unresolvedCatalogPaths?: Set<string>
}> => {
const { decisionMatrix } = program
const { requiredCatalogPaths } = decisionMatrix
if (requiredCatalogPaths.size === 0) return { program }
const catalogPathToIndicatorIdMap = await getVariableIdsByCatalogPath(
[...requiredCatalogPaths],
knex
)
const unresolvedCatalogPaths = new Set(
[...requiredCatalogPaths].filter(
(path) => !catalogPathToIndicatorIdMap.get(path)
)
)
const colSlugsToUpdate =
decisionMatrix.allColumnsWithIndicatorIdsOrCatalogPaths.map(
(col) => col.slug
)
// In the decision matrix table, replace any catalog paths with their corresponding indicator ids
// If a catalog path is not found, it will be left as is
const newDecisionMatrixTable =
decisionMatrix.tableWithOriginalColumnNames.replaceCells(
colSlugsToUpdate,
(val) => {
if (typeof val === "string") {
const vals = val.split(" ")
const updatedVals = vals.map(
(val) =>
catalogPathToIndicatorIdMap.get(val)?.toString() ??
val
)
return updatedVals.join(" ")
}
return val
}
)
// Write the result to the "graphers" block
const grapherBlockLine = program.getRowMatchingWords(
ExplorerGrammar.graphers.keyword
)
if (grapherBlockLine === -1)
throw new Error(
`"graphers" block not found in explorer ${program.slug}`
)
const newProgram = program.updateBlock(
grapherBlockLine,
newDecisionMatrixTable.toMatrix()
)
// Next, we also need to update the "columns" block of the explorer
program.columnDefsByTableSlug.forEach((_columnDefs, tableSlug) => {
const lineNoInProgram = newProgram.getRowMatchingWords(
ExplorerGrammar.columns.keyword,
tableSlug
)
// This should, in theory, never happen because columnDefsByTableSlug gets generated from such a block
if (lineNoInProgram === -1)
throw new Error(
`Column defs not found for explorer ${program.slug} and table ${tableSlug}`
)
const columnDefTable = new CoreTable(
newProgram.getBlock(lineNoInProgram)
)
const newColumnDefsTable = columnDefTable.combineColumns(
[
ColumnGrammar.variableId.keyword,
ColumnGrammar.catalogPath.keyword,
],
{
slug: ColumnGrammar.variableId.keyword,
type: ColumnTypeNames.Integer,
},
(row) => {
const variableId = row[ColumnGrammar.variableId.keyword]
if (isNotErrorValueOrEmptyCell(variableId)) return variableId
const catalogPath = row[ColumnGrammar.catalogPath.keyword]
if (
isNotErrorValueOrEmptyCell(catalogPath) &&
typeof catalogPath === "string"
) {
return (
catalogPathToIndicatorIdMap.get(catalogPath) ??
ErrorValueTypes.NoMatchingVariableId
)
}
return ErrorValueTypes.NoMatchingVariableId
}
)
newProgram.updateBlock(lineNoInProgram, newColumnDefsTable.toMatrix())
})
return { program: newProgram.clone, unresolvedCatalogPaths }
}
export const bakeAllPublishedExplorers = async (
outputFolder: string,
explorerAdminServer: ExplorerAdminServer,
knex: db.KnexReadonlyTransaction
) => {
// remove all existing explorers, since we're re-baking every single one anyway
await fs.remove(outputFolder)
await fs.mkdirp(outputFolder)
const published = await explorerAdminServer.getAllPublishedExplorers()
await bakeExplorersToDir(outputFolder, published, knex)
}
const bakeExplorersToDir = async (
directory: string,
explorers: ExplorerProgram[] = [],
knex: db.KnexReadonlyTransaction
) => {
for (const explorer of explorers) {
await write(
`${directory}/${explorer.slug}.html`,
await renderExplorerPage(explorer, knex)
)
}
}
export const bakeAllExplorerRedirects = async (
outputFolder: string,
explorerAdminServer: ExplorerAdminServer,
knex: db.KnexReadonlyTransaction
) => {
const explorers = await explorerAdminServer.getAllExplorers()
const redirects = explorerRedirectTable.rows
for (const redirect of redirects) {
const { migrationId, path: redirectPath, baseQueryStr } = redirect
const transform = explorerUrlMigrationsById[migrationId]
if (!transform) {
throw new Error(
`No explorer URL migration with id '${migrationId}'. Fix the list of explorer redirects and retry.`
)
}
const { explorerSlug } = transform
const program = explorers.find(
(program) => program.slug === explorerSlug
)
if (!program) {
throw new Error(
`No explorer with slug '${explorerSlug}'. Fix the list of explorer redirects and retry.`
)
}
const html = await renderExplorerPage(program, knex, {
urlMigrationSpec: {
explorerUrlMigrationId: migrationId,
baseQueryStr,
},
})
await write(path.join(outputFolder, `${redirectPath}.html`), html)
}
}
// todo: merge with SiteBaker's?
const write = async (outPath: string, content: string) => {
await fs.mkdirp(path.dirname(outPath))
await fs.writeFile(outPath, content)
console.log(outPath)
}