-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[backend] Improv performance on articles list (#898)
- Loading branch information
1 parent
a41c76c
commit 379d4ac
Showing
8 changed files
with
293 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
openbas-api/src/main/java/io/openbas/rest/channel/ChannelHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package io.openbas.rest.channel; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.openbas.database.model.Article; | ||
import io.openbas.database.model.Inject; | ||
import io.openbas.injectors.channel.model.ChannelContent; | ||
import io.openbas.rest.channel.model.VirtualArticle; | ||
import io.openbas.rest.exception.ElementNotFoundException; | ||
|
||
import java.time.Instant; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import static io.openbas.database.model.Inject.SPEED_STANDARD; | ||
import static io.openbas.injectors.channel.ChannelContract.CHANNEL_PUBLISH; | ||
import static java.util.Comparator.naturalOrder; | ||
import static java.util.Comparator.nullsFirst; | ||
|
||
public class ChannelHelper { | ||
|
||
private ChannelHelper() { | ||
|
||
} | ||
|
||
public static Article enrichArticleWithVirtualPublication( | ||
List<Inject> injects, | ||
Article article, | ||
ObjectMapper mapper) { | ||
return enrichArticleWithVirtualPublication( | ||
injects, | ||
List.of(article), | ||
mapper).stream() | ||
.findFirst() | ||
.orElseThrow(ElementNotFoundException::new); | ||
} | ||
|
||
public static List<Article> enrichArticleWithVirtualPublication( | ||
List<Inject> injects, | ||
List<Article> articles, | ||
ObjectMapper mapper) { | ||
Instant now = Instant.now(); | ||
Map<String, Instant> toPublishArticleIdsMap = injects.stream() | ||
.filter(inject -> inject.getInjectorContract().getId().equals(CHANNEL_PUBLISH)) | ||
.filter(inject -> inject.getContent() != null) | ||
.sorted(Comparator.comparing(Inject::getDependsDuration)) | ||
.flatMap(inject -> convertToVirtualArticles(inject, now, mapper)) | ||
.filter(Objects::nonNull) | ||
.distinct() | ||
.collect(Collectors.toMap(VirtualArticle::id, VirtualArticle::date)); | ||
return articles.stream() | ||
.peek(article -> article.setVirtualPublication(toPublishArticleIdsMap.get(article.getId()))) | ||
.sorted(Comparator.comparing(Article::getVirtualPublication, nullsFirst(naturalOrder())) | ||
.thenComparing(Article::getCreatedAt) | ||
.reversed()) | ||
.toList(); | ||
} | ||
|
||
private static Stream<VirtualArticle> convertToVirtualArticles(Inject inject, Instant now, ObjectMapper mapper) { | ||
Instant virtualInjectDate = inject.computeInjectDate(now, SPEED_STANDARD); | ||
try { | ||
ChannelContent content = mapper.treeToValue(inject.getContent(), ChannelContent.class); | ||
return content.getArticles() | ||
.stream() | ||
.map(article -> new VirtualArticle(virtualInjectDate, article)); | ||
} catch (JsonProcessingException e) { | ||
// Log the error if necessary | ||
return Stream.empty(); | ||
} | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
openbas-api/src/main/java/io/openbas/rest/channel/ExerciseArticleApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package io.openbas.rest.channel; | ||
|
||
import io.openbas.aop.LogExecutionTime; | ||
import io.openbas.database.model.Article; | ||
import io.openbas.database.model.Inject; | ||
import io.openbas.database.repository.ArticleRepository; | ||
import io.openbas.database.repository.InjectRepository; | ||
import io.openbas.database.specification.ArticleSpecification; | ||
import io.openbas.database.specification.InjectSpecification; | ||
import io.openbas.rest.channel.output.ArticleOutput; | ||
import io.openbas.rest.helper.RestBehavior; | ||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
import static io.openbas.injectors.channel.ChannelContract.CHANNEL_PUBLISH; | ||
import static io.openbas.rest.channel.ChannelHelper.enrichArticleWithVirtualPublication; | ||
import static io.openbas.rest.exercise.ExerciseApi.EXERCISE_URI; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class ExerciseArticleApi extends RestBehavior { | ||
|
||
private final InjectRepository injectRepository; | ||
private final ArticleRepository articleRepository; | ||
|
||
@PreAuthorize("isExerciseObserver(#exerciseId)") | ||
@GetMapping(EXERCISE_URI + "/{exerciseId}/articles") | ||
@Transactional(readOnly = true) | ||
@LogExecutionTime | ||
public Iterable<ArticleOutput> exerciseArticles(@PathVariable @NotBlank final String exerciseId) { | ||
List<Inject> injects = this.injectRepository.findAll( | ||
InjectSpecification.fromExercise(exerciseId) | ||
.and(InjectSpecification.fromContract(CHANNEL_PUBLISH)) | ||
); | ||
List<Article> articles = this.articleRepository.findAll(ArticleSpecification.fromExercise(exerciseId)); | ||
return enrichArticleWithVirtualPublication(injects, articles, this.mapper) | ||
.stream() | ||
.map(ArticleOutput::from) | ||
.toList(); | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
openbas-api/src/main/java/io/openbas/rest/channel/ScenarioArticleApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package io.openbas.rest.channel; | ||
|
||
import io.openbas.aop.LogExecutionTime; | ||
import io.openbas.database.model.Article; | ||
import io.openbas.database.model.Inject; | ||
import io.openbas.database.repository.ArticleRepository; | ||
import io.openbas.database.repository.InjectRepository; | ||
import io.openbas.database.specification.ArticleSpecification; | ||
import io.openbas.database.specification.InjectSpecification; | ||
import io.openbas.rest.channel.output.ArticleOutput; | ||
import io.openbas.rest.helper.RestBehavior; | ||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
import static io.openbas.injectors.channel.ChannelContract.CHANNEL_PUBLISH; | ||
import static io.openbas.rest.channel.ChannelHelper.enrichArticleWithVirtualPublication; | ||
import static io.openbas.rest.scenario.ScenarioApi.SCENARIO_URI; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class ScenarioArticleApi extends RestBehavior { | ||
|
||
private final InjectRepository injectRepository; | ||
private final ArticleRepository articleRepository; | ||
|
||
@PreAuthorize("isScenarioObserver(#scenarioId)") | ||
@GetMapping(SCENARIO_URI + "/{scenarioId}/articles") | ||
@Transactional(readOnly = true) | ||
@LogExecutionTime | ||
public Iterable<ArticleOutput> scenarioArticles(@PathVariable @NotBlank final String scenarioId) { | ||
List<Inject> injects = this.injectRepository.findAll( | ||
InjectSpecification.fromScenario(scenarioId) | ||
.and(InjectSpecification.fromContract(CHANNEL_PUBLISH)) | ||
); | ||
List<Article> articles = this.articleRepository.findAll(ArticleSpecification.fromScenario(scenarioId)); | ||
return enrichArticleWithVirtualPublication(injects, articles, this.mapper) | ||
.stream() | ||
.map(ArticleOutput::from) | ||
.toList(); | ||
} | ||
|
||
} |
86 changes: 86 additions & 0 deletions
86
openbas-api/src/main/java/io/openbas/rest/channel/output/ArticleOutput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package io.openbas.rest.channel.output; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import io.openbas.database.model.Article; | ||
import io.openbas.database.model.Document; | ||
import io.openbas.database.model.Exercise; | ||
import io.openbas.database.model.Scenario; | ||
import jakarta.persistence.Transient; | ||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.Data; | ||
|
||
import java.time.Instant; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static java.util.Optional.ofNullable; | ||
|
||
@Data | ||
public class ArticleOutput { | ||
|
||
@JsonProperty("article_id") | ||
@NotBlank | ||
private String id; | ||
|
||
@JsonProperty("article_name") | ||
private String name; | ||
|
||
@JsonProperty("article_content") | ||
private String content; | ||
|
||
@JsonProperty("article_author") | ||
private String author; | ||
|
||
@JsonProperty("article_shares") | ||
private Integer shares; | ||
|
||
@JsonProperty("article_likes") | ||
private Integer likes; | ||
|
||
@JsonProperty("article_comments") | ||
private Integer comments; | ||
|
||
@JsonProperty("article_exercise") | ||
private String exercise; | ||
|
||
@JsonProperty("article_scenario") | ||
private String scenario; | ||
|
||
@JsonProperty("article_channel") | ||
@NotBlank | ||
private String channel; | ||
|
||
@JsonProperty("article_documents") | ||
private List<String> documents = new ArrayList<>(); | ||
|
||
@Transient | ||
private Instant virtualPublication; | ||
|
||
@JsonProperty("article_virtual_publication") | ||
public Instant getVirtualPublication() { | ||
return this.virtualPublication; | ||
} | ||
|
||
@JsonProperty("article_is_scheduled") | ||
public boolean isScheduledPublication() { | ||
return this.virtualPublication != null; | ||
} | ||
|
||
public static ArticleOutput from(@org.jetbrains.annotations.NotNull final Article article) { | ||
ArticleOutput articleOutput = new ArticleOutput(); | ||
articleOutput.setId(article.getId()); | ||
articleOutput.setName(article.getName()); | ||
articleOutput.setContent(article.getContent()); | ||
articleOutput.setAuthor(article.getAuthor()); | ||
articleOutput.setShares(article.getShares()); | ||
articleOutput.setLikes(article.getLikes()); | ||
articleOutput.setComments(article.getComments()); | ||
articleOutput.setExercise(ofNullable(article.getExercise()).map(Exercise::getId).orElse(null)); | ||
articleOutput.setScenario(ofNullable(article.getScenario()).map(Scenario::getId).orElse(null)); | ||
articleOutput.setChannel(article.getChannel().getId()); | ||
articleOutput.setDocuments(article.getDocuments().stream().map(Document::getId).toList()); | ||
articleOutput.setVirtualPublication(article.getVirtualPublication()); | ||
return articleOutput; | ||
} | ||
|
||
} |
Oops, something went wrong.