Skip to content
This repository has been archived by the owner on Jul 17, 2023. It is now read-only.

Commit

Permalink
GUAC-586: Use data source when determining active user count.
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-jumper committed Sep 3, 2015
1 parent 3c46dda commit d2924a5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 32 deletions.
83 changes: 56 additions & 27 deletions guacamole/src/main/webapp/app/groupList/directives/guacGroupList.js
Expand Up @@ -93,54 +93,47 @@ angular.module('groupList').directive('guacGroupList', [function guacGroupList()

// Required services
var activeConnectionService = $injector.get('activeConnectionService');
var dataSourceService = $injector.get('dataSourceService');

// Required types
var GroupListItem = $injector.get('GroupListItem');

/**
* The number of active connections associated with a given
* connection identifier. If this information is unknown, or there
* are no active connections for a given identifier, no number will
* be stored.
* Map of data source identifier to the number of active
* connections associated with a given connection identifier.
* If this information is unknown, or there are no active
* connections for a given identifier, no number will be stored.
*
* @type Object.<String, Number>
* @type Object.<String, Object.<String, Number>>
*/
var connectionCount = {};

/**
* A list of all items which should appear at the root level. As
* connections and connection groups from multiple data sources may
* be included in a guacGroupList, there may be multiple root
* items, even if the root connection group is shown.
*
* @type GroupListItem[]
*/
$scope.rootItems = [];

// Count active connections by connection identifier
activeConnectionService.getActiveConnections()
.success(function activeConnectionsRetrieved(activeConnections) {

// Count each active connection by identifier
angular.forEach(activeConnections, function addActiveConnection(activeConnection) {

// If counter already exists, increment
var identifier = activeConnection.connectionIdentifier;
if (connectionCount[identifier])
connectionCount[identifier]++;

// Otherwise, initialize counter to 1
else
connectionCount[identifier] = 1;

});

});

/**
* Returns the number of active usages of a given connection.
*
* @param {String} dataSource
* The identifier of the data source containing the given
* connection.
*
* @param {Connection} connection
* The connection whose active connections should be counted.
*
* @returns {Number}
* The number of currently-active usages of the given
* connection.
*/
var countActiveConnections = function countActiveConnections(connection) {
return connectionCount[connection.identifier];
var countActiveConnections = function countActiveConnections(dataSource, connection) {
return connectionCount[dataSource][connection.identifier];
};

/**
Expand Down Expand Up @@ -178,12 +171,22 @@ angular.module('groupList').directive('guacGroupList', [function guacGroupList()
// Set contents whenever the connection group is assigned or changed
$scope.$watch('connectionGroups', function setContents(connectionGroups) {

// Reset stored data
var dataSources = [];
$scope.rootItems = [];
connectionCount = {};

// If connection groups are given, add them to the interface
if (connectionGroups) {

// Add each provided connection group
angular.forEach(connectionGroups, function addConnectionGroup(connectionGroup, dataSource) {

// Prepare data source for active connection counting
dataSources.push(dataSource);
connectionCount[dataSource] = {};

// Create root item for current connection group
var rootItem = GroupListItem.fromConnectionGroup(dataSource, connectionGroup,
!!$scope.connectionTemplate, countActiveConnections);

Expand All @@ -199,6 +202,32 @@ angular.module('groupList').directive('guacGroupList', [function guacGroupList()
}

});

// Count active connections by connection identifier
dataSourceService.apply(
activeConnectionService.getActiveConnections,
dataSources
)
.then(function activeConnectionsRetrieved(activeConnectionMap) {

// Within each data source, count each active connection by identifier
angular.forEach(activeConnectionMap, function addActiveConnections(activeConnections, dataSource) {
angular.forEach(activeConnections, function addActiveConnection(activeConnection) {

// If counter already exists, increment
var identifier = activeConnection.connectionIdentifier;
if (connectionCount[dataSource][identifier])
connectionCount[dataSource][identifier]++;

// Otherwise, initialize counter to 1
else
connectionCount[dataSource][identifier] = 1;

});
});

});

}

});
Expand Down
15 changes: 10 additions & 5 deletions guacamole/src/main/webapp/app/groupList/types/GroupListItem.js
Expand Up @@ -143,7 +143,9 @@ angular.module('groupList').factory('GroupListItem', ['ConnectionGroup', functio
* @param {Function} [countActiveConnections]
* A getter which returns the current number of active connections for
* the given connection. If omitted, the number of active connections
* known at the time this function was called is used instead.
* known at the time this function was called is used instead. This
* function will be passed, in order, the data source identifier and
* the connection in question.
*
* @returns {GroupListItem}
* A new GroupListItem which represents the given connection.
Expand All @@ -169,7 +171,7 @@ angular.module('groupList').factory('GroupListItem', ['ConnectionGroup', functio

// Use getter, if provided
if (countActiveConnections)
return countActiveConnections(connection);
return countActiveConnections(dataSource, connection);

return connection.activeConnections;

Expand Down Expand Up @@ -201,13 +203,16 @@ angular.module('groupList').factory('GroupListItem', ['ConnectionGroup', functio
* @param {Function} [countActiveConnections]
* A getter which returns the current number of active connections for
* the given connection. If omitted, the number of active connections
* known at the time this function was called is used instead.
* known at the time this function was called is used instead. This
* function will be passed, in order, the data source identifier and
* the connection group in question.
*
* @param {Function} [countActiveConnectionGroups]
* A getter which returns the current number of active connections for
* the given connection group. If omitted, the number of active
* connections known at the time this function was called is used
* instead.
* instead. This function will be passed, in order, the data source
* identifier and the connection group in question.
*
* @returns {GroupListItem}
* A new GroupListItem which represents the given connection group,
Expand Down Expand Up @@ -257,7 +262,7 @@ angular.module('groupList').factory('GroupListItem', ['ConnectionGroup', functio

// Use getter, if provided
if (countActiveConnectionGroups)
return countActiveConnectionGroups(connectionGroup);
return countActiveConnectionGroups(dataSource, connectionGroup);

return connectionGroup.activeConnections;

Expand Down

0 comments on commit d2924a5

Please sign in to comment.