Skip to content

Commit

Permalink
feat: add cron workflow to update external politician ranking
Browse files Browse the repository at this point in the history
  • Loading branch information
Th1nkK1D committed Jan 24, 2024
1 parent be62b35 commit e0b3af6
Show file tree
Hide file tree
Showing 9 changed files with 251 additions and 24 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/update-ranking.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Update politician ranking
on:
workflow_dispatch:
schedule:
- cron: '0 0 1 * *'
jobs:
update-ranking:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: 20
cache: 'yarn'
- name: Install dependencies
run: yarn install
- name: Test
run: yarn test
- name: Fetch politician ranking data
run: yarn update:politician-ranking
- name: Deploy to Github Page
uses: JamesIves/github-pages-deploy-action@v4
with:
folder: out
commit-message: 'feat: update external politican ranking data'
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"story:build": "HISTOIRE_BASE=/stories/ histoire build",
"story:preview": "histoire preview",
"gen:component": "hygen component new",
"test": "vitest"
"test": "vitest",
"update:politician-ranking": "tsx scripts/politician-ranking/run-update-job.ts"
},
"lint-staged": {
"*.md": "doctoc --github --update-only",
Expand All @@ -29,8 +30,7 @@
"d3": "^7.8.5",
"dayjs": "^1.11.10",
"keen-slider": "^6.8.6",
"scrollama": "^3.2.0",
"tailwind-merge": "^2.1.0"
"scrollama": "^3.2.0"
},
"devDependencies": {
"@commitlint/cli": "^17.7.0",
Expand Down Expand Up @@ -61,8 +61,10 @@
"sass": "^1.67.0",
"svelte": "^4.0.5",
"svelte-check": "^3.4.3",
"tailwind-merge": "^2.1.0",
"tailwindcss": "^3.3.3",
"tslib": "^2.4.1",
"tsx": "^4.7.0",
"typescript": "^5.0.0",
"vite": "^4.4.2",
"vitest": "^0.34.6",
Expand Down
7 changes: 2 additions & 5 deletions src/lib/ranking/gun.ts → scripts/politician-ranking/gun.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { fetchPoliticians } from '$lib/datasheets';
import type { Politician } from '$models/politician';

let gunResult:
Expand All @@ -8,7 +7,7 @@ let gunResult:
}
| undefined;

export const getMostGun = async () => {
export const getPoliticianWithMostGun = async (politicians: Politician[]) => {
if (gunResult) return gunResult;

// Get latest files
Expand Down Expand Up @@ -68,10 +67,8 @@ export const getMostGun = async () => {
});

// Match politician data
const activePoliticians = (await fetchPoliticians()).filter(({ isActive }) => isActive);

for (const { firstname: gFirst, lastname: gLast, value } of sortedGunOwner) {
const searchResult = activePoliticians.find(
const searchResult = politicians.find(
({ firstname: pFirst, lastname: pLast }) => pFirst === gFirst && pLast === gLast
);
if (searchResult) {
Expand Down
38 changes: 38 additions & 0 deletions scripts/politician-ranking/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { HighlightedPolitician } from '$components/Index/StatCard.svelte';
import { existsSync, mkdirSync, writeFileSync } from 'fs';
import { join } from 'path';
import { getPoliticianWithMostViewLastMonth } from './wikipedia';
import { getPoliticianWithMostGun } from './gun';
import { fetchPoliticians } from '$lib/datasheets';

const OUT_DIR = './out';
const OUT_FILE = 'politician-ranking.json';

export interface RankingFile {
politicianWithMostWikipediaVisit: Omit<HighlightedPolitician, 'reason'>;
politicianWithMostGun: Omit<HighlightedPolitician, 'reason'>;
updatedAt: Date;
}

export async function writePoliticianRankingFile() {
console.info('Fetching politicians...');
const activePoliticians = (await fetchPoliticians()).filter(({ isActive }) => isActive);

console.info('Fetching wikipedia views...');
const politicianWithMostWikipediaVisit = await getPoliticianWithMostViewLastMonth(
activePoliticians
);

console.info('Fetching gun ownership...');
const politicianWithMostGun = await getPoliticianWithMostGun(activePoliticians);

const rankingFile: RankingFile = {
politicianWithMostWikipediaVisit,
politicianWithMostGun,
updatedAt: new Date()
};

if (!existsSync(OUT_DIR)) mkdirSync(OUT_DIR);

writeFileSync(join(OUT_DIR, OUT_FILE), JSON.stringify(rankingFile));
}
3 changes: 3 additions & 0 deletions scripts/politician-ranking/run-update-job.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { writePoliticianRankingFile } from '.';

writePoliticianRankingFile();
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { fetchPoliticians } from '$lib/datasheets';
import type { Politician } from '$models/politician';
import dayjs from 'dayjs';
import { movingForwardPolitician } from '../../mocks/data/politician';
import { movingForwardPolitician } from '../../src/mocks/data/politician';

export interface PoliticianResult {
politician: Politician;
Expand All @@ -10,11 +9,9 @@ export interface PoliticianResult {

let wikiResult: PoliticianResult | undefined = undefined;

export async function getPoliticianWithMostViewLastMonth() {
export async function getPoliticianWithMostViewLastMonth(politicians: Politician[]) {
if (!wikiResult) {
const activePoliticians = (await fetchPoliticians()).filter(({ isActive }) => isActive);

wikiResult = await _getPoliticianWithMostViewLastMonth(activePoliticians);
wikiResult = await _getPoliticianWithMostViewLastMonth(politicians);
}
return wikiResult;
}
Expand Down
12 changes: 2 additions & 10 deletions src/routes/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { building } from '$app/environment';
import type { HighlightedPolitician } from '$components/Index/StatCard.svelte';
import { HighlightedReason } from '$components/Index/StatCard.svelte';
import { safeFind } from '$lib/datasheets/processor.js';
import { fetchPoliticians, fetchVotes, fetchVotings } from '$lib/datasheets';
import { getMostGun } from '$lib/ranking/gun.js';
import { getPoliticianWithMostViewLastMonth } from '$lib/ranking/wikipedia.js';
import { movingForwardPolitician } from '../mocks/data/politician.js';
import type { ComponentProps } from 'svelte';
import type VoteCard from '$components/VoteCard/VoteCard.svelte';
Expand Down Expand Up @@ -101,20 +98,15 @@ export async function load() {
}
];

let wikipediaPolitician: Omit<HighlightedPolitician, 'reason'> = {
const wikipediaPolitician: Omit<HighlightedPolitician, 'reason'> = {
politician: movingForwardPolitician,
value: 476263
};
let gunPolitician: Omit<HighlightedPolitician, 'reason'> = {
const gunPolitician: Omit<HighlightedPolitician, 'reason'> = {
politician: movingForwardPolitician,
value: 25
};

if (building) {
wikipediaPolitician = await getPoliticianWithMostViewLastMonth();
gunPolitician = await getMostGun();
}

const chuanLeekpai = safeFind(activePoliticians, (p) => p.id === 'ชวน-หลีกภัย');
const banyatBantadtan = safeFind(activePoliticians, (p) => p.id === 'บัญญัติ-บรรทัดฐาน');

Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"extends": "./.svelte-kit/tsconfig.json",
"include": ["scripts/**/*.ts"],
"compilerOptions": {
"allowJs": true,
"checkJs": false,
Expand Down
Loading

0 comments on commit e0b3af6

Please sign in to comment.