2016-03-08 08:18:41 +00:00
|
|
|
from flask import (
|
|
|
|
|
render_template,
|
|
|
|
|
session,
|
|
|
|
|
flash
|
|
|
|
|
)
|
|
|
|
|
|
2016-01-06 16:40:38 +00:00
|
|
|
from flask_login import login_required
|
2015-12-18 10:26:56 +00:00
|
|
|
from app.main import main
|
2016-01-15 16:10:24 +00:00
|
|
|
from app.main.dao.services_dao import get_service_by_id
|
2016-02-02 09:38:58 +00:00
|
|
|
from app.main.dao import templates_dao
|
2016-03-17 11:45:48 +00:00
|
|
|
from app import job_api_client, statistics_api_client
|
2015-12-18 10:26:56 +00:00
|
|
|
|
|
|
|
|
|
2016-02-02 14:24:08 +00:00
|
|
|
@main.route("/services/<service_id>/dashboard")
|
2016-01-06 16:40:38 +00:00
|
|
|
@login_required
|
2016-01-15 17:46:09 +00:00
|
|
|
def service_dashboard(service_id):
|
2016-03-10 11:53:29 +00:00
|
|
|
templates = templates_dao.get_service_templates(service_id)['data']
|
|
|
|
|
jobs = job_api_client.get_job(service_id)['data']
|
|
|
|
|
|
|
|
|
|
service = get_service_by_id(service_id)
|
|
|
|
|
session['service_name'] = service['data']['name']
|
|
|
|
|
session['service_id'] = service['data']['id']
|
|
|
|
|
|
|
|
|
|
if session.get('invited_user'):
|
|
|
|
|
session.pop('invited_user', None)
|
|
|
|
|
service_name = service['data']['name']
|
2016-03-11 10:16:06 +00:00
|
|
|
message = 'You have successfully accepted your invitation and been added to {}'.format(service_name)
|
2016-03-10 11:53:29 +00:00
|
|
|
flash(message, 'default_with_tick')
|
2016-03-08 08:18:41 +00:00
|
|
|
|
2016-03-17 11:45:48 +00:00
|
|
|
statistics = statistics_api_client.get_statistics_for_service(service_id)['data']
|
|
|
|
|
|
2015-12-18 10:26:56 +00:00
|
|
|
return render_template(
|
2016-03-17 11:45:48 +00:00
|
|
|
'views/dashboard/dashboard.html',
|
2016-03-14 16:50:02 +00:00
|
|
|
jobs=jobs[:5],
|
2016-02-04 17:13:57 +00:00
|
|
|
more_jobs_to_show=(len(jobs) > 5),
|
2016-02-04 12:20:24 +00:00
|
|
|
free_text_messages_remaining='250,000',
|
2016-01-13 12:10:29 +00:00
|
|
|
spent_this_month='0.00',
|
2016-03-17 11:45:48 +00:00
|
|
|
service=service['data'],
|
|
|
|
|
statistics=expand_statistics(statistics),
|
|
|
|
|
templates=templates,
|
2016-02-02 14:24:08 +00:00
|
|
|
service_id=str(service_id))
|
2016-03-17 11:45:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def expand_statistics(statistics, danger_zone=25):
|
|
|
|
|
|
|
|
|
|
if not statistics or not statistics[0]:
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
today = statistics[0]
|
|
|
|
|
|
|
|
|
|
today.update({
|
2016-03-17 14:40:08 +00:00
|
|
|
'emails_failure_rate':
|
|
|
|
|
int(today['emails_error'] / today['emails_requested'] * 100) if today['emails_requested'] else 0,
|
|
|
|
|
'sms_failure_rate':
|
|
|
|
|
int(today['sms_error'] / today['sms_requested'] * 100) if today['sms_requested'] else 0
|
2016-03-17 11:45:48 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
today.update({
|
|
|
|
|
'emails_percentage_of_danger_zone': min(today['emails_failure_rate'] / (danger_zone / 100), 100),
|
|
|
|
|
'sms_percentage_of_danger_zone': min(today['sms_failure_rate'] / (danger_zone / 100), 100)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return today
|