Viewport onto your services
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

208 lines
5.8 KiB

# coding=utf8
#
# events.py: Cupola event viewing
# Copyright (C) 2015 Sam Black <samwwwblack@lapwing.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
__author__ = "Sam Black <samwwwblack@lapwing.org>"
import json
from datetime import datetime
from datetime import timedelta
from dateutil import rrule
from flask import Blueprint
from flask import jsonify
from flask import render_template
from flask_security import login_required
from cupola.models.events import Event
from cupola.models.events import EventGroup
from cupola.models.projects import Project
events = Blueprint("events", __name__, url_prefix="/event")
@events.route("/")
@login_required
def index():
"""
List all events in timeline fashion.
:return: HTML view of event
:rtype: jinja2.Template
"""
event_group = EventGroup.objects.order_by("-last_seen").all()
return render_template("events/index.html", event_groups=event_group)
@events.route("/<event_id>")
@login_required
def view_event(event_id):
"""
View event information.
This expects the Event's `event_id`,
rather than the ObjectId.
:param event_id: Event to view
:type event_id: str
:return: HTML view of event
:rtype: jinja2.Template
"""
event = Event.objects.get_or_404(event_id=event_id)
return render_template("events/view.html", event=event)
@events.route("/group/<event_group_id>")
@login_required
def view_group(event_group_id):
"""
View an event group.
:param event_group_id: event group
:type event_group_id: str
:return: HTML view of event group
:rtype: jinja2.Template
"""
event_group = EventGroup.objects.get_or_404(id=event_group_id)
return render_template("events/view_group.html", event_group=event_group)
@events.route("/group/<event_group_id>/chart")
@login_required
def view_group_chart(event_group_id):
"""
Event group data for charts.
:param event_group_id: event group
:type event_group_id: str
:return: c3 chart data
:rtype: JSON
"""
group = EventGroup.objects(id=event_group_id).first()
chart_data = {
"date": []
}
now = datetime.date(datetime.now())
days = list(rrule.rrule(rrule.DAILY, count=16,
until=now + timedelta(days=1),
dtstart=now - timedelta(days=14)))
data = []
for idx, day in enumerate(days):
if idx > 14:
break
chart_data["date"].append(day.strftime("%Y-%m-%d"))
daily_events = Event.objects(group=group,
timestamp__gt=day,
timestamp__lt=days[idx + 1]).count()
data.append(daily_events)
chart_data[group.message] = data
return jsonify(chart_data)
@events.route("/project/<project_id>/")
@login_required
def event_groups(project_id):
"""
Return event group information for c3 charting.
:param project_id: project to retrieve event groups for.
:type project_id: str
:return: c3 chart data
:rtype: JSON
"""
project = Project.objects.get_or_404(id=project_id)
groups = EventGroup.objects(
project=project).order_by("-event_count").limit(5)
chart_data = {
"columns": [["date"]],
"groups": None
}
col_groups = []
now = datetime.date(datetime.now())
days = list(rrule.rrule(rrule.DAILY, count=15,
until=now + timedelta(days=1),
dtstart=now - timedelta(days=13)))
for group in groups:
col_groups.append(group.message)
data = [group.message]
for idx, day in enumerate(days):
if idx > 13:
break
chart_data["columns"][0].append(day.strftime("%Y-%m-%d"))
daily_events = Event.objects(group=group,
timestamp__gt=day,
timestamp__lt=days[idx + 1]).count()
data.append(daily_events)
chart_data["columns"].append(data)
chart_data["groups"] = [col_groups]
return jsonify(**chart_data)
@events.route("/dashboard/")
@login_required
def dashboard():
"""
Get all events per project.
:return: c3 chart data
:rtype: JSON
"""
groups = EventGroup.objects.order_by("-event_count").limit(5)
chart_data = {
"columns": [["date"]],
"groups": None
}
col_groups = []
now = datetime.date(datetime.now())
days = list(rrule.rrule(rrule.DAILY, count=15,
until=now + timedelta(days=1),
dtstart=now - timedelta(days=13)))
for group in groups:
col_groups.append(group.message)
data = [group.message]
for idx, day in enumerate(days):
if idx > 13:
break
chart_data["columns"][0].append(day.strftime("%Y-%m-%d"))
daily_events = Event.objects(group=group,
timestamp__gt=day,
timestamp__lt=days[idx + 1]).count()
data.append(daily_events)
chart_data["columns"].append(data)
chart_data["groups"] = [col_groups]
return jsonify(**chart_data)