Passed
Pull Request — master (#298)
by
unknown
04:09
created

build.controllers.ELineController.get_circuits()   B

Complexity

Conditions 6

Size

Total Lines 26
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6.1215

Importance

Changes 0
Metric Value
cc 6
eloc 23
nop 3
dl 0
loc 26
ccs 17
cts 20
cp 0.85
crap 6.1215
rs 8.3946
c 0
b 0
f 0
1
"""ELineController."""
2
# pylint: disable=unnecessary-lambda,invalid-name
3 1
import os
4 1
from datetime import datetime
5 1
from typing import Dict, Optional
6
7 1
import pymongo
8 1
from pymongo.collection import ReturnDocument
9 1
from pymongo.errors import AutoReconnect
10 1
from pymongo.operations import UpdateOne
11 1
from tenacity import retry_if_exception_type, stop_after_attempt, wait_random
12
13 1
from kytos.core import log
14 1
from kytos.core.db import Mongo
15 1
from kytos.core.retry import before_sleep, for_all_methods, retries
16 1
from napps.kytos.mef_eline.db.models import EVCBaseDoc
17
18
19 1
@for_all_methods(
20
    retries,
21
    stop=stop_after_attempt(
22
        int(os.environ.get("MONGO_AUTO_RETRY_STOP_AFTER_ATTEMPT", "3"))
23
    ),
24
    wait=wait_random(
25
        min=int(os.environ.get("MONGO_AUTO_RETRY_WAIT_RANDOM_MIN", "1")),
26
        max=int(os.environ.get("MONGO_AUTO_RETRY_WAIT_RANDOM_MAX", "1")),
27
    ),
28
    before_sleep=before_sleep,
29
    retry=retry_if_exception_type((AutoReconnect,)),
30
)
31 1
class ELineController:
32
    """E-Line Controller"""
33
34 1
    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...
35 1
        self.mongo = get_mongo()
36 1
        self.db_client = self.mongo.client
37 1
        self.db = self.db_client[self.mongo.db_name]
38
39 1
    def bootstrap_indexes(self) -> None:
40
        """Bootstrap mef_eline relaeted indexes."""
41 1
        index_tuples = [
42
            ("evcs", [("circuit_scheduler.id", pymongo.ASCENDING)]),
43
            ("evcs", [("archived", pymongo.ASCENDING)]),
44
        ]
45 1
        for collection, keys in index_tuples:
46 1
            if self.mongo.bootstrap_index(collection, keys):
47 1
                log.info(
48
                    f"Created DB index {keys}, collection: {collection}"
49
                )
50
51 1
    def get_circuits(self, archived: Optional[bool] = False,
52
                     metadata: dict = None) -> Dict:
53
        """Get all circuits from database."""
54 1
        aggregation = []
55 1
        options = {"null": None, "true": True, "false": False}
56 1
        match_filters = {"$match": {}}
57 1
        aggregation.append(match_filters)
58 1
        if archived is not None:
59 1
            archived = options.get(archived, False)
60 1
            match_filters["$match"]["archived"] = archived
61 1
        if metadata:
62 1
            for key in metadata:
63 1
                if "metadata." in key[:9]:
64 1
                    try:
65 1
                        match_filters["$match"][key] = int(metadata[key])
66
                    except ValueError:
67
                        item = metadata[key].lower()
68
                        match_filters["$match"][key] = options.get(item, item)
69 1
        print("AGREE -> ", aggregation)
70 1
        aggregation.extend([
71
                {"$sort": {"_id": 1}},
72
                {"$project": EVCBaseDoc.projection()},
73
            ]
74
        )
75 1
        circuits = self.db.evcs.aggregate(aggregation)
76 1
        return {"circuits": {value["id"]: value for value in circuits}}
77
78 1
    def get_circuit(self, circuit_id: str) -> Optional[Dict]:
79
        """Get a circuit."""
80
        return self.db.evcs.find_one({"_id": circuit_id},
81
                                     EVCBaseDoc.projection())
82
83 1
    def upsert_evc(self, evc: Dict) -> Optional[Dict]:
84
        """Update or insert an EVC"""
85 1
        utc_now = datetime.utcnow()
86 1
        model = EVCBaseDoc(
87
            **{
88
                **evc,
89
                **{"_id": evc["id"]}
90
            }
91
        )
92 1
        updated = self.db.evcs.find_one_and_update(
93
            {"_id": evc["id"]},
94
            {
95
                "$set": model.dict(exclude={"inserted_at"}, exclude_none=True),
96
                "$setOnInsert": {"inserted_at": utc_now},
97
            },
98
            return_document=ReturnDocument.AFTER,
99
            upsert=True,
100
        )
101 1
        return updated
102
103 1
    def update_evcs(self, circuit_ids: list, metadata: dict, action: str):
104
        """Update a bulk of EVC"""
105 1
        utc_now = datetime.utcnow()
106 1
        metadata = {f"metadata.{k}": v for k, v in metadata.items()}
107 1
        if action == "add":
108 1
            payload = {"$set": metadata}
109
        elif action == "del":
110
            payload = {"$unset": metadata}
111 1
        ops = []
112 1
        for _id in circuit_ids:
113 1
            ops.append(
114
                UpdateOne(
115
                    {"_id": _id},
116
                    {
117
                        **payload,
0 ignored issues
show
introduced by
The variable payload does not seem to be defined for all execution paths.
Loading history...
118
                        "$setOnInsert": {"inserted_at": utc_now}
119
                    },
120
                    upsert=False,
121
                )
122
            )
123
        return self.db.evcs.bulk_write(ops).modified_count
124