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

import br.gov.servicos.config.DestaquesConfig;
import br.gov.servicos.piwik.PiwikClient;
import br.gov.servicos.piwik.PiwikPage;
import br.gov.servicos.servico.Servico;
import br.gov.servicos.servico.ServicoRepository;
import lombok.experimental.FieldDefaults;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;
import static java.util.stream.Stream.concat;
import static java.util.stream.Stream.empty;
import static java.util.stream.StreamSupport.stream;
import static lombok.AccessLevel.PRIVATE;
import static org.springframework.data.domain.Sort.Direction.DESC;

@Slf4j
@Controller
@FieldDefaults(level = PRIVATE, makeFinal = true)
class IndexController {

    PiwikClient piwikClient;
    ServicoRepository servicos;
    DestaquesConfig destaques;

    @Autowired
    IndexController(ServicoRepository servicos, DestaquesConfig destaques, PiwikClient piwikClient) {
        this.servicos = servicos;
        this.destaques = destaques;
        this.piwikClient = piwikClient;
    }

    @RequestMapping("/")
    ModelAndView index() {
        return new ModelAndView("index", "destaques", servicosParaExibir());
    }

    @RequestMapping("/maisAcessados")
    ModelAndView maisAcessados() {
        return new ModelAndView("index", "destaques", servicosParaExibirMaisAcessados());
    }

    private List<Servico> servicosParaExibirMaisAcessados() {
        return concat(servicosMaisAcessados(), outrosServicos())
                .limit(10)
                .collect(toList());
    }

    private Stream<Servico> servicosMaisAcessados() {
        log.debug("Piwik: listando serviços mais acessados...");
        Stream<Servico> servicos = this.piwikClient.getPageUrls("week", "yesterday").stream()
                .filter(PiwikPage::isServicoUrl)
                .sorted((a, b) -> a.getUniqueVisitors().compareTo(b.getUniqueVisitors()))
                .map(p -> p.getUrl().replace("/servico/", ""))
                .map(this.servicos::findOne)
                .filter(Objects::nonNull);

        log.debug("Piwik: serviços mais acessados obtidos");
        return servicos;
    }

    private List<Servico> servicosParaExibir() {
        return concat(buscaDestaquesSeNecessario(), outrosServicos())
                .limit(15)
                .collect(toList());
    }

    private Stream<Servico> buscaDestaquesSeNecessario() {
        if (destaques.getServicos().isEmpty())<commit_msg>Limite de serviços em uma constante - #304<commit_after>package br.gov.servicos.frontend;

import br.gov.servicos.config.DestaquesConfig;
import br.gov.servicos.piwik.PiwikClient;
import br.gov.servicos.piwik.PiwikPage;
import br.gov.servicos.servico.Servico;
import br.gov.servicos.servico.ServicoRepository;
import lombok.experimental.FieldDefaults;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;
import static java.util.stream.Stream.concat;
import static java.util.stream.Stream.empty;
import static java.util.stream.StreamSupport.stream;
import static lombok.AccessLevel.PRIVATE;
import static org.springframework.data.domain.Sort.Direction.DESC;

@Slf4j
@Controller
@FieldDefaults(level = PRIVATE, makeFinal = true)
class IndexController {

    private static final int QTD_SERVICOS_DESTAQUES = 10;

    PiwikClient piwikClient;
    ServicoRepository servicos;
    DestaquesConfig destaques;

    @Autowired
    IndexController(ServicoRepository servicos, DestaquesConfig destaques, PiwikClient piwikClient) {
        this.servicos = servicos;
        this.destaques = destaques;
        this.piwikClient = piwikClient;
    }

    @RequestMapping("/")
    ModelAndView index() {
        return new ModelAndView("index", "destaques", servicosParaExibir());
    }

    @RequestMapping("/maisAcessados")
    ModelAndView maisAcessados() {
        return new ModelAndView("index", "destaques", servicosParaExibirMaisAcessados());
    }

    private List<Servico> servicosParaExibirMaisAcessados() {
        return concat(servicosMaisAcessados(), outrosServicos())
                .limit(QTD_SERVICOS_DESTAQUES)
                .collect(toList());
    }

    private Stream<Servico> servicosMaisAcessados() {
        log.debug("Piwik: listando serviços mais acessados...");
        Stream<Servico> servicos = this.piwikClient.getPageUrls("week", "yesterday").stream()
                .filter(PiwikPage::isServicoUrl)
                .sorted((a, b) -> a.getUniqueVisitors().compareTo(b.getUniqueVisitors()))
                .map(p -> p.getUrl().replace("/servico/", ""))
                .map(this.servicos::findOne)
                .filter(Objects::nonNull);

        log.debug("Piwik: serviços mais acessados obtidos");
        return servicos;
    }

    private List<Servico> servicosParaExibir() {
        return concat(buscaDestaquesSeNecessario(), outrosServicos())
                .limit(QTD_SERVICOS_DESTAQUES)
                .collect(toList());
    }

    private Stream<Servico> buscaDestaquesSeNecessario() {
        if (destaques.getServicos().isEmpty())
{'fraction_non_alphanumeric': 0.11190233977619532, 'fraction_numerical': 0.00254323499491353, 'mean_word_length': 3.7381526104417673, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3832584', 'n_tokens_mistral': 1873, 'n_tokens_neox': 1795, 'n_words': 297}