Skip to content

Commit

Permalink
update senators api
Browse files Browse the repository at this point in the history
  • Loading branch information
robertotcestari committed Sep 16, 2024
1 parent 7b521aa commit affebfa
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ export async function updatePartySummary(year: number) {
const sanitized = expensesByParty.map((row) => {
return {
party: row.party,
total_expenses: row.total,
total_per_senator: row.total / groupedByParty[row.party].length,
total_expenses: parseFloat(Number(row.total)?.toFixed(2)),
total_per_senator: parseFloat(
(row.total / groupedByParty[row.party].length).toFixed(2)
),
senator_ids: groupedByParty[row.party],
};
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@ export async function updateUFSummary(year: number) {
.groupBy(expenses.uf);
// get unique parties from the expenses table

const sanitized = data.map((row) => {
return {
uf: row.uf,
total_expenses: parseFloat(Number(row.total)?.toFixed(2)),
};
});

await db
.delete(summaries)
.where(and(eq(summaries.year, year.toString()), eq(summaries.type, 'uf')));

await db.insert(summaries).values({
type: 'uf',
year: year.toString(),
summary: data,
summary: sanitized,
});
}
4 changes: 3 additions & 1 deletion src/features/codante-apis/senator-expenses/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { scrapeExpenses } from './expenses/scrapeExpenses';
import { updatePartySummary } from './summaries/updatePartySummary';
import { updateUFSummary } from './summaries/updateUFSummary';

updater();

export async function updater() {
const YEAR = 2024;

// update expenses
console.log('Scraping expenses...');
await scrapeExpenses(YEAR);
// await scrapeExpenses(YEAR);

// update summaries
console.log('Updating party summary...');
Expand Down
7 changes: 6 additions & 1 deletion src/scheduler/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { Cron } from 'croner';
import { updater } from '../features/codante-apis/senator-expenses/updater';

const job = Cron('0 2 * * *', async () => {
// const job = Cron('0 2 * * *', async () => {
// console.log('Running ');
// await updater();
// });

const job = Cron('* * * * *', async () => {
console.log('Running ');
await updater();
});

0 comments on commit affebfa

Please sign in to comment.