ServerDowntimeNotifier/LastStateCrawler.py

52 lines
1.1 KiB
Python

import pymysql
def getWebsiteStates(conn: pymysql.Connection) -> dict:
# Get latest known states to check against
cur = conn.cursor()
cur.execute("""
WITH summary AS (
SELECT web.website_id,
web.success,
ROW_NUMBER() OVER(PARTITION BY web.website_id
ORDER BY web.ping_id DESC) AS rk
FROM website_pings web)
SELECT s.*
FROM summary s
WHERE s.rk = 1;
""")
websiteStates = cur.fetchall()
cur.close()
returnStates = {}
for site in websiteStates:
returnStates[site[0]] = (site[1] == 1)
return returnStates
def getServerStates(conn: pymysql.Connection) -> dict:
# Get latest known states to check against
cur = conn.cursor()
cur.execute("""
WITH summary AS (
SELECT srv.server_id,
srv.success,
ROW_NUMBER() OVER(PARTITION BY srv.server_id
ORDER BY srv.ping_id DESC) AS rk
FROM server_pings srv)
SELECT s.*
FROM summary s
WHERE s.rk = 1;
""")
serverStates = cur.fetchall()
cur.close()
returnStates = {}
for server in serverStates:
returnStates[server[0]] = (server[1] == 1)
return returnStates