Skip to content

Commit

Permalink
Perf requête ⚡
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentlaine committed Oct 1, 2024
1 parent 4c32611 commit 73c19fe
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
23 changes: 14 additions & 9 deletions src/data/data.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -40,6 +40,7 @@ export class DataService {
private readonly regionRepository: Repository<Region>,
@InjectRepository(BassinVersant)
private readonly bassinVersantRepository: Repository<BassinVersant>,
private dataSource: DataSource,
) {
}

Expand Down Expand Up @@ -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,
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/zones/zones.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 73c19fe

Please sign in to comment.