From 73c19fe3fb84d0d4953406197281f7b01045394d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vincent=20Lain=C3=A9?= Date: Tue, 1 Oct 2024 09:31:00 +0200 Subject: [PATCH] =?UTF-8?q?Perf=20requ=C3=AAte=20=E2=9A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/data/data.service.ts | 23 ++++++++++++++--------- src/zones/zones.service.ts | 2 +- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/data/data.service.ts b/src/data/data.service.ts index d025d86..6bd6345 100644 --- a/src/data/data.service.ts +++ b/src/data/data.service.ts @@ -1,7 +1,7 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; import { VigieauLogger } from '../logger/vigieau.logger'; import { InjectRepository } from '@nestjs/typeorm'; -import { Repository } from 'typeorm'; +import { DataSource, Repository } from 'typeorm'; import { StatisticDepartement } from './entities/statistic_departement.entity'; import { Departement } from '../zones/entities/departement.entity'; import moment from 'moment'; @@ -40,6 +40,7 @@ export class DataService { private readonly regionRepository: Repository, @InjectRepository(BassinVersant) private readonly bassinVersantRepository: Repository, + private dataSource: DataSource, ) { } @@ -166,7 +167,7 @@ export class DataService { const stat = await this.statisticCommuneRepository.findOne({ select: { id: true, - restrictions: true, + restrictions: !dateDebut && !dateFin, commune: { id: true, code: true, @@ -180,13 +181,17 @@ export class DataService { }, }, }); - if (dateDebut && dateFin) { - const dateBegin = moment(dateDebut, 'YYYY-MM').startOf('month'); - const dateEnd = moment(dateFin, 'YYYY-MM').endOf('month'); - stat.restrictions = stat.restrictions - .filter((r: any) => { - return moment(r.date, 'YYYY-MM').isSameOrAfter(dateBegin) && moment(r.date, 'YYYY-MM').isSameOrBefore(dateEnd); - }); + if (dateDebut || dateFin) { + const dateBegin = dateDebut ? moment(dateDebut, 'YYYY-MM').startOf('month') : moment(); + const dateEnd = dateFin ? moment(dateFin, 'YYYY-MM').endOf('month') : moment(); + const r = await this.dataSource.query(` + SELECT jsonb_agg(r) as filtered_restrictions + FROM statistic_commune, + jsonb_array_elements(restrictions) AS r + WHERE statistic_commune.id = $1 + AND (r->>'date')::date BETWEEN $2 AND $3 + `, [stat.id, dateBegin.format('YYYY-MM-DD'), dateEnd.format('YYYY-MM-DD')]); + stat.restrictions = r[0].filtered_restrictions; } return stat; } diff --git a/src/zones/zones.service.ts b/src/zones/zones.service.ts index 025e93c..0e94056 100644 --- a/src/zones/zones.service.ts +++ b/src/zones/zones.service.ts @@ -411,7 +411,7 @@ export class ZonesService { const count = await this.arreteMunicipalRepository .createQueryBuilder('arrete_municipal') .where({ - updatedAt: MoreThan(this.lastUpdateAm.toLocaleString('sv')), + updated_at: MoreThan(this.lastUpdateAm.toLocaleString('sv')), }) .getCount(); if (count > 0) {