Skip to content

Commit

Permalink
Merge pull request #405 from RADAR-base/release-2.1.1
Browse files Browse the repository at this point in the history
Release 2.2.0
  • Loading branch information
mpgxvii authored Jan 10, 2023
2 parents 59d3334 + b9df04d commit 992bcae
Show file tree
Hide file tree
Showing 16 changed files with 163 additions and 168 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ apply plugin: 'io.spring.dependency-management'
apply plugin: 'scala'

group = 'org.radarbase'
version = '2.1.0'
version = '2.2.0'
sourceCompatibility = 11

repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableJpaAuditing
@EnableConfigurationProperties({FcmServerConfig.class})
@EnableTransactionManagement
@EnableAsync
@EnableScheduling
public class ApplicationConfig {}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
@RestController
public class FcmNotificationController {

private transient FcmNotificationService notificationService;
private final transient FcmNotificationService notificationService;

public FcmNotificationController(FcmNotificationService notificationService) {
this.notificationService = notificationService;
Expand Down
18 changes: 5 additions & 13 deletions src/main/java/org/radarbase/appserver/converter/UserConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

package org.radarbase.appserver.converter;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import org.radarbase.appserver.dto.fcm.FcmUserDto;
Expand All @@ -40,20 +41,11 @@
public class UserConverter implements Converter<User, FcmUserDto> {

public static UserMetrics getValidUserMetrics(FcmUserDto fcmUserDto) {
UserMetrics userMetrics;
if (fcmUserDto.getLastOpened() == null && fcmUserDto.getLastDelivered() == null) {
userMetrics = new UserMetrics(LocalDateTime.now().toInstant(ZoneOffset.UTC), null);
} else if (fcmUserDto.getLastDelivered() == null) {
userMetrics = new UserMetrics(fcmUserDto.getLastOpened(), null);
} else if (fcmUserDto.getLastOpened() == null) {
userMetrics =
new UserMetrics(
LocalDateTime.now().toInstant(ZoneOffset.UTC), fcmUserDto.getLastDelivered());
} else {
userMetrics = new UserMetrics(fcmUserDto.getLastOpened(), fcmUserDto.getLastDelivered());
Instant lastOpened = fcmUserDto.getLastOpened();
if (lastOpened == null) {
lastOpened = Instant.now();
}

return userMetrics;
return new UserMetrics(lastOpened, fcmUserDto.getLastDelivered());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/** @author yatharthranjan */
@Repository
Expand All @@ -43,4 +45,7 @@ public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByFcmToken(String fcmToken);

void deleteById(@NotNull Long id);

@Transactional(propagation= Propagation.REQUIRES_NEW)
User save(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public NotificationStateEventService(

@Transactional
public void addNotificationStateEvent(NotificationStateEvent notificationStateEvent) {
if (notificationStateEvent.getState() == MessageState.CANCELLED) {
// the notification will be removed shortly
return;
}
notificationStateEventRepository.save(notificationStateEvent);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -83,14 +84,8 @@ public QuestionnaireScheduleService(ProtocolGenerator protocolGenerator, UserRep
this.taskRepository = taskRepository;
this.protocolGenerator = protocolGenerator;
this.scheduleGeneratorService = scheduleGeneratorService;
this.init();
}

public void init() {
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
executorService.scheduleWithFixedDelay(
this::generateAllSchedules, 1, 1, TimeUnit.HOURS);
}

@Transactional
public List<Task> getTasksUsingProjectIdAndSubjectId(String projectId, String subjectId) {
Expand Down Expand Up @@ -148,6 +143,11 @@ public Schedule generateScheduleUsingProjectIdAndSubjectId(String projectId, Str
@Transactional
public Schedule generateScheduleForUser(User user) {
Protocol protocol = protocolGenerator.getProtocolForSubject(user.getSubjectId());
if (protocol == null) {
Schedule emptySchedule = new Schedule();
subjectScheduleMap.put(user.getSubjectId(), emptySchedule);
return emptySchedule;
}
Schedule prevSchedule = getScheduleForSubject(user.getSubjectId());
String prevTimezone = prevSchedule.getTimezone() != null ? prevSchedule.getTimezone() : user.getTimezone();
if (!Objects.equals(prevSchedule.getVersion(), protocol.getVersion()) || !prevTimezone.equals(user.getTimezone())) {
Expand All @@ -174,14 +174,15 @@ public Schedule generateScheduleUsingProjectIdAndSubjectIdAndAssessment(String p
return schedule;
}

public Map<String, Schedule> generateAllSchedules() {
@Scheduled(fixedRate = 3_600_000)
public void generateAllSchedules() {
List<User> users = this.userRepository.findAll();

return users.parallelStream()
users.parallelStream()
.map(u -> {
Schedule schedule = this.generateScheduleForUser(u);
return new Pair<String, Schedule>(u.getSubjectId(), schedule);
}).collect(Collectors.toMap(Pair::getKey, Pair::getValue));
});
}

public Schedule getScheduleForSubject(String subjectId) {
Expand Down
Loading

0 comments on commit 992bcae

Please sign in to comment.