2016-04-16 11:51:44 -05:00
|
|
|
##########################################################################
|
|
|
|
#
|
|
|
|
# pgAdmin 4 - PostgreSQL Tools
|
|
|
|
#
|
2017-01-04 07:33:32 -06:00
|
|
|
# Copyright (C) 2013 - 2017, The pgAdmin Development Team
|
2016-04-16 11:51:44 -05:00
|
|
|
# This software is released under the PostgreSQL Licence
|
|
|
|
#
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
"""A blueprint module implementing the dashboard frame."""
|
|
|
|
MODULE_NAME = 'dashboard'
|
|
|
|
|
2016-05-05 10:42:16 -05:00
|
|
|
from functools import wraps
|
|
|
|
|
2016-06-03 03:16:11 -05:00
|
|
|
from flask import render_template, url_for, Response, g
|
2016-07-22 10:25:23 -05:00
|
|
|
from flask_babel import gettext
|
|
|
|
from flask_security import login_required
|
2016-04-16 11:51:44 -05:00
|
|
|
from pgadmin.utils import PgAdminModule
|
2016-12-09 05:59:13 -06:00
|
|
|
from pgadmin.utils.ajax import make_response as ajax_response,\
|
|
|
|
internal_server_error
|
2016-04-16 11:51:44 -05:00
|
|
|
from pgadmin.utils.ajax import precondition_required
|
|
|
|
from pgadmin.utils.driver import get_driver
|
|
|
|
from pgadmin.utils.menu import Panel
|
2016-05-05 10:42:16 -05:00
|
|
|
from pgadmin.utils.preferences import Preferences
|
|
|
|
|
2016-06-21 08:12:14 -05:00
|
|
|
from config import PG_DEFAULT_DRIVER
|
|
|
|
|
2016-04-16 11:51:44 -05:00
|
|
|
|
|
|
|
class DashboardModule(PgAdminModule):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(DashboardModule, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def get_own_menuitems(self):
|
|
|
|
return {}
|
|
|
|
|
|
|
|
def get_own_javascripts(self):
|
|
|
|
return [{
|
|
|
|
'name': 'pgadmin.dashboard',
|
|
|
|
'path': url_for('dashboard.index') + 'dashboard',
|
|
|
|
'when': None
|
|
|
|
}]
|
|
|
|
|
2016-05-05 10:42:16 -05:00
|
|
|
def get_own_stylesheets(self):
|
|
|
|
"""
|
|
|
|
Returns:
|
|
|
|
list: the stylesheets used by this module.
|
|
|
|
"""
|
|
|
|
stylesheets = [
|
|
|
|
url_for('dashboard.static', filename='css/dashboard.css')
|
|
|
|
]
|
|
|
|
return stylesheets
|
|
|
|
|
2016-04-16 11:51:44 -05:00
|
|
|
def get_panels(self):
|
|
|
|
return [
|
|
|
|
Panel(
|
|
|
|
name='dashboard',
|
|
|
|
priority=1,
|
|
|
|
title=gettext('Dashboard'),
|
|
|
|
icon='fa fa-tachometer',
|
2016-05-05 10:42:16 -05:00
|
|
|
content='',
|
2016-04-16 11:51:44 -05:00
|
|
|
isCloseable=False,
|
2016-05-05 10:42:16 -05:00
|
|
|
isPrivate=True,
|
|
|
|
isIframe=False)
|
2016-04-16 11:51:44 -05:00
|
|
|
]
|
|
|
|
|
2016-05-05 10:42:16 -05:00
|
|
|
def register_preferences(self):
|
|
|
|
"""
|
|
|
|
register_preferences
|
|
|
|
Register preferences for this module.
|
|
|
|
"""
|
|
|
|
# Register options for the PG and PPAS help paths
|
2016-12-09 05:59:13 -06:00
|
|
|
self.dashboard_preference = Preferences('dashboards',
|
|
|
|
gettext('Dashboards'))
|
2016-05-05 10:42:16 -05:00
|
|
|
|
|
|
|
self.session_stats_refresh = self.dashboard_preference.register(
|
|
|
|
'dashboards', 'session_stats_refresh',
|
|
|
|
gettext("Session statistics refresh rate"), 'integer',
|
|
|
|
1, min_val=1, max_val=999999,
|
|
|
|
category_label=gettext('Graphs'),
|
|
|
|
help_str=gettext('The number of seconds between graph samples.')
|
|
|
|
)
|
|
|
|
|
|
|
|
self.session_stats_refresh = self.dashboard_preference.register(
|
|
|
|
'dashboards', 'tps_stats_refresh',
|
|
|
|
gettext("Transaction throughput refresh rate"), 'integer',
|
|
|
|
1, min_val=1, max_val=999999,
|
|
|
|
category_label=gettext('Graphs'),
|
|
|
|
help_str=gettext('The number of seconds between graph samples.')
|
|
|
|
)
|
|
|
|
|
|
|
|
self.session_stats_refresh = self.dashboard_preference.register(
|
|
|
|
'dashboards', 'ti_stats_refresh',
|
|
|
|
gettext("Tuples in refresh rate"), 'integer',
|
|
|
|
1, min_val=1, max_val=999999,
|
|
|
|
category_label=gettext('Graphs'),
|
|
|
|
help_str=gettext('The number of seconds between graph samples.')
|
|
|
|
)
|
|
|
|
|
|
|
|
self.session_stats_refresh = self.dashboard_preference.register(
|
|
|
|
'dashboards', 'to_stats_refresh',
|
|
|
|
gettext("Tuples out refresh rate"), 'integer',
|
|
|
|
1, min_val=1, max_val=999999,
|
|
|
|
category_label=gettext('Graphs'),
|
|
|
|
help_str=gettext('The number of seconds between graph samples.')
|
|
|
|
)
|
|
|
|
|
|
|
|
self.session_stats_refresh = self.dashboard_preference.register(
|
|
|
|
'dashboards', 'bio_stats_refresh',
|
|
|
|
gettext("Block I/O statistics refresh rate"), 'integer',
|
|
|
|
1, min_val=1, max_val=999999,
|
|
|
|
category_label=gettext('Graphs'),
|
|
|
|
help_str=gettext('The number of seconds between graph samples.')
|
|
|
|
)
|
|
|
|
|
2016-04-16 11:51:44 -05:00
|
|
|
|
|
|
|
blueprint = DashboardModule(MODULE_NAME, __name__)
|
|
|
|
|
|
|
|
|
|
|
|
def check_precondition(f):
|
|
|
|
"""
|
|
|
|
This function will behave as a decorator which will check
|
|
|
|
database connection before running view, it also adds
|
|
|
|
manager, conn & template_path properties to self
|
|
|
|
"""
|
|
|
|
|
|
|
|
@wraps(f)
|
2016-05-05 10:42:16 -05:00
|
|
|
def wrap(*args, **kwargs):
|
2016-04-16 11:51:44 -05:00
|
|
|
# Here args[0] will hold self & kwargs will hold gid,sid,did
|
2016-05-05 10:42:16 -05:00
|
|
|
|
2016-06-03 03:16:11 -05:00
|
|
|
g.manager = get_driver(
|
2016-05-05 10:42:16 -05:00
|
|
|
PG_DEFAULT_DRIVER).connection_manager(
|
2016-04-16 11:51:44 -05:00
|
|
|
kwargs['sid']
|
|
|
|
)
|
2016-06-03 03:16:11 -05:00
|
|
|
g.conn = g.manager.connection()
|
2016-05-05 10:42:16 -05:00
|
|
|
|
2016-04-16 11:51:44 -05:00
|
|
|
# If DB not connected then return error to browser
|
2016-06-03 03:16:11 -05:00
|
|
|
if not g.conn.connected():
|
2016-12-09 05:59:13 -06:00
|
|
|
if f.__name__ in ['activity', 'prepared', 'locks', 'config']:
|
|
|
|
return precondition_required(
|
|
|
|
gettext("Please connect to the selected server"
|
|
|
|
" to view the table.")
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
return precondition_required(
|
|
|
|
gettext("Please connect to the selected server"
|
|
|
|
" to view the graph.")
|
|
|
|
)
|
|
|
|
|
|
|
|
if 'did' in kwargs:
|
|
|
|
db_conn = g.manager.connection(did=kwargs['did'])
|
|
|
|
# If the selected DB not connected then return error to browser
|
|
|
|
if not db_conn.connected():
|
|
|
|
if f.__name__ in ['activity', 'prepared', 'locks', 'config']:
|
|
|
|
return precondition_required(
|
|
|
|
gettext("Please connect to the selected database"
|
|
|
|
" to view the table.")
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
return precondition_required(
|
|
|
|
gettext("Please connect to the selected database to"
|
|
|
|
" view the graph.")
|
|
|
|
)
|
2016-04-16 11:51:44 -05:00
|
|
|
|
2016-05-05 10:42:16 -05:00
|
|
|
# Set template path for sql scripts
|
2016-06-03 03:16:11 -05:00
|
|
|
g.server_type = g.manager.server_type
|
|
|
|
g.version = g.manager.version
|
2016-10-14 19:18:17 -05:00
|
|
|
|
2017-01-30 05:25:02 -06:00
|
|
|
g.template_path = 'dashboard/sql/#{0}#'.format(g.version)
|
2016-05-05 10:42:16 -05:00
|
|
|
|
|
|
|
return f(*args, **kwargs)
|
2016-04-16 11:51:44 -05:00
|
|
|
|
|
|
|
return wrap
|
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route("/dashboard.js")
|
|
|
|
@login_required
|
|
|
|
def script():
|
|
|
|
"""render the required javascript"""
|
2016-12-09 05:59:13 -06:00
|
|
|
return Response(response=render_template("dashboard/js/dashboard.js",
|
|
|
|
_=gettext),
|
2016-04-16 11:51:44 -05:00
|
|
|
status=200,
|
|
|
|
mimetype="application/javascript")
|
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route('/')
|
|
|
|
@blueprint.route('/<int:sid>')
|
|
|
|
@blueprint.route('/<int:sid>/<int:did>')
|
|
|
|
@login_required
|
|
|
|
def index(sid=None, did=None):
|
2016-05-05 10:42:16 -05:00
|
|
|
"""
|
|
|
|
Renders the welcome, server or database dashboard
|
|
|
|
Args:
|
|
|
|
sid: Server ID
|
|
|
|
did: Database ID
|
|
|
|
|
|
|
|
Returns: Welcome/Server/database dashboard
|
|
|
|
|
|
|
|
"""
|
|
|
|
rates = {}
|
|
|
|
|
|
|
|
prefs = Preferences.module('dashboards')
|
|
|
|
|
2016-10-14 19:18:17 -05:00
|
|
|
# Get the server version
|
|
|
|
if sid is not None:
|
|
|
|
g.manager = get_driver(
|
|
|
|
PG_DEFAULT_DRIVER).connection_manager(sid)
|
|
|
|
g.conn = g.manager.connection()
|
|
|
|
|
|
|
|
g.version = g.manager.version
|
|
|
|
|
|
|
|
if not g.conn.connected():
|
|
|
|
g.version = 0
|
|
|
|
|
2016-05-05 10:42:16 -05:00
|
|
|
session_stats_refresh_pref = prefs.preference('session_stats_refresh')
|
|
|
|
rates['session_stats_refresh'] = session_stats_refresh_pref.get()
|
|
|
|
tps_stats_refresh_pref = prefs.preference('tps_stats_refresh')
|
|
|
|
rates['tps_stats_refresh'] = tps_stats_refresh_pref.get()
|
|
|
|
ti_stats_refresh_pref = prefs.preference('ti_stats_refresh')
|
|
|
|
rates['ti_stats_refresh'] = ti_stats_refresh_pref.get()
|
|
|
|
to_stats_refresh_pref = prefs.preference('to_stats_refresh')
|
|
|
|
rates['to_stats_refresh'] = to_stats_refresh_pref.get()
|
|
|
|
bio_stats_refresh_pref = prefs.preference('bio_stats_refresh')
|
|
|
|
rates['bio_stats_refresh'] = bio_stats_refresh_pref.get()
|
|
|
|
|
2016-04-16 11:51:44 -05:00
|
|
|
# Show the appropriate dashboard based on the identifiers passed to us
|
|
|
|
if sid is None and did is None:
|
|
|
|
return render_template('/dashboard/welcome_dashboard.html')
|
|
|
|
if did is None:
|
2016-12-09 05:59:13 -06:00
|
|
|
return render_template('/dashboard/server_dashboard.html',
|
|
|
|
sid=sid,
|
|
|
|
rates=rates,
|
|
|
|
version=g.version)
|
2016-04-16 11:51:44 -05:00
|
|
|
else:
|
2016-12-09 05:59:13 -06:00
|
|
|
return render_template('/dashboard/database_dashboard.html',
|
|
|
|
sid=sid,
|
|
|
|
did=did,
|
|
|
|
rates=rates,
|
|
|
|
version=g.version)
|
2016-05-05 10:42:16 -05:00
|
|
|
|
|
|
|
|
|
|
|
def get_data(sid, did, template):
|
|
|
|
"""
|
|
|
|
Generic function to get server stats based on an SQL template
|
|
|
|
Args:
|
|
|
|
sid: The server ID
|
|
|
|
did: The database ID
|
|
|
|
template: The SQL template name
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
# Allow no server ID to be specified (so we can generate a route in JS)
|
|
|
|
# but throw an error if it's actually called.
|
|
|
|
if not sid:
|
|
|
|
return internal_server_error(errormsg='Server ID not specified.')
|
|
|
|
|
|
|
|
sql = render_template(
|
2016-06-03 03:16:11 -05:00
|
|
|
"/".join([g.template_path, template]), did=did
|
2016-05-05 10:42:16 -05:00
|
|
|
)
|
2016-06-03 03:16:11 -05:00
|
|
|
status, res = g.conn.execute_dict(sql)
|
2016-05-05 10:42:16 -05:00
|
|
|
|
|
|
|
if not status:
|
|
|
|
return internal_server_error(errormsg=res)
|
|
|
|
|
|
|
|
return ajax_response(
|
|
|
|
response=res['rows'],
|
|
|
|
status=200
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route('/session_stats/')
|
|
|
|
@blueprint.route('/session_stats/<int:sid>')
|
|
|
|
@blueprint.route('/session_stats/<int:sid>/<int:did>')
|
|
|
|
@login_required
|
|
|
|
@check_precondition
|
|
|
|
def session_stats(sid=None, did=None):
|
|
|
|
"""
|
|
|
|
This function returns server session statistics
|
|
|
|
:param sid: server id
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
return get_data(sid, did, 'session_stats.sql')
|
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route('/tps_stats/')
|
|
|
|
@blueprint.route('/tps_stats/<int:sid>')
|
|
|
|
@blueprint.route('/tps_stats/<int:sid>/<int:did>')
|
|
|
|
@login_required
|
|
|
|
@check_precondition
|
|
|
|
def tps_stats(sid=None, did=None):
|
|
|
|
"""
|
|
|
|
This function returns server TPS throughput
|
|
|
|
:param sid: server id
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
return get_data(sid, did, 'tps_stats.sql')
|
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route('/ti_stats/')
|
|
|
|
@blueprint.route('/ti_stats/<int:sid>')
|
|
|
|
@blueprint.route('/ti_stats/<int:sid>/<int:did>')
|
|
|
|
@login_required
|
|
|
|
@check_precondition
|
|
|
|
def ti_stats(sid=None, did=None):
|
|
|
|
"""
|
|
|
|
This function returns server tuple input statistics
|
|
|
|
:param sid: server id
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
return get_data(sid, did, 'ti_stats.sql')
|
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route('/to_stats/')
|
|
|
|
@blueprint.route('/to_stats/<int:sid>')
|
|
|
|
@blueprint.route('/to_stats/<int:sid>/<int:did>')
|
|
|
|
@login_required
|
|
|
|
@check_precondition
|
|
|
|
def to_stats(sid=None, did=None):
|
|
|
|
"""
|
|
|
|
This function returns server tuple output statistics
|
|
|
|
:param sid: server id
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
return get_data(sid, did, 'to_stats.sql')
|
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route('/bio_stats/')
|
|
|
|
@blueprint.route('/bio_stats/<int:sid>')
|
|
|
|
@blueprint.route('/bio_stats/<int:sid>/<int:did>')
|
|
|
|
@login_required
|
|
|
|
@check_precondition
|
|
|
|
def bio_stats(sid=None, did=None):
|
|
|
|
"""
|
|
|
|
This function returns server block IO statistics
|
|
|
|
:param sid: server id
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
return get_data(sid, did, 'bio_stats.sql')
|
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route('/activity/')
|
|
|
|
@blueprint.route('/activity/<int:sid>')
|
|
|
|
@blueprint.route('/activity/<int:sid>/<int:did>')
|
|
|
|
@login_required
|
|
|
|
@check_precondition
|
|
|
|
def activity(sid=None, did=None):
|
|
|
|
"""
|
|
|
|
This function returns server activity information
|
|
|
|
:param sid: server id
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
return get_data(sid, did, 'activity.sql')
|
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route('/locks/')
|
|
|
|
@blueprint.route('/locks/<int:sid>')
|
|
|
|
@blueprint.route('/locks/<int:sid>/<int:did>')
|
|
|
|
@login_required
|
|
|
|
@check_precondition
|
|
|
|
def locks(sid=None, did=None):
|
|
|
|
"""
|
|
|
|
This function returns server lock information
|
|
|
|
:param sid: server id
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
return get_data(sid, did, 'locks.sql')
|
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route('/prepared/')
|
|
|
|
@blueprint.route('/prepared/<int:sid>')
|
|
|
|
@blueprint.route('/prepared/<int:sid>/<int:did>')
|
|
|
|
@login_required
|
|
|
|
@check_precondition
|
|
|
|
def prepared(sid=None, did=None):
|
|
|
|
"""
|
|
|
|
This function returns prepared XACT information
|
|
|
|
:param sid: server id
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
return get_data(sid, did, 'prepared.sql')
|
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route('/config/')
|
|
|
|
@blueprint.route('/config/<int:sid>')
|
|
|
|
@login_required
|
|
|
|
@check_precondition
|
|
|
|
def config(sid=None):
|
|
|
|
"""
|
|
|
|
This function returns server config information
|
|
|
|
:param sid: server id
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
return get_data(sid, None, 'config.sql')
|