<commit_before>package nl.royvanrijn.eborp;

import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.stream.Collectors;

public class DetectionEndpoint implements DetectionListener {

    private static final Logger log = LoggerFactory.getLogger(DetectionEndpoint.class);


    private ObjectMapper mapper;
    private List<EborpSample> samples;
    private ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);


    public DetectionEndpoint() {
        mapper = new ObjectMapper();
        samples = new ArrayList<>();

        //executor.scheduleAtFixedRate(new EporbAnalysis(), 5, 5, TimeUnit.SECONDS);
    }

    class EporbAnalysis implements Runnable {
        @Override
        public void run() {

            final long fiveMinutesAgo = System.currentTimeMillis() - (300 * 1000);
            List<EborpSample> lastSamples = samples.stream().filter(s -> s.getEpoch() > fiveMinutesAgo).collect(Collectors.toList());
            samples = lastSamples;



        }
    }


    @Override
    public void onDetection(String data) throws IOException {
        log.info(data);

        EborpSample eborpSample = mapper.readValue(data, EborpSample.class);
        samples.add(eborpSample);

        /**
         * TODO:
         *
         *  - Parse the JSON into something useful
         *  - Temporary store and matching of the received detections
         *  - Store the results for future analysis
         *
         * Data:
         *
         * Step 1: A single detection:
         *            {"epoch":1430962085987,"MAC":"2c:54:cf:ea:69:0d","dBm":-89,"ID":e9f5bff0-bf12-45b3-b532-0d4fccced24a}
         *            {"epoch":1430962085987,"MAC":"2c:54:cf:ea:69:0d","dBm":-89,"ID":e9f5bff0-bf12-45b3-b532-0d4fccced24a}
         *            {"epoch":1430962085987,"MAC":"2c:54:cf:ea:69:0d","dBm":-89,"ID":e9f5bff0-bf12-45b3-b532-0d4fccced24a}
         *            {"epoch":1430962085987,"MAC":"2c:54:cf:ea:69:0d","dBm":-89,"ID":e9f5bff0-bf12-45b3-b532-0d4fccced24a}
         * 		Meaning:
         * 	        epoch:  Time of measurement
         * 	        MAC:    The found MAC address<commit_msg>Some looping magic.<commit_after>package nl.royvanrijn.eborp;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class DetectionEndpoint implements DetectionListener {

    private static final Logger log = LoggerFactory.getLogger(DetectionEndpoint.class);

    private ElasticsearchClient client;
    private ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);


    public DetectionEndpoint() {
        client = new ElasticsearchClient();
        executor.scheduleAtFixedRate(new EporbAnalysis(), 5, 5, TimeUnit.SECONDS);
    }

    class EporbAnalysis implements Runnable {
        @Override
        public void run() {

            Map<String, List<EborpSample>> samples = client.getAll("1000d");


            Map<String, Map<String, List<Integer>>> step3 = new HashMap<>();
            Map<String, Map<String, Integer>> step4 = new HashMap<>();

            for (Map.Entry<String, List<EborpSample>> listEntry : samples.entrySet()) {
                String macAddress = listEntry.getKey();
                List<EborpSample> value = listEntry.getValue();

                Map<String, List<Integer>> macAssociatedValue = step3.put(macAddress, new HashMap<>());

                for (EborpSample eborpSample : value) {
                    String pie = eborpSample.getSource();
                    if (!macAssociatedValue.containsKey(pie)) {
                        macAssociatedValue.put(pie, new ArrayList<>());
                    }
                    macAssociatedValue.get(pie).add(eborpSample.getDbm());
                }

                Map<String, Integer> step4Intermediate = new HashMap<>();
                for (Map.Entry<String, List<Integer>> stringListEntry : macAssociatedValue.entrySet()) {
                    String pie = stringListEntry.getKey();
                    List<Integer> signals = stringListEntry.getValue();
                    Collections.sort(signals);
                    // Calculate the average strength from the lowest half (skip the outliers)
                    int total = 0;
                    for (int i = signals.size() - (signals.size() / 2); i < signals.size(); i++) {
                        total += signals.get(i);
                    }
                    step4Intermediate.put(pie, total);
                }

                step4.put(macAddress, step4Intermediate);
            }


        }
    }


    @Override
    public void onDetection(String data) throws IOException {
        log.info(data);

        client.addSample(data);

        /**
         * TODO:
         *
         *  - Parse the JSON into something useful
         *  - Temporary store and matching of the received detections
         *  - Store the results for future analysis
         *
         * Data:
         *
         * Step 1: A single detection:
         *            {"epoch":1430962085987,"MAC":"2c:54:cf:ea:69:0d","dBm":-89,"ID":e9f5bff0-bf12-45b3-b532-0d4fccced24a}
         *            {"epoch":1430962085987,"MAC":"2c:54:cf:ea:69:0d","dBm":-89,"ID":e9f5bff0-bf12-45b3-b532-0d4fccced24a}
         *            {"epoch":1430962085987,"MAC":"2c:54:cf:ea:69:0d","dBm":-89,"ID":e9f5bff0-bf12-45b3-b532-0d4fccced24a}
         *            {"epoch":1430962085987,"MAC":"2c:54:cf:ea:69:0d","dBm":-89,"ID":e9f5bff0-bf12-45b3-b532-0d4fccced24a}
         * 		Meaning:
         * 	        epoch:  Time of measurement
         * 	        MAC:    The found MAC address
{'fraction_non_alphanumeric': 0.11861252115059222, 'fraction_numerical': 0.05414551607445008, 'mean_word_length': 2.3226531759415403, 'pattern_counts': {'":': 32, '<': 28, '<?xml version=': 0, '>': 29, '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': '6079275', 'n_tokens_mistral': 1993, 'n_tokens_neox': 1785, 'n_words': 322}