Skip to content

Commit 0f5c745

Browse files
committed
Perf requête ⚡
1 parent 4c32611 commit 0f5c745

1 file changed

Lines changed: 14 additions & 9 deletions

File tree

src/data/data.service.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
22
import { VigieauLogger } from '../logger/vigieau.logger';
33
import { InjectRepository } from '@nestjs/typeorm';
4-
import { Repository } from 'typeorm';
4+
import { DataSource, Repository } from 'typeorm';
55
import { StatisticDepartement } from './entities/statistic_departement.entity';
66
import { Departement } from '../zones/entities/departement.entity';
77
import moment from 'moment';
@@ -40,6 +40,7 @@ export class DataService {
4040
private readonly regionRepository: Repository<Region>,
4141
@InjectRepository(BassinVersant)
4242
private readonly bassinVersantRepository: Repository<BassinVersant>,
43+
private dataSource: DataSource,
4344
) {
4445
}
4546

@@ -166,7 +167,7 @@ export class DataService {
166167
const stat = await this.statisticCommuneRepository.findOne({
167168
select: {
168169
id: true,
169-
restrictions: true,
170+
restrictions: !dateDebut && !dateFin,
170171
commune: {
171172
id: true,
172173
code: true,
@@ -180,13 +181,17 @@ export class DataService {
180181
},
181182
},
182183
});
183-
if (dateDebut && dateFin) {
184-
const dateBegin = moment(dateDebut, 'YYYY-MM').startOf('month');
185-
const dateEnd = moment(dateFin, 'YYYY-MM').endOf('month');
186-
stat.restrictions = stat.restrictions
187-
.filter((r: any) => {
188-
return moment(r.date, 'YYYY-MM').isSameOrAfter(dateBegin) && moment(r.date, 'YYYY-MM').isSameOrBefore(dateEnd);
189-
});
184+
if (dateDebut || dateFin) {
185+
const dateBegin = dateDebut ? moment(dateDebut, 'YYYY-MM').startOf('month') : moment();
186+
const dateEnd = dateFin ? moment(dateFin, 'YYYY-MM').endOf('month') : moment();
187+
stat.restrictions = await this.dataSource.query(`
188+
SELECT r
189+
FROM statistic_commune,
190+
jsonb_array_elements(restrictions) AS r
191+
WHERE statistic_commune.id = $1
192+
AND (r->>'date')::date BETWEEN $2 AND $3
193+
`, [stat.id, dateBegin.format('YYYY-MM-DD'), dateEnd.format('YYYY-MM-DD')]);
194+
stat.restrictions = stat.restrictions.map(r => r.r);
190195
}
191196
return stat;
192197
}

0 commit comments

Comments
 (0)