-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreporting_db.py
145 lines (118 loc) · 4.38 KB
/
reporting_db.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
import MySQLdb
import MySQLdb.cursors
import config.config as config
import json
import datetime
db = MySQLdb.connect(host=config.DATABASE_CONFIG['host'],user=config.DATABASE_CONFIG['dbuser'],
passwd=config.DATABASE_CONFIG['dbpass'],db=config.DATABASE_CONFIG['dbname'],
cursorclass=MySQLdb.cursors.DictCursor)
# Get Current Status of all sites ever entered into the system
# Columns: id, createdAt, site, status
def getCurrentStatus(page, limit):
cursor = db.cursor()
pg = (page - 1) * limit
cursor.execute("""SELECT * FROM currentStatus LIMIT %s,%s""", (pg, limit))
data = cursor.fetchall()
db.commit()
cursor.close()
return data
# Get Sites that are being monitored for UI
def getSites(page, limit):
cursor = db.cursor()
pg = (page - 1) * limit
cursor.execute("""SELECT * FROM sites WHERE visible = 1 LIMIT %s,%s""", (pg, limit))
data = cursor.fetchall()
db.commit()
cursor.close()
return data
# Get Sites that are being monitored for site checking - gets all sites if they are active and visibe
def getSitesForCheck():
cursor = db.cursor()
cursor.execute("""SELECT * FROM sites WHERE visible = 1 and active = 1""")
data = cursor.fetchall()
db.commit()
cursor.close()
return data
# Get all activity ever recorded. Recorded every 15 minutes by default.
# Columns: id, activityType, createdAt, sitesAffected (returns one site and reports activity type as up or down)
def getActivity(page, limit):
cursor = db.cursor()
pg = (page - 1) * limit
cursor.execute("""SELECT * FROM activity ORDER BY id DESC LIMIT %s,%s""", (pg, limit))
data = cursor.fetchall()
db.commit()
cursor.close()
return data
# Get all outages reported - multiple sites can be listed
# Columns: id, createdAt, sitesAffected (returns array), numberOfSites
def getOutages(page, limit):
cursor = db.cursor()
pg = (page - 1) * limit
cursor.execute("""SELECT * FROM outages ORDER BY id DESC LIMIT %s,%s""", (pg, limit))
data = cursor.fetchall()
db.commit()
cursor.close()
return data
# Get downtime counts - the count increases by 1 if site is still down and resets to 0 when site comes back up
# Columns: id, created_at, site, downCount
# Ideally want to archive data from this table at certain points
def getDowntimeCounts(page, limit):
cursor = db.cursor()
pg = (page - 1) * limit
cursor.execute("""SELECT * FROM downtimeCounts LIMIT %s,%s""", (pg, limit))
data = cursor.fetchall()
db.commit()
cursor.close()
return data
# Get downtime counts if they are greater than 3 because 1 or 2 occurrences can be blips.
def getDownTimeCountsGreaterThanThree():
cursor = db.cursor()
cursor.execute("""SELECT * FROM downtimeCounts where downCount >= 3""")
data = cursor.fetchall()
db.commit()
cursor.close()
return data
# Get LED status for each LED. Can be used later to indicate uptime status on screen instead of lights
# Columns: id, color, pin, updateDate, status
def getLedActive(color):
cursor = db.cursor()
cursor.execute("""SELECT active FROM ledStatus where color = %s""", [color])
data = cursor.fetchone()
db.commit()
cursor.close()
return data['active']
# Get LED status for UI
def getLedStatus():
cursor = db.cursor()
cursor.execute("""SELECT * FROM ledStatus""")
data = cursor.fetchall()
db.commit()
cursor.close()
return data
# Gets cron jobs as defined in the database and whether or not they are enabled
# Columns: id, comment, updateDate, cronName, cronVal, cronScript, enabled
def getCronSettings():
cursor = db.cursor()
cursor.execute("""SELECT * FROM cronSettings""")
data = cursor.fetchall()
db.commit()
cursor.close()
return data
# Get single cron to view and update
def getOneCron(comment):
cursor = db.cursor()
cursor.execute("""SELECT * FROM cronSettings where comment = %s""", [comment])
data = cursor.fetchone()
db.commit()
cursor.close()
return data
# Get all email notifications that were triggered along with if they were successful or fail
# Columns: id, createdAt, content, status - content lists the sites in an array
def getNotifications(page, limit):
cursor = db.cursor()
pg = (page - 1) * limit
cursor.execute("""SELECT * from notifications ORDER BY id DESC LIMIT %s, %s""", (pg, limit))
data = cursor.fetchall()
db.commit()
cursor.close()
return data