Skip to content

Commit

Permalink
Merge pull request CESNET#3146 from cuadradek/groupStatsMethods
Browse files Browse the repository at this point in the history
Core - new methods for group stats
  • Loading branch information
zlamalp authored Mar 31, 2021
2 parents d564213 + 548a3a3 commit 33469f4
Show file tree
Hide file tree
Showing 8 changed files with 243 additions and 0 deletions.
20 changes: 20 additions & 0 deletions perun-base/src/main/resources/perun-roles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1456,6 +1456,26 @@ perun_policies:
include_policies:
- default_policy

getGroupMembersCountsByVoStatus_Group_policy:
policy_roles:
- PERUNOBSERVER:
- VOOBSERVER: Vo
- GROUPADMIN: Group
- GROUPOBSERVER: Group
- VOADMIN: Vo
include_policies:
- default_policy

getGroupMembersCountsByGroupStatus_Group_policy:
policy_roles:
- PERUNOBSERVER:
- VOOBSERVER: Vo
- GROUPADMIN: Group
- GROUPOBSERVER: Group
- VOADMIN: Vo
include_policies:
- default_policy

addAdmin_Group_User_policy:
policy_roles:
- GROUPADMIN: Group
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,28 @@ public interface GroupsManager {
*/
int getGroupMembersCount(PerunSession perunSession, Group group) throws GroupNotExistsException, PrivilegeException;

/**
* Returns counts of group members by their status in VO.
*
* @param sess
* @param group
* @return map of member status in VO to count of group members with the status
* @throws GroupNotExistsException when the group doesn't exist
* @throws PrivilegeException
*/
Map<Status, Integer> getGroupMembersCountsByVoStatus(PerunSession sess, Group group) throws GroupNotExistsException, PrivilegeException;

/**
* Returns counts of group members by their group status.
*
* @param sess
* @param group
* @return map of member status in group to count of group members with the status
* @throws GroupNotExistsException when the group doesn't exist
* @throws PrivilegeException
*/
Map<MemberGroupStatus, Integer> getGroupMembersCountsByGroupStatus(PerunSession sess, Group group) throws GroupNotExistsException, PrivilegeException;

/**
* Get groups of Vo by ACCESS RIGHTS:
* If user is:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,24 @@ public interface GroupsManagerBl {
*/
int getGroupMembersCount(PerunSession perunSession, Group group);

/**
* Returns counts of group members by their status in VO.
*
* @param sess
* @param group
* @return map of member status in VO to count of group members with the status
*/
Map<Status, Integer> getGroupMembersCountsByVoStatus(PerunSession sess, Group group);

/**
* Returns counts of group members by their group status.
*
* @param sess
* @param group
* @return map of member status in group to count of group members with the status
*/
Map<MemberGroupStatus, Integer> getGroupMembersCountsByGroupStatus(PerunSession sess, Group group);

/**
* Checks whether the user is member of the group.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1297,6 +1297,32 @@ public int getGroupMembersCount(PerunSession sess, Group group) {
return members.size();
}

@Override
public Map<Status, Integer> getGroupMembersCountsByVoStatus(PerunSession sess, Group group) {
List<Member> members = this.getGroupMembers(sess, group);

Map<Status, Integer> counts = new HashMap<>();
for (Status status : Status.values()) {
counts.put(status, 0);
}
members.forEach(member -> counts.computeIfPresent(member.getStatus(), (key, value) -> value + 1));

return counts;
}

@Override
public Map<MemberGroupStatus, Integer> getGroupMembersCountsByGroupStatus(PerunSession sess, Group group) {
List<Member> members = this.getGroupMembers(sess, group);

Map<MemberGroupStatus, Integer> counts = new HashMap<>();
for (MemberGroupStatus status : MemberGroupStatus.values()) {
counts.put(status, 0);
}
members.forEach(member -> counts.computeIfPresent(member.getGroupStatus(), (key, value) -> value + 1));

return counts;
}

@Override
public List<User> getAdmins(PerunSession perunSession, Group group, boolean onlyDirectAdmins) {
if(onlyDirectAdmins) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,32 @@ public int getGroupMembersCount(PerunSession sess, Group group) throws GroupNotE
return getGroupsManagerBl().getGroupMembersCount(sess, group);
}

@Override
public Map<Status, Integer> getGroupMembersCountsByVoStatus(PerunSession sess, Group group) throws GroupNotExistsException, PrivilegeException {
Utils.checkPerunSession(sess);
getGroupsManagerBl().checkGroupExists(sess, group);

// Authorization
if (!AuthzResolver.authorizedInternal(sess, "getGroupMembersCountsByVoStatus_Group_policy", group)) {
throw new PrivilegeException(sess, "getGroupMembersCountsByVoStatus");
}

return getGroupsManagerBl().getGroupMembersCountsByVoStatus(sess, group);
}

@Override
public Map<MemberGroupStatus, Integer> getGroupMembersCountsByGroupStatus(PerunSession sess, Group group) throws GroupNotExistsException, PrivilegeException {
Utils.checkPerunSession(sess);
getGroupsManagerBl().checkGroupExists(sess, group);

// Authorization
if (!AuthzResolver.authorizedInternal(sess, "getGroupMembersCountsByGroupStatus_Group_policy", group)) {
throw new PrivilegeException(sess, "getGroupMembersCountsByGroupStatus");
}

return getGroupsManagerBl().getGroupMembersCountsByGroupStatus(sess, group);
}

@Override
public void addAdmin(PerunSession sess, Group group, User user) throws AlreadyAdminException, PrivilegeException, GroupNotExistsException, UserNotExistsException, RoleCannotBeManagedException {
Utils.checkPerunSession(sess);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3004,6 +3004,60 @@ public void getGroupMembersCountWhenGroupNotExists() throws Exception {

}

@Test
public void getGroupMembersCountsByVoStatus() throws Exception {
System.out.println(CLASS_NAME + "getGroupMembersCountsByVoStatus");

vo = setUpVo();
setUpGroup(vo);

Member member = setUpMember(vo);
groupsManager.addMember(sess, group, member);

Member disabledMember = setUpMember(vo);
groupsManager.addMember(sess, group, disabledMember);
perun.getMembersManager().setStatus(sess, disabledMember, Status.DISABLED);

Map<Status, Integer> counts = groupsManager.getGroupMembersCountsByVoStatus(sess, group);
assertThat(counts.get(Status.VALID)).isEqualTo(1);
assertThat(counts.get(Status.DISABLED)).isEqualTo(1);
assertThat(counts.get(Status.INVALID)).isEqualTo(0);
assertThat(counts.get(Status.EXPIRED)).isEqualTo(0);
}

@Test (expected=GroupNotExistsException.class)
public void getGroupMembersCountsByVoStatusWhenGroupNotExists() throws Exception {
System.out.println(CLASS_NAME + "getGroupMembersCountsByVoStatusWhenGroupNotExists");

groupsManager.getGroupMembersCountsByVoStatus(sess, new Group());
}

@Test
public void getGroupMembersCountsByGroupStatus() throws Exception {
System.out.println(CLASS_NAME + "getGroupMembersCountsByGroupStatus");

vo = setUpVo();
setUpGroup(vo);

Member member = setUpMember(vo);
groupsManager.addMember(sess, group, member);

Member expiredMember = setUpMember(vo);
groupsManager.addMember(sess, group, expiredMember);
groupsManager.setMemberGroupStatus(sess, expiredMember, group, MemberGroupStatus.EXPIRED);

Map<MemberGroupStatus, Integer> counts = groupsManager.getGroupMembersCountsByGroupStatus(sess, group);
assertThat(counts.get(MemberGroupStatus.VALID)).isEqualTo(1);
assertThat(counts.get(MemberGroupStatus.EXPIRED)).isEqualTo(1);
}

@Test (expected=GroupNotExistsException.class)
public void getGroupMembersCountsByGroupStatusWhenGroupNotExists() throws Exception {
System.out.println(CLASS_NAME + "getGroupMembersCountsByGroupStatusWhenGroupNotExists");

groupsManager.getGroupMembersCountsByGroupStatus(sess, new Group());
}

@Test
public void getAllGroups() throws Exception {
System.out.println(CLASS_NAME + "getAllGroups");
Expand Down
37 changes: 37 additions & 0 deletions perun-openapi/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,15 @@ components:
additionalProperties:
type: string

MapStringIntegerResponse:
description: "returns Map<String,Integer>"
content:
application/json:
schema:
type: object
additionalProperties:
type: integer

MapStringMapStringStringResponse:
description: "returns Map<String,Map<String,String>>"
content:
Expand Down Expand Up @@ -11374,6 +11383,34 @@ paths:
default:
$ref: '#/components/responses/ExceptionResponse'

/json/groupsManager/getGroupMembersCountsByVoStatus:
get:
tags:
- GroupsManager
operationId: getGroupMembersCountsByVoStatus
summary: Returns counts of group members by their status in VO.
parameters:
- $ref: '#/components/parameters/groupId'
responses:
'200':
$ref: '#/components/responses/MapStringIntegerResponse'
default:
$ref: '#/components/responses/ExceptionResponse'

/json/groupsManager/getGroupMembersCountsByGroupStatus:
get:
tags:
- GroupsManager
operationId: getGroupMembersCountsByGroupStatus
summary: Returns counts of group members by their group status.
parameters:
- $ref: '#/components/parameters/groupId'
responses:
'200':
$ref: '#/components/responses/MapStringIntegerResponse'
default:
$ref: '#/components/responses/ExceptionResponse'

/json/groupsManager/getMemberRichGroupsWithAttributesByNames:
get:
tags:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cz.metacentrum.perun.core.api.Group;
import cz.metacentrum.perun.core.api.Member;
Expand All @@ -13,6 +14,7 @@
import cz.metacentrum.perun.core.api.RichGroup;
import cz.metacentrum.perun.core.api.RichMember;
import cz.metacentrum.perun.core.api.RichUser;
import cz.metacentrum.perun.core.api.Status;
import cz.metacentrum.perun.core.api.User;
import cz.metacentrum.perun.core.api.Vo;
import cz.metacentrum.perun.core.api.exceptions.NotGroupMemberException;
Expand Down Expand Up @@ -715,6 +717,44 @@ public Integer call(ApiCaller ac, Deserializer parms) throws PerunException {
}
},

/*#
* Returns counts of group members by their status in VO.
*
* @throw GroupNotExistsException When the group doesn't exist
*
* @param group int Group <code>id</code>
* @return Map<String, Integer> map of member status in VO to count of group members with the status
*/
getGroupMembersCountsByVoStatus {
@Override
public Map<String, Integer> call(ApiCaller ac, Deserializer parms) throws PerunException {
Map<Status, Integer> counts = ac.getGroupsManager().getGroupMembersCountsByVoStatus(ac.getSession(),
ac.getGroupById(parms.readInt("group")));

// convert Status to String
return counts.entrySet().stream().collect(Collectors.toMap(e -> e.getKey().toString(), Map.Entry::getValue));
}
},

/*#
* Returns counts of group members by their group status.
*
* @throw GroupNotExistsException When the group doesn't exist
*
* @param group int Group <code>id</code>
* @return Map<String, Integer> map of member status in group to count of group members with the status
*/
getGroupMembersCountsByGroupStatus {
@Override
public Map<String, Integer> call(ApiCaller ac, Deserializer parms) throws PerunException {
Map<MemberGroupStatus, Integer> counts = ac.getGroupsManager().getGroupMembersCountsByGroupStatus(ac.getSession(),
ac.getGroupById(parms.readInt("group")));

// convert MemberGroupStatus to String
return counts.entrySet().stream().collect(Collectors.toMap(e -> e.getKey().toString(), Map.Entry::getValue));
}
},

/*#
* Returns all groups in a VO.
*
Expand Down

0 comments on commit 33469f4

Please sign in to comment.