Skip to content

Commit

Permalink
🔨 incorporate PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
danyx23 committed Feb 27, 2024
1 parent 55b756f commit afa89e1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
23 changes: 14 additions & 9 deletions baker/algolia/indexExplorersToAlgolia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { getAnalyticsPageviewsByUrlObj } from "../../db/model/Pageview.js"
import { chunkParagraphs } from "../chunk.js"
import { SearchIndexName } from "../../site/search/searchTypes.js"
import { Chart } from "../../db/model/Chart.js"
import { Knex } from "knex"

type ExplorerBlockColumns = {
type: "columns"
Expand Down Expand Up @@ -111,16 +112,19 @@ function getNullishJSONValueAsPlaintext(value: string): string {
return value !== "null" ? cheerio.load(value)("body").text() : ""
}

const getExplorerRecords = async (): Promise<ExplorerRecord[]> => {
const pageviews = await getAnalyticsPageviewsByUrlObj(db.knexInstance())
const getExplorerRecords = async (
knex: Knex<any, any[]>
): Promise<ExplorerRecord[]> => {
const pageviews = await getAnalyticsPageviewsByUrlObj(knex)

// Fetch info about all charts used in explorers, as linked by the explorer_charts table
const graphersUsedInExplorers = await db
.queryMysql(
.knexRaw<{ chartId: number }>(
`
SELECT DISTINCT chartId
FROM explorer_charts
`
`,
knex
)
.then((results: { chartId: number }[]) =>
results.map(({ chartId }) => chartId)
Expand All @@ -129,17 +133,18 @@ const getExplorerRecords = async (): Promise<ExplorerRecord[]> => {
.then((charts) => keyBy(charts, "id"))

const explorerRecords = await db
.queryMysql(
.knexRaw<Omit<ExplorerEntry, "views_7d">>(
`
SELECT slug,
COALESCE(config->>"$.explorerSubtitle", "null") AS subtitle,
COALESCE(config->>"$.explorerTitle", "null") AS title,
COALESCE(config->>"$.blocks", "null") AS blocks
FROM explorers
WHERE isPublished = true
`
`,
knex
)
.then((results: ExplorerEntry[]) =>
.then((results) =>
results.flatMap(({ slug, title, subtitle, blocks }) => {
const textFromExplorer = extractTextFromExplorer(
blocks,
Expand Down Expand Up @@ -190,8 +195,8 @@ const indexExplorersToAlgolia = async () => {
try {
const index = client.initIndex(SearchIndexName.Explorers)

await db.getConnection()
const records = await getExplorerRecords()
const knex = db.knexInstance()
const records = await getExplorerRecords(knex)
await index.replaceAllObjects(records)

await db.closeTypeOrmAndKnexConnections()
Expand Down
4 changes: 2 additions & 2 deletions baker/algolia/indexToAlgolia.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,12 @@ function generateGdocRecords(

// Generate records for countries, WP posts (not including posts that have been succeeded by Gdocs equivalents), and Gdocs
const getPagesRecords = async (knex: Knex<any, any[]>) => {
const pageviews = await getAnalyticsPageviewsByUrlObj(db.knexInstance())
const pageviews = await getAnalyticsPageviewsByUrlObj(knex)
const gdocs = await GdocPost.getPublishedGdocs()
const publishedGdocsBySlug = keyBy(gdocs, "slug")
// TODO: the knex instance should be handed down as a parameter
const slugsWithPublishedGdocsSuccessors =
await db.getSlugsWithPublishedGdocsSuccessors(db.knexInstance())
await db.getSlugsWithPublishedGdocsSuccessors(knex)
const postsApi = await getPostsFromSnapshots(knex, undefined, (post) => {
// Two things can happen here:
// 1. There's a published Gdoc with the same slug
Expand Down

0 comments on commit afa89e1

Please sign in to comment.