Skip to content

Commit

Permalink
Merge branch 'dev' into documentation-update
Browse files Browse the repository at this point in the history
  • Loading branch information
blootsvoets authored Oct 9, 2023
2 parents 6895c3b + b4ebd3e commit 0fed45f
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 37 deletions.
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ apply plugin: 'io.spring.dependency-management'
apply plugin: 'scala'

group = 'org.radarbase'
version = '2.4.0-SNAPSHOT'
version = '2.4.1-SNAPSHOT'

java {
toolchain {
Expand Down Expand Up @@ -57,7 +57,6 @@ sourceSets {

dependencies {
implementation('org.springframework.boot:spring-boot-starter-data-jpa')
implementation('org.springframework.boot:spring-boot-starter-data-rest')
implementation('org.springframework.boot:spring-boot-starter-web')
implementation('org.springframework.boot:spring-boot-starter-quartz')
implementation('org.springframework.boot:spring-boot-starter-security')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.radarbase.appserver.event.state.dto.DataMessageStateEventDto;
import org.radarbase.appserver.event.state.dto.NotificationStateEventDto;
import org.radarbase.appserver.service.DataMessageStateEventService;
import org.radarbase.appserver.service.NotificationStateEventService;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.event.TransactionalEventListener;

import java.util.Map;

@Slf4j
@Component
Expand All @@ -55,7 +59,9 @@ public MessageStateEventListener(ObjectMapper objectMapper,
*
* @param event the event to respond to
*/
@EventListener(value = NotificationStateEventDto.class)
@Transactional(propagation = Propagation.REQUIRES_NEW)
@TransactionalEventListener(classes = NotificationStateEventDto.class)
@Async
public void onNotificationStateChange(NotificationStateEventDto event) {
String info = convertMapToString(event.getAdditionalInfo());
log.debug("ID: {}, STATE: {}", event.getNotification().getId(), event.getState());
Expand All @@ -65,7 +71,9 @@ public void onNotificationStateChange(NotificationStateEventDto event) {
notificationStateEventService.addNotificationStateEvent(eventEntity);
}

@EventListener(value = DataMessageStateEventDto.class)
@Transactional(propagation = Propagation.REQUIRES_NEW)
@TransactionalEventListener(value = DataMessageStateEventDto.class)
@Async
public void onDataMessageStateChange(DataMessageStateEventDto event) {
String info = convertMapToString(event.getAdditionalInfo());
log.debug("ID: {}, STATE: {}", event.getDataMessage().getId(), event.getState());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.radarbase.appserver.event.state.dto.NotificationStateEventDto;
import org.radarbase.appserver.event.state.dto.TaskStateEventDto;
import org.radarbase.appserver.service.TaskStateEventService;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.event.TransactionalEventListener;

import java.util.Map;

Expand All @@ -51,7 +56,9 @@ public TaskStateEventListener(ObjectMapper objectMapper,
*
* @param event the event to respond to
*/
@EventListener(value = TaskStateEventDto.class)
@Transactional(propagation = Propagation.REQUIRES_NEW)
@TransactionalEventListener(classes = TaskStateEventDto.class)
@Async
public void onTaskStateChange(TaskStateEventDto event) {
String info = convertMapToString(event.getAdditionalInfo());
log.debug("ID: {}, STATE: {}", event.getTask().getId(), event.getState());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.NoArgsConstructor;
import net.minidev.json.annotate.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.radarbase.appserver.dto.fcm.FcmNotificationDto;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@
import java.util.Optional;
import org.radarbase.appserver.entity.DataMessage;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.stereotype.Repository;

/**
* @author yatharthranjan
*/
@Repository
@RepositoryRestResource(exported = false)
public interface DataMessageRepository extends JpaRepository<DataMessage, Long> {

List<DataMessage> findByUserId(Long userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@
import java.util.List;
import org.radarbase.appserver.entity.DataMessageStateEvent;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.stereotype.Repository;

@Repository
@RepositoryRestResource(exported = false)
public interface DataMessageStateEventRepository extends
JpaRepository<DataMessageStateEvent, Long> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@
import jakarta.validation.constraints.NotNull;
import org.radarbase.appserver.entity.Notification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.stereotype.Repository;

/** @author yatharthranjan */
@Repository
@RepositoryRestResource(exported = false)
public interface NotificationRepository extends JpaRepository<Notification, Long> {

List<Notification> findByUserId(Long userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@
import java.util.List;
import org.radarbase.appserver.entity.NotificationStateEvent;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.stereotype.Repository;

@Repository
@RepositoryRestResource(exported = false)
public interface NotificationStateEventRepository extends
JpaRepository<NotificationStateEvent, Long> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@
import java.util.Optional;
import org.radarbase.appserver.entity.Project;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.stereotype.Repository;

/** @author yatharthranjan */
@Repository
@RepositoryRestResource(exported = false)
public interface ProjectRepository extends JpaRepository<Project, Long> {

Optional<Project> findByProjectId(String projectId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@

import org.radarbase.appserver.entity.TaskStateEvent;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@RepositoryRestResource(exported = false)
public interface TaskStateEventRepository extends
JpaRepository<TaskStateEvent, Long> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@
import jakarta.validation.constraints.NotNull;
import org.radarbase.appserver.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.stereotype.Repository;

/** @author yatharthranjan */
@Repository
@RepositoryRestResource(exported = false)
public interface UserRepository extends JpaRepository<User, Long> {

Optional<User> findBySubjectId(String subjectId);
Expand Down
23 changes: 17 additions & 6 deletions src/main/java/org/radarbase/appserver/service/GithubClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,23 @@
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.concurrent.Executor;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
public class GithubClient {
private static final String GITHUB_API_URI = "api.github.com";
private static final String GITHUB_API_ACCEPT_HEADER = "application/vnd.github.v3+json";
private final transient HttpClient client;

@Nonnull
private final transient String authorizationHeader;

private transient final Duration httpTimeout;
private transient final Executor executor;

@Value("${security.github.client.maxContentLength:1000000}")
private transient int maxContentLength;
Expand All @@ -64,10 +68,11 @@ public GithubClient(
@Value("${security.github.client.token:}") String githubToken) {
this.authorizationHeader = githubToken != null ? "Bearer " + githubToken.trim() : "";
this.httpTimeout = Duration.ofSeconds(httpTimeout);
this.client = HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.NORMAL)
.connectTimeout(this.httpTimeout)
.build();
this.executor = new ThreadPoolExecutor(0,
8,
30,
TimeUnit.SECONDS,
new SynchronousQueue<>());
}

public String getGithubContent(String url) throws IOException, InterruptedException {
Expand All @@ -93,10 +98,16 @@ public String getGithubContent(String url) throws IOException, InterruptedExcept
}

private HttpResponse<InputStream> makeRequest(URI uri) throws InterruptedException {
HttpClient client = HttpClient.newBuilder()
.executor(executor)
.version(HttpClient.Version.HTTP_1_1)
.followRedirects(HttpClient.Redirect.NORMAL)
.connectTimeout(this.httpTimeout)
.build();
try {
return client.send(getRequest(uri), HttpResponse.BodyHandlers.ofInputStream());
} catch (IOException ex) {
log.error("Failed to retrieve data from github: {}", ex.toString());
log.error("Failed to retrieve data from github {}: {}", uri, ex.toString());
throw new ResponseStatusException(HttpStatus.BAD_GATEWAY, "Github responded with an error.");
}
}
Expand Down
20 changes: 11 additions & 9 deletions src/main/java/org/radarbase/appserver/service/TaskService.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,16 @@ private void addTaskStateEvent(Task t, TaskState state, Instant time) {
@Transactional
public Task updateTaskStatus(Task oldTask, TaskState state) {
User user = oldTask.getUser();
if (this.taskRepository.existsByUserIdAndNameAndTimestamp(user.getId(), oldTask.getName(), oldTask.getTimestamp())) {
if (state.equals(TaskState.COMPLETED)) {
oldTask.setCompleted(true);
oldTask.setTimeCompleted(Timestamp.from(Instant.now()));
}
oldTask.setStatus(state);
return this.taskRepository.saveAndFlush(oldTask);
} else throw new NotFoundException(
"The Task does not exists. Please Use add endpoint");

if (!this.taskRepository.existsByUserIdAndNameAndTimestamp(user.getId(), oldTask.getName(), oldTask.getTimestamp())) {
throw new NotFoundException("The Task " + oldTask.getId() + " does not exist to set to state " + state + ". Please Use add endpoint");
}

if (state.equals(TaskState.COMPLETED)) {
oldTask.setCompleted(true);
oldTask.setTimeCompleted(Timestamp.from(Instant.now()));
}
oldTask.setStatus(state);
return this.taskRepository.saveAndFlush(oldTask);
}
}

0 comments on commit 0fed45f

Please sign in to comment.