<commit_before>package nl.wiegman.home.energie;

import static org.apache.commons.collections.CollectionUtils.isNotEmpty;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import nl.wiegman.home.DateTimeUtil;
import nl.wiegman.home.cache.CacheService;

@Service
public class MeterstandService {

    private static final Logger LOGGER = LoggerFactory.getLogger(MeterstandService.class);

    private static final String THREE_AM = "0 0 3 * * *";

    private final MeterstandRepository meterstandRepository;
    private final MeterstandServiceCached meterstandServiceCached;
    private final CacheService cacheService;

    @Autowired
    public MeterstandService(MeterstandRepository meterstandRepository, MeterstandServiceCached meterstandServiceCached,
            CacheService cacheService) {

        this.meterstandRepository = meterstandRepository;
        this.meterstandServiceCached = meterstandServiceCached;
        this.cacheService = cacheService;
    }

    public Meterstand save(Meterstand meterstand) {
        return meterstandRepository.save(meterstand);
    }

    @Scheduled(cron = THREE_AM)
    public void dailyCleanup() {
        Date today = new Date();
        cleanup(DateUtils.addDays(today, -1));
        cleanup(DateUtils.addDays(today, -2));
        cleanup(DateUtils.addDays(today, -3));
    }

    public void cleanup(Date date) {
        Date from = DateTimeUtil.getStartOfDayAsDate(date);
        Date to = DateUtils.addDays(from, 1);

        List<Meterstand> meterstandenOnDay = meterstandRepository.findByDatumtijdBetween(from.getTime(), to.getTime()-1);

        Map<Integer, List<Meterstand>> byHour = meterstandenOnDay.stream()
                .collect(Collectors.groupingBy(item -> item.getDatumtijdAsDate().getHours()));

        byHour.values().forEach(this::cleanupHour);

        cacheService.clear(VerbruikServiceCached.CACHE_NAME_GAS_VERBRUIK_IN_PERIODE);
        cacheService.clear(VerbruikServiceCached.CACHE_NAME_STROOM_VERBRUIK_IN_PERIODE);
    }<commit_msg>Run daily cleanup of MeterstandService at two AM so it doesn't run at the same time as the MinderGas upload<commit_after>package nl.wiegman.home.energie;

import static org.apache.commons.collections.CollectionUtils.isNotEmpty;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import nl.wiegman.home.DateTimeUtil;
import nl.wiegman.home.cache.CacheService;

@Service
public class MeterstandService {

    private static final Logger LOGGER = LoggerFactory.getLogger(MeterstandService.class);

    private static final String TWO_AM = "0 0 2 * * *";

    private final MeterstandRepository meterstandRepository;
    private final MeterstandServiceCached meterstandServiceCached;
    private final CacheService cacheService;

    @Autowired
    public MeterstandService(MeterstandRepository meterstandRepository, MeterstandServiceCached meterstandServiceCached,
            CacheService cacheService) {

        this.meterstandRepository = meterstandRepository;
        this.meterstandServiceCached = meterstandServiceCached;
        this.cacheService = cacheService;
    }

    public Meterstand save(Meterstand meterstand) {
        return meterstandRepository.save(meterstand);
    }

    @Scheduled(cron = TWO_AM)
    public void dailyCleanup() {
        Date today = new Date();
        cleanup(DateUtils.addDays(today, -1));
        cleanup(DateUtils.addDays(today, -2));
        cleanup(DateUtils.addDays(today, -3));
    }

    public void cleanup(Date date) {
        Date from = DateTimeUtil.getStartOfDayAsDate(date);
        Date to = DateUtils.addDays(from, 1);

        List<Meterstand> meterstandenOnDay = meterstandRepository.findByDatumtijdBetween(from.getTime(), to.getTime()-1);

        Map<Integer, List<Meterstand>> byHour = meterstandenOnDay.stream()
                .collect(Collectors.groupingBy(item -> item.getDatumtijdAsDate().getHours()));

        byHour.values().forEach(this::cleanupHour);

        cacheService.clear(VerbruikServiceCached.CACHE_NAME_GAS_VERBRUIK_IN_PERIODE);
        cacheService.clear(VerbruikServiceCached.CACHE_NAME_STROOM_VERBRUIK_IN_PERIODE);
    }
{'fraction_non_alphanumeric': 0.09008835958509412, 'fraction_numerical': 0.004994237418363427, 'mean_word_length': 4.4580712788259955, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '372574', 'n_tokens_mistral': 1553, 'n_tokens_neox': 1526, 'n_words': 275}