This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
1265 lines (989 loc) · 38.7 KB
/
database.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""This is the class handling the Database. More to come."""
from dependencies.entities import (
User,
Role,
MachineType,
Machine,
Maintenance,
Intervention,
Use,
)
from dependencies.exceptions import (
DuplicatedIdException,
InvalidIdException,
InvalidQueryException,
)
from time import time
from pymongo import MongoClient, DESCENDING
from pymongo.errors import DuplicateKeyError
import toml
class DatabaseBackend:
"""Class handling the connection from and to the database."""
def __init__(self, settings_path="settings.toml") -> None:
"""Create instance of Database.
Args:
settings_path (str, optional): TOML file settings. Defaults to "settings.toml".
"""
self._settings_path = settings_path
self._client = None
self._db = None
# dict of collections and its relative unique index
self._collections = {
"roles": "role_id",
"machine_types": "type_id",
"users": "user_id",
"machines": "machine_id",
"maintenances": "maintenance_id",
"interventions": "intervention_id",
"uses": "use_id",
}
self._loadSettings()
self._connect()
self._initCollections()
def _loadSettings(self) -> None:
"""Load settings from TOML file."""
self._settings = toml.load(self._settings_path)
self._url = self._settings["database"]["url"]
self._name = self._settings["database"]["name"]
def _connect(self) -> None:
"""Connect to MongoDB server."""
self._client = MongoClient(self._url)
self._db = self._client[self._name]
def _initCollections(self) -> None:
"""Initialize the collections in the database."""
collections = self._db.list_collection_names()
for c, i in self._collections.items():
if c not in collections:
self._db[c].create_index(i, unique=True)
def _queryCollection(self, collection_name: str) -> list[dict]:
"""Query a collection and return the dict without the MongoDB mandatory _id.
Args:
collection_name (str): name of the collection
Returns:
list[dict]: list of dicts as returned from MongoDB
"""
collection = self._db[collection_name]
return list(collection.find(projection={"_id": 0}))
def _getUserIdFromCard(self, card_UUID: str = None) -> int:
collection = self._db["users"]
result = collection.find_one({"card_UUID": card_UUID})
return result["user_id"]
def _validateRole(self, role_id: int) -> bool:
collection = self._db["roles"]
return collection.count_documents({"role_id": role_id}) == 1
def _validateMachineType(self, type_id: int) -> bool:
collection = self._db["machine_types"]
return collection.count_documents({"type_id": type_id}) == 1
def _validateUser(self, user_id: int) -> bool:
collection = self._db["users"]
return collection.count_documents({"user_id": user_id}) == 1
def _validateMachine(self, machine_id: int) -> bool:
collection = self._db["machines"]
return collection.count_documents({"machine_id": machine_id}) == 1
def _validateMaintenance(self, maintenance_id: int) -> bool:
collection = self._db["maintenances"]
return collection.count_documents({"maintenance_id": maintenance_id}) == 1
def _isUserCurrentlyUsing(self, machine_id: int, user_id: int) -> bool:
collection = self._db["uses"]
return (
collection.count_documents(
{"user_id": user_id, "machine_id": machine_id, "end_timestamp": None}
)
> 0
)
class DatabaseHelper(DatabaseBackend):
def __init__(self, settings_path="settings.toml") -> None:
super().__init__(settings_path)
def getUserUses(self, user_id: int = None, card_UUID: str = None) -> list[Use]:
"""Return uses from a certain user.
Args:
user_id (int, optional): id of a User. Not necessary if card_UUID is passed
card_UUID (str, optional): card_UUID of a User. Not necessary if user_id is passed
Returns:
list[Use]
"""
if not user_id:
user_id = self._getUserIdFromCard(card_UUID=card_UUID)
if not self._validateUser(user_id=user_id):
raise InvalidIdException("User", user_id)
collection = self._db["uses"]
results = collection.find({"user_id": user_id}, projection={"_id": 0})
return [Use.from_dict(r) for r in results]
def isMachineCurrentlyUsed(self, machine_id: int) -> bool:
"""Return True if the machine is currently being used, False otherwise.
Args:
machine_id (int): id of the Machine
Returns:
bool
"""
if not self._validateMachine(machine_id=machine_id):
raise InvalidIdException("Machine", machine_id)
collection = self._db["uses"]
return (
collection.count_documents(
{"machine_id": machine_id, "end_timestamp": None}
)
== 1
)
def getCurrentlyUsedMachines(self) -> list[Machine]:
"""Get a list of the Machine that are being used in this moment.
Returns:
list[Machine]
"""
collection = self._db["uses"]
results = collection.find({"end_timestamp": None}, projection={"_id": 0})
return [Machine.from_dict(r["machine_id"]) for r in results]
def getUserTotalTime(self, user_id: int) -> float:
"""Return total time the User has used the machines.
Args:
user_id (int): id of the User
Returns:
float: User time in seconds
"""
if not self._validateUser(user_id=user_id):
raise InvalidIdException("User", user_id)
collection = self._db["uses"]
uses = collection.find(
{"user_id": user_id, "end_timestamp": {"$exists": True, "$ne": None}}
)
total = 0
for u in uses:
total += u["end_timestamp"] - u["start_timestamp"]
return total
def isUserAuthroizedForMachine(
self, machine_id: int, user_id: int = None, card_UUID: str = None
) -> bool:
"""Return True if a User is authorized to use a Machine.
Args:
machine_id (int): id of the Machine
user_id (int, optional): id of a User. Not necessary if card_UUID is passed
card_UUID (str, optional): card_UUID of a User. Not necessary if user_id is passed
Returns:
bool
"""
if user_id is None and card_UUID is None:
raise InvalidQueryException("User", "user id or card UUID")
if not self._validateMachine(machine_id=machine_id):
raise InvalidIdException("Machine id", machine_id)
if user_id is None:
user_id = self.getUserFromCardUUID(card_UUID=card_UUID).user_id
if not self._validateUser(user_id=user_id):
raise InvalidIdException("User id", user_id)
role = self.getUserRole(user_id)
if role.authorize_all:
return True
machine_type = self.getMachineMachineType(machine_id)
authorizations = self.getUserAuthorization(user_id)
return any(a.type_id == machine_type for a in authorizations)
def getUserFromCardUUID(self, card_UUID: str) -> User:
"""Return user from the UUID of its card.
Args:
card_UUID (str): UUID of the card
Returns:
User
"""
collection = self._db["users"]
result = collection.find_one({"card_UUID": card_UUID}, projection={"_id": 0})
if not result:
raise InvalidQueryException("Card UUID", card_UUID)
return User.from_dict(result)
class DatabaseInterface(DatabaseHelper):
def __init__(self, settings_path="settings.toml") -> None:
super().__init__(settings_path)
def dropCollections(self) -> None:
"""Drop all colletcions in database. Needless to say that this is pretty dangerous."""
for c in self._db.list_collection_names():
self._db.drop_collection(c)
def addRole(
self, role_id: int, role_name: str, authorize_all: bool = False
) -> None:
"""Add a Role to the database.
Args:
role_id (int): id of the Role
role_name (str): name of the Role
"""
collection = self._db["roles"]
r = Role(role_id=role_id, role_name=role_name, authorize_all=authorize_all)
try:
collection.insert_one(r.serialize())
except DuplicateKeyError:
raise DuplicatedIdException("Role", role_id)
def getRole(self, role_id: int) -> Role:
"""Return Role by its id.
Args:
role_id (int): id of the Role
Returns:
Role
"""
collection = self._db["roles"]
result = collection.find_one(
{"role_id": role_id}, projection={"_id": 0, "role_id": 1}
)
if result is None:
raise InvalidIdException("Role", role_id)
for r in self.listRoles():
if r.role_id == result["role_id"]:
return r
def editRoleName(self, role_id: int, new_role_name: str) -> None:
"""Change the name of a Role.
Args:
role_id (int): id of the Role
new_role_name (str): new name of the Role
"""
r = Role(role_id, new_role_name)
collection = self._db["roles"]
collection.update_one(
{"role_id": role_id}, {"$set": {"role_name": r.role_name}}
)
def removeRole(self, role_id: int) -> None:
"""Remove a Role from the database.
Args:
role_id (int): id of the Role
"""
collection = self._db["roles"]
collection.delete_one({"role_id": role_id})
def listRoles(self) -> list[Role]:
"""List all Roles in the database.
Returns:
list[Role]: list of all Roles
"""
return [Role.from_dict(r) for r in self._queryCollection("roles")]
def addMachineType(self, type_id: int, type_name: str) -> None:
"""Add a MachineType to the database.
Args:
type_id (int): id of the MachineType
type_name (str): name of the MachineType
"""
collection = self._db["machine_types"]
t = MachineType(type_id=type_id, type_name=type_name)
try:
collection.insert_one(t.serialize())
except DuplicateKeyError:
raise DuplicatedIdException("Type", type_id)
def getMachineType(self, type_id: int) -> MachineType:
"""Return MachineType by it id.
Args:
type_id (int): id of the MachineType
Returns:
MachineType
"""
collection = self._db["machine_types"]
result = collection.find_one(
{"type_id": type_id}, projection={"_id": 0, "type_id": 1}
)
if result is None:
raise InvalidIdException("MachineType", type_id)
for t in self.listMachineTypes():
if t.type_id == result["type_id"]:
return t
def editMachineTypeName(self, type_id: int, new_type_name: str) -> None:
"""Change the name of a MachineType.
Args:
type_id (int): id of the MachineType
new_type_name (str): new name of the type
"""
t = MachineType(type_id, new_type_name)
collection = self._db["machine_types"]
collection.update_one(
{"type_id": type_id}, {"$set": {"type_name": t.type_name}}
)
def removeMachineType(self, type_id: int) -> None:
"""Remove a MachineType from the database.
Args:
type_id (int): id of the MachineType
"""
collection = self._db["machine_types"]
collection.delete_one({"type_id": type_id})
def listMachineTypes(self) -> list[MachineType]:
"""List all Machine types in the database.
Returns:
list[MachineType]: list of all Machine types
"""
return [
MachineType.from_dict(t) for t in self._queryCollection("machine_types")
]
def addUser(
self,
name: str,
surname: str,
role_id: int,
user_id: int = None,
card_UUID: str = None,
) -> int:
"""Add a User to the database.
Args:
name (str): Name of the User
surname (str): Surname of the User
role_id (int): id of the Role.
user_id (int, optional): id of the User, optional. Will be assigned automatically.
card_UUID (str, optional): UUID of the RFID card.
Returns:
int: id of the User
"""
if not self._validateRole(role_id=role_id):
raise InvalidIdException("Role id", role_id)
collection = self._db["users"]
if user_id is None:
if collection.count_documents({}) == 0:
user_id = 0
else:
# find the highest User id and add 1
last = list(collection.find({}).sort("user_id", DESCENDING).limit(1))[0]
user_id = last["user_id"] + 1
u = User(
user_id=user_id,
name=name,
surname=surname,
role_id=role_id,
card_UUID=card_UUID,
)
try:
collection.insert_one(u.serialize())
return user_id
except DuplicateKeyError:
raise DuplicatedIdException("User", user_id)
def listUsers(self) -> list[User]:
"""Return all User in database.
Returns:
list[User]: list of users
"""
return [User.from_dict(u) for u in self._queryCollection("users")]
def getUser(
self, user_id: int = None, user_name: str = None, user_surname: str = None
) -> User:
"""Return a User from its user_id or its combination of name and surname.
Args:
user_id (int, optional): id of the User. Not necessary if name and surnames are provided
user_name (str, optional): name of the User. Must be passed along the surname. \
Not necessary if id is provided.
user_surname (str, optional): surname of the User. Must be passed along the name. \
Not necessary if id is provided.
Returns:
User
"""
collection = self._db["users"]
result = None
if user_name and user_surname:
result = collection.find_one(
{"name": user_name, "surname": user_surname}, projection={"_id": 0}
)
elif user_id is not None:
result = collection.find_one({"user_id": user_id}, projection={"_id": 0})
if result is None:
raise InvalidIdException("User", user_id)
return User.from_dict(result)
def setUserRole(
self,
user_id: int,
role_id: int,
) -> None:
"""Set the Role of a User.
Args:
user_id (int): id of the User
role_id (int): id of the Role
"""
if not self._validateRole(role_id=role_id):
raise InvalidIdException("Role id", role_id)
collection = self._db["users"]
collection.update_one({"user_id": user_id}, {"$set": {"role_id": role_id}})
def getUserRole(self, user_id: int) -> Role:
"""Return the Role of a User.
Args:
user_id (int): id of the User
Returns:
Role
"""
collection = self._db["users"]
result = collection.find_one(
{"user_id": user_id}, projection={"_id": 0, "role_id": 1}
)
if result is None:
raise InvalidIdException("User", user_id)
for r in self.listRoles():
if r.role_id == result["role_id"]:
return r
def setUserId(
self,
user_id: int,
new_id: int,
) -> None:
"""Set the id of a User.
Args:
user_id (int): id of the User
new_id (int): new id of the User
"""
collection = self._db["users"]
collection.update_one({"user_id": user_id}, {"$set": {"user_id": new_id}})
def getUserId(self, name: str, surname: str) -> int:
"""Get a User id from its name and surname.
Args:
name (str): Name of the User.
surname (str): Surname of the User
Returns:
int: User id
"""
collection = self._db["users"]
result = collection.find_one(
{"name": name, "surname": surname}, projection={"_id": 0, "user_id": 1}
)
if result:
return result["user_id"]
raise InvalidQueryException("User name and surname", f"{name} {surname}")
def setUserAuthorization(
self,
user_id: int,
authorization_ids: list[int],
) -> None:
"""Set the authorization for an User.
Args:
user_id (int): id of the User
authorization_ids (list[int]): Machine types that the User is authorized to use
"""
if not isinstance(authorization_ids, list):
raise InvalidQueryException("Authorization ids", authorization_ids)
machine_types = [m.type_id for m in self.listMachineTypes()]
for i in authorization_ids:
if i not in machine_types:
raise InvalidIdException("MachineType", i)
collection = self._db["users"]
collection.update_one(
{"user_id": user_id}, {"$set": {"authorization_ids": authorization_ids}}
)
def getUserAuthorization(self, user_id: int) -> list[MachineType]:
"""Return the authorization of a User.
Args:
user_id (int): id of the User
Returns:
list[MachineType]: list of Machine types the User is authorized to use
"""
collection = self._db["users"]
result = collection.find_one(
{"user_id": user_id}, projection={"_id": 0, "authorization_ids": 1}
)
if result is None:
raise InvalidIdException("User", user_id)
if not result["authorization_ids"]:
return []
authorizations = []
for t in self.listMachineTypes():
if t.type_id in result["authorization_ids"]:
authorizations.append(t)
return authorizations
def setUserCardUUID(
self,
user_id: int,
card_UUID: str,
):
"""Set the UUID of the RFID card for a User.
Args:
user_id (int): id of the User
card_UUID (str): UUID of the RFID card
"""
collection = self._db["users"]
collection.update_one({"user_id": user_id}, {"$set": {"card_UUID": card_UUID}})
def getUserCardUUID(self, user_id: int) -> str:
"""Get the UUID of the card of a User according to its id.
Args:
user_id (int): id of the User
Returns:
str: UUID of the RFID card
"""
collection = self._db["users"]
result = collection.find_one(
{"user_id": user_id}, projection={"_id": 0, "card_UUID": 1}
)
if result is None:
raise InvalidIdException("User", user_id)
return result["card_UUID"]
def addMachine(
self,
machine_id: int,
machine_name: str,
machine_type: int,
machine_hours: float = 0,
) -> None:
"""Add a Machine to the database.
Args:
machine_id (int): id of the Machine
machine_name (str): name of the Machine
machine_type (int): type of the Machine
machine_hours (float, optional): Hours the Machine has been used. Defaults to 0.
"""
collection = self._db["machines"]
if not self._validateMachineType(type_id=machine_type):
raise InvalidQueryException("Machine type", machine_type)
m = Machine(
machine_id=machine_id,
machine_name=machine_name,
machine_type=machine_type,
machine_hours=machine_hours,
)
try:
collection.insert_one(m.serialize())
except DuplicateKeyError:
raise DuplicatedIdException("Machine", machine_id)
def getMachine(self, machine_id: int) -> Machine:
"""Return Machine by its id.
Args:
machine_id (int): id of the Machine
Returns:
Machine
"""
collection = self._db["machines"]
result = collection.find_one(
{"machine_id": machine_id}, projection={"_id": 0, "machine_id": 1}
)
if result is None:
raise InvalidIdException("Machine", machine_id)
for m in self.listMachines():
if m.machine_id == result["machine_id"]:
return m
def removeMachine(self, machine_id: int) -> None:
"""Remove a Machine from the database.
Args:
machine_id (int): id of the Machine
"""
collection = self._db["machines"]
collection.delete_one({"machine_id": machine_id})
def listMachines(self) -> list[Machine]:
"""List all the machines in the database.
Returns:
list[Machine]
"""
return [Machine.from_dict(m) for m in self._queryCollection("machines")]
def getMachineHours(self, machine_id: int) -> float:
"""Get the Machine hours of a certain Machine according to its id.
Args:
machine_id (int): the id of the Machine
Returns:
float: Machine hours
"""
collection = self._db["machines"]
result = collection.find_one({"machine_id": machine_id})
if result is None:
raise InvalidIdException("Machine", machine_id)
return result["machine_hours"]
def setMachinesHours(self, machine_id: int, machine_hours: float) -> None:
"""Set the Machine hours of a particular Machine.
Args:
machine_id (int): id of the Machine
machine_hours (float): hours the Machine has run
"""
collection = self._db["machines"]
collection.update_one(
{"machine_id": machine_id}, {"$set": {"machine_hours": machine_hours}}
)
def getMachineMachineType(self, machine_id: int) -> int:
"""Get the type of the Machine according to its id.
Args:
machine_id (int): Machine id
Returns:
int: MachineType id
"""
collection = self._db["machines"]
result = collection.find_one({"machine_id": machine_id})
if result is None:
raise InvalidIdException("Machine", machine_id)
return result["machine_type"]
def setMachineMachineType(self, machine_id: int, machine_type: int) -> None:
"""Set the type of a Machine.
Args:
machine_id (int): id of the Machine
machine_type (int): MachineType id
"""
if machine_type not in [t.type_id for t in self.listMachineTypes()]:
raise InvalidIdException("MachineType", machine_type)
collection = self._db["machines"]
collection.update_one(
{"machine_id": machine_id}, {"$set": {"machine_type": machine_type}}
)
def getMachineName(self, machine_id: int) -> str:
"""Return the name of a Machine.
Args:
machine_id (int): id of the Machine
Returns:
str: name of the Machine
"""
collection = self._db["machines"]
result = collection.find_one({"machine_id": machine_id})
if result is None:
raise InvalidIdException("Machine", machine_id)
return result["machine_name"]
def setMachineName(self, machine_id: int, new_machine_name: str) -> None:
"""Set a new name for a Machine.
Args:
machine_id (int): id of the Machine
new_machine_name (str): new name of the Machine
"""
collection = self._db["machines"]
collection.update_one(
{"machine_id": machine_id}, {"$set": {"machine_name": new_machine_name}}
)
def addMachineMaintenance(self, machine_id: int, maintenance_id: int) -> None:
"""Add a Maintenance for a Machine.
Args:
machine_id (int): id of the Machine
maintenance_id (int): id of the Maintenance
"""
if not self._validateMaintenance(maintenance_id=maintenance_id):
raise InvalidIdException("Maintenance", maintenance_id)
if not self._validateMachine(machine_id=machine_id):
raise InvalidIdException("Machine", machine_id)
collection = self._db["machines"]
result = collection.find_one({"machine_id": machine_id})
if not result["maintenances"]:
collection.update_one(
{"machine_id": machine_id},
{"$set": {"maintenances": [maintenance_id]}},
)
else:
collection.update_one(
{"machine_id": machine_id},
{"$push": {"maintenances": maintenance_id}},
)
def getMachineMaintenances(self, machine_id: int) -> list[Maintenance]:
"""Get Maintenances for a Machine.
Args:
machine_id (int): id of the Machine
Returns:
list[Maintenance]: list of Maintenances
"""
collection = self._db["machines"]
result = collection.find_one({"machine_id": machine_id})
if result is None:
raise InvalidIdException("Machine", machine_id)
return [
m
for m in self.listMaintenances()
if m.maintenance_id in result["maintenances"]
]
def removeMachineMaintenance(self, machine_id: int, maintenance_id: int) -> None:
"""Remove a Maintenance from a Machine.
Args:
machine_id (int): id of the Machine
maintenance_id (int): id of the Maintenances
"""
if not self._validateMaintenance(maintenance_id=maintenance_id):
raise InvalidIdException("Maintenance", maintenance_id)
if not self._validateMachine(machine_id=machine_id):
raise InvalidIdException("Machine", machine_id)
collection = self._db["machines"]
result = collection.find_one({"machine_id": machine_id})
if len(result["maintenances"]) == 0:
raise InvalidIdException("Machine", machine_id)
collection.update_one(
{"machine_id": machine_id},
{"$pull": {"maintenances": maintenance_id}},
)
def addMaintenance(
self, hours_between: float, description: str = None, maintenance_id: int = None
) -> int:
"""Add a Maintenance to database and return its IDS.
Args:
hours_between (float): hours between consecutive interventions
description (str, optional): Description of the Maintenance. Defaults to None.
maintenance_id (int, optional): id of the Maintenance. Automatically assigned if \
not passed.
Returns:
int: id of the Maintenance
"""
collection = self._db["maintenances"]
if maintenance_id is None:
if collection.count_documents({}) == 0:
maintenance_id = 0
else:
# find the highest User id and add 1
last = list(
collection.find({}).sort("maintenance_id", DESCENDING).limit(1)
)[0]
maintenance_id = last["maintenance_id"] + 1
m = Maintenance(
maintenance_id=maintenance_id,
hours_between=hours_between,
description=description,
)
try:
collection.insert_one(m.serialize())
except DuplicateKeyError:
raise DuplicatedIdException("Maintenance", maintenance_id)
return maintenance_id
def getMaintenance(self, maintenance_id: int) -> Maintenance:
"""Return Maintenance by its id.
Args:
maintenance_id (int): id of the Maintenance
Returns:
Role
"""
collection = self._db["maintenances"]
result = collection.find_one(
{"maintenance_id": maintenance_id},
)
if result is None:
raise InvalidIdException("Maintenance", maintenance_id)
for m in self.listMaintenances():
if m.maintenance_id == result["maintenance_id"]:
return m
def removeMaintenance(self, maintenance_id: int) -> None:
"""Remove a maintenance according to it id.
Args:
maintenance_id (int): id of the Maintenance
"""
collection = self._db["maintenances"]
collection.delete_one({"maintenance_id": maintenance_id})
def getMaintenanceDescription(self, maintenance_id: int) -> str:
"""Get a Maintenance description according to its id.
Args:
maintenance_id (int): id of the Maintenance
Returns:
str: description of the Maintenance
"""
collection = self._db["maintenances"]
result = collection.find_one(
{"maintenance_id": maintenance_id},
)
if result is None:
raise InvalidIdException("Maintenance", maintenance_id)
return result["description"]
def setMaintenanceDescription(
self, maintenance_id: int, new_maintenance_description: str
) -> None:
"""Set the description for a Maintenance.
Args:
maintenance_id (int): id of the Maintenance
new_maintenance_description (str): new Maintenance description
"""
collection = self._db["maintenances"]
collection.update_one(
{"maintenance_id": maintenance_id},
{"$set": {"description": new_maintenance_description}},
)
def getMaintenanceHoursBetween(self, maintenance_id: int) -> float:
"""Get hours between a Maintenance.
Args:
maintenance_id (int): id of the Maintenance
Returns:
float: hours between a Maintenance
"""
collection = self._db["maintenances"]
result = collection.find_one(
{"maintenance_id": maintenance_id},
)
if result is None:
raise InvalidIdException("Maintenance", maintenance_id)
return result["hours_between"]
def setMaintenanceHoursBetween(
self, maintenance_id: int, new_hours_between: float
) -> None:
"""Set hours between Maintenances.
Args: