delete_liveness_non_existent()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
import sys
4
import os
5
import pymongo
6
7
from napps.kytos.of_lldp.controllers import LivenessController
8
9
liveness_controller = LivenessController()
10
11
12
def list_liveness_non_existent(liveness_controller: LivenessController) -> list[dict]:
13
    """List liveness documents which don't exist on switches.interfaces docs."""
14
    db = liveness_controller.db
15
    return list(
16
        db.liveness.find({"_id": {"$nin": db.switches.distinct("interfaces.id")}})
17
    )
18
19
20
def delete_liveness_non_existent(liveness_controller: LivenessController) -> int:
21
    """Delete liveness documents which don't exist on switches.interfaces docs."""
22
    db = liveness_controller.db
23
    return db.liveness.delete_many(
24
        {"_id": {"$nin": db.switches.distinct("interfaces.id")}}
25
    ).deleted_count
26
27
28
if __name__ == "__main__":
29
    cmds = {
30
        "list": lambda: list_liveness_non_existent(liveness_controller),
31
        "delete": lambda: f"Deleted {delete_liveness_non_existent(liveness_controller)} document(s)",
32
    }
33
    try:
34
        cmd = os.environ["CMD"]
35
    except KeyError:
36
        print("Please set the 'CMD' env var.")
37
        sys.exit(1)
38
    try:
39
        for command in cmd.split(","):
40
            print(cmds[command]())
41
    except KeyError as e:
42
        print(
43
            f"Unknown cmd: {str(e)}. 'CMD' env var has to be one of these {list(cmds.keys())}."
44
        )
45
        sys.exit(1)
46
    except pymongo.errors.PyMongoError as e:
47
        print(f"pymongo error: {str(e)}")
48
        sys.exit(1)
49