<commit_before>package com.gurkensalat.osm.mosques.service;

import com.gurkensalat.osm.entity.OsmPlace;
import com.gurkensalat.osm.mosques.entity.StatisticsEntry;
import com.gurkensalat.osm.mosques.repository.StatisticsRepository;
import com.gurkensalat.osm.repository.OsmPlaceRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class StatisticsServiceImpl implements StatisticsService
{
    private final static Logger LOGGER = LoggerFactory.getLogger(StatisticsServiceImpl.class);

    @Autowired
    private StatisticsRepository statisticsRepository;

    @Autowired
    private OsmPlaceRepository osmPlaceRepository;

    public void calculate()
    {

    /*
     * select
     *   ADDR_COUNTRY, COUNT(*) as COUNT, AVG(LAT) as CENTERPOINT_LAT, AVG(LON) as CENTERPOINT_LON, MIN(LON) as MIN_LON, MAX(LON) as MAX_LON, MIN(LAT) as MIN_LAT, MAX(LAT) AS MAX_LAT
     * from OSM_PLACES
     * group by ADDR_COUNTRY;
     */
        // Inefficient as hell, but will be improved later on
        statisticsRepository.invalidateAll();
        statisticsRepository.deleteAllInvalid();

        Iterable<OsmPlace> places = osmPlaceRepository.findAll();
        if (places != null)
        {
            for (OsmPlace place : places)
            {
                String countryCode = place.getAddress().getCountry();
                StatisticsEntry entry = persistStatisticsEntry(countryCode);

                entry.setOsmMosqueNodes(entry.getOsmMosqueNodes() + 1);

                entry.setMinLat(Math.min(entry.getMinLat(), place.getLat()));
                entry.setMaxLat(Math.max(entry.getMaxLat(), place.getLat()));

                entry.setMinLon(Math.min(entry.getMinLon(), place.getLon()));
                entry.setMaxLon(Math.max(entry.getMaxLon(), place.getLon()));

                entry.setCentroidLat((entry.getMinLat() + entry.getMaxLat()) / 2);
                entry.setCentroidLon((entry.getMinLon() + entry.getMaxLon()) / 2);

                entry = statisticsRepository.save(entry);
            }
        }

        // StatisticsEntry statisticsEntry = persistStatisticsEntry(countryCode, country);

        // statisticsEntry.setOsmMosqueNodes(root.getNodes().size());
        // statisticsEntry = statisticsRepository.save(statisticsEntry);
    }

    private StatisticsEntry persistStatisticsEntry(String countryCode)
    {
        StatisticsEntry entry = null;

        try
        {
            List<StatisticsEntry> entries = statisticsRepository.findByCountryCode(countryCode);
            if ((entries == null) || (entries.size() == 0))
            {
                // Place could not be found, insert it...
                StatisticsEntry tempEntry = new StatisticsEntry();
                tempEntry.setCountryCode(countryCode);
                tempEntry.setCountryName(countryCode);<commit_msg>Hacky optimization of database writes<commit_after>package com.gurkensalat.osm.mosques.service;

import com.gurkensalat.osm.entity.OsmPlace;
import com.gurkensalat.osm.mosques.entity.StatisticsEntry;
import com.gurkensalat.osm.mosques.repository.StatisticsRepository;
import com.gurkensalat.osm.repository.OsmPlaceRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.TreeMap;

@Component
public class StatisticsServiceImpl implements StatisticsService
{
    private final static Logger LOGGER = LoggerFactory.getLogger(StatisticsServiceImpl.class);

    @Autowired
    private StatisticsRepository statisticsRepository;

    @Autowired
    private OsmPlaceRepository osmPlaceRepository;

    public void calculate()
    {

    /*
     * select
     *   ADDR_COUNTRY, COUNT(*) as COUNT, AVG(LAT) as CENTERPOINT_LAT, AVG(LON) as CENTERPOINT_LON, MIN(LON) as MIN_LON, MAX(LON) as MAX_LON, MIN(LAT) as MIN_LAT, MAX(LAT) AS MAX_LAT
     * from OSM_PLACES
     * group by ADDR_COUNTRY;
     */
        // Inefficient as hell, but will be improved later on
        statisticsRepository.invalidateAll();
        // statisticsRepository.deleteAllInvalid();

        HashMap<String, StatisticsEntry> entries = new HashMap<>();

        Iterable<OsmPlace> places = osmPlaceRepository.findAll();
        if (places != null)
        {
            for (OsmPlace place : places)
            {
                String countryCode = place.getAddress().getCountry();
                StatisticsEntry entry = entries.get(countryCode);
                if (entry == null)
                {
                    // Try to load entry from database
                    entry = persistStatisticsEntry(countryCode);

                    // Initialize all Integer attributes
                    entry.setOsmMosqueNodes(0);

                    entry.setMinLat(1000);
                    entry.setMaxLat(-1000);

                    entry.setMinLon(1000);
                    entry.setMaxLon(-1000);

                    entry.setCentroidLat(0);
                    entry.setCentroidLon(0);

                    entry = statisticsRepository.save(entry);

                    entries.put(countryCode, entry);
                }

                // We already have a valid entry in the Map
                entry.setOsmMosqueNodes(entry.getOsmMosqueNodes() + 1);

                entry.setMinLat(Math.min(entry.getMinLat(), place.getLat()));
                entry.setMaxLat(Math.max(entry.getMaxLat(), place.getLat()));

                entry.setMinLon(Math.min(entry.getMinLon(), place.getLon()));
                entry.setMaxLon(Math.max(entry.getMaxLon(), place.getLon()));

                entry.setCentroidLat((entry.getMinLat() + entry.getMaxLat()) / 2);
                entry.setCentroidLon((entry.getMinLon() + entry.getMaxLon()) / 2);
            }
        }

        // Done, now save all entries...
        for (StatisticsEntry entry : entries.values())
        {
            try
            {
                entry.setValid(true);
                entry = statisticsRepository.save(entry);
            }
            catch (Exception e)
            {
                LOGGER.error("While saving {}", entry, e);
            }
        }

        // StatisticsEntry statisticsEntry = persistStatisticsEntry(countryCode, country);

        // statisticsEntry.setOsmMosqueNodes(root.getNodes().size());
        // statisticsEntry = statisticsRepository.save(statisticsEntry);
    }

    private StatisticsEntry persistStatisticsEntry(String countryCode)
    {
        StatisticsEntry entry = null;

        try
        {
            List<StatisticsEntry> entries = statisticsRepository.findByCountryCode(countryCode);
            if ((entries == null) || (entries.size() == 0))
            {
                // Place could not be found, insert it...
                StatisticsEntry tempEntry = new StatisticsEntry();
                tempEntry.setCountryCode(countryCode);
                tempEntry.setCountryName(countryCode);
{'fraction_non_alphanumeric': 0.10048712595685456, 'fraction_numerical': 0.004314544189283229, 'mean_word_length': 2.3912222746578573, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '1470148', 'n_tokens_mistral': 2048, 'n_tokens_neox': 1944, 'n_words': 369}