-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulationMetricsTool.py
More file actions
127 lines (98 loc) · 5.03 KB
/
populationMetricsTool.py
File metadata and controls
127 lines (98 loc) · 5.03 KB
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
import time
import datetime
import pandas as pd
from sqlsrvrHandler import SQLServer
from colorama import init; init()
from colorama import Fore, Back, Style
logWarn = '\n[LOG]' + Fore.YELLOW + Style.BRIGHT + '[WARNING] ' + Style.RESET_ALL
#-------------------------------------------------------------------------------------------
print(Fore.CYAN + Style.BRIGHT)
print('-----------------------')
print('POPULATION METRICS TOOL')
print('-----------------------')
print(Style.RESET_ALL)
print(datetime.datetime.now().strftime('%m/%d/%Y - %H:%M:%S'))
serverConnect = True
tableConnect = True
while True:
while serverConnect:
SERVER = input('\nSERVER : ')
DATABASE = input('DATABASE : ')
spellCheck = True
while True:
startTime = time.time()
dT = datetime.datetime.now()
print(dT.strftime('\n%H:%M:%S\n'))
try:
CNXN = SQLServer(SERVER, DATABASE)
print('\n[LOG] ' + Fore.GREEN + Style.BRIGHT + 'Connected' + Style.RESET_ALL)
serverConnect = False
break
except:
if spellCheck:
spellCheck = input('\nIs SERVER and DATABASE spelled correctly? ( Y / N ) : ')
if spellCheck.upper() == 'Y':
spellCheck = False
continue
elif spellCheck.upper() == 'N':
break
print(logWarn + 'SQL Server Time Out - Trying Again in 1 Minute')
time.sleep(60)
continue
if tableConnect:
TABLE = input('\nTABLE : ')
tableConnect = False
COLUMN = input('\nCOLUMN : ')
try:
for tableIdx, table in enumerate([TABLE]):
print('\n[LOG] Profiling Table: {}'.format(table))
queryStart = time.time()
query = 'select {} \
from {}.{}'.format(COLUMN, DATABASE, table)
chunk = 10000
data = CNXN.queryToDF(query, chunksize = chunk)
queryEnd = time.time()
print('\nQuery Execution: {}'.format(queryEnd - queryStart))
for idx, df in enumerate(data):
print('Chunk: {}'.format(idx + 1), end = '\r', flush = True)
nullCount = df.isna().sum()
zeroCountNum = (df == 0).sum()
zeroCountChar = (df == '0').sum()
zeroCount = zeroCountNum + zeroCountChar
populated = -(nullCount + zeroCount) + len(df.index)
countedData = pd.concat([populated, nullCount, zeroCount], axis = 1, join = 'outer')
countedData.insert(0, range(len(df.index)), len(df.index))
countedData.columns = ['total', 'populated', 'nullCount', 'zeroCount']
if idx == 0:
outData = countedData
else:
outData = outData.add(countedData, fill_value = 0)
analysisEnd = time.time()
print()
print(Back.GREEN)
print()
print(outData)
print(Style.RESET_ALL)
# csvCheck = input('Save Data to .csv? ( Y / N ) : ')
# if csvCheck == 'Y':
# outData.to_csv('{}_{}.csv'.format(table, COLUMN), header = True)
print('\nAnalysis Execution: {}\n'.format(analysisEnd - queryEnd))
except:
print(logWarn + 'Invalid Table / Column Name')
endTime = time.time()
print('[LOG] Analysis Complete')
print('Total Execution Time: {}'.format(endTime - startTime))
cont = input('\nCONTINUE? ( Y / N ) : ')
if cont.upper() == 'Y':
newTable = input('Do you want to profile a different table? ( Y / N ) : ')
if newTable.upper() == 'Y':
tableConnect = True
newServer = input('Is a different server required? ( Y / N ) : ')
if newServer.upper() == 'Y':
serverConnect = True
continue
elif cont.upper() == 'N':
break
print(Fore.YELLOW + Style.BRIGHT)
input('PRESS ENTER TO EXIT')
print(Style.RESET_ALL)