mirror of
https://github.com/pgadmin-org/pgadmin4.git
synced 2025-02-25 18:55:31 -06:00
Refactor to remove duplicate code for the cancel/terminate buttons in the dashboard.
This commit is contained in:
parent
cb25bb0e93
commit
f2929046fa
@ -23,19 +23,33 @@ function(url_for, gettext, r, $, _, pgAdmin, Backbone, Backgrid, Flotr,
|
|||||||
is_database_dashboard = false;
|
is_database_dashboard = false;
|
||||||
|
|
||||||
// Custom BackGrid cell, Responsible for cancelling active sessions
|
// Custom BackGrid cell, Responsible for cancelling active sessions
|
||||||
var cancelQueryCell = Backgrid.Extension.DeleteCell.extend({
|
var customDashboardActionCell = Backgrid.Extension.DeleteCell.extend({
|
||||||
render: function () {
|
render: function () {
|
||||||
this.$el.empty();
|
this.$el.empty();
|
||||||
this.$el.html(
|
var self = this,
|
||||||
"<i class='fa fa-stop' data-toggle='tooltip' " +
|
cell_action = self.column.get('cell_action');
|
||||||
"title='" + gettext('Cancel the active query') +
|
// if cancel query button then
|
||||||
"'></i>"
|
if (cell_action === 'cancel') {
|
||||||
);
|
this.$el.html(
|
||||||
|
"<i class='fa fa-stop' data-toggle='tooltip' " +
|
||||||
|
"title='" + gettext('Cancel the active query') +
|
||||||
|
"'></i>"
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
this.$el.html(
|
||||||
|
"<i class='fa fa-times-circle' data-toggle='tooltip' " +
|
||||||
|
"title='" + gettext('Terminate the session') +
|
||||||
|
"'></i>"
|
||||||
|
);
|
||||||
|
}
|
||||||
this.delegateEvents();
|
this.delegateEvents();
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
deleteRow: function(e) {
|
deleteRow: function(e) {
|
||||||
var self = this;
|
var self = this,
|
||||||
|
title, txtConfirm, txtSuccess, txtError, action_url,
|
||||||
|
cell_action = self.column.get('cell_action');
|
||||||
|
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
var canDeleteRow = Backgrid.callByNeed(
|
var canDeleteRow = Backgrid.callByNeed(
|
||||||
@ -54,88 +68,33 @@ function(url_for, gettext, r, $, _, pgAdmin, Backbone, Backgrid, Flotr,
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var title = gettext('Cancel Active Query?'),
|
if (cell_action === 'cancel') {
|
||||||
|
title = gettext('Cancel Active Query?');
|
||||||
txtConfirm = gettext('Are you sure you wish to cancel the active query?');
|
txtConfirm = gettext('Are you sure you wish to cancel the active query?');
|
||||||
|
txtSuccess = gettext('Active query cancelled successfully.')
|
||||||
alertify.confirm(
|
txtError = gettext('An error occurred whilst cancelling the active query.')
|
||||||
title,
|
action_url = cancel_query_url + self.model.get('pid')
|
||||||
txtConfirm,
|
} else {
|
||||||
function(evt) {
|
title = gettext('Terminate Session?');
|
||||||
$.ajax({
|
|
||||||
url: cancel_query_url + self.model.get('pid'),
|
|
||||||
type:'DELETE',
|
|
||||||
success: function(res) {
|
|
||||||
if (res == gettext('Success')) {
|
|
||||||
alertify.success(gettext('Active query cancelled successfully.'));
|
|
||||||
refresh_grid();
|
|
||||||
} else {
|
|
||||||
alertify.error(gettext('An error occurred whilst cancelling the active query.'));
|
|
||||||
}
|
|
||||||
},
|
|
||||||
error: function(xhr, status, error) {
|
|
||||||
try {
|
|
||||||
var err = $.parseJSON(xhr.responseText);
|
|
||||||
if (err.success == 0) {
|
|
||||||
alertify.error(err.errormsg);
|
|
||||||
}
|
|
||||||
} catch (e) {}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
function(evt) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
var terminateSessionCell = Backgrid.Extension.DeleteCell.extend({
|
|
||||||
render: function () {
|
|
||||||
this.$el.empty();
|
|
||||||
this.$el.html(
|
|
||||||
"<i class='fa fa-times-circle' data-toggle='tooltip' " +
|
|
||||||
"title='" + gettext('Terminate the session') +
|
|
||||||
"'></i>"
|
|
||||||
);
|
|
||||||
this.delegateEvents();
|
|
||||||
return this;
|
|
||||||
},
|
|
||||||
deleteRow: function(e) {
|
|
||||||
var self = this;
|
|
||||||
e.preventDefault();
|
|
||||||
|
|
||||||
var canDeleteRow = Backgrid.callByNeed(
|
|
||||||
self.column.get('canDeleteRow'), self.column, self.model
|
|
||||||
);
|
|
||||||
// If we are not allowed to cancel the query, return from here
|
|
||||||
if(!canDeleteRow)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// This will refresh the grid
|
|
||||||
var refresh_grid = function() {
|
|
||||||
if(is_server_dashboard) {
|
|
||||||
$('#btn_server_activity_refresh').click();
|
|
||||||
} else if(is_database_dashboard) {
|
|
||||||
$('#btn_database_activity_refresh').click();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
var title = gettext('Terminate Session?'),
|
|
||||||
txtConfirm = gettext('Are you sure you wish to terminate the session?');
|
txtConfirm = gettext('Are you sure you wish to terminate the session?');
|
||||||
|
txtSuccess = gettext('Session terminateed successfully.')
|
||||||
|
txtError = gettext('An error occurred whilst terminateing the active query.')
|
||||||
|
action_url = terminate_session_url + self.model.get('pid')
|
||||||
|
}
|
||||||
|
|
||||||
alertify.confirm(
|
alertify.confirm(
|
||||||
title,
|
title,
|
||||||
txtConfirm,
|
txtConfirm,
|
||||||
function(evt) {
|
function(evt) {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: terminate_session_url + self.model.get('pid'),
|
url: action_url,
|
||||||
type:'DELETE',
|
type:'DELETE',
|
||||||
success: function(res) {
|
success: function(res) {
|
||||||
if (res == gettext('Success')) {
|
if (res == gettext('Success')) {
|
||||||
alertify.success(gettext('Session terminateed successfully.'));
|
alertify.success(txtSuccess);
|
||||||
refresh_grid();
|
refresh_grid();
|
||||||
} else {
|
} else {
|
||||||
alertify.error(gettext('An error occurred whilst terminateing the active query.'));
|
alertify.error(txtError);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
error: function(xhr, status, error) {
|
error: function(xhr, status, error) {
|
||||||
@ -155,6 +114,7 @@ function(url_for, gettext, r, $, _, pgAdmin, Backbone, Backgrid, Flotr,
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// Subnode Cell, which will display subnode control
|
// Subnode Cell, which will display subnode control
|
||||||
var SessionDetailsCell = Backgrid.Extension.ObjectCell.extend({
|
var SessionDetailsCell = Backgrid.Extension.ObjectCell.extend({
|
||||||
enterEditMode: function () {
|
enterEditMode: function () {
|
||||||
@ -737,17 +697,19 @@ function(url_for, gettext, r, $, _, pgAdmin, Backbone, Backgrid, Flotr,
|
|||||||
// Add cancel active query button
|
// Add cancel active query button
|
||||||
server_activity_columns.unshift({
|
server_activity_columns.unshift({
|
||||||
name: "pg-backform-delete", label: "",
|
name: "pg-backform-delete", label: "",
|
||||||
cell: cancelQueryCell,
|
cell: customDashboardActionCell,
|
||||||
|
cell_action: 'cancel',
|
||||||
editable: false, cell_priority: -1,
|
editable: false, cell_priority: -1,
|
||||||
canDeleteRow: pgAdmin.Dashboard.can_cancel_active_query,
|
canDeleteRow: pgAdmin.Dashboard.can_take_action,
|
||||||
postgres_version: version
|
postgres_version: version
|
||||||
});
|
});
|
||||||
|
|
||||||
server_activity_columns.unshift({
|
server_activity_columns.unshift({
|
||||||
name: "pg-backform-delete", label: "",
|
name: "pg-backform-delete", label: "",
|
||||||
cell: terminateSessionCell,
|
cell: customDashboardActionCell,
|
||||||
|
cell_action: 'terminate',
|
||||||
editable: false, cell_priority: -1,
|
editable: false, cell_priority: -1,
|
||||||
canDeleteRow: pgAdmin.Dashboard.can_terminate_session,
|
canDeleteRow: pgAdmin.Dashboard.can_take_action,
|
||||||
postgres_version: version
|
postgres_version: version
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -1082,16 +1044,18 @@ function(url_for, gettext, r, $, _, pgAdmin, Backbone, Backgrid, Flotr,
|
|||||||
|
|
||||||
database_activity_columns.unshift({
|
database_activity_columns.unshift({
|
||||||
name: "pg-backform-delete", label: "",
|
name: "pg-backform-delete", label: "",
|
||||||
cell: cancelQueryCell,
|
cell: customDashboardActionCell,
|
||||||
|
cell_action: 'cancel',
|
||||||
editable: false, cell_priority: -1,
|
editable: false, cell_priority: -1,
|
||||||
canDeleteRow: pgAdmin.Dashboard.can_cancel_active_query,
|
canDeleteRow: pgAdmin.Dashboard.can_take_action,
|
||||||
postgres_version: version
|
postgres_version: version
|
||||||
});
|
});
|
||||||
database_activity_columns.unshift({
|
database_activity_columns.unshift({
|
||||||
name: "pg-backform-delete", label: "",
|
name: "pg-backform-delete", label: "",
|
||||||
cell: terminateSessionCell,
|
cell: customDashboardActionCell,
|
||||||
|
cell_action: 'terminate',
|
||||||
editable: false, cell_priority: -1,
|
editable: false, cell_priority: -1,
|
||||||
canDeleteRow: pgAdmin.Dashboard.can_terminate_session,
|
canDeleteRow: pgAdmin.Dashboard.can_take_action,
|
||||||
postgres_version: version
|
postgres_version: version
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -1264,12 +1228,15 @@ function(url_for, gettext, r, $, _, pgAdmin, Backbone, Backgrid, Flotr,
|
|||||||
toggleVisibility: function(flag) {
|
toggleVisibility: function(flag) {
|
||||||
dashboardVisible = flag;
|
dashboardVisible = flag;
|
||||||
},
|
},
|
||||||
can_cancel_active_query: function(m) {
|
can_take_action: function(m) {
|
||||||
// We will validate if user is allowed to cancel the active query
|
// We will validate if user is allowed to cancel the active query
|
||||||
// If there is only one active session means it probably our main
|
// If there is only one active session means it probably our main
|
||||||
// connection session
|
// connection session
|
||||||
var active_sessions = m.collection.where({'state': 'active'}),
|
var active_sessions = m.collection.where({'state': 'active'}),
|
||||||
pg_version = this.get('postgres_version') || null;
|
pg_version = this.get('postgres_version') || null,
|
||||||
|
cell_action = this.get('cell_action') || null,
|
||||||
|
is_cancel_session = cell_action === 'cancel',
|
||||||
|
txtAction = is_cancel_session ? gettext('cancel') : gettext('terminate');
|
||||||
|
|
||||||
// With PG10, We have background process showing on dashboard
|
// With PG10, We have background process showing on dashboard
|
||||||
// We will not allow user to cancel them as they will fail with error
|
// We will not allow user to cancel them as they will fail with error
|
||||||
@ -1278,17 +1245,21 @@ function(url_for, gettext, r, $, _, pgAdmin, Backbone, Backgrid, Flotr,
|
|||||||
// Background processes do not have database field populated
|
// Background processes do not have database field populated
|
||||||
if (pg_version && pg_version >= 100000 && !m.get('datname')) {
|
if (pg_version && pg_version >= 100000 && !m.get('datname')) {
|
||||||
alertify.info(
|
alertify.info(
|
||||||
gettext('You cannot cancel background worker processes.')
|
gettext('You cannot ') +
|
||||||
|
txtAction +
|
||||||
|
gettext(' background worker processes.')
|
||||||
);
|
);
|
||||||
return false;
|
return false;
|
||||||
// If it is the last active connection on maintenance db then error out
|
// If it is the last active connection on maintenance db then error out
|
||||||
} else if (maintenance_database == m.get('datname') &&
|
} else if (maintenance_database == m.get('datname') &&
|
||||||
m.get('state') == 'active' && active_sessions.length == 1) {
|
m.get('state') == 'active' && active_sessions.length == 1) {
|
||||||
alertify.error(
|
alertify.error(
|
||||||
gettext('You are not allowed to cancel the main active session.')
|
gettext('You are not allowed to ') +
|
||||||
|
txtAction +
|
||||||
|
gettext(' the main active session.')
|
||||||
);
|
);
|
||||||
return false;
|
return false;
|
||||||
} else if(m.get('state') == 'idle') {
|
} else if(is_cancel_session && m.get('state') == 'idle') {
|
||||||
// If this session is already idle then do nothing
|
// If this session is already idle then do nothing
|
||||||
alertify.info(
|
alertify.info(
|
||||||
gettext('The session is already in idle state.')
|
gettext('The session is already in idle state.')
|
||||||
@ -1303,49 +1274,13 @@ function(url_for, gettext, r, $, _, pgAdmin, Backbone, Backgrid, Flotr,
|
|||||||
} else {
|
} else {
|
||||||
// Do not allow to cancel someone else session to non-super user
|
// Do not allow to cancel someone else session to non-super user
|
||||||
alertify.error(
|
alertify.error(
|
||||||
gettext('Superuser privileges are required to cancel another users query.')
|
gettext('Superuser privileges are required to ') +
|
||||||
|
txtAction +
|
||||||
|
gettext(' another users query.')
|
||||||
);
|
);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
can_terminate_session: function(m) {
|
|
||||||
// We will validate if user is allowed to cancel the active query
|
|
||||||
// If there is only one active session means it probably our main
|
|
||||||
// connection session
|
|
||||||
var active_sessions = m.collection.where({'state': 'active'}),
|
|
||||||
pg_version = this.get('postgres_version') || null;
|
|
||||||
|
|
||||||
// With PG10, We have background process showing on dashboard
|
|
||||||
// We will not allow user to cancel them as they will fail with error
|
|
||||||
// anyway, so better usability we will throw our on notification
|
|
||||||
|
|
||||||
// Background processes do not have database field populated
|
|
||||||
if (pg_version && pg_version >= 100000 && !m.get('datname')) {
|
|
||||||
alertify.info(
|
|
||||||
gettext('You cannot terminate background worker processes.')
|
|
||||||
);
|
|
||||||
return false;
|
|
||||||
// If it is the last active connection on maintenance db then error out
|
|
||||||
} else if (maintenance_database == m.get('datname') &&
|
|
||||||
m.get('state') == 'active' && active_sessions.length == 1) {
|
|
||||||
alertify.error(
|
|
||||||
gettext('You are not allowed to terminate the main active session.')
|
|
||||||
);
|
|
||||||
return false;
|
|
||||||
} else if(is_super_user) {
|
|
||||||
// Super user can do anything
|
|
||||||
return true;
|
|
||||||
} else if (current_user && current_user == m.get('usename')) {
|
|
||||||
// Non-super user can terminate only their active queries
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
// Do not allow to cancel someone else session to non-super user
|
|
||||||
alertify.error(
|
|
||||||
gettext('Superuser privileges are required to terminate another users query.')
|
|
||||||
);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return pgAdmin.Dashboard;
|
return pgAdmin.Dashboard;
|
||||||
|
Loading…
Reference in New Issue
Block a user