-
Notifications
You must be signed in to change notification settings - Fork 0
/
er_3g_15min_v2020_04_14.py
157 lines (128 loc) · 5.01 KB
/
er_3g_15min_v2020_04_14.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import pandas as pd
import glob
import os
parse_dates = ['DATETIME_SVR', 'DATETIME_ID']
def main():
engine1()
def engine1():
""" testing """
tablename = "oper_er_3g_15min_dwhdb_MSK".lower()
storage = 'temp/oper_er_3g_15min_dwhdb_MSK.csv'
file_path = "Green_log/er_3g_15min/dwhdb_MSK/*.res"
datatype_dict = {'LAC': 'uint16', 'CELL_ID': 'uint16',
'CDR_Attempts': 'uint16',
'CDR_Drops': 'uint16',
'CunSR_Attempts': 'uint16',
'CunSR_Drops': 'uint16',
'CunSSR_Attempts': 'uint16',
'CunSSR_Drops': 'uint16',
'RAB_DR_PS_Attempts': 'uint16',
'RAB_DR_PS_Drops': 'uint16',
'RAB_FR_PS_Attempts': 'uint16',
'RAB_FR_PS_Drops': 'uint16',
'RAB_Setup_FR_CS_Attempts': 'uint16',
'RAB_Setup_FR_CS_Drops': 'uint16',
'RRC_CSetup_FR_CS_Attempts': 'uint16',
'RRC_CSetup_FR_CS_Drops': 'uint16',
'RRC_CSetup_FR_PS_Attempts': 'uint16',
'RRC_CSetup_FR_PS_Drops': 'uint16',
'U_CunSR_PS_Attempts': 'uint16',
'U_CunSR_PS_Drops': 'uint16',
'U_CunSSR_PS_Attempts': 'uint16',
'U_CunSSR_PS_Drops': 'uint16'
}
filelist = glob.glob(file_path)
filelist.sort(key=os.path.getmtime)
filelist = filelist[-6:]
# print(filelist)
df_from_each_file = (
pd.read_csv(
f,
delimiter=';',
decimal='.',
header=0,
index_col=["LAC", "CELL_ID", "CELL_NAME", "BeelineObject", 'Element', 'Server'],
# usecols=["DATETIME_ID", "LAC", "CELL_ID", "CELL_NAME", "CDR", "CDR_Attempts", "CDR_Drops"],
parse_dates=["DATETIME_ID"],
dtype=datatype_dict
) for f in filelist)
df100 = pd.concat(df_from_each_file, ignore_index=False)
df100['DD'] = df100['DATETIME_ID'].dt.date
del df100['DATETIME_ID']
print(df100.head())
# get last date dataframe:
idx = df100.groupby(["LAC", "CELL_ID"])['DD'].transform(max) == df100['DD']
df200 = df100[idx]
del df100
print('df200 last cell date:')
print(df200.head())
agglist = {
'DD': ['count'],
'CDR': 'mean',
'CDR_Attempts': 'sum',
'CDR_Drops': 'sum',
'CunSR': 'mean',
'CunSR_Attempts': 'sum',
'CunSR_Drops': 'sum',
'CunSSR': 'mean',
'CunSSR_Attempts': 'sum',
'CunSSR_Drops': 'sum',
'RAB_DR_PS': 'mean',
'RAB_DR_PS_Attempts': 'sum',
'RAB_DR_PS_Drops': 'sum',
'RAB_FR_PS': 'mean',
'RAB_FR_PS_Attempts': 'sum',
'RAB_FR_PS_Drops': 'sum',
'RAB_Setup_FR_CS': 'mean',
'RAB_Setup_FR_CS_Attempts': 'sum',
'RAB_Setup_FR_CS_Drops': 'sum',
'RRC_CSetup_FR_CS': 'mean',
'RRC_CSetup_FR_CS_Attempts': 'sum',
'RRC_CSetup_FR_CS_Drops': 'sum',
'RRC_CSetup_FR_PS': 'mean',
'RRC_CSetup_FR_PS_Attempts': 'sum',
'RRC_CSetup_FR_PS_Drops': 'sum',
'U_CunSR_PS': 'mean',
'U_CunSR_PS_Attempts': 'sum',
'U_CunSR_PS_Drops': 'sum',
'U_CunSSR_PS': 'mean',
'U_CunSSR_PS_Attempts': 'sum',
'U_CunSSR_PS_Drops': 'sum'
}
df400 = df200.groupby(["LAC", "CELL_ID", "CELL_NAME", "BeelineObject", 'Element', 'Server', 'DD']).agg(agglist)
# put all columns levels to one level
df400.columns = ['_'.join(col) for col in df400.columns]
del df200
print('df400 saving...')
print('df400 to file...')
df400 = df400.round(decimals=2)
df400.reset_index().to_csv(storage, index=False, header=True, decimal=',', sep='\t', float_format='%.1f')
print('df400 to Oracle...')
from sqlalchemy import create_engine
from sqlalchemy import types
from sqlalchemy.types import Date # String, DateTime
engine = create_engine('oracle://CC:CC@RAN_dcn')
# con = engine.connect()
# con.execute("TRUNCATE TABLE CC.SERVER2_ER_3G_15MIN")
df400 = df400.reset_index(drop=False, inplace=False)
df400.columns = map(lambda x: str(x).upper(), df400.columns)
# print(df400.info())
df401 = df400
# set VARCHAR(50) type for all string objects
dtyp = {c: types.VARCHAR(50)
for c in df401.columns[df401.dtypes == 'object'].tolist()}
# set Date type for DD
del dtyp["DD"]
dtyp["DD"] = Date
df401.to_sql(tablename, engine, if_exists='replace', index=False, dtype=dtyp)
# con.close()
# save timestamp to Oracle oper_log
import datetime
dt_now = datetime.datetime.now()
dict_ = {'filename': tablename, 'DT': dt_now}
df = pd.DataFrame([dict_])
dtyp = {'filename': types.VARCHAR(50), 'DT': types.DateTime}
df.to_sql('oper_log', engine, if_exists='append', index=False, dtype=dtyp)
# todo: in Oracle make procedure to delete oper_log > 2 days
if __name__ == '__main__':
main()