Highlighted long running queries on the dashboards. Fixes #1975

This commit is contained in:
Akshay Joshi committed 2021-07-01 18:10:55 +05:30
1 parent d90472014d
commit ef67409d61
12 files changed
+147 -6

No files matched your search

+48 -2
View File
@@ -8,6 +8,7 @@
##########################################################################
"""A blueprint module implementing the dashboard frame."""
import math
from functools import wraps
from flask import render_template, url_for, Response, g, request
from flask_babelex import gettext
@@ -150,6 +151,15 @@ class DashboardModule(PgAdminModule):
'details')
)
self.long_running_query_threshold = self.dashboard_preference.register(
'display', 'long_running_query_threshold',
gettext('Long running query thresholds'), 'threshold',
None, category_label=PREF_LABEL_DISPLAY,
help_str=gettext('Set the warning and alert threshold value to '
'highlight the long-running queries on the '
'dashboard.')
)
def get_exposed_url_endpoints(self):
"""
Returns:
@@ -300,13 +310,14 @@ def index(sid=None, did=None):
)
def get_data(sid, did, template):
def get_data(sid, did, template, check_long_running_query=False):
"""
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
check_long_running_query:
Returns:
@@ -324,12 +335,47 @@ def get_data(sid, did, template):
if not status:
return internal_server_error(errormsg=res)
# Check the long running query status and set the row type.
if check_long_running_query:
get_long_running_query_status(res['rows'])
return ajax_response(
response=res['rows'],
status=200
)
def get_long_running_query_status(activities):
"""
This function is used to check the long running query and set the
row type to highlight the row color accordingly
"""
dash_preference = Preferences.module('dashboards')
long_running_query_threshold = \
dash_preference.preference('long_running_query_threshold').get()
if long_running_query_threshold is not None:
long_running_query_threshold = long_running_query_threshold.split('|')
warning_value = float(long_running_query_threshold[0]) \
if long_running_query_threshold[0] != '' else math.inf
alert_value = float(long_running_query_threshold[1]) \
if long_running_query_threshold[1] != '' else math.inf
for row in activities:
row['row_type'] = None
# We care for only those queries which are in active state.
if row['state'] != 'active':
continue
active_since = float(row['active_since'])
if active_since > warning_value:
row['row_type'] = 'warning'
if active_since > alert_value:
row['row_type'] = 'alert'
@blueprint.route('/dashboard_stats',
endpoint='dashboard_stats')
@blueprint.route('/dashboard_stats/<int:sid>',
@@ -376,7 +422,7 @@ def activity(sid=None, did=None):
:param sid: server id
:return:
"""
return get_data(sid, did, 'activity.sql')
return get_data(sid, did, 'activity.sql', True)
@blueprint.route('/locks/', endpoint='locks')
@@ -437,12 +437,31 @@ define('pgadmin.dashboard', [
var data = new Data();
var HighlightedRow = Backgrid.Row.extend({
render: function() {
Backgrid.Row.prototype.render.call(this);
var row_type = this.model.get('row_type');
if (_.isUndefined(row_type) || _.isNull(row_type)) {
this.$el.removeClass('alert');
this.$el.removeClass('warning');
} else if (row_type === 'warning') {
this.$el.addClass('warning');
} else if (row_type === 'alert') {
this.$el.addClass('alert');
}
return this;
}
});
// Set up the grid
var grid = new Backgrid.Grid({
emptyText: gettext('No data found'),
columns: columns,
collection: data,
className: 'backgrid presentation table table-bordered table-noouter-border table-hover',
row: HighlightedRow
});
// Render the grid
@@ -12,7 +12,8 @@ SELECT
query,
pg_catalog.to_char(state_change, 'YYYY-MM-DD HH24:MI:SS TZ') AS state_change,
pg_catalog.to_char(query_start, 'YYYY-MM-DD HH24:MI:SS TZ') AS query_start,
backend_type
backend_type,
CASE WHEN state = 'active' THEN ROUND((extract(epoch from now() - query_start) / 60)::numeric, 2) ELSE 0 END AS active_since
FROM
pg_catalog.pg_stat_activity
{% if did %}WHERE
@@ -11,7 +11,8 @@ SELECT
pg_catalog.pg_blocking_pids(pid) AS blocking_pids,
query,
pg_catalog.to_char(state_change, 'YYYY-MM-DD HH24:MI:SS TZ') AS state_change,
pg_catalog.to_char(query_start, 'YYYY-MM-DD HH24:MI:SS TZ') AS query_start
pg_catalog.to_char(query_start, 'YYYY-MM-DD HH24:MI:SS TZ') AS query_start,
CASE WHEN state = 'active' THEN ROUND((extract(epoch from now() - query_start) / 60)::numeric, 2) ELSE 0 END AS active_since
FROM
pg_catalog.pg_stat_activity
{% if did %}WHERE
@@ -10,7 +10,8 @@ SELECT
CASE WHEN waiting THEN '{{ _('yes') }}' ELSE '{{ _('no') }}' END AS waiting,
query,
pg_catalog.to_char(state_change, 'YYYY-MM-DD HH24:MI:SS TZ') AS state_change,
pg_catalog.to_char(query_start, 'YYYY-MM-DD HH24:MI:SS TZ') AS query_start
pg_catalog.to_char(query_start, 'YYYY-MM-DD HH24:MI:SS TZ') AS query_start,
CASE WHEN state = 'active' THEN ROUND((extract(epoch from now() - query_start) / 60)::numeric, 2) ELSE 0 END AS active_since
FROM
pg_catalog.pg_stat_activity
{% if did %}WHERE