Skip to content

Commit

Permalink
fix(backend): free memory and useless data for stats & exports
Browse files Browse the repository at this point in the history
  • Loading branch information
pYassine committed Jan 20, 2025
1 parent 8d5cd39 commit 86d9238
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 100 deletions.
20 changes: 20 additions & 0 deletions packages/backend/src/_migrations/1737390477602-manual-migration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { IsNull, MigrationInterface } from "typeorm";
import { structureStatsReportingQuestionsRepository } from "../database";
import { domifaConfig } from "../config";

export class ManualMigration1737390477602 implements MigrationInterface {
public async up(): Promise<void> {
if (
domifaConfig().envId === "prod" ||
domifaConfig().envId === "preprod" ||
domifaConfig().envId === "local"
) {
console.log("Delete useless data in questions");
await structureStatsReportingQuestionsRepository.delete({
confirmationDate: IsNull(),
});
}
}

public async down(): Promise<void> {}
}
55 changes: 31 additions & 24 deletions packages/backend/src/stats/controllers/stats.private.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,15 @@ import { structureStatsInPeriodGenerator } from "../services";
import { statsQuestionsCoreBuilder } from "../services/statsQuestionsCoreBuilder.service";

import { addDays, format } from "date-fns";
import { StructureStatsFull } from "@domifa/common";
import {
StructureStatsFull,
StructureStatsReportingQuestions,
} from "@domifa/common";
import { StructureStatsReportingDto } from "../dto";
import { structureStatsReportingQuestionsRepository } from "../../database";
import {
structureStatsReportingQuestionsRepository,
StructureStatsReportingQuestionsTable,
} from "../../database";
import { AppLogsService } from "../../modules/app-logs/app-logs.service";

@Controller("stats")
Expand All @@ -52,30 +58,31 @@ export class StatsPrivateController {
},
});

if (!stats) {
throw new Error("CANNOT_UPDATE_REPORT");
}

await structureStatsReportingQuestionsRepository.update(
{
structureId: user.structureId,
year: reportingDto.year,
const questionsToAdd: StructureStatsReportingQuestions = {
...reportingDto,
completedBy: {
id: user.id,
nom: user.nom,
prenom: user.prenom,
},
{
waitingList: reportingDto.waitingList,
waitingTime: reportingDto.waitingTime,
workers: reportingDto.workers,
volunteers: reportingDto.volunteers,
humanCosts: reportingDto.humanCosts,
totalCosts: reportingDto.totalCosts,
completedBy: {
id: user.id,
nom: user.nom,
prenom: user.prenom,
structureId: user.structureId,
confirmationDate: new Date(),
};

if (!stats) {
await structureStatsReportingQuestionsRepository.save(
new StructureStatsReportingQuestionsTable(questionsToAdd)
);
} else {
await structureStatsReportingQuestionsRepository.update(
{
structureId: user.structureId,
year: reportingDto.year,
},
confirmationDate: new Date(),
}
);
questionsToAdd
);
}
return;
}

@AllowUserStructureRoles("responsable", "admin", "simple")
Expand Down
11 changes: 9 additions & 2 deletions packages/backend/src/stats/dto/structure-stats-reporting.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,26 @@ export class StructureStatsReportingDto {

@IsNumber()
@IsOptional()
@Max(1000)
workers: number | null;

@IsNumber()
@Min(0)
@Max(10000)
volunteers: number | null;

@IsNumber()
@Max(1000000)
@Min(0)
humanCosts: number | null;

@IsNumber()
@Max(1000000)
@Min(0)
totalCosts: number | null;

@IsNumber()
@Min(2020)
@Max(2035)
@Min(new Date().getFullYear() - 5) // 5 years ago
@Max(new Date().getFullYear()) // current year
year: number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,6 @@ export class ExportStructureUsagersController {
let currentRowEntretiens = 2;

const processChunk = async (chunk: StructureUsagerExport[]) => {
console.log({
date: new Date(),
structureId: user.structureId,
currentRowUsagers,
});
const { firstSheetUsagers, secondSheetEntretiens } =
renderStructureUsagersRows(chunk, user.structure);

Expand All @@ -108,14 +103,15 @@ export class ExportStructureUsagersController {
"DATE_DERNIER_PASSAGE",
]);

// Ajoute les lignes à la première feuille
XLSX.utils.sheet_add_json(wsUsagers, firstSheetUsagers, {
skipHeader: true,
origin: currentRowUsagers,
cellDates: true,
dateNF: "DD/MM/YYYY",
});

currentRowUsagers += firstSheetUsagers.length;
firstSheetUsagers.length = 0;

applyDateFormat(secondSheetEntretiens, ["USAGER_DATE_NAISSANCE"]);

Expand All @@ -125,13 +121,13 @@ export class ExportStructureUsagersController {
cellDates: true,
dateNF: "DD/MM/YYYY",
});

currentRowEntretiens += secondSheetEntretiens.length;
secondSheetEntretiens.length = 0;
};

await this.usagersService.exportByChunks(
user.structureId,
5000,
user,
2000,
statut,
processChunk
);
Expand Down
21 changes: 13 additions & 8 deletions packages/backend/src/usagers/services/usagers.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export class UsagersService {
}

public async exportByChunks(
structureId: number,
user: Pick<UserStructureAuthenticated, "id" | "structureId" | "prenom">,
chunkSize: number = 5000,
statut: UsagersFilterCriteriaStatut,
processChunk: (chunk: StructureUsagerExport[]) => Promise<void>
Expand All @@ -259,7 +259,7 @@ export class UsagersService {

let whereClause = 'WHERE u."structureId" = $1';
let countWhereClause = `${whereClause}`;
const countParams: any[] = [structureId];
const countParams: any[] = [user.structureId];

if (statut !== UsagersFilterCriteriaStatut.TOUS) {
countWhereClause += ` AND u.statut = $2`;
Expand Down Expand Up @@ -329,8 +329,8 @@ export class UsagersService {
LIMIT $2 OFFSET $3
`;

while (true) {
const queryParams: any[] = [structureId, chunkSize, skip];
while (total < count) {
const queryParams: any[] = [user.structureId, chunkSize, skip];
if (statut !== UsagersFilterCriteriaStatut.TOUS) {
queryParams.push(statut);
}
Expand All @@ -345,17 +345,22 @@ export class UsagersService {
total += chunk.length;
skip += chunk.length;

console.log({
console.table({
timestamp: new Date().toISOString(),
processedCount: total,
totalCount: count,
totalCount: parseInt(count, 10),
progression: `${Math.round((total / parseInt(count, 10)) * 100)}%`,
chunkSize: chunk.length,
skip,
structureId,
userId: user.id,
structureId: user.structureId,
});

chunk.length = 0;
}

if (total !== parseInt(count)) {
console.warn(
console.log(
`⚠️ Différence détectée - Exportés: ${total}, Total attendu: ${count}`
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ <h2>
<option value="TWO_WEEKS">7 à 15 jours</option>
<option value="ONE_MONTH">Entre 2 semaines et 1 mois</option>
<option value="ONE_TO_SIX_MONTHS">Entre 1 et 6 mois</option>
<option value="MORE_THAN_SIX_MONTHS">Plus de 6 moiss</option>
<option value="MORE_THAN_SIX_MONTHS">Plus de 6 mois</option>
</select>
<p
*ngIf="(f.waitingTime.dirty || submitted) && f.waitingTime.errors"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class ReportingFormComponent implements OnInit {
public structureStatsForm: FormGroup;

private readonly subscription = new Subscription();
@Input() currentReport: StructureStatsReportingQuestions;
@Input({ required: true }) currentReport: StructureStatsReportingQuestions;
@Output() getReportings = new EventEmitter<void>();

public readonly REPORTNG_QUESTIONS_LABELS = REPORTNG_QUESTIONS_LABELS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ <h2 class="d-none d-print-block">
<nav id="statut-selector" class="mt-4 row d-print-none">
<button
type="button"
*ngFor="let report of reports"
*ngFor="let year of years"
[ngClass]="
!isCustomDates && selectedYear === report.year
!isCustomDates && selectedYear === year
? 'selected-section col'
: 'col'
"
(click)="onYearChange(report.year)"
(click)="onYearChange(year)"
>
Année {{ report.year }}
Année {{ year }}
</button>
<button
type="button"
Expand Down Expand Up @@ -78,8 +78,8 @@ <h2 class="d-none d-print-block">
<input
class="form-control"
placeholder="jj/mm/aaaa"
[minDate]="minDateDebut"
[maxDate]="maxDateDebut"
[minDate]="minStartDate"
[maxDate]="maxStartDate"
ngbDatepicker
id="dateDebutValide"
outsideDays="hidden"
Expand Down Expand Up @@ -120,8 +120,8 @@ <h2 class="d-none d-print-block">
<input
class="form-control"
placeholder="jj/mm/aaaa"
[minDate]="minDateFin"
[maxDate]="maxDateFin"
[minDate]="minEndDate"
[maxDate]="maxEndDate"
ngbDatepicker
[(ngModel)]="toDate"
#dFin="ngbDatepicker"
Expand Down
Loading

0 comments on commit 86d9238

Please sign in to comment.