<commit_before>package br.gov.servicos.editor.frontend;

import br.gov.servicos.editor.servicos.Orgao;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.github.slugify.Slugify;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import lombok.experimental.FieldDefaults;
import lombok.experimental.NonFinal;
import lombok.experimental.Wither;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.function.Predicate;
import java.util.zip.GZIPInputStream;

import static java.util.stream.Collectors.toList;
import static lombok.AccessLevel.PRIVATE;

@Slf4j
@Component
@FieldDefaults(level = PRIVATE, makeFinal = true)
public class Orgaos implements InitializingBean {

    public static final ClassPathResource RESOURCE = new ClassPathResource("estrutura-organizacional.json.gz");

    RestTemplate rest;
    ObjectReader reader;
    Slugify slugify;

    @NonFinal
    EstruturaOrganizacional estruturaOrganizacional;

    @Autowired
    public Orgaos(RestTemplate rest, ObjectMapper mapper, Slugify slugify) {
        this.rest = rest;
        this.slugify = slugify;
        this.reader = mapper.reader(EstruturaOrganizacional.class);
    }

    @Override
    @SneakyThrows
    public void afterPropertiesSet() throws Exception {
        log.debug("Lendo estrutura organizacional de {}...", RESOURCE);
        estruturaOrganizacional = reader.readValue(new GZIPInputStream(RESOURCE.getInputStream()));
        log.debug("Estrutura organizacional lida com sucesso", RESOURCE);
    }

    @Cacheable("orgaos")
    public List<Orgao> get(String termo) {
        log.info("Buscando órgãos com termo '{}'", termo);
        return estruturaOrganizacional.getUnidades()
                .stream()
                .filter(new FiltroDeOrgaos(termo))
                .map(u -> new Orgao().withNome(String.format("%s (%s)", u.getNome(), u.getSigla()))
                        .withId(u.getCodigoUnidade()))
                .sorted((l, r) -> l.getNome().compareTo(r.getNome()))
                .collect(toList());
    }

    @Data
    @Wither
    @NoArgsConstructor
    @AllArgsConstructor
    public static class EstruturaOrganizacional {
        Servico servico;
        List<Unidade> unidades;
    }

    @Data
    @Wither
    @NoArgsConstructor<commit_msg>Log para buscas por órgãos - #209<commit_after>package br.gov.servicos.editor.frontend;

import br.gov.servicos.editor.servicos.Orgao;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.github.slugify.Slugify;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import lombok.experimental.FieldDefaults;
import lombok.experimental.NonFinal;
import lombok.experimental.Wither;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.function.Predicate;
import java.util.zip.GZIPInputStream;

import static java.util.stream.Collectors.toList;
import static lombok.AccessLevel.PRIVATE;
import static net.logstash.logback.marker.Markers.append;

@Slf4j
@Component
@FieldDefaults(level = PRIVATE, makeFinal = true)
public class Orgaos implements InitializingBean {

    public static final ClassPathResource RESOURCE = new ClassPathResource("estrutura-organizacional.json.gz");

    RestTemplate rest;
    ObjectReader reader;
    Slugify slugify;

    @NonFinal
    EstruturaOrganizacional estruturaOrganizacional;

    @Autowired
    public Orgaos(RestTemplate rest, ObjectMapper mapper, Slugify slugify) {
        this.rest = rest;
        this.slugify = slugify;
        this.reader = mapper.reader(EstruturaOrganizacional.class);
    }

    @Override
    @SneakyThrows
    public void afterPropertiesSet() throws Exception {
        log.debug("Lendo estrutura organizacional de {}...", RESOURCE);
        estruturaOrganizacional = reader.readValue(new GZIPInputStream(RESOURCE.getInputStream()));
        log.debug("Estrutura organizacional lida com sucesso", RESOURCE);
    }

    @Cacheable("orgaos")
    public List<Orgao> get(String termo) {
        List<Orgao> busca = estruturaOrganizacional.getUnidades()
                .stream()
                .filter(new FiltroDeOrgaos(termo))
                .map(u -> new Orgao().withNome(String.format("%s (%s)", u.getNome(), u.getSigla()))
                        .withId(u.getCodigoUnidade()))
                .sorted((l, r) -> l.getNome().compareTo(r.getNome()))
                .collect(toList());

        log.info(append("orgaos.termo", termo).and(append("orgaos.resultados", busca.size())),
                "Buscando órgãos com termo '{}': {} resultados", termo, busca.size());

        return busca;
    }

    @Data
    @Wither
    @NoArgsConstructor
    @AllArgsConstructor
    public static class EstruturaOrganizacional {
        Servico servico;
        List<Unidade> unidades;
    }

    @Data
    @Wither
    @NoArgsConstructor
{'fraction_non_alphanumeric': 0.10424710424710425, 'fraction_numerical': 0.0015795015795015794, 'mean_word_length': 3.7452123230641132, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1214891', 'n_tokens_mistral': 1813, 'n_tokens_neox': 1740, 'n_words': 321}