-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsc_status.py
90 lines (73 loc) · 3.08 KB
/
sc_status.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
import pymysql.cursors
import logging
from AwsStuff.sc_creds import AWS_MYSQL_ADD, AWS_MYSQL_USER, AWS_MYSQL_PW
class SC_Status:
def __init__(self):
if len(logging.getLogger().handlers) > 0:
# The Lambda environment pre-configures a handler logging to stderr. If a handler is already configured,
# `.basicConfig` does not execute. Thus we set the level directly.
logging.getLogger().setLevel(logging.INFO)
else:
logging.basicConfig(level=logging.INFO)
@staticmethod
def log(cls, string):
logging.error(string)
print(string)
def connect(self):
try:
REGION = 'eu-west-2a'
port = 3306
self.connection = pymysql.connect(AWS_MYSQL_ADD,
user=AWS_MYSQL_USER,
passwd=AWS_MYSQL_PW,
db="scdb",
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor,
connect_timeout=5)
self.mycursor = self.connection.cursor()
except self.connection.Error as error:
print("Error connecting to MYSQL DB")
self.log(self, "Error connecting to MYSQL DB trying to get list of dead games.")
def disconnect(self):
self.mycursor.close()
self.connection.close()
""" GET LIST OF DEAD GAMES """
def bring_out_the_dead(self): # monty python joke
self.mycursor.execute(f"SELECT gamenumber FROM dead")
dead = self.mycursor.fetchall()
list_of_dead = list()
for game in dead:
list_of_dead.append(game['gamenumber'])
return list_of_dead
def bring_out_the_living(self):
self.mycursor.execute(f"SELECT gamenumber FROM main")
alive = self.mycursor.fetchall()
alive_list = list()
for game in alive:
alive_list.append(game['gamenumber'])
return alive_list
def get_remainingtops(self):
alive_list = self.bring_out_the_living()
dead_list = self.bring_out_the_dead()
""" Remove dead from alive """
for game in alive_list:
if game in dead_list:
print(f"GN {game} needs to be removed from main as its dead")
alive_list.remove(game)
dict_of_alive_rt = dict()
for game in alive_list:
self.mycursor.execute(f"SELECT remainingtop FROM sc_log "
f"WHERE gnumber = {game} "
f"ORDER BY remainingtop "
f"LIMIT 1;")
rt = self.mycursor.fetchone()
if not rt: # if nothing found
print(f"GN {game} missing RT from sc_log")
else:
rt = rt['remainingtop']
dict_of_alive_rt[game] = rt
return dict_of_alive_rt, dead_list
# s = SC_Status()
# s.connect()
# s.get_remainingtops()
# s.disconnect()