TeamRepositoryImpl.java

  1. package no.nav.data.team.team;

  2. import io.micrometer.core.annotation.Timed;
  3. import no.nav.data.common.storage.domain.GenericStorage;
  4. import no.nav.data.team.team.domain.Team;
  5. import org.springframework.context.annotation.Lazy;
  6. import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
  7. import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
  8. import org.springframework.stereotype.Repository;

  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.UUID;
  12. import java.util.stream.Collectors;

  13. import static no.nav.data.common.utils.MetricUtils.DB_QUERY_TIMED;
  14. import static no.nav.data.common.utils.MetricUtils.QUERY;
  15. import static no.nav.data.common.utils.StreamUtils.convert;

  16. @Repository
  17. public class TeamRepositoryImpl implements TeamRepositoryCustom {

  18.     private final TeamRepository teamRepository;
  19.     private final NamedParameterJdbcTemplate template;

  20.     public TeamRepositoryImpl(@Lazy TeamRepository teamRepository, NamedParameterJdbcTemplate template) {
  21.         this.teamRepository = teamRepository;
  22.         this.template = template;
  23.     }

  24.     @Override
  25.     @Timed(value = DB_QUERY_TIMED, extraTags = {QUERY, "TeamRepositoryCustom.findByCluster"}, percentiles = {.99, .75, .50})
  26.     public List<Team> findByCluster(UUID teamId) {
  27.         var resp = template.queryForList("select id from generic_storage where data #>'{clusterIds}' ?? :teamId",
  28.                 new MapSqlParameterSource().addValue("teamId", teamId.toString()));
  29.         return get(resp);
  30.     }

  31.     private List<Team> get(List<Map<String, Object>> resp) {
  32.         List<UUID> ids = resp.stream().map(i -> ((UUID) i.values().iterator().next())).collect(Collectors.toList());
  33.         return convert(teamRepository.findAllById(ids), GenericStorage::toTeam);
  34.     }
  35. }