Test Failed
Pull Request — master (#64)
by
unknown
03:02
created

MaintenanceController.get_window()   A

Complexity

Conditions 2

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
"""MaintenanceController."""
2
3
# pylint: disable=invalid-name
4
import os
5
6
from bson.codec_options import CodecOptions
7
import pymongo
8
from pymongo.errors import AutoReconnect
9
from tenacity import retry_if_exception_type, stop_after_attempt, wait_random
10
11
from kytos.core import log
12
from kytos.core.db import Mongo
13
from kytos.core.retry import before_sleep, for_all_methods, retries
14
from napps.kytos.maintenance.models import MaintenanceWindow, MaintenanceID
15
16
17
@for_all_methods(
18
    retries,
19
    stop=stop_after_attempt(
20
        int(os.environ.get("MONGO_AUTO_RETRY_STOP_AFTER_ATTEMPT", 3))
21
    ),
22
    wait=wait_random(
23
        min=int(os.environ.get("MONGO_AUTO_RETRY_WAIT_RANDOM_MIN", 0.1)),
24
        max=int(os.environ.get("MONGO_AUTO_RETRY_WAIT_RANDOM_MAX", 1)),
25
    ),
26
    before_sleep=before_sleep,
27
    retry=retry_if_exception_type((AutoReconnect,)),
28
)
29
class MaintenanceController:
30
    """MaintenanceController."""
31
32
    def __init__(self, get_mongo=lambda: Mongo()) -> None:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable Mongo does not seem to be defined.
Loading history...
33
        """Constructor of MaintenanceController."""
34
        self.mongo = get_mongo()
35
        self.db_client = self.mongo.client
36
        self.db = self.db_client[self.mongo.db_name]
37
        self.windows = self.db['maintenance.windows'].with_options(
38
            codec_options=CodecOptions(
39
                tz_aware=True,
40
            )
41
        )
42
43
    def bootstrap_indexes(self) -> None:
44
        """Bootstrap all maintenance related indexes."""
45
        index_tuples = [
46
            ("maintenance.windows", [("id", pymongo.ASCENDING)]),
47
        ]
48
        for collection, keys in index_tuples:
49
            if self.mongo.bootstrap_index(collection, keys):
50
                log.info(
51
                    f"Created DB index {keys}, collection: {collection})"
52
                )
53
54
    def add_window(self, window: MaintenanceWindow):
55
        self.windows.insert_one(window.dict())
56
57
    def get_window(self, mw_id: MaintenanceID) -> MaintenanceWindow:
58
        window = self.windows.find_one(
59
            {'id': mw_id},
60
            projection = {'_id': False}
61
        )
62
        if window is not None:
63
            window = MaintenanceWindow.construct(**window)
64
        return window
65
66
    def udpate_window(self, window: MaintenanceWindow):
67
        self.windows.update_one(
68
            {'id': window.id},
69
            {'$set': window.dict()}
70
        )
71
72
    def get_windows(self) -> list[MaintenanceWindow]:
73
        windows = self.windows.find(projection={'_id': False})
74
        return list(windows)
75
76
    def remove_window(self, mw_id: MaintenanceID):
77
        self.windows.delete_one({'id': mw_id})