Skip to content

Commit

Permalink
log: add winston
Browse files Browse the repository at this point in the history
  • Loading branch information
fufeck committed Oct 7, 2024
1 parent b9abe9c commit fa259a9
Show file tree
Hide file tree
Showing 21 changed files with 341 additions and 82 deletions.
6 changes: 3 additions & 3 deletions apps/api/src/lib/utils/csv.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Numero } from '@/shared/entities/numero.entity';
import { Toponyme } from '@/shared/entities/toponyme.entity';
import { Row } from '../types/validator.types';
import { ObjectId } from 'mongodb';
import { Logger } from '@/shared/utils/logger.utils';

export type FromCsvType = {
isValid?: boolean;
Expand Down Expand Up @@ -189,7 +190,7 @@ export async function extractFromCsv(
}: {
rows: Row[];
parseOk: boolean;
} = await validate(file, { profile: '1.4-relax' });
} = await validate(file, { profile: '1.45-relax' });

if (!parseOk) {
return { isValid: false };
Expand All @@ -211,8 +212,7 @@ export async function extractFromCsv(
toponymes: communesData.toponymes,
};
} catch (error) {
console.log('ERROR extractFromCsv');
console.error(error);
Logger.error(`Impossible d'extraire la BAL sur CSV`, error);
return { isValid: false, validationError: error.message };
}
}
3 changes: 3 additions & 0 deletions apps/api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import { ValidationPipe } from '@nestjs/common';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';

import { ApiModule } from './api.module';
import { WinstonLogger } from '@/shared/modules/logger/logger.service';
import { Logger } from '@/shared/utils/logger.utils';

async function bootstrap() {
const app = await NestFactory.create(ApiModule, {
cors: true,
logger: new WinstonLogger(Logger),
});

const config = new DocumentBuilder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Module, MiddlewareConsumer, forwardRef } from '@nestjs/common';
import { Module, MiddlewareConsumer, forwardRef, Logger } from '@nestjs/common';

import { ApiDepotModule } from '@/shared/modules/api_depot/api_depot.module';

Expand All @@ -14,7 +14,7 @@ import { PublicationModule } from '@/shared/modules/publication/publication.modu
forwardRef(() => BaseLocaleModule),
PublicationModule,
],
providers: [HabilitationService, BaseLocaleMiddleware],
providers: [HabilitationService, BaseLocaleMiddleware, Logger],
controllers: [HabilitationController],
exports: [HabilitationService],
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { HttpException, HttpStatus, Injectable, Logger } from '@nestjs/common';

import {
Habilitation,
Expand All @@ -14,15 +14,37 @@ export class HabilitationService {
constructor(
private readonly baseLocaleService: BaseLocaleService,
private readonly apiDepotService: ApiDepotService,
private readonly logger: Logger,
) {}

async findOne(habilitationId: string): Promise<Habilitation> {
return this.apiDepotService.findOneHabiliation(habilitationId);
try {
return await this.apiDepotService.findOneHabiliation(habilitationId);
} catch (error) {
this.logger.error(
`Impossible de trouver l'habilitation ${habilitationId}`,
error,
);
const { message } = (error.response.data as any) || 'No server response';
throw new HttpException(message, HttpStatus.BAD_GATEWAY);
}
}

async isValid(habilitationId: string): Promise<boolean> {
const habilitation: Habilitation =
await this.apiDepotService.findOneHabiliation(habilitationId);
let habilitation: Habilitation;

try {
habilitation =
await this.apiDepotService.findOneHabiliation(habilitationId);
} catch (error) {
this.logger.error(
`Impossible de trouver l'habilitation ${habilitationId}`,
error,
);
const { message } = (error.response.data as any) || 'No server response';
throw new HttpException(message, HttpStatus.BAD_GATEWAY);
}

// On verifie que l'habilitation est valide
if (habilitation.status !== StatusHabiliation.ACCEPTED) {
return false;
Expand Down Expand Up @@ -51,9 +73,18 @@ export class HabilitationService {
);
}
}

const habilitation: Habilitation =
await this.apiDepotService.createOneHabiliation(baseLocale);
let habilitation: Habilitation;
try {
habilitation =
await this.apiDepotService.createOneHabiliation(baseLocale);
} catch (error) {
this.logger.error(
`Impossible de créer une habilitation pour la commune ${baseLocale.commune}`,
error,
);
const { message } = (error.response.data as any) || 'No server response';
throw new HttpException(message, HttpStatus.BAD_GATEWAY);
}

await this.baseLocaleService.updateHabilitation(baseLocale, habilitation);

Expand All @@ -72,8 +103,12 @@ export class HabilitationService {
try {
await this.apiDepotService.sendPinCodeHabiliation(habilitationId);
} catch (error) {
const { statusCode, message } = error.response.data;
throw new HttpException(message, statusCode);
this.logger.error(
`Impossible d'envoyer le code pour l'habilitation ${habilitationId}`,
error,
);
const { message } = (error.response.data as any) || 'No server response';
throw new HttpException(message, HttpStatus.BAD_GATEWAY);
}
}

Expand All @@ -94,8 +129,12 @@ export class HabilitationService {
);
return data;
} catch (error) {
const { statusCode, message } = error.response.data;
throw new HttpException(message, statusCode);
this.logger.error(
`Impossible de valider le code pour l'habilitation ${habilitationId}`,
error,
);
const { message } = (error.response.data as any) || 'No server response';
throw new HttpException(message, HttpStatus.BAD_GATEWAY);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Module, forwardRef } from '@nestjs/common';
import { Logger, Module, forwardRef } from '@nestjs/common';
import { PopulateService } from './populate.service';
import { BaseLocaleModule } from '../../base_locale.module';
import { ApiDepotModule } from '@/shared/modules/api_depot/api_depot.module';
Expand All @@ -10,7 +10,7 @@ import { BanPlateformModule } from '@/shared/modules/ban_plateform/ban_plateform
forwardRef(() => ApiDepotModule),
forwardRef(() => BanPlateformModule),
],
providers: [PopulateService],
providers: [PopulateService, Logger],
exports: [PopulateService],
})
export class PopulateModule {}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { FromCsvType, extractFromCsv } from '@/lib/utils/csv.utils';
import { ApiDepotService } from '@/shared/modules/api_depot/api_depot.service';
import { BanPlateformService } from '@/shared/modules/ban_plateform/ban_plateform.service';
import { Inject, Injectable, forwardRef } from '@nestjs/common';
import { Inject, Injectable, forwardRef, Logger } from '@nestjs/common';

@Injectable()
export class PopulateService {
constructor(
private apiDepotService: ApiDepotService,
@Inject(forwardRef(() => BanPlateformService))
private banPlateformService: BanPlateformService,
private readonly logger: Logger,
) {}

private async extractFromApiDepot(codeCommune: string): Promise<FromCsvType> {
Expand All @@ -17,7 +18,6 @@ export class PopulateService {
await this.apiDepotService.downloadCurrentRevisionFile(codeCommune);

const result: FromCsvType = await extractFromCsv(fileData, codeCommune);

if (!result.isValid) {
throw new Error('Invalid CSV file');
}
Expand All @@ -32,7 +32,6 @@ export class PopulateService {
await this.banPlateformService.getBanAssemblage(codeCommune);

const result = await extractFromCsv(file, codeCommune);

if (!result.isValid) {
throw new Error('Invalid CSV file');
}
Expand All @@ -50,7 +49,7 @@ export class PopulateService {
return data;
}

console.error(
this.logger.error(
`Aucune adresse n’a pu être extraite avec le code commune: ${codeCommune}`,
);

Expand Down
8 changes: 4 additions & 4 deletions apps/api/test/habilitation.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,10 @@ describe('HABILITATION MODULE', () => {
const response = await request(app.getHttpServer())
.get(`/bases-locales/${balId}/habilitation`)
.set('Authorization', `Bearer ${token}`)
.expect(404);
.expect(502);

const responseExpected = {
statusCode: 404,
statusCode: 502,
message: 'L’identifiant de l’habilitation demandé n’existe pas',
};

Expand Down Expand Up @@ -434,10 +434,10 @@ describe('HABILITATION MODULE', () => {
.post(`/bases-locales/${balId}/habilitation/email/validate-pin-code`)
.set('Authorization', `Bearer ${token}`)
.send({ code: '123456' })
.expect(412);
.expect(502);

expect(response.body).toEqual({
statusCode: 412,
statusCode: 502,
message: 'Code non valide, 9 tentatives restantes',
});
});
Expand Down
3 changes: 2 additions & 1 deletion apps/cron/src/cron.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Module } from '@nestjs/common';
import { Logger, Module } from '@nestjs/common';
import { ScheduleModule } from '@nestjs/schedule';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { MailerModule } from '@nestjs-modules/mailer';
Expand Down Expand Up @@ -51,6 +51,7 @@ import { CacheModule } from '@/shared/modules/cache/cache.module';
DetectConflictTask,
RemoveSoftDeleteBalTask,
RemoveDemoBalTask,
Logger,
],
})
export class CronModule {}
7 changes: 6 additions & 1 deletion apps/cron/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { NestFactory } from '@nestjs/core';
import { CronModule } from './cron.module';

import { WinstonLogger } from '@/shared/modules/logger/logger.service';
import { Logger } from '@/shared/utils/logger.utils';

async function bootstrap() {
await NestFactory.createApplicationContext(CronModule);
await NestFactory.createApplicationContext(CronModule, {
logger: new WinstonLogger(Logger),
});
}
bootstrap();
9 changes: 5 additions & 4 deletions apps/cron/src/task_queue.class.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Logger } from '@/shared/utils/logger.utils';

export type Task = {
title: string;
run(): Promise<void>;
Expand All @@ -19,14 +21,13 @@ export class TaskQueue {

while (this.queue.length > 0) {
const task = this.queue.shift();
console.log(`TASK START ${task.title}`);
Logger.info(`TASK START ${task.title}`);
try {
await task.run();
} catch (error) {
console.error(`TASK ERROR ${task.title}`);
console.error(error);
Logger.error(`TASK ERROR ${task.title}`, error);
}
console.log(`TASK END ${task.title}`);
Logger.info(`TASK END ${task.title}`);
}

this.isTaskRunning = false;
Expand Down
18 changes: 10 additions & 8 deletions apps/cron/src/tasks/detect_conflict.task.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { In, Repository } from 'typeorm';

Expand All @@ -25,6 +25,7 @@ export class DetectConflictTask implements Task {
@InjectRepository(BaseLocale)
private basesLocalesRepository: Repository<BaseLocale>,
private cacheService: CacheService,
private readonly logger: Logger,
) {}

public async run() {
Expand All @@ -33,14 +34,13 @@ export class DetectConflictTask implements Task {
KEY_DETECT_CONFLICT_PUBLISHED_SINCE,
);
const detectConflictPublishedSince = new Date(cache?.value || '1970-01-01');
console.log('Detect conflict since : ', detectConflictPublishedSince);
this.logger.log(`Detect conflict since : ${detectConflictPublishedSince}`);
const currentRevisions: Revision[] =
await this.apiDepotService.getCurrentRevisions(
detectConflictPublishedSince,
);
console.log(
'Number of current revisions processed : ',
currentRevisions.length,
this.logger.log(
`Number of current revisions processed : ${currentRevisions.length}`,
);
const revisedCommunes = currentRevisions.map((r) => r.codeCommune);

Expand All @@ -53,8 +53,10 @@ export class DetectConflictTask implements Task {
try {
await this.updateConflictStatus(codeCommune);
} catch (error) {
console.error(`Unable to detect conflict for ${codeCommune}`);
console.error(error);
this.logger.error(
`Unable to detect conflict for ${codeCommune}`,
error,
);
}
}
}
Expand All @@ -73,7 +75,7 @@ export class DetectConflictTask implements Task {
await this.apiDepotService.getCurrentRevision(codeCommune);

if (!currentRevision) {
console.error(
this.logger.error(
`Comportement inattendu : pas de révision courante pour la commune ${codeCommune}`,
);
return;
Expand Down
13 changes: 8 additions & 5 deletions apps/cron/src/tasks/sync_outdated.task.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { InjectRepository } from '@nestjs/typeorm';
import { FindOptionsWhere, JsonContains, LessThan, Repository } from 'typeorm';
import { Injectable } from '@nestjs/common';
import { Injectable, Logger } from '@nestjs/common';
import { sub } from 'date-fns';

import {
Expand All @@ -19,6 +19,7 @@ export class SyncOutdatedTask implements Task {
@InjectRepository(BaseLocale)
private basesLocalesRepository: Repository<BaseLocale>,
private readonly publicationService: PublicationService,
private readonly logger: Logger,
) {}

public async run() {
Expand All @@ -33,15 +34,17 @@ export class SyncOutdatedTask implements Task {

const bals: BaseLocale[] = await this.basesLocalesRepository.findBy(where);

console.log(`Number of outdated bases locales to sync : ${bals.length}`);
this.logger.log(
`Number of outdated bases locales to sync : ${bals.length}`,
SyncOutdatedTask.name,
);

for (const bal of bals) {
try {
console.log(`Syncing BAL : ${bal.id}`);
this.logger.log(`Syncing BAL : ${bal.id}`);
await this.publicationService.exec(bal.id);
} catch (error) {
console.error(`Unable to sync BAL : ${bal.id}`);
console.error(error);
this.logger.error(`Unable to sync BAL : ${bal.id}`, error);
}
}
}
Expand Down
Loading

0 comments on commit fa259a9

Please sign in to comment.