Skip to content

Commit

Permalink
fix: test report generation
Browse files Browse the repository at this point in the history
  • Loading branch information
theolemague committed Feb 5, 2025
1 parent 6171ff9 commit 4d4442d
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 26 deletions.
6 changes: 6 additions & 0 deletions process/nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"ignore": ["node_modules"],
"watch": ["src"],
"exec": "npm run start-dev",
"ext": "ts"
}
161 changes: 161 additions & 0 deletions process/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion process/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
"migrate": "prisma migrate dev --name init",
"generate": "prisma generate",
"build": "prisma generate && tsc",
"dev": "tsx watch src/index.ts",
"dev": "nodemon",
"stop": "pm2 stop process",
"start-dev": "ts-node src/index.ts",
"start": "pm2 start dist/index.js --name process",
"reload": "pm2 reload process"
},
Expand Down Expand Up @@ -55,6 +56,7 @@
"@types/react": "^19.0.0",
"@types/uuid": "^10.0.0",
"@types/yauzl": "^2.10.3",
"nodemon": "^3.1.9",
"prettier-plugin-organize-imports": "^4.1.0",
"prisma": "^6.2.1"
}
Expand Down
7 changes: 4 additions & 3 deletions process/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,12 @@ const leboncoinJob = new CronJob(

// Every first Tuesday of the month at 10:00 AM
const reportJob = new CronJob(
"0 10 * * 2",
// "0 10 * * 2",
"51 10 * * *",
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
5 changes: 5 additions & 0 deletions process/src/jobs/report/generate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ export const generate = async (year: number, month: number) => {

for (let i = 0; i < publishers.length; i++) {
const publisher = publishers[i];
console.log(`[Report] Generating report for ${year}-${month} for ${publisher.name}`);
const res = await generateReport(publisher, year, month);

const obj = {
name: `Rapport ${MONTHS[month]} ${year}`,
month,
Expand All @@ -94,12 +96,15 @@ export const generate = async (year: number, month: number) => {
} as Report;

if (res.error) {
console.error(`[Report] Error generating report for ${year}-${month} for ${publisher.name}:`, res.error);
obj.status = "NOT_GENERATED_ERROR_GENERATION";
errors.push({ id: publisher._id.toString(), name: publisher.name, error: "Erreur lors de la génération du rapport" });
} else if (!res.objectName) {
console.error(`[Report] No data for ${year}-${month} for ${publisher.name}`);
obj.status = "NOT_GENERATED_NO_DATA";
errors.push({ id: publisher._id.toString(), name: publisher.name, error: "Données insuffisantes pour générer le rapport" });
} else {
console.log(`[Report] Report generated for ${year}-${month} for ${publisher.name}`);
obj.objectName = res.objectName;
obj.url = res.url;
obj.dataTemplate = res.data.receive?.hasStats && res.data.send?.hasStats ? "BOTH" : res.data.receive?.hasStats ? "RECEIVE" : "SEND";
Expand Down
30 changes: 15 additions & 15 deletions process/src/jobs/report/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@ const handler = async () => {
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 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}`);

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
14 changes: 7 additions & 7 deletions process/src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,16 +445,16 @@ export interface Report {
objectName: string | null;
publisherId: string;
publisherName: string;
sent: boolean;

dataTemplate: "BOTH" | "RECEIVE" | "SEND" | "NONE";

sentAt: Date | null;
sentTo: string[];
dataTemplate: "BOTH" | "RECEIVE" | "SEND" | "NONE";
clicksFrom: number;
clicksTo: number;
applyFrom: number;
applyTo: number;

status: string;

data: StatsReport;
error: string;

createdAt: Date;
updatedAt: Date;
}
Expand Down

0 comments on commit 4d4442d

Please sign in to comment.