Skip to content

Commit

Permalink
WIP of fetching explorers from the DB instead of git
Browse files Browse the repository at this point in the history
  • Loading branch information
rakyi committed Apr 25, 2024
1 parent d65c4ca commit 06adc8c
Showing 1 changed file with 41 additions and 15 deletions.
56 changes: 41 additions & 15 deletions explorerAdminServer/ExplorerAdminServer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ import {
EXPLORERS_GIT_CMS_FOLDER,
ExplorersRouteResponse,
} from "../explorer/ExplorerConstants.js"
import * as db from "../db/db.js"
import { simpleGit, SimpleGit } from "simple-git"
import { GitCommit, keyBy, sortBy } from "@ourworldindata/utils"
import {
DbPlainExplorer,
GitCommit,

Check warning on line 14 in explorerAdminServer/ExplorerAdminServer.tsx

View workflow job for this annotation

GitHub Actions / eslint

'GitCommit' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 14 in explorerAdminServer/ExplorerAdminServer.tsx

View workflow job for this annotation

GitHub Actions / eslint

'GitCommit' is defined but never used. Allowed unused vars must match /^_/u
keyBy,
sortBy,
} from "@ourworldindata/utils"
import { uniq } from "lodash"

export class ExplorerAdminServer {
constructor(gitDir: string) {
Expand Down Expand Up @@ -60,20 +67,39 @@ export class ExplorerAdminServer {
}
}

// todo: make private? once we remove covid legacy stuff?
async getExplorerFromFile(filename: string) {
const fullPath = this.absoluteFolderPath + filename
const content = await fs.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 getExplorerFromSlug(knex: db.KnexReadonlyTransaction, slug: string) {
const explorerConfig = await db.knexRawFirst<
Pick<DbPlainExplorer, "config">
>(knex, `SELECT config FROM explorers WHERE slug = ?`, [slug])
if (!explorerConfig) {
throw new Error(`Explorer not found: ${slug}`)
}
const config = JSON.parse(explorerConfig.config)
const entries = Object.entries(config)
const out: string[][] = []
for (const [key, value] of entries) {
if (key === "blocks" || key === "_version") continue
if (typeof value === "string") {
out.push([key, value])
} else if (Array.isArray(value)) {
out.push([key, ...value])
}
}
for (const block of config.blocks) {
out.push([block.type, ...block.args])
if (block.block) {
const columns = uniq(
block.block.flatMap((row: Record<string, string>) =>
Object.keys(row)
)
) as string[]
out.push(["", ...columns])
for (const row of block.block) {
out.push(["", ...columns.map((col) => row[col])])
}
}
}
return new ExplorerProgram(slug, config)
}

async getAllPublishedExplorers() {
Expand Down

0 comments on commit 06adc8c

Please sign in to comment.