000_unset_active   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 65
dl 0
loc 104
rs 10
c 0
b 0
f 0
wmc 6

4 Functions

Rating   Name   Duplication   Size   Complexity  
A unset_switches_and_intfs() 0 14 1
A aggregate_unset_links() 0 20 2
A unset_links() 0 18 1
A aggregate_unset_switches_and_intfs() 0 18 2
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
4
import sys
5
import os
6
7
from kytos.core.db import Mongo
8
9
10
def unset_links(mongo: Mongo) -> None:
11
    """Unset links"""
12
    print(
13
        "Trying to $unset links 'active' and metadata[last_status_is_active|last_status_change|notified_up_at]..."
14
    )
15
    db = mongo.client[mongo.db_name]
16
    res = db.links.update_many(
17
        {},
18
        {
19
            "$unset": {
20
                "active": 1,
21
                "metadata.last_status_is_active": 1,
22
                "metadata.last_status_change": 1,
23
                "metadata.notified_up_at": 1,
24
            }
25
        },
26
    )
27
    print(f"Modified {res.modified_count} links objects")
28
29
30
def aggregate_unset_links(mongo: Mongo) -> None:
31
    """Aggregate unset links."""
32
    db = mongo.client[mongo.db_name]
33
    res = db.links.aggregate(
34
        [
35
            {
36
                "$unset": [
37
                    "active",
38
                    "metadata.last_status_is_active",
39
                    "metadata.last_status_change",
40
                    "metadata.notified_up_at",
41
                ]
42
            }
43
        ]
44
    )
45
    print(
46
        "Aggregating links $unset active and metadata[last_status_is_active|last_status_change|notified_up_at]"
47
    )
48
    for doc in res:
49
        print(doc)
50
51
52
def unset_switches_and_intfs(mongo: Mongo) -> None:
53
    """Unset switches and interfaces"""
54
    print("Trying to $unset switches and interfaces 'active'")
55
    db = mongo.client[mongo.db_name]
56
    res = db.switches.update_many(
57
        {},
58
        {
59
            "$unset": {
60
                "active": 1,
61
                "interfaces.$[].active": 1,
62
            }
63
        },
64
    )
65
    print(f"Modified {res.modified_count} switches objects")
66
67
68
def aggregate_unset_switches_and_intfs(mongo: Mongo) -> None:
69
    """Aggregate unset switches and interfaces."""
70
    db = mongo.client[mongo.db_name]
71
    res = db.switches.aggregate(
72
        [
73
            {
74
                "$unset": [
75
                    "active",
76
                    "interfaces.active",
77
                ]
78
            }
79
        ]
80
    )
81
    print(
82
        "Aggregating switches and interfaces $unset active"
83
    )
84
    for doc in res:
85
        print(doc)
86
87
88
if __name__ == "__main__":
89
    mongo = Mongo()
90
    cmds = {
91
        "aggregate_unset_links": aggregate_unset_links,
92
        "unset_links": unset_links,
93
        "aggregate_unset_switches_and_intfs": aggregate_unset_switches_and_intfs,
94
        "unset_switches_and_intfs": unset_switches_and_intfs,
95
    }
96
    try:
97
        cmd = os.environ["CMD"]
98
        cmds[cmd](mongo)
99
    except KeyError:
100
        print(
101
            f"Please set the 'CMD' env var. \nIt has to be one of these: {list(cmds.keys())}"
102
        )
103
        sys.exit(1)
104