Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add table related_charts #4453

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions baker/GrapherBaker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ import { logErrorAndMaybeCaptureInSentry } from "../serverUtils/errorLog.js"

import { getTagToSlugMap } from "./GrapherBakingUtils.js"
import { knexRaw } from "../db/db.js"
import { getRelatedChartsForVariable } from "../db/model/Chart.js"
import {
getRelatedChartsForVariable,
getRelatedChartsForChart,
} from "../db/model/Chart.js"
import pMap from "p-map"

const renderDatapageIfApplicable = async (
Expand Down Expand Up @@ -193,11 +196,18 @@ export async function renderDataPageV2(

// Get the charts this variable is being used in (aka "related charts")
// and exclude the current chart to avoid duplicates
datapageData.allCharts = await getRelatedChartsForVariable(
knex,
variableId,
grapher && "id" in grapher ? [grapher.id as number] : []
)
if (grapher && "id" in grapher) {
datapageData.allCharts = await getRelatedChartsForChart(
knex,
grapher.id as number
)
} else {
datapageData.allCharts = await getRelatedChartsForVariable(
knex,
variableId,
[]
)
}

datapageData.relatedResearch =
await getRelatedResearchAndWritingForVariable(knex, variableId)
Expand Down
32 changes: 32 additions & 0 deletions db/migration/1736933781669-AddRelatedChartsTable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { MigrationInterface, QueryRunner } from "typeorm"

export class AddRelatedChartsTable1736933781669 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`-- sql
CREATE TABLE related_charts (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
chartId INT NOT NULL,
relatedChartId INT NOT NULL,
label VARCHAR(255) NOT NULL,
reviewer VARCHAR(255) DEFAULT NULL,
reason TEXT DEFAULT NULL,
score DOUBLE DEFAULT NULL,
updatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT related_charts_ibfk_1
FOREIGN KEY (chartId) REFERENCES charts (id)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT related_charts_ibfk_2
FOREIGN KEY (relatedChartId) REFERENCES charts (id)
ON DELETE CASCADE
ON UPDATE CASCADE,
KEY idx_related_charts_chartId (chartId),
UNIQUE KEY uq_chartId_relatedChartId_reviewer (chartId, relatedChartId, reviewer)
)
`)
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP TABLE related_charts`)
}
}
27 changes: 27 additions & 0 deletions db/model/Chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,33 @@ export const getRelatedChartsForVariable = async (
)
}

export const getRelatedChartsForChart = async (
knex: db.KnexReadonlyTransaction,
chartId: number
): Promise<RelatedChart[]> => {
return db.knexRaw<RelatedChart>(
knex,
`-- sql
SELECT
chart_configs.slug,
chart_configs.full->>"$.title" AS title,
chart_configs.full->>"$.variantName" AS variantName,
MAX(chart_tags.keyChartLevel) as keyChartLevel
FROM charts
JOIN chart_configs ON charts.configId=chart_configs.id
INNER JOIN chart_tags ON charts.id=chart_tags.chartId
INNER JOIN related_charts ON charts.id=related_charts.relatedChartId
WHERE related_charts.chartId = ${chartId}
AND related_charts.reviewer = 'production'
AND related_charts.label = 'good'
AND chart_configs.full->>"$.isPublished" = "true"
GROUP BY charts.id
ORDER BY related_charts.score DESC
LIMIT 6
`
)
}

export const getChartEmbedUrlsInPublishedWordpressPosts = async (
knex: db.KnexReadonlyTransaction
): Promise<string[]> => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"startLocalCloudflareFunctions": "wrangler pages dev",
"startDeployQueueServer": "node --enable-source-maps ./itsJustJavascript/baker/startDeployQueueServer.js",
"startLernaWatcher": "lerna watch --scope '@ourworldindata/*' -- lerna run build --scope=\\$LERNA_PACKAGE_NAME --include-dependents",
"startTmuxServer": "node_modules/tmex/tmex dev \"yarn startLernaWatcher\" \"yarn startAdminDevServer\" \"yarn startViteServer\"",
"startTmuxServer": "node_modules/tmex/tmex dev \"yarn startLernaWatcher\" \"yarn startAdminDevServer\" \"yarn startSiteFront\"",
"startViteServer": "vite dev",
"startSiteFront": "./devTools/vite/startVite.sh",
"startLocalBakeServer": "http-server ./localBake -p 3000",
Expand Down
Loading