Passed
Pull Request — master (#166)
by Italo Valcy
03:51
created

build.db.models   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
eloc 86
dl 0
loc 114
ccs 55
cts 55
cp 1
rs 10
c 0
b 0
f 0

2 Methods

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