Skip to content

Commit

Permalink
Update and optimize logging repository and services
Browse files Browse the repository at this point in the history
#deploy-levende-arbeidsfoehold-ansettelse

Refactor LoggRepository to use Mono for count and remove unnecessary vault dependencies. Added delay in ArbeidsforholdService to manage requests better and set restart policy in docker-compose. Introduced PageableHandlerMethodArgumentResolver for custom pageable handling and adjusted entity timestamp types.
  • Loading branch information
krharum committed Oct 18, 2024
1 parent 1036b3f commit eb09075
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 23 deletions.
10 changes: 0 additions & 10 deletions apps/levende-arbeidsforhold-ansettelse/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ dependencies {
implementation "org.springframework.boot:spring-boot-starter-oauth2-client"
implementation "org.springframework.boot:spring-boot-starter-data-r2dbc"

implementation "org.springframework.cloud:spring-cloud-starter-vault-config"

implementation "io.r2dbc:r2dbc-h2"
implementation "org.postgresql:r2dbc-postgresql"
implementation "org.flywaydb:flyway-core"
Expand All @@ -36,14 +34,6 @@ dependencies {
implementation "org.springdoc:springdoc-openapi-starter-webflux-ui:$versions.springdoc"
implementation "io.swagger.core.v3:swagger-annotations-jakarta:$versions.swagger"

implementation "org.hibernate.validator:hibernate-validator"

testImplementation "org.springframework.boot:spring-boot-starter-test"

implementation "org.projectlombok:lombok"
annotationProcessor "org.projectlombok:lombok"
testAnnotationProcessor "org.projectlombok:lombok"

implementation "com.zaxxer:HikariCP"
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package no.nav.testnav.levendearbeidsforholdansettelse.config;

import no.nav.testnav.levendearbeidsforholdansettelse.utility.PageableHandlerMethodArgumentResolver;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.config.WebFluxConfigurer;
import org.springframework.web.reactive.result.method.annotation.ArgumentResolverConfigurer;

@Configuration
public class WebConfig implements WebFluxConfigurer {

@Override
public void configureArgumentResolvers(ArgumentResolverConfigurer configurer) {
configurer.addCustomResolver(new PageableHandlerMethodArgumentResolver());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import no.nav.testnav.levendearbeidsforholdansettelse.entity.AnsettelseLogg;
import no.nav.testnav.levendearbeidsforholdansettelse.service.LoggService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("/api/v1/logg")
Expand All @@ -21,8 +24,11 @@ public class LoggController {

@GetMapping
@Operation(description = "Henter logger i hht forespørsel")
public Flux<Page<AnsettelseLogg>> getAnsettelser(Pageable pageable) {
public Mono<Page<AnsettelseLogg>> getAnsettelser(@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size,
@RequestParam(defaultValue = "id") String... sort) {

var pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, sort));
return loggService.getAnsettelseLogg(pageable);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package no.nav.testnav.levendearbeidsforholdansettelse.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
Expand All @@ -15,7 +16,7 @@
import org.springframework.data.relational.core.mapping.Table;

import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.LocalDateTime;

@Data
@Builder
Expand All @@ -28,6 +29,8 @@ public class AnsettelseLogg implements Persistable<Integer> {
private boolean isNew;

@Id
@NotNull
@Column("id")
private Integer id;

@Size(max = 255)
Expand All @@ -42,7 +45,7 @@ public class AnsettelseLogg implements Persistable<Integer> {

@NotNull
@Column("timestamp")
private OffsetDateTime timestamp;
private LocalDateTime timestamp;

@NotNull
@Column("ansattfra")
Expand Down Expand Up @@ -70,6 +73,7 @@ public int hashCode() {
}

@Override
@JsonIgnore
public boolean isNew() {
return isNew;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package no.nav.testnav.levendearbeidsforholdansettelse.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Data;
Expand Down Expand Up @@ -55,6 +56,7 @@ public String getId() {
}

@Override
@JsonIgnore
public boolean isNew() {
return isNew;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package no.nav.testnav.levendearbeidsforholdansettelse.repository;

import no.nav.testnav.levendearbeidsforholdansettelse.entity.AnsettelseLogg;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.reactive.ReactiveSortingRepository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

public interface LoggRepository extends ReactiveSortingRepository<AnsettelseLogg, Long> {

Flux<Page<AnsettelseLogg>> findAllBy(Pageable pageable);
Flux<AnsettelseLogg> findAllBy(Pageable pageable);

Mono<Long> countAllBy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import reactor.core.publisher.Mono;

import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.LocalDateTime;
import java.util.Map;

import static no.nav.testnav.levendearbeidsforholdansettelse.entity.JobbParameterNavn.ARBEIDSFORHOLD_TYPE;
Expand All @@ -36,7 +36,7 @@ public Mono<AnsettelseLogg> lagreAnsettelse(KanAnsettesDTO kanAnsette, Map<Strin
.isNew(true)
.folkeregisterident(kanAnsette.getIdent())
.organisasjonsnummer(kanAnsette.getOrgnummer())
.timestamp(OffsetDateTime.now())
.timestamp(LocalDateTime.now())
.arbeidsforholdType(parametere.get(ARBEIDSFORHOLD_TYPE.value))
.stillingsprosent(Integer.parseInt(parametere.get(STILLINGSPROSENT.value)))
.ansattfra(LocalDate.now())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void runAnsettelseService() {

//Initialiserer liste over alderspenn og liste med tidligste og seneste gyldig dato for ansttelse
var datoIntervaller = SannsynlighetVelger.getDatointervaller();
/**/

//Kjører ansettelse per org
Flux.zip(parametere, yrkeskoder)
.flatMap(tuple -> Flux.range(0, (int) getParameterValue(tuple.getT1(), ANTALL_ORGANISASJONER))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.Duration;
import java.time.LocalDate;
import java.util.List;

Expand Down Expand Up @@ -53,8 +54,8 @@ public Flux<Arbeidsforhold> getArbeidsforhold(String ident) {
public Flux<HttpStatusCode> opprettArbeidsforhold(KanAnsettesDTO kanAnsettes, String yrke,
String arbeidsforholdstype, Integer stillingsprosent) {

return aaregConsumer.opprettArbeidsforhold(lagArbeidsforhold(kanAnsettes, yrke, arbeidsforholdstype, stillingsprosent));

return aaregConsumer.opprettArbeidsforhold(lagArbeidsforhold(kanAnsettes, yrke, arbeidsforholdstype, stillingsprosent))
.delayElements(Duration.ofSeconds(1));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import no.nav.testnav.levendearbeidsforholdansettelse.repository.AnsettelseLoggRepository;
import no.nav.testnav.levendearbeidsforholdansettelse.repository.LoggRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Service
@RequiredArgsConstructor
Expand All @@ -16,9 +18,12 @@ public class LoggService {
private final LoggRepository loggRepository;
private final AnsettelseLoggRepository ansettelseLoggRepository;

public Flux<Page<AnsettelseLogg>> getAnsettelseLogg(Pageable pageable) {
public Mono<Page<AnsettelseLogg>> getAnsettelseLogg(Pageable pageable) {

return loggRepository.findAllBy(pageable);
return loggRepository.findAllBy(pageable)
.collectList()
.zipWith(loggRepository.countAllBy())
.map(tuple -> new PageImpl<>(tuple.getT1(), pageable, tuple.getT2()));
}

public Flux<AnsettelseLogg> getIdent(String ident) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package no.nav.testnav.levendearbeidsforholdansettelse.utility;

import org.springframework.core.MethodParameter;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.reactive.BindingContext;
import org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;

import static java.util.Objects.nonNull;

public class PageableHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {

private static final String DEFAULT_PAGE = "0";
private static final String DEFAULT_SIZE = "10";
private static final Integer MAX_SIZE = 50;

@Override
public boolean supportsParameter(MethodParameter parameter) {
return Pageable.class.equals(parameter.getParameterType());
}

@Override
public Mono<Object> resolveArgument(MethodParameter methodParameter,
BindingContext bindingContext,
ServerWebExchange serverWebExchange) {

var pageValues = serverWebExchange.getRequest()
.getQueryParams()
.getOrDefault("page", List.of(DEFAULT_PAGE));
var sizeValues = serverWebExchange.getRequest()
.getQueryParams()
.getOrDefault("size", List.of(DEFAULT_SIZE));

var page = pageValues.getFirst();

var sortParam = serverWebExchange.getRequest()
.getQueryParams().getFirst("sort");

var sort = Sort.unsorted();

if (nonNull(sortParam)) {
var parts = sortParam.split(",");
if (parts.length == 2) {
var property = parts[0];
var direction = Sort.Direction.fromString(parts[1]);
sort = Sort.by(direction, property);
}
}

return Mono.just(
PageRequest.of(
Integer.parseInt(page),
Math.min(Integer.parseInt(sizeValues.getFirst()),
MAX_SIZE), sort
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-----------------------------------
-- M O D I F Y I N D E C I E S --
-----------------------------------

ALTER TABLE ansettelse_logg
ALTER COLUMN id SET DEFAULT NEXTVAL('ansettelse_logg_id_seq');
10 changes: 10 additions & 0 deletions docs/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,14 @@ services:
- POSTGRES_HOST_AUTH_METHOD=trust
ports:
- "5434:5432"
restart: always

postgres-levende-arbeidforhold:
container_name: postgres-levende-arbeidforhold-test
image: postgres:latest
environment:
- POSTGRES_DB=postgres-levende-arbeidforhold-test
- POSTGRES_HOST_AUTH_METHOD=trust
ports:
- "5435:5432"
restart: always

0 comments on commit eb09075

Please sign in to comment.