File size: 1,920 Bytes
d687dbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from ast import Dict, List
from dataclasses import dataclass
import datetime

future_exit_time = datetime.datetime(9999, 12, 31, 23, 59, 59)
@dataclass
class Event:
    id: int
    trailer_id: int
    facility_id: int
    entrance_time: datetime
    exit_time: datetime
    @classmethod
    def from_dict(cls, data: Dict):
        return cls(
            id=data["id"],
            trailer_id=data["trailer_id"],
            facility_id =data["facility_id"],
            entrance_time = datetime.fromisoformat(data["entrance_time"]),
            exit_time = (datetime.fromisoformat(data["exit_time"]) if data["exit_time"] != None else future_exit_time),
        )


from dataclasses import dataclass, field
from typing import List
from datetime import datetime

@dataclass
class TimeWindow:
    start_time: datetime
    exit_time: datetime
    events: List[Event] = field(default_factory=list)

    @classmethod
    def from_event(cls, event: Event):
        return cls(
            start_time=event.entrance_time,
            exit_time=event.exit_time,
            events=[event]
        )

    def get_events_count(self):
        return len(self.events)
    
    def has_overlap_with_event(self, event: Event):
        # when the event overlaps with the window from the left
        if event.exit_time >=self.start_time and event.entrance_time <=self.start_time:
            return True
        # when the event overlaps with the window from the right
        elif event.entrance_time <=self.exit_time and event.exit_time >=self.exit_time:
            return True
        # when the event is completely inside the window
        elif event.entrance_time >=self.start_time and event.exit_time <=self.exit_time:
            return True
        else:
            return False
        
    def is_exact_match(self, event: Event):
        return event.entrance_time == self.start_time and event.exit_time == self.exit_time