forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExplorerAdminServer.tsx
277 lines (250 loc) · 9.77 KB
/
ExplorerAdminServer.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import React from "react"
import { existsSync, readdir, writeFile, mkdirp, readFile } from "fs-extra"
import path from "path"
import { queryMysql } from "../db/db"
import { getBlockContent } from "../db/wpdb"
import {
EXPLORER_FILE_SUFFIX,
ExplorerProgram,
} from "../explorer/ExplorerProgram"
import { Router } from "express"
import { ExplorerPage } from "../site/ExplorerPage"
import {
EXPLORERS_GIT_CMS_FOLDER,
EXPLORERS_PREVIEW_ROUTE,
GetAllExplorersRoute,
ExplorersRouteResponse,
DefaultNewExplorerSlug,
EXPLORERS_ROUTE_FOLDER,
} from "../explorer/ExplorerConstants"
import simpleGit, { SimpleGit } from "simple-git"
import { slugify } from "../clientUtils/Util"
import { GrapherInterface } from "../grapher/core/GrapherInterface"
import { Grapher, GrapherProgrammaticInterface } from "../grapher/core/Grapher"
import { GitCommit, JsonError } from "../clientUtils/owidTypes"
import ReactDOMServer from "react-dom/server"
import {
explorerRedirectTable,
getExplorerRedirectForPath,
} from "./ExplorerRedirects"
import { explorerUrlMigrationsById } from "../explorer/urlMigrations/ExplorerUrlMigrations"
import { ExplorerPageUrlMigrationSpec } from "../explorer/urlMigrations/ExplorerPageUrlMigrationSpec"
export class ExplorerAdminServer {
constructor(gitDir: string, baseUrl: string) {
this.gitDir = gitDir
this.baseUrl = baseUrl
}
private baseUrl: string
private gitDir: string
// we store explorers in a subdir of the gitcms for now. idea is we may store other things in there later.
private get absoluteFolderPath() {
return this.gitDir + "/" + EXPLORERS_GIT_CMS_FOLDER + "/"
}
private _simpleGit?: SimpleGit
private get simpleGit() {
if (!this._simpleGit)
this._simpleGit = simpleGit({
baseDir: this.gitDir,
binary: "git",
maxConcurrentProcesses: 1,
})
return this._simpleGit
}
async getAllExplorersCommand() {
// Download all explorers for the admin index page
try {
const explorers = await this.getAllExplorers()
const branches = await this.simpleGit.branchLocal()
const gitCmsBranchName = await branches.current
const needsPull = false // todo: add
return {
success: true,
gitCmsBranchName,
needsPull,
explorers: explorers.map((explorer) => explorer.toJson()),
} as ExplorersRouteResponse
} catch (err) {
console.log(err)
return {
success: false,
errorMessage: err,
} as ExplorersRouteResponse
}
}
addMockBakedSiteRoutes(app: Router) {
app.get(`/${EXPLORERS_ROUTE_FOLDER}/:slug`, async (req, res) => {
res.set("Access-Control-Allow-Origin", "*")
const explorers = await this.getAllPublishedExplorers()
const explorerProgram = explorers.find(
(program) => program.slug === req.params.slug
)
if (explorerProgram)
res.send(await this.renderExplorerPage(explorerProgram))
else
throw new JsonError(
"A published explorer with that slug was not found",
404
)
})
app.get("/*", async (req, res, next) => {
const explorerRedirect = getExplorerRedirectForPath(req.path)
// If no explorer redirect exists, continue to next express handler
if (!explorerRedirect) return next()
const { migrationId, baseQueryStr } = explorerRedirect
const { explorerSlug } = explorerUrlMigrationsById[migrationId]
const program = await this.getExplorerFromSlug(explorerSlug)
res.send(
await this.renderExplorerPage(program, {
explorerUrlMigrationId: migrationId,
baseQueryStr,
})
)
})
}
addAdminRoutes(app: Router) {
app.get("/errorTest.csv", async (req, res) => {
// Add `table /admin/errorTest.csv?code=404` to test fetch download failures
const code =
req.query.code && !isNaN(parseInt(req.query.code))
? req.query.code
: 400
res.status(code)
return `Simulating code ${code}`
})
app.get(`/${GetAllExplorersRoute}`, async (req, res) => {
res.send(await this.getAllExplorersCommand())
})
app.get(`/${EXPLORERS_PREVIEW_ROUTE}/:slug`, async (req, res) => {
const slug = slugify(req.params.slug)
const filename = slug + EXPLORER_FILE_SUFFIX
if (slug === DefaultNewExplorerSlug)
return res.send(
await this.renderExplorerPage(
new ExplorerProgram(DefaultNewExplorerSlug, "")
)
)
if (!slug || !existsSync(this.absoluteFolderPath + filename))
return res.send(`File not found`)
const explorer = await this.getExplorerFromFile(filename)
return res.send(await this.renderExplorerPage(explorer))
})
}
// todo: make private? once we remove covid legacy stuff?
async getExplorerFromFile(filename: string) {
const fullPath = this.absoluteFolderPath + filename
const content = await readFile(fullPath, "utf8")
const commits = await this.simpleGit.log({ file: fullPath, n: 1 })
return new ExplorerProgram(
filename.replace(EXPLORER_FILE_SUFFIX, ""),
content,
commits.latest as GitCommit
)
}
async getExplorerFromSlug(slug: string) {
return this.getExplorerFromFile(`${slug}${EXPLORER_FILE_SUFFIX}`)
}
async renderExplorerPage(
program: ExplorerProgram,
urlMigrationSpec?: ExplorerPageUrlMigrationSpec
) {
const { requiredGrapherIds } = program.decisionMatrix
let grapherConfigRows: any[] = []
if (requiredGrapherIds.length)
grapherConfigRows = await queryMysql(
`SELECT id, config FROM charts WHERE id IN (?)`,
[requiredGrapherIds]
)
const wpContent = program.wpBlockId
? await getBlockContent(program.wpBlockId)
: undefined
const grapherConfigs: GrapherInterface[] = grapherConfigRows.map(
(row) => {
const config: GrapherProgrammaticInterface = JSON.parse(
row.config
)
config.id = row.id // Ensure each grapher has an id
config.manuallyProvideData = true
return new Grapher(config).toObject()
}
)
return (
`<!doctype html>` +
ReactDOMServer.renderToStaticMarkup(
<ExplorerPage
grapherConfigs={grapherConfigs}
program={program}
wpContent={wpContent}
baseUrl={this.baseUrl}
urlMigrationSpec={urlMigrationSpec}
/>
)
)
}
async bakeAllPublishedExplorers(outputFolder: string) {
const published = await this.getAllPublishedExplorers()
await this.bakeExplorersToDir(outputFolder, published)
}
private async getAllPublishedExplorers() {
const explorers = await this.getAllExplorers()
return explorers.filter((exp) => exp.isPublished)
}
private async getAllExplorers() {
if (!existsSync(this.absoluteFolderPath)) return []
const files = await readdir(this.absoluteFolderPath)
const explorerFiles = files.filter((filename) =>
filename.endsWith(EXPLORER_FILE_SUFFIX)
)
const explorers: ExplorerProgram[] = []
for (const filename of explorerFiles) {
const explorer = await this.getExplorerFromFile(filename)
explorers.push(explorer)
}
return explorers
}
private async write(outPath: string, content: string) {
await mkdirp(path.dirname(outPath))
await writeFile(outPath, content)
console.log(outPath)
}
private async bakeExplorersToDir(
directory: string,
explorers: ExplorerProgram[] = []
) {
for (const explorer of explorers) {
await this.write(
`${directory}/${explorer.slug}.html`,
await this.renderExplorerPage(explorer)
)
}
}
async bakeAllExplorerRedirects(outputFolder: string) {
const explorers = await this.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 this.renderExplorerPage(program, {
explorerUrlMigrationId: migrationId,
baseQueryStr,
})
await this.write(
path.join(outputFolder, `${redirectPath}.html`),
html
)
}
}
}