Skip to content

Commit 0cb8e84

Browse files
committed
Optimisation méthode getInterval
1 parent 63262c7 commit 0cb8e84

File tree

1 file changed

+14
-33
lines changed

1 file changed

+14
-33
lines changed

src/model/Events.js

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { PouchDBCollection } from './PouchDBCollection.js'
2-
import { findLast, sortBy } from 'lodash'
2+
import { findLast, sortBy, dropWhile } from 'lodash'
33
import { DateTime } from 'luxon'
44
import { toDateTime } from '@/helpers/dates.js'
55

@@ -34,53 +34,34 @@ export class EventsCollection extends PouchDBCollection {
3434
* @param {any} start
3535
* @param {any} end
3636
*/
37-
async getInterval(userId, start, end) {
37+
async getInterval(userId, start, end, { groupEvents = true } = {}) {
3838
start = toDateTime(start)
3939
end = toDateTime(end)
40+
41+
// Charge les évènements jusqu'à 7 jours avant le début de la plage
4042
let events = await this.findUserEvents(userId, start.minus({ days: 7 }), end)
4143

4244
if (!events.length) {
4345
return []
4446
}
4547

46-
const startISO = start.toISO()
47-
const endISO = end.toISO()
48+
// Les évènements sont triés par date croissante avec les rotations avant les SV
49+
events = dropWhile(events, evt => toDateTime(evt.end) < start)
4850

49-
// possibilité d'optimiser en utilisant une boucle s'arrêtant dès que evt.start >= start
50-
let hasOverlap
51-
events = events.filter(evt => {
52-
if (evt.tag === 'rotation' && evt.start < startISO && evt.end >= startISO) {
53-
hasOverlap = evt
54-
// console.log('isRotation before')
55-
return true
56-
}
57-
if (evt.end >= startISO) {
58-
// console.log('end after start')
59-
return true
60-
}
61-
if (hasOverlap) {
62-
// console.log('is duty in before start')
63-
return evt?.rotationId === hasOverlap._id
64-
}
65-
return false
66-
})
67-
68-
const hasOverlapEnd = findLast(events, evt => evt.tag === 'rotation' && evt.end > endISO)
69-
if (hasOverlapEnd) { // Ajouter les SV manquants
70-
const lastSvs = await this.findUserEvents(userId, end, hasOverlapEnd.end, { inclusive_start: false })
71-
.filter(evt => {
72-
return evt.rotationId === hasOverlapEnd._id
73-
})
51+
const rotationOverlappingEnd = findLast(events, evt => evt.tag === 'rotation' && toDateTime(evt.end) > end)
52+
if (rotationOverlappingEnd) { // Ajouter les SV manquants
53+
const lastSvs = (await this.findUserEvents(userId, end, rotationOverlappingEnd.end, { inclusive_start: false }))
54+
.filter(evt => evt.rotationId === rotationOverlappingEnd._id)
7455
.toArray()
75-
if (lastSvs && lastSvs.length) {
56+
if (lastSvs?.length) {
7657
events.push(...lastSvs)
7758
}
7859
}
7960

80-
const groupedEvents = this.constructor.groupEvents(events)
81-
console.log(`%cEvents.getInterval%c : ${userId}`, 'color:teal', 'color:inherit', groupedEvents, hasOverlap, hasOverlapEnd)
61+
events = groupEvents ? this.constructor.groupEvents(events) : events
62+
console.log(`%cEvents.getInterval%c : ${userId}-${start.toISO()}-${end.toISO()}`, 'color:teal', 'color:inherit', events)
8263

83-
return groupedEvents
64+
return events
8465
}
8566

8667
// Liste les évènements dont le début est compris entre 'start' et 'end'

0 commit comments

Comments
 (0)