Skip to content

Commit

Permalink
EVDOC01-155: Millora als logs de jobs/hooks (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
mollerentornos authored Oct 30, 2024
1 parent 9326b20 commit 7bb2a01
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public static enum PermissionRoles { READ, WRITE }
boolean addMemberToTeam(String userId, String teamId) throws MicrosoftCredentialsException;
boolean addOwnerToTeam(String userId, String teamId) throws MicrosoftCredentialsException;

SynchronizationStatus addUsersToTeamOrGroup(String teamId, List<MicrosoftUser> members, SynchronizationStatus status, LinkedList<String> roles) throws MicrosoftCredentialsException;
SynchronizationStatus addUsersToTeamOrGroup(SiteSynchronization ss, List<MicrosoftUser> members, SynchronizationStatus status, LinkedList<String> roles) throws MicrosoftCredentialsException;

boolean removeUserFromGroup(String userId, String groupId) throws MicrosoftCredentialsException;
boolean removeMemberFromTeam(String memberId, String teamId) throws MicrosoftCredentialsException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@

import org.sakaiproject.microsoft.api.model.MicrosoftLog;

import java.time.ZonedDateTime;
import java.util.List;

public interface MicrosoftLoggingService {

void saveLog(MicrosoftLog log);

List<MicrosoftLog> findAll();

List<MicrosoftLog> findFromZonedDateTime(ZonedDateTime zonedDateTime);
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ public class MicrosoftLog {
public static final String EVENT_TOO_MANY_REQUESTS = "event.too_many_requests";
public static final String EVENT_USER_NOT_FOUND_ON_TEAM = "event.user_not_found_on_team";

public static final String EVENT_JOB_RESULT = "event.job_result";

public static final String[] MICROSOFT_ERRORS = {EVENT_TOO_MANY_REQUESTS};

public enum Status {
KO, OK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
import org.sakaiproject.microsoft.api.model.MicrosoftLog;
import org.sakaiproject.serialization.SerializableRepository;

import java.time.ZonedDateTime;
import java.util.List;

public interface MicrosoftLoggingRepository extends SerializableRepository<MicrosoftLog, String> {

List<MicrosoftLog> getLogsFromZonedDateTime(ZonedDateTime zonedDateTime);
}
Original file line number Diff line number Diff line change
Expand Up @@ -1202,8 +1202,9 @@ public boolean addOwnerToTeam(String userId, String teamId) throws MicrosoftCred
}

@Override
public SynchronizationStatus addUsersToTeamOrGroup(String teamId, List<MicrosoftUser> members, SynchronizationStatus status, LinkedList<String> roles) throws MicrosoftCredentialsException {
public SynchronizationStatus addUsersToTeamOrGroup(SiteSynchronization ss, List<MicrosoftUser> members, SynchronizationStatus status, LinkedList<String> roles) throws MicrosoftCredentialsException {
boolean res = false;
String teamId = ss.getTeamId();
String dataKey = roles.contains(MicrosoftUser.OWNER) ? "ownerId" : "memberId";
boolean generalError = false;

Expand Down Expand Up @@ -1269,6 +1270,7 @@ public SynchronizationStatus addUsersToTeamOrGroup(String teamId, List<Microsoft
.status((pendingMember != null && pendingMember.isGuest()) ? MicrosoftLog.Status.OK : MicrosoftLog.Status.KO)
.addData("origin", sakaiProxy.getActionOrigin())
.addData("teamId", teamId)
.addData("siteId", ss.getSiteId())
.addData(dataKey, pendingMember != null ? pendingMember.getId() : "null")
.build());

Expand All @@ -1281,6 +1283,7 @@ public SynchronizationStatus addUsersToTeamOrGroup(String teamId, List<Microsoft
.status(MicrosoftLog.Status.OK)
.addData("origin", sakaiProxy.getActionOrigin())
.addData("teamId", teamId)
.addData("siteId", ss.getSiteId())
.addData(dataKey, member.getId())
.build());
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import lombok.Setter;
import lombok.extern.log4j.Log4j2;

import java.time.ZonedDateTime;
import java.util.List;

@Log4j2
@Transactional
public class MicrosoftLoggingServiceImpl implements MicrosoftLoggingService {
Expand All @@ -35,6 +38,15 @@ public class MicrosoftLoggingServiceImpl implements MicrosoftLoggingService {
public void saveLog(MicrosoftLog log) {
microsoftLoggingRepository.save(log);
}


@Override
public List<MicrosoftLog> findAll() {
return (List<MicrosoftLog>) microsoftLoggingRepository.findAll();
}

@Override
public List<MicrosoftLog> findFromZonedDateTime(ZonedDateTime zonedDateTime) {
return (List<MicrosoftLog>) microsoftLoggingRepository.getLogsFromZonedDateTime(zonedDateTime);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ public SynchronizationStatus runSiteSynchronization(SiteSynchronization ss) thro
}
}
//add members to team
ret = microsoftCommonService.addUsersToTeamOrGroup(ss.getTeamId(), filteredMembersFound, ret, new LinkedList<>());
ret = microsoftCommonService.addUsersToTeamOrGroup(ss, filteredMembersFound, ret, new LinkedList<>());

List<MicrosoftUser> filteredOwnersFound = microsoftCommonService.getUsers(filteredSiteMembers.getOwners().keySet(), mappedMicrosoftUserId);
List<String> filteredOwnersNotFound = filteredSiteMembers.getOwners().keySet().stream().filter(id -> filteredOwnersFound.stream().noneMatch(mu -> mu != null && mu.getEmail().equalsIgnoreCase(id))).collect(Collectors.toList());
Expand Down Expand Up @@ -741,7 +741,7 @@ public SynchronizationStatus runSiteSynchronization(SiteSynchronization ss) thro
}
}

ret = microsoftCommonService.addUsersToTeamOrGroup(ss.getTeamId(), filteredOwnersFound, ret, new LinkedList<>(Collections.singletonList(MicrosoftUser.OWNER)));
ret = microsoftCommonService.addUsersToTeamOrGroup(ss, filteredOwnersFound, ret, new LinkedList<>(Collections.singletonList(MicrosoftUser.OWNER)));

//process all group synchronizations related
if (ss.getGroupSynchronizationsList() != null && ss.getGroupSynchronizationsList().size() > 0) {
Expand Down Expand Up @@ -1141,7 +1141,7 @@ private void siteCreated(String siteId) throws MicrosoftGenericException {
if (site != null) {
//check filters
if (!siteFilter.match(site)) {
break;
return;
}

teamId = microsoftCommonService.createTeam(site.getTitle(), credentials.getEmail());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,26 @@
*/
package org.sakaiproject.microsoft.impl.jobs;

import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.sakaiproject.authz.api.SecurityService;
import org.sakaiproject.microsoft.api.MicrosoftConfigurationService;
import org.sakaiproject.microsoft.api.MicrosoftLoggingService;
import org.sakaiproject.microsoft.api.MicrosoftSynchronizationService;
import org.sakaiproject.microsoft.api.data.MicrosoftLogInvokers;
import org.sakaiproject.microsoft.api.data.SakaiSiteFilter;
import org.sakaiproject.microsoft.api.data.SynchronizationStatus;
import org.sakaiproject.microsoft.api.exceptions.MicrosoftGenericException;
import org.sakaiproject.microsoft.api.model.MicrosoftLog;
import org.sakaiproject.microsoft.api.model.SiteSynchronization;
import org.sakaiproject.tool.api.Session;
import org.sakaiproject.tool.api.SessionManager;
Expand All @@ -45,6 +53,9 @@ public class RunSynchronizationsJob implements Job {

@Setter
private MicrosoftSynchronizationService microsoftSynchronizationService;

@Setter
private MicrosoftLoggingService microsoftLoggingService;

@Setter
MicrosoftConfigurationService microsoftConfigurationService;
Expand All @@ -57,6 +68,8 @@ public void init() {
public void execute(JobExecutionContext context) throws JobExecutionException {
log.info("RunSynchronizationsJob started.");
Session session = sessionManager.getCurrentSession();
ZonedDateTime startTime = ZonedDateTime.now();

try {
session.setUserEid("admin");
session.setUserId("admin");
Expand Down Expand Up @@ -88,6 +101,19 @@ public void execute(JobExecutionContext context) throws JobExecutionException {
finally {
session.clear();
}

List<MicrosoftLog> logs = microsoftLoggingService.findFromZonedDateTime(startTime);
//if log is not from job invoker and is an error, remove it
logs = logs.stream().filter(l -> Arrays.stream(MicrosoftLog.MICROSOFT_ERRORS).noneMatch(e -> e.equals(l.getEvent())) && l.getContext().containsKey("origin") && l.getContext().get("origin").equals(MicrosoftLogInvokers.JOB.getCode())).collect(Collectors.toList());
Set<String> sites = logs.stream().filter(l -> l.getEvent().equals(MicrosoftLog.EVENT_ADD_MEMBER) || l.getEvent().equals(MicrosoftLog.EVENT_ADD_OWNER) || l.getEvent().equals(MicrosoftLog.EVENT_USER_ADDED_TO_CHANNEL)).collect(Collectors.toList()).stream().map(l -> l.getContext().get("siteId")).collect(Collectors.toSet());
Map<String, String> data = new HashMap<>();
data.put("log_amount", String.valueOf(logs.size()));
data.put("start_time", startTime.toLocalDateTime().toString());
data.put("sites_modified_amount", String.valueOf(sites.size()));
data.put("sites_modified", String.join(",", sites));
data.putAll(logs.stream().collect(Collectors.groupingBy(MicrosoftLog::getEvent, Collectors.counting())).entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> String.valueOf(e.getValue()))));

microsoftLoggingService.saveLog(MicrosoftLog.builder().event(MicrosoftLog.EVENT_JOB_RESULT).context(data).build());

log.info("RunSynchronizationsJob completed.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@
import org.sakaiproject.microsoft.api.persistence.MicrosoftLoggingRepository;
import org.sakaiproject.serialization.BasicSerializableRepository;

import java.time.ZonedDateTime;
import java.util.List;
import java.util.stream.Collectors;

public class MicrosoftLoggingRepositoryImpl extends BasicSerializableRepository<MicrosoftLog, String> implements MicrosoftLoggingRepository {

@Override
public List<MicrosoftLog> getLogsFromZonedDateTime(ZonedDateTime zonedDateTime) {
String query = "SELECT ml FROM MicrosoftLog ml WHERE ml.eventDate >= :zonedDateTime";
query += " ORDER BY ml.id DESC, ml.eventDate DESC";
return sessionFactory.getCurrentSession().createQuery(query, MicrosoftLog.class)
.setParameter("zonedDateTime", zonedDateTime)
.stream()
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
<bean id="org.sakaiproject.microsoft.impl.jobs.RunSynchronizationsJob"
class="org.sakaiproject.microsoft.impl.jobs.RunSynchronizationsJob">
<property name="microsoftSynchronizationService" ref="org.sakaiproject.microsoft.api.MicrosoftSynchronizationService" />
<property name="MicrosoftLoggingService" ref="org.sakaiproject.microsoft.api.MicrosoftLoggingService" />
<property name="microsoftConfigurationService" ref="org.sakaiproject.microsoft.api.MicrosoftConfigurationService" />
<property name="securityService" ref="org.sakaiproject.authz.api.SecurityService" />
<property name="sessionManager" ref="org.sakaiproject.tool.api.SessionManager" />
Expand Down

0 comments on commit 7bb2a01

Please sign in to comment.