change uptimes to daily, weekly and monthly

- /api/current_status renamed to /api/quick
 - add week_uptime and month_uptime to responses
This commit is contained in:
Luna Mendes 2018-09-05 21:17:26 -03:00
parent e14720a8c8
commit 5428dcee3c
2 changed files with 2441 additions and 2426 deletions

View File

@ -1,8 +1,15 @@
import time
from decimal import Decimal
from sanic import Blueprint, response
bp = Blueprint(__name__)
# a day, 7 days and 30 days in milliseconds
DAY_MSEC = 86400000
WEEK_MSEC = 604800000
MONTH_MSEC = 2592000000
def get_status(manager):
res = {}
@ -43,53 +50,42 @@ def get_graphs(manager):
return res
def calc_uptime(manager):
def _raw_uptime(cur, service_name: str, worker, time_period: int) -> str:
"""Calculate uptime for a service given a timeframe."""
# get total period of the last 24 hours
max_tstamp = time.time() * 1000
min_tstamp = max_tstamp - time_period
t_period = max_tstamp - min_tstamp
# get all polls that failed
cur.execute(f"""
SELECT COUNT(*) FROM {service_name}
WHERE status = 0 AND timestamp > ?
""", (min_tstamp, ))
row = cur.fetchone()
down_hits = row[0]
# total downtime in seconds
downtime = down_hits * worker.service['poll']
# calculate total downtime in percentage
percent = Decimal(downtime) / Decimal(t_period)
return str(Decimal(100) - percent)
def calc_uptime(manager, time_frame):
res = {}
for name, worker in manager.workers.items():
cur = manager.conn.cursor()
# get total period we've been polling the service
cur.execute(f"""
SELECT min(timestamp), max(timestamp) from {name}
""")
row = cur.fetchone()
min_tstamp, max_tstamp = row[0], row[1]
t_period = max_tstamp - min_tstamp
# get all polls that failed
cur.execute(f"""
SELECT COUNT(*) FROM {name}
WHERE status = 0
""")
row = cur.fetchone()
down_hits = row[0]
# total downtime in seconds
downtime = down_hits * worker.service['poll']
# calculate total downtime in percentage
percent = Decimal(downtime) / Decimal(t_period)
# uptime - downtime = real uptime
res[name] = str(Decimal(100) - percent)
res[name] = _raw_uptime(cur, name, worker, time_frame)
return res
@bp.get('/api/current_status')
async def get_cur_status(request):
manager = request.app.manager
return response.json({
**get_status(manager),
**{
'uptime': calc_uptime(manager)
}
})
@bp.get('/api/status')
async def get_full_status(request):
manager = request.app.manager
@ -97,5 +93,24 @@ async def get_full_status(request):
return response.json({
'status': get_status(manager),
'graph': get_graphs(manager),
'uptime': calc_uptime(manager)
# uptime stuff
'uptime': calc_uptime(manager, DAY_MSEC),
'week_uptime': calc_uptime(manager, WEEK_MSEC),
'month_uptime': calc_uptime(manager, MONTH_MSEC),
})
@bp.get('/api/quick')
async def quick_status(request):
"""basically /api/status without the graphs"""
manager = request.app.manager
return response.json({
'status': get_status(manager),
# uptime stuff
'uptime': calc_uptime(manager, DAY_MSEC),
'week_uptime': calc_uptime(manager, WEEK_MSEC),
'month_uptime': calc_uptime(manager, MONTH_MSEC),
})

File diff suppressed because it is too large Load Diff