Skip to content
This repository has been archived by the owner on Sep 18, 2019. It is now read-only.

Commit

Permalink
Get data from db, compute pings success
Browse files Browse the repository at this point in the history
  • Loading branch information
grewn0uille committed Feb 22, 2018
1 parent 8c941bf commit 97b8411
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions sherlog/graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import datetime

import pygal

from itertools import groupby
from operator import attrgetter
from sherlog.log_parser import get_config, get_session
from sherlog.model import Log


def gen_graph(data, date_range, title):
line_chart = pygal.Line()
line_chart.title = title
line_chart.x_labels = map(str, date_range)
line_chart.add('', data)
return line_chart.render_response()


def compute_ok_percent(data, occurences):
ok_true = [x.ok for x in data].count(True)
return ok_true * 100 / occurences


def build_list_ping(data):
return [x for x in data if x.command]


def build_list_service(data):
return [x for x in data if not x.command]


def get_data(server_name, ping_service, start):
dbsession = get_session(get_config())
if 'ping6' in ping_service:
data = (
dbsession.query(Log)
.filter(Log.server_name == server_name)
.filter(Log.command.contains('ping6'))
.filter(Log.start > start)
.all()
)
elif 'ping' in ping_service:
data = (
dbsession.query(Log)
.filter(Log.server_name == server_name)
.filter(Log.command.contains('ping'))
.filter(Log.start > start)
.all()
)
else:
data = (
dbsession.query(Log)
.filter(Log.server_name == server_name)
.filter(Log.command == '')
.filter(Log.start > start)
.all()
)
return data


def build_graph(server_name, interval, ping_service):
current_date = datetime.datetime.now()
tmp_date = datetime.datetime(2018, 1, 5, 14, 13, 37, 903083)
data = get_data(server_name, ping_service, tmp_date)
if 'ping' in ping_service:
build_data = build_list_ping(data)
else:
build_data = build_list_service(data)
build_data.sort(key=lambda data: data.start.day)
grouped_by_days = groupby(build_data, lambda data:data.start.day)
for key,group in grouped_by_days:
group = list(group)
res = compute_ok_percent(group, len(group))
print(res)

0 comments on commit 97b8411

Please sign in to comment.