Skip to content

Commit

Permalink
fix report generation 2
Browse files Browse the repository at this point in the history
  • Loading branch information
theolemague committed Feb 5, 2025
1 parent db43e6f commit 046a1e9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 25 deletions.
2 changes: 1 addition & 1 deletion process/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"migrate": "prisma migrate dev --name init",
"generate": "prisma generate",
"build": "prisma generate && tsc",
"build": "prisma generate && tsc --build --clean",
"dev": "nodemon",
"stop": "pm2 stop process",
"start-dev": "ts-node src/index.ts",
Expand Down
7 changes: 3 additions & 4 deletions process/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,11 @@ const leboncoinJob = new CronJob(

// Every first Tuesday of the month at 10:00 AM
const reportJob = new CronJob(
// "0 10 * * 2",
"16 14 * * *",
"0 10 * * 2",
async () => {
// if not the first Tuesday of the month, return
// const date = new Date();
// if (date.getDay() !== 2 || date.getDate() > 7) return;
const date = new Date();
if (date.getDay() !== 2 || date.getDate() > 7) return;

runnings.report = true;
const checkInId = Sentry.captureCheckIn({
Expand Down
2 changes: 1 addition & 1 deletion process/src/jobs/report/generate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const generateReport = async (publisher: Publisher, year: number, month:
}
};

export const generate = async (year: number, month: number) => {
export const generateReports = async (year: number, month: number) => {
const publishers = await PublisherModel.find({ automated_report: true });
let count = 0;
const errors = [] as { id: string; name: string; error: string }[];
Expand Down
36 changes: 18 additions & 18 deletions process/src/jobs/report/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { SLACK_PRODUCT_CHANNEL_ID } from "../../config";
import { captureException } from "../../error";
import { postMessage } from "../../services/slack";
import { generate } from "./generate";
import { send } from "./send";
import { generateReports } from "./generate";
import { sendReports } from "./send";

const handler = async () => {
const start = new Date();
Expand All @@ -12,29 +12,29 @@ const handler = async () => {
const year = month === 11 ? new Date().getFullYear() - 1 : new Date().getFullYear();

console.log(`[Report] Generating report for ${year}-${month}`);
const generationRes = await generate(year, month);
const generationRes = await generateReports(year, month);
console.log(`[Report] Generated ${generationRes.count} report with ${generationRes.errors.length} errors`);
if (generationRes.errors.length > 0) {
console.error(`[Report] Errors`, JSON.stringify(generationRes.errors, null, 2));
captureException(`Report generation with failure`, `Errors while genrating report`);
}

// console.log(`[Report] Sending report for ${year}-${month}`);
// const sendingRes = await send(year, month);
// console.log(`[Report] Sent ${sendingRes.count} report, ${sendingRes.skipped.length} skipped and ${sendingRes.errors.length} errors`);
// if (sendingRes.errors.length > 0) {
// console.error(`[Report] Errors`, JSON.stringify(sendingRes.errors, null, 2));
// captureException(`Report sending with failure`, `Errors while sending report`);
// }
// console.log(`[Report] Sending slack message for ${year}-${month}`);
console.log(`[Report] Sending report for ${year}-${month}`);
const sendingRes = await sendReports(year, month);
console.log(`[Report] Sent ${sendingRes.count} report, ${sendingRes.skipped.length} skipped and ${sendingRes.errors.length} errors`);
if (sendingRes.errors.length > 0) {
console.error(`[Report] Errors`, JSON.stringify(sendingRes.errors, null, 2));
captureException(`Report sending with failure`, `Errors while sending report`);
}
console.log(`[Report] Sending slack message for ${year}-${month}`);

// await postMessage(
// {
// title: `Rapports d'impact du ${month + 1 < 10 ? `0${month + 1}` : month + 1}/${year} générés et envoyés`,
// text: `Rapport générés: ${generationRes.count}, emails envoyés: ${sendingRes.count}, non envoyés: ${sendingRes.skipped.length}, erreurs: ${generationRes.errors.length + sendingRes.errors.length}\n\nListe des rapports d'impact du mois [ici](https://app.api-engagement.beta.gouv.fr/admin-report?month=${month}&year=${year})`,
// },
// SLACK_PRODUCT_CHANNEL_ID,
// );
await postMessage(
{
title: `Rapports d'impact du ${month + 1 < 10 ? `0${month + 1}` : month + 1}/${year} générés et envoyés`,
text: `Rapport générés: ${generationRes.count}, emails envoyés: ${sendingRes.count}, non envoyés: ${sendingRes.skipped.length}, erreurs: ${generationRes.errors.length + sendingRes.errors.length}\n\nListe des rapports d'impact du mois [ici](https://app.api-engagement.beta.gouv.fr/admin-report?month=${month}&year=${year})`,
},
SLACK_PRODUCT_CHANNEL_ID,
);
} catch (error: any) {
console.error(error);
captureException(`Report generation failed`, `${error.message} while generating report`);
Expand Down
2 changes: 1 addition & 1 deletion process/src/jobs/report/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const sendReport = async (report: Report) => {
}
};

export const send = async (year: number, month: number) => {
export const sendReports = async (year: number, month: number) => {
const publishers = await PublisherModel.find({ automated_report: true });
const users = await UserModel.find({});

Expand Down

0 comments on commit 046a1e9

Please sign in to comment.