Skip to content

Commit

Permalink
🔨 (db) migrate Pageview to knex
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiamersmann authored and danyx23 committed Feb 27, 2024
1 parent a2b1345 commit 55b756f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 18 deletions.
13 changes: 9 additions & 4 deletions adminSiteServer/apiRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import {
DbRawVariable,
DbRawOrigin,
parseOriginsRow,
AnalyticsPageviewsTableName,
} from "@ourworldindata/types"
import {
getVariableDataRoute,
Expand All @@ -67,7 +68,6 @@ import { getDatasetById, setTagsForDataset } from "../db/model/Dataset.js"
import { User } from "../db/model/User.js"
import { GdocPost } from "../db/model/Gdoc/GdocPost.js"
import { GdocBase, Tag as TagEntity } from "../db/model/Gdoc/GdocBase.js"
import { Pageview } from "../db/model/Pageview.js"
import {
syncDatasetToGitRepo,
removeDatasetFromGitRepo,
Expand Down Expand Up @@ -480,9 +480,14 @@ apiRouter.get(
).then((chart) => chart?.config?.slug)
if (!slug) return {}

const pageviewsByUrl = await Pageview.findOneBy({
url: `https://ourworldindata.org/grapher/${slug}`,
})
const pageviewsByUrl = await db.knexRawFirst(
"select * from ?? where url = ?",
db.knexInstance(),
[
AnalyticsPageviewsTableName,
`https://ourworldindata.org/grapher/${slug}`,
]
)

return {
pageviews: pageviewsByUrl ?? undefined,
Expand Down
4 changes: 2 additions & 2 deletions baker/algolia/indexChartsToAlgolia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { isPathRedirectedToExplorer } from "../../explorerAdminServer/ExplorerRe
import { ChartRecord, SearchIndexName } from "../../site/search/searchTypes.js"
import { KeyChartLevel, OwidGdocLinkType, isNil } from "@ourworldindata/utils"
import { MarkdownTextWrap } from "@ourworldindata/components"
import { Pageview } from "../../db/model/Pageview.js"
import { getAnalyticsPageviewsByUrlObj } from "../../db/model/Pageview.js"
import { Link } from "../../db/model/Link.js"
import { getRelatedArticles } from "../../db/model/Post.js"
import { Knex } from "knex"
Expand Down Expand Up @@ -59,7 +59,7 @@ const getChartsRecords = async (
)
}

const pageviews = await Pageview.getViewsByUrlObj()
const pageviews = await getAnalyticsPageviewsByUrlObj(knex)

const records: ChartRecord[] = []
for (const c of chartsToIndex) {
Expand Down
4 changes: 2 additions & 2 deletions baker/algolia/indexExplorersToAlgolia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { getAlgoliaClient } from "./configureAlgolia.js"
import * as db from "../../db/db.js"
import { ALGOLIA_INDEXING } from "../../settings/serverSettings.js"
import { Pageview } from "../../db/model/Pageview.js"
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"
Expand Down Expand Up @@ -112,7 +112,7 @@ function getNullishJSONValueAsPlaintext(value: string): string {
}

const getExplorerRecords = async (): Promise<ExplorerRecord[]> => {
const pageviews = await Pageview.getViewsByUrlObj()
const pageviews = await getAnalyticsPageviewsByUrlObj(db.knexInstance())

// Fetch info about all charts used in explorers, as linked by the explorer_charts table
const graphersUsedInExplorers = await db
Expand Down
4 changes: 2 additions & 2 deletions baker/algolia/indexToAlgolia.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
PageType,
SearchIndexName,
} from "../../site/search/searchTypes.js"
import { Pageview } from "../../db/model/Pageview.js"
import { getAnalyticsPageviewsByUrlObj } from "../../db/model/Pageview.js"
import { GdocPost } from "../../db/model/Gdoc/GdocPost.js"
import { ArticleBlocks } from "../../site/gdocs/components/ArticleBlocks.js"
import React from "react"
Expand Down Expand Up @@ -196,7 +196,7 @@ 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 Pageview.getViewsByUrlObj()
const pageviews = await getAnalyticsPageviewsByUrlObj(db.knexInstance())
const gdocs = await GdocPost.getPublishedGdocs()
const publishedGdocsBySlug = keyBy(gdocs, "slug")
// TODO: the knex instance should be handed down as a parameter
Expand Down
32 changes: 24 additions & 8 deletions db/model/Pageview.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { keyBy } from "lodash"
import { Entity, Column, BaseEntity } from "typeorm"
import { RawPageview } from "@ourworldindata/utils"
import * as db from "../db.js"
import { Knex } from "knex"
import {
DbPlainAnalyticsPageview,
AnalyticsPageviewsTableName,
} from "@ourworldindata/types"

@Entity("analytics_pageviews")
export class Pageview extends BaseEntity implements RawPageview {
Expand All @@ -18,13 +24,23 @@ export class Pageview extends BaseEntity implements RawPageview {
@Column() views_14d!: number
/** Sum of pageviews over the last 365 days. */
@Column() views_365d!: number
}

static async getViewsByUrlObj(): Promise<{ [url: string]: RawPageview }> {
const pageviews = await Pageview.find()
export async function getAnalyticsPageviewsByUrlObj(
knex: Knex<any, any[]>
): Promise<{
[url: string]: DbPlainAnalyticsPageview
}> {
const pageviews = await db.knexRaw<DbPlainAnalyticsPageview>(
"SELECT * FROM ??",
knex,
[AnalyticsPageviewsTableName]
)

// Normalize URLs to be relative to the root of the site.
// This also filters out any URLs that don't start with ourworldindata.org.
const pageviewsNormalized: RawPageview[] = pageviews.flatMap((p) => {
// Normalize URLs to be relative to the root of the site.
// This also filters out any URLs that don't start with ourworldindata.org.
const pageviewsNormalized = pageviews.flatMap(
(p: DbPlainAnalyticsPageview) => {
if (p.url.startsWith("https://ourworldindata.org/"))
return [
{
Expand All @@ -36,8 +52,8 @@ export class Pageview extends BaseEntity implements RawPageview {
},
]
else return []
})
}
)

return keyBy(pageviewsNormalized, (p) => p.url)
}
return keyBy(pageviewsNormalized, (p) => p.url)
}

0 comments on commit 55b756f

Please sign in to comment.