GUAC-586: Use data source when determining active user count.

This commit is contained in:
Michael Jumper
2015-09-01 13:01:56 -07:00
parent 3c46dda5bc
commit d2924a5e79
2 changed files with 66 additions and 32 deletions

View File

@@ -93,45 +93,38 @@ 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.
*
@@ -139,8 +132,8 @@ angular.module('groupList').directive('guacGroupList', [function guacGroupList()
* 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];
};
/**
@@ -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);
@@ -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;
});
});
});
}
});

View File

@@ -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.
@@ -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;
@@ -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,
@@ -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;