Passed
Push — master ( 4fc02b...d65ad0 )
by Antonio
03:34
created

build.db.models   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
eloc 85
dl 0
loc 117
ccs 54
cts 54
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A DocumentBaseModel.dict() 0 8 5
A EVCBaseDoc.projection() 0 39 1
1
"""Models for Mongo DB"""
2
# pylint: disable=no-self-use,unused-argument,invalid-name,unused-argument
3
# pylint: disable=no-self-argument,no-name-in-module
4
5 1
from datetime import datetime
6 1
from typing import Dict, List, Optional
7
8 1
from pydantic import BaseModel, Field
9
10
11 1
class DocumentBaseModel(BaseModel):
12
    """Base model for Mongo documents"""
13
14 1
    id: str = Field(None, alias="_id")
15 1
    inserted_at: Optional[datetime]
16 1
    updated_at: Optional[datetime]
17
18 1
    def dict(self, **kwargs) -> Dict:
19
        """Return a dictionary representation of the model"""
20 1
        values = super().dict(**kwargs)
21 1
        if "id" in values and values["id"]:
22 1
            values["_id"] = values["id"]
23 1
        if "exclude" in kwargs and "_id" in kwargs["exclude"]:
24 1
            del values["_id"]
25 1
        return values
26
27
28 1
class CircuitScheduleDoc(BaseModel):
29
    """EVC circuit schedule model"""
30
31 1
    id: str
32 1
    date: Optional[str]
33 1
    frequency: Optional[str]
34 1
    interval: Optional[int]
35 1
    action: str
36
37
38 1
class TAGDoc(BaseModel):
39
    """TAG model"""
40 1
    tag_type: int
41 1
    value: int
42
43
44 1
class UNIDoc(BaseModel):
45
    """UNI model"""
46
47 1
    tag: Optional[TAGDoc]
48 1
    interface_id: str
49
50
51 1
class EVCBaseDoc(DocumentBaseModel):
52
    """Base model for EVC documents"""
53
54 1
    uni_a: UNIDoc
55 1
    uni_z: UNIDoc
56 1
    name: str
57 1
    request_time: Optional[datetime]
58 1
    start_date: Optional[datetime]
59 1
    end_date: Optional[datetime]
60 1
    queue_id: Optional[int]
61 1
    bandwidth: int = 0
62 1
    primary_path: Optional[List]
63 1
    backup_path: Optional[List]
64 1
    current_path: Optional[List]
65 1
    primary_links: Optional[List]
66 1
    backup_links: Optional[List]
67 1
    backup_links: Optional[List]
68 1
    dynamic_backup_path: bool
69 1
    creation_time: datetime
70 1
    owner: Optional[str]
71 1
    priority: int
72 1
    circuit_scheduler: List[CircuitScheduleDoc]
73 1
    archived: bool = False
74 1
    metadata: Optional[Dict] = None
75 1
    active: bool
76 1
    enabled: bool
77
78 1
    @staticmethod
79 1
    def projection() -> Dict:
80
        """Base projection of EVCBaseDoc model."""
81 1
        time_fmt = "%Y-%m-%dT%H:%M:%S"
82 1
        return {
83
            "_id": 0,
84
            "id": 1,
85
            "uni_a": 1,
86
            "uni_z": 1,
87
            "name": 1,
88
            "bandwidth": 1,
89
            "primary_path": 1,
90
            "backup_path": 1,
91
            "current_path": 1,
92
            "dynamic_backup_path": 1,
93
            "priority": 1,
94
            "circuit_scheduler": 1,
95
            "archived": 1,
96
            "metadata": 1,
97
            "active": 1,
98
            "enabled": 1,
99
            "owner": {"$ifNull": ["$owner", None]},
100
            "queue_id": {"$ifNull": ["$queue_id", None]},
101
            "primary_links": {"$ifNull": ["$primary_links", []]},
102
            "backup_links": {"$ifNull": ["$backup_links", []]},
103
            "start_date": {"$dateToString": {
104
                "format": time_fmt, "date": "$start_date"
105
            }},
106
            "creation_time": {"$dateToString": {
107
                "format": time_fmt, "date": "$creation_time"
108
            }},
109
            "request_time": {"$dateToString": {
110
                "format": time_fmt, "date": {
111
                    "$ifNull": ["$request_time", "$inserted_at"]
112
                }
113
            }},
114
            "end_date": {"$dateToString": {
115
                "format": time_fmt, "date": {
116
                    "$ifNull": ["$end_date", None]
117
                }
118
            }},
119
        }
120