Skip to content

Commit

Permalink
enhance: explorer views based on grapherIds are handled
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelgerber committed Mar 18, 2024
1 parent 614030a commit 19cbb86
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions baker/algolia/indexExplorerViewsToAlgolia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { GridBoolean } from "../../gridLang/GridLangConstants.js"
import { getAnalyticsPageviewsByUrlObj } from "../../db/model/Pageview.js"
import fs from "fs-extra"
import { keyBy } from "lodash"

interface ExplorerViewEntry {
viewTitle: string
Expand Down Expand Up @@ -37,6 +38,9 @@ interface ExplorerViewEntryWithExplorerInfo extends ExplorerViewEntry {
objectID?: string
}

// Creates a search-ready string from a choice.
// Special handling is pretty much only necessary for checkboxes: If they are not ticked, then their name is not included.
// Imagine a "Per capita" checkbox, for example. If it's not ticked, then we don't want searches for "per capita" to wrongfully match it.
const explorerChoiceToViewSettings = (
choices: ExplorerChoiceParams,
decisionMatrix: DecisionMatrix
Expand Down Expand Up @@ -99,9 +103,6 @@ const getExplorerViewRecordsForExplorerSlug = async (
)
})

// TODO: Handle grapherId and fetch title/subtitle
// TODO: Handle indicator-based explorers

const record: ExplorerViewEntry = {
viewTitle: explorerDecisionMatrix.selectedRow.title,
viewSubtitle: explorerDecisionMatrix.selectedRow.subtitle,
Expand All @@ -119,6 +120,44 @@ const getExplorerViewRecordsForExplorerSlug = async (
return record
})

// Enrich `grapherId`-powered views with title/subtitle
const grapherIds = records
.filter((record) => record.viewGrapherId !== undefined)
.map((record) => record.viewGrapherId as number)

if (grapherIds.length) {
console.log(
`Fetching grapher info from ${grapherIds.length} graphers for explorer ${slug}`
)
const grapherIdToTitle = await knex
.table("charts")
.select(
"id",
knex.raw("config->>'$.title' as title"),
knex.raw("config->>'$.subtitle' as subtitle")
)
.whereIn("id", grapherIds)
.andWhereRaw("config->>'$.isPublished' = 'true'")
.then((rows) => keyBy(rows, "id"))

for (const record of records) {
if (record.viewGrapherId !== undefined) {
const grapherInfo = grapherIdToTitle[record.viewGrapherId]
if (grapherInfo === undefined) {
console.warn(
`Grapher id ${record.viewGrapherId} not found for explorer ${slug}`
)
continue
}
record.viewTitle = grapherInfo.title
record.viewSubtitle = grapherInfo.subtitle
record.titleLength = grapherInfo.title?.length
}
}
}

// TODO: Handle indicator-based explorers

return records
}

Expand Down

0 comments on commit 19cbb86

Please sign in to comment.