forked from spoutin/sample_code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
142 lines (115 loc) · 4.3 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import logging
from datetime import date, datetime, time, timedelta
import pandas as pd
import pytz
from sample_code.dao.audit import AuditDAO
from sample_code.dao.reporting import ReportDAO
from sample_code.dao.usage import UsageDAO
from sample_code.settings import (
AUDIT_REPLICASET,
AUDIT_SERVER,
REPLICASET_A,
REPLICASET_B,
REPLICASET_C,
SERVER_A,
SERVER_B,
SERVER_C,
)
logger = logging.getLogger(__name__)
class Main:
def __init__(self) -> None:
logger.info("Initiate clients with databases")
self.reportingClient = ReportDAO()
self.auditClient = AuditDAO(
mongoServers=AUDIT_SERVER,
mongoReplicaset=AUDIT_REPLICASET,
)
self.usageClient_A = UsageDAO(
mongoServers=SERVER_A,
mongoReplicaset=REPLICASET_A,
)
self.usageClient_B = UsageDAO(
mongoServers=SERVER_B,
mongoReplicaset=REPLICASET_B,
)
self.usageClient_C = UsageDAO(
mongoServers=SERVER_C,
mongoReplicaset=REPLICASET_C,
)
def get_auldata_subscribers(
self, auditRangeStart: datetime, auditRangeEnd: datetime
):
logger.info(
f"Get subscribers for the range between {auditRangeStart.isoformat()} and {auditRangeEnd.isoformat()}"
)
res = self.auditClient.get_subscribers(auditRangeStart, auditRangeEnd)
return pd.DataFrame(list(res))
def compare(self, auldataSubs):
logger.info(f"Start comparing subscribers's data")
subListA = []
subListB = []
subListC = []
for _, row in auldataSubs.iterrows():
remainder = int(row["ban"]) % 3
if remainder == 0:
subListA.append(row)
elif remainder == 1:
subListB.append(row)
elif remainder == 2:
subListC.append(row)
self.run_compare_on_node("A", subListA)
self.run_compare_on_node("B", subListB)
self.run_compare_on_node("C", subListC)
def run_compare_on_node(self, node: str, subList: list):
logger.info(f"Start comparing subscribers's data on the node {node}")
to_date = lambda d: datetime.strptime(d, "%Y-%m-%dT%H:%M:%SZ").astimezone(
pytz.timezone("US/Eastern")
)
if len(subList) > 0:
auditDate = datetime.today().strftime("%Y-%m-%d %H:%M:%S")
usageClient = getattr(self, f"usageClient_{node}", None)
if not usageClient:
raise Exception("Wrong node!")
usageResult = pd.DataFrame(
columns=[
"extSubId",
"MDN",
"BAN",
"start",
"end",
"bytesIn",
"bytesOut",
]
)
for subscriber in subList:
effectiveDate = to_date(subscriber["effectiveDate"])
expiryDate = to_date(subscriber["expiryDate"])
res = usageClient.get_subscriber_usage(
subscriber["subscriberId"], effectiveDate, expiryDate
)
usageResult = pd.concat([usageResult, pd.DataFrame(res)], axis=0)
if len(usageResult) > 0:
data = [
[
row["extSubId"],
row["MDN"],
row["BAN"],
row["start"],
row["end"],
int(row["bytesIn"]) + int(row["bytesOut"]),
auditDate,
]
for _, row in usageResult.iterrows()
]
self.reportingClient.insert_reporting_data(data)
if __name__ == "__main__":
logger.info("Start the main script")
mainClient = Main()
mainClient.reportingClient.create_reporting_table()
auditDate = date.today() - timedelta(days=1)
auditRangeStart = datetime.combine(auditDate, time(0, 0, 0))
auditRangeEnd = datetime.combine(auditDate, time(23, 59, 59))
auldataSubs = mainClient.get_auldata_subscribers(auditRangeStart, auditRangeEnd)
mainClient.compare(auldataSubs)
mainClient.reportingClient.clean_reporting_data()
logger.info("Finish the main script")