Skip to content

Commit

Permalink
enhance: ability to filter chartViewMetadata
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelgerber committed Dec 12, 2024
1 parent bc35b47 commit 6a376a8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
4 changes: 2 additions & 2 deletions baker/SiteBaker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ import { getTombstones } from "../db/model/GdocTombstone.js"
import { bakeAllMultiDimDataPages } from "./MultiDimBaker.js"
import { getAllLinkedPublishedMultiDimDataPages } from "../db/model/MultiDimDataPage.js"
import { getPublicDonorNames } from "../db/model/Donor.js"
import { getAllChartViewsMetadata } from "../db/model/ChartView.js"
import { getChartViewsMetadata } from "../db/model/ChartView.js"

type PrefetchedAttachments = {
donors: string[]
Expand Down Expand Up @@ -462,7 +462,7 @@ export class SiteBaker {
name: `✅ Prefetched ${publishedAuthors.length} authors`,
})

const chartViewMetadata = await getAllChartViewsMetadata(knex)
const chartViewMetadata = await getChartViewsMetadata(knex)
const chartViewMetadataByName = keyBy(chartViewMetadata, "name")
this.progressBar.tick({
name: `✅ Prefetched ${chartViewMetadata.length} chart views`,
Expand Down
17 changes: 11 additions & 6 deletions db/model/ChartView.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { ChartViewMetadata, JsonString } from "@ourworldindata/types"
import * as db from "../db.js"

export const getAllChartViewsMetadata = async (
knex: db.KnexReadonlyTransaction
export const getChartViewsMetadata = async (
knex: db.KnexReadonlyTransaction,
names?: string[]
): Promise<ChartViewMetadata[]> => {
type RawRow = Omit<ChartViewMetadata, "queryParamsForParentChart"> & {
queryParamsForParentChart: JsonString
}
const rows: RawRow[] = await db.knexRaw(
knex,
`-- sql
let rows: RawRow[]

const query = `-- sql
SELECT cv.name,
cc.full ->> "$.title" as title,
chartConfigId,
Expand All @@ -20,7 +21,11 @@ JOIN chart_configs cc on cc.id = cv.chartConfigId
JOIN charts pc on cv.parentChartId = pc.id
JOIN chart_configs pcc on pc.configId = pcc.id
`
)

if (names) {
if (names.length === 0) return []
rows = await db.knexRaw(knex, `${query} WHERE cv.name IN (?)`, [names])
} else rows = await db.knexRaw(knex, query)

return rows.map((row) => ({
...row,
Expand Down
4 changes: 2 additions & 2 deletions db/model/Gdoc/GdocBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ import {
OwidGdocLinkType,
OwidGdocType,
} from "@ourworldindata/types"
import { getAllChartViewsMetadata } from "../ChartView.js"
import { getChartViewsMetadata } from "../ChartView.js"

export class GdocBase implements OwidGdocBaseInterface {
id!: string
Expand Down Expand Up @@ -701,7 +701,7 @@ export class GdocBase implements OwidGdocBaseInterface {
knex: db.KnexReadonlyTransaction
): Promise<void> {
// TODO: Filter down to only those that are used in the Gdoc
const result = await getAllChartViewsMetadata(knex)
const result = await getChartViewsMetadata(knex)
this.chartViewMetadata = keyBy(result, "name")
}

Expand Down

0 comments on commit 6a376a8

Please sign in to comment.