Skip to content

Commit

Permalink
Add Database -> Show Connected Clients
Browse files Browse the repository at this point in the history
  • Loading branch information
Paxa committed Nov 13, 2021
1 parent 12b725c commit d65306b
Show file tree
Hide file tree
Showing 10 changed files with 314 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app/controllers/server_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class ServerController {
async showConnectionList () {
var server = global.App.currentTab.instance.connection.server;
var dialog = new Dialog.ConnectionList(server);
// dialog.showWindow();
}
}


module.exports = ServerController;
global.ServerController = ServerController;
6 changes: 6 additions & 0 deletions app/models/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ class Server extends ModelBase {
});
}

listConnections(variable) {
return this.q(`SELECT * FROM pg_stat_activity \n order by state_change is null, datname nulls first, usename, application_name`).then(data => {
return Promise.resolve(data.rows);
});
}

createDatabase(dbname, template, encoding) {
var sql = `CREATE DATABASE "${dbname}"`;
if (encoding) sql += " ENCODING '" + encoding + "'";
Expand Down
10 changes: 10 additions & 0 deletions app/top_menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ var template /*: Electron.MenuItemConstructorOptions[] */ = [
{
label: 'Database',
submenu: [
{
label: 'Show Connected Clients',
click: () => {
(new ServerController).showConnectionList();
},
enabled: false
},
{ type: 'separator' },
{
label: 'Create Database',
click: () => {
Expand Down Expand Up @@ -339,11 +347,13 @@ var checkDbMenu = function () {
}

if (db && db != Connection.defaultDatabaseName) {
enableItem("Database", "Show Connected Clients");
enableItem("Database", "Refresh Database");
enableItem("Database", "Rename Database");
enableItem("Database", "Export Database");
enableItem("Database", "Drop Database");
} else {
disableItem("Database", "Show Connected Clients");
disableItem("Database", "Refresh Database");
disableItem("Database", "Rename Database");
disableItem("Database", "Export Database");
Expand Down
1 change: 1 addition & 0 deletions app/views/dialogs/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ module.exports = {
RelatedRecords: require('./related_records'),
ShowSql: require('./show_sql'),
UserGrants: require('./user_grants'),
ConnectionList: require('./connection_list'),
};
27 changes: 27 additions & 0 deletions app/views/dialogs/connection_list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class ConnectionList extends DialogBase {
/*::
server: string[]
*/
constructor (server) {
super(null, {
title: "Connected Clients",
dialogClass: "connection-list",
server: server
});
this.showWindow();
}

async showWindow () {
var connections = await this.server.listConnections();
var nodes = App.renderView('dialogs/connection_list', {connections: connections});
this.content = this.renderWindow(this.title, nodes);
this.content.find('code').forEach(codeElement => {
window.hljs.highlightBlock(codeElement);
})
}
}

/*::
declare var ConnectionList__: typeof ConnectionList
*/
module.exports = ConnectionList;
31 changes: 31 additions & 0 deletions assets/styles/elements/_dialogs.scss
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,35 @@
margin-top: 15px;
}
}
}

.alertify-inner.connection-list {
width: 900px;
//max-width: 350px;
margin-bottom: 5px;
table {
td, th {
word-break: break-all;
}
code {
max-width: 700px;
margin: 1px 0;

@include dialog-code;

padding: 3px 5px;
overflow: auto;
max-height: 400px;
box-shadow: inset 0px 0px 2px 0px rgba(0, 0, 0, 0.15);
text-align: left;
white-space: pre-wrap;
word-break: normal;
}
i.null {
font-style: normal;
color: #b8b8b8;
font-size: 12px;
opacity: 0.75;
}
}
}
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ global.Model = require('./app/models/all');
require('./app/controllers/import_controller');
require('./app/controllers/export_controller');
require('./app/controllers/updates_controller');
require('./app/controllers/server_controller');

require('./app/views/history_window');

Expand Down
26 changes: 26 additions & 0 deletions public/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -1918,6 +1918,32 @@ small.help-icon {
.related-records-dialog table ~ table {
margin-top: 15px; }

.alertify-inner.connection-list {
width: 900px;
margin-bottom: 5px; }
.alertify-inner.connection-list table td, .alertify-inner.connection-list table th {
word-break: break-all; }
.alertify-inner.connection-list table code {
max-width: 700px;
margin: 1px 0;
padding: 5px;
background: #f5f5f5;
display: block;
font-family: Consolas, "Liberation Mono", Menlo, Monaco, Lucida Console, Courier New, monospace, serif;
word-break: break-all;
padding: 3px 5px;
overflow: auto;
max-height: 400px;
box-shadow: inset 0px 0px 2px 0px rgba(0, 0, 0, 0.15);
text-align: left;
white-space: pre-wrap;
word-break: normal; }
.alertify-inner.connection-list table i.null {
font-style: normal;
color: #b8b8b8;
font-size: 12px;
opacity: 0.75; }

/*
*Original style from softwaremaniacs.org (c) Ivan Sagalaev <[email protected]> */
.hljs {
Expand Down
169 changes: 169 additions & 0 deletions views/cache.js

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions views/dialogs/connection_list.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
.rescol-wrapper.with-borders
.rescol-header-wrapper
.rescol-content-wrapper
table
thead
tr
th PID
th Database
th User
th IP
th Application
th State
th Query
tbody
each client in connections
tr
td= client.pid
td
if client.datname
= client.datname
else
i.null NULL
td= client.usename
td= client.client_addr
td= client.state_change ? client.application_name : client.backend_type
td= client.state
td
if client.state == 'active'
code.result.pgsql= client.query

p.buttons
button.cancel Close

0 comments on commit d65306b

Please sign in to comment.