-
Notifications
You must be signed in to change notification settings - Fork 1
/
DBCount.py
115 lines (96 loc) · 2.8 KB
/
DBCount.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
import cx_Oracle
import snowflake.connector
import sys
import csv
import os
from tabulate import tabulate
import pymsteams
import regex
import sqlalchemy.pool as pool
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
exp_dir = os.path.normpath('C:/Users/2019.8.3')
exp_file_name = 'ODS_Count_Diff.csv'
exp_path = os.path.join(exp_dir, exp_file_name)
o_host = 'ods'
o_port = 000
sid = 'ODSC'
user = 'pravin'
pwd = '****'
sid = cx_Oracle.makedsn(o_host, o_port, sid=sid)
def runSQL(table):
statement = "select '{0}', count(0) from {0}".format(table.replace(' ',''))
return statement
if __name__ == '__main__':
tables = [
'MAT_DEGREE ',
'MAT_INTEREST ']
my_list = []
x = []
try:
#conn_str =
cstr = 'oracle://{user}:{password}@{sid}'.format(
user = user,
password = pwd,
sid = sid
)
engine = create_engine(
cstr,
convert_unicode = False,
pool_recycle = 10,
pool_size = 50,
echo = True
)
#metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
cursor = engine.connect()
ctx = snowflake.connector.connect(
****)
cursor2 = ctx.cursor()
for table in tables:
sql = runSQL(table)
cursor.execute(sql)
o_count = cursor.fetchone()[1]
if re.match(re.compile('^M'),table):
cursor2.execute("USE SCHEMA BRAMGR;")
cursor2.execute(sql)
s_count = cursor2.fetchone()[1]
elif re.match(re.compile('^G'),table):
cursor2.execute("USE SCHEMA BRARAL;")
cursor2.execute(sql)
s_count = cursor2.fetchone()[1]
elif re.match(re.compile('^S'),table):
cursor2.execute("USE SCHEMA BRAURN;")
cursor2.execute(sql)
s_count = cursor2.fetchone()[1]
elif re.match(re.compile('^T'),table):
cursor2.execute("USE SCHEMA BRASMGR;")
cursor2.execute(sql)
s_count = cursor2.fetchone()[1]
my_list.append([table,o_count,s_count, o_count - s_count])
outputFile = open(exp_path,'w', newline='')
writer = csv.DictWriter(outputFile,fieldnames=["Table Name","Source","Target","Difference"])
writer.writeheader()
output = csv.writer(outputFile)
for data in my_list:
output.writerow(data)
if data[3] > 100:
x.append(data)
html = """{table}"""
text = html.format(table=tabulate(x,headers=["Table Name", "Source Count", "Target Count", "Difference"], tablefmt="html"))
myteams = pymsteams.connectorcard("https://outlook.office.com/webhook/d132ea53-c316-4fc2-ac8e-7dc745f45b9b@9d9a57a9-f226-4188-bad7-fc1cb39566b6/IncomingWebhook/a4da1a")
myteams.text(text)
myteams.title("ODS Tables")
myteams.send()
except cx_Oracle.Error as exc:
error, = exc.args
print("Oracle Error", error.code)
print("Message", error.message)
#finally:
#outputFile.close()
#cursor.close()
#cursor2.close()
#t.cancel()
#pool.release(curcon)
#curcon.close()