-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
con_postg.py
199 lines (139 loc) · 5.59 KB
/
con_postg.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import psycopg2
import os
class DB():
"""class manage postgresql database"""
def __init__(self):
#self.user_db = {'host':os.environ['HOST'],
# 'database':os.environ['DATABASE'],
# 'user': os.environ['USER'],
# 'password':os.environ['PASSWORD']}
self.herokuDB = os.environ['DATABASE_URL']
def start(self):
xconnect = psycopg2.connect(self.herokuDB)
cur = xconnect.cursor()
try:
cur.execute("""create table Match (
id_match serial primary key,
status_code integer,
acuracy varchar(10),
private varchar(50),
public varchar(50),
amount float8,
assets integer
);""")
xconnect.commit()
cur.execute("""create table Error (
id_err serial primary key,
status_code integer,
private varchar(50),
public varchar(50),
message text
);""")
xconnect.commit()
cur.execute("""create table Statistics (
id_session serial primary key ,
match integer ,
not_found integer,
error integer,
criticalerror integer
);""")
xconnect.commit()
xconnect.close()
return True
except psycopg2.errors.DuplicateTable:
return True
except Exception as err:
xconnect.close()
return (False, err)
def added_match(self, status_code, acuracy, private, public, amount = 0, assets = 0):
"""method for insert data into the tables"""
xconnect = psycopg2.connect(self.herokuDB)
cur = xconnect.cursor()
match = """INSERT INTO match(status_code, acuracy, private, public, amount, assets) VALUES (%s,%s,%s,%s,%s,%s);"""
insert = (status_code, acuracy, private, public, amount, assets)
clio = cur.execute(match, insert)
xconnect.commit()
xconnect.close()
return clio
def added_error(self, status_code, message , private = 'not have', public = 'not have' ):
"""method for save a logs of errors"""
xconnect = psycopg2.connect(self.herokuDB)
cur = xconnect.cursor()
try:
match = """insert into Error (status_code, private, public, message) values (%s,%s,%s,%s);"""
insert = (status_code, private, public, message)
cur.execute(match, insert)
xconnect.commit()
xconnect.close()
return True
except Exception as err:
xconnect.close()
return False
def added_std(self, upgrade, match = 0, not_found = 0,error = 0, critical_error = 0 ):
"""method for save sttistics info and make reports"""
xconnect = psycopg2.connect(self.herokuDB )
cur = xconnect.cursor()
try:
if upgrade == False:
cur.execute("""insert into Statistics (match, not_found, error, criticalerror) values (%s,%s,%s,%s); """, (match, not_found,error,critical_error))
xconnect.commit()
xconnect.close()
elif upgrade == True:
cur.execute("""select id_session from Statistics ORDER BY id_session DESC LIMIT 1;""")
col = cur.fetchone()
print(col)
cur.execute("""update Statistics SET match = %s, not_found = %s,error = %s, criticalerror = %s WHERE id_session = %s;""", (match,not_found,error, critical_error, col))
xconnect.commit()
xconnect.close()
return True
except Exception as err:
xconnect.close()
return (False, err)
def getter_report(self):
xconnect = psycopg2.connect(self.herokuDB )
cur = xconnect.cursor()
list_report = []
try:
cur.execute("""select SUM (match) AS total_match from Statistics """)
list_report.append(cur.fetchone()[0])
cur.execute("""select SUM (not_found) as nt from Statistics""")
list_report.append(cur.fetchone()[0])
cur.execute("""select SUM (error) as error from Statistics""")
list_report.append(cur.fetchone()[0])
cur.execute("""select SUM (criticalerror) as cerror from Statistics""")
list_report.append(cur.fetchone()[0])
xconnect.close()
return (True, list_report)
except Exception as err:
xconnect.close()
return(False , err)
def getter_match(self):
xconnect = psycopg2.connect(self.herokuDB )
cur = xconnect.cursor()
try:
cur.execute("""select * from match""")
total = cur.fetchall()
xconnect.close()
return (True, total)
except Exception as err:
xconnect.close()
return(False , err)
def getter_error(self):
xconnect = psycopg2.connect(self.herokuDB )
cur = xconnect.cursor()
try:
cur.execute("""select * from error""")
total = cur.fetchall()
xconnect.close()
return (True, total)
except Exception as err:
xconnect.close()
return(False , err)
if __name__ == '__main__':
db = DB()
db.start()
db.added_match(202, 'good', 'sdasdasdsdad', 'asdadasdasd', 2213123, 123)
db.added_std(False,100,3,1,1)
print(db.getter_report())
print(db.getter_match())
print(db.getter_error())