from flask import render_template, abort, redirect, url_for, abort, flash, request, current_app, make_response, g from flask_login import login_required, login_user, logout_user, current_user from flask_sqlalchemy import get_debug_queries from . import main from .. import db from ..email import send_email from ..models import User, Permission, Deployment, Service, Region, Address, Domain, contact_proxmaster import base64 @main.after_app_request def after_request(response): for query in get_debug_queries(): if query.duration >= current_app.config['SLOW_DB_QUERY_TIME']: current_app.logger.warning('Slow query: %s\nParameters: %s\nDuration: %fs\nContext: %s\n' % (query.statement, query.parameters, query.duration, query.context)) return response #STATIC PAGES @main.route("/", methods=['GET']) def index(): return render_template('main/index.html') @main.route("/chat", methods=['GET']) def chat(): return render_template('main/livechat.html') #@main.route("/aboutus", methods=['GET']) #def about(): # return render_template('main/aboutus.html') @main.route("/terms", methods=['GET']) def terms(): return render_template('main/terms.html') #DASHBOARD @main.route("/dashboard", methods=['GET', 'POST']) @login_required def dashboard(): cuser = current_user inv_deployments = cuser.inv_deployments.filter_by(cancelled=False).order_by(Deployment.date_created.desc()).all() inv_deploycubeids = [] inv_deployments_list = [] for invcls in inv_deployments: if invcls.user_id == cuser.pid and invcls.enabled == True: inv_deploycubeids.extend([invcls.machine_id]) inv_deployments_list.extend([invcls.machine_alias]) inv_services = cuser.inv_services.filter_by(cancelled=False).order_by(Service.date_last_charge.asc()).all() inv_services_list = [] for invcls in inv_services: if invcls.user_id == cuser.pid and invcls.enabled == True: inv_services_list.extend([invcls.description]) inv_domains = cuser.inv_domains.filter_by(cancelled=False).order_by(Domain.date_created.desc()).all() inv_domains_list = [] for invcls in inv_domains: if invcls.user_id == cuser.pid and invcls.enabled == True: inv_domains_list.extend([invcls.fqdn]) inv_addresses = cuser.inv_addresses.order_by(Address.ip.asc()).all() inv_addresses_list = [] for invcls in inv_addresses: if invcls.user_id == cuser.pid and invcls.enabled == True: inv_addresses_list.extend([invcls.ip]) sys_regions = Region.query.all() regions = {} for region in sys_regions: regions[region.pid] = region.description #extract rrd and status from the deployments rrd = {} statuses = {} for cubeid in inv_deploycubeids: rrd[cubeid] = {} try: query = contact_proxmaster({}, 'vmrrd', cubeid) except Exception as e: current_app.logger.error(e) flash('Deploy #{} unreachable. Support is notified'.format(str(cubeid))) send_email(current_app.config['MAIL_USERNAME'], 'Cube {} is unreachable'.format(cubeid), 'vmanager/email/adm_unreachable', user=current_user, cubeid=cubeid) graphs_list = ['net', 'cpu', 'mem', 'hdd'] try: for graph in graphs_list: raw = query[graph]['image'].encode('raw_unicode_escape') rrd[cubeid][graph] = base64.b64encode(raw).decode() status = { cubeid : query['status'] } statuses.update(status) except Exception as e: current_app.logger.error(e) flash('Deploy #{} unreachable. Support is notified'.format(str(cubeid))) send_email(current_app.config['MAIL_USERNAME'], 'Cube {} is unreachable'.format(cubeid), 'vmanager/email/adm_unreachable', user=current_user, cubeid=cubeid ) #current_app.logger.info('[{}] Enabled deployments: {}, services: {}, domains: {}, addresses: {}'.format(current_user.email, inv_deployments_list, inv_services_list, inv_domains_list, inv_addresses_list )) return render_template('main/dashboard.html', rrd=rrd, status=statuses, inv_deployments=inv_deployments, inv_services=inv_services, inv_domains=inv_domains, inv_addresses=inv_addresses, region=regions)