<commit_before>/**
 * Created by <NAME> on 12/3/2016.
 */
package com.evapps.Service.Auxiliary;

import com.evapps.Entity.History;
import com.evapps.Entity.Product;
import com.evapps.Entity.Receipt;
import com.evapps.Entity.User;
import com.evapps.Repository.HistoryRepository;
import com.evapps.Repository.ProductRepository;
import com.evapps.Repository.ReceiptRepository;
import com.evapps.Repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Service
public class StatisticService {

    // Repositories
    @Autowired
    private HistoryRepository historyRepository;
    @Autowired
    private ProductRepository productRepository;
    @Autowired
    private ReceiptRepository receiptRepository;
    @Autowired
    private UserRepository userRepository;

    // Functions
    // Product related Statistics
    public Map<Integer, Integer> productViewStatistics(boolean option){

        Map<Integer, Integer> statistic = fetchProductLegend();

        try {
            for (History history: historyRepository.findAll())
                for (Product product: history.getBrowsingHistory())
                    statistic.replace(product.getProductId(), statistic.get(product.getProductId()) + 1);

            return statistic;
        } catch (Exception exp) {
            //
        }

        return null;
    }

    public Map<Integer, Integer> productPurchaseStatistics(){

        Map<Integer, Integer> statistic = fetchProductLegend();

        try {
            for (Receipt receipt: receiptRepository.findAll())
                for (Integer p: receipt.getProductList())
                    statistic.replace(p, statistic.get(p) + 1);

            return statistic;
        } catch (Exception exp) {
            //
        }

        return null;
    }

    public Map<String, Integer> productSupplierStatistics(){

        try {
            Map<String, Integer> statistic = fetchSupplierLegend();

            for (Product product: productRepository.findAll())
                statistic.replace(product.getSupplier(), statistic.get(product.getSupplier()) + 1);

            return statistic;

        } catch (Exception exp) {
            //
        }

        return null;
    }

    // Transaction Related Functions
    public Map<String, Float> userAveragePurchaseByDollar(){

        try {
            Map<String, Float> statistics = fetchUserLegend();

            for (String email: statistics.keySet()) {
                int count = 0;
                for (Receipt receipt : receiptRepository.findByUser(email)) {
                    statistics.replace(email, statistics.get(email) + receipt.getTotal());
                    count++;
                }

                statistics.replace(email, statistics.get(email)/count);
            }

            return statistics;
        } catch (Exception exp) {
            //
        }

        return null;
    }


    // Auxiliary Functions
    private Map<Integer, Integer> fetchProductLegend(){
        Map<Integer, Integer> legend = new HashMap<>();
<commit_msg>Add userAverageNumberOfItemPurchased<commit_after>/**
 * Created by <NAME> on 12/3/2016.
 */
package com.evapps.Service.Auxiliary;

import com.evapps.Entity.History;
import com.evapps.Entity.Product;
import com.evapps.Entity.Receipt;
import com.evapps.Entity.User;
import com.evapps.Repository.HistoryRepository;
import com.evapps.Repository.ProductRepository;
import com.evapps.Repository.ReceiptRepository;
import com.evapps.Repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Service
public class StatisticService {

    // Repositories
    @Autowired
    private HistoryRepository historyRepository;
    @Autowired
    private ProductRepository productRepository;
    @Autowired
    private ReceiptRepository receiptRepository;
    @Autowired
    private UserRepository userRepository;

    // Functions
    // Product related Statistics
    public Map<Integer, Integer> productViewStatistics(boolean option){

        Map<Integer, Integer> statistic = fetchProductLegend();

        try {
            for (History history: historyRepository.findAll())
                for (Product product: history.getBrowsingHistory())
                    statistic.replace(product.getProductId(), statistic.get(product.getProductId()) + 1);

            return statistic;
        } catch (Exception exp) {
            //
        }

        return null;
    }

    public Map<Integer, Integer> productPurchaseStatistics(){

        Map<Integer, Integer> statistic = fetchProductLegend();

        try {
            for (Receipt receipt: receiptRepository.findAll())
                for (Integer p: receipt.getProductList())
                    statistic.replace(p, statistic.get(p) + 1);

            return statistic;
        } catch (Exception exp) {
            //
        }

        return null;
    }

    public Map<String, Integer> productSupplierStatistics(){

        try {
            Map<String, Integer> statistic = fetchSupplierLegend();

            for (Product product: productRepository.findAll())
                statistic.replace(product.getSupplier(), statistic.get(product.getSupplier()) + 1);

            return statistic;

        } catch (Exception exp) {
            //
        }

        return null;
    }

    // Transaction Related Functions
    public Map<String, Float> userAveragePurchaseByDollar(){

        try {
            Map<String, Float> statistics = fetchUserLegend();

            for (String email: statistics.keySet()) {
                int count = 0;
                for (Receipt receipt : receiptRepository.findByUser(email)) {
                    statistics.replace(email, statistics.get(email) + receipt.getTotal());
                    count++;
                }

                statistics.replace(email, statistics.get(email)/count);
            }

            return statistics;
        } catch (Exception exp) {
            //
        }

        return null;
    }

    public Map<String, Float> userAverageNumberOfItemPurchase(){

        try {
            Map<String, Float> statistics = fetchUserLegend();

            for (String email: statistics.keySet()) {
                int count = 0;
                for (Receipt receipt : receiptRepository.findByUser(email)) {
                    statistics.replace(email, statistics.get(email) + receipt.getProductList().size());
                    count++;
                }

                statistics.replace(email, statistics.get(email)/count);
            }

            return statistics;
        } catch (Exception exp) {
            //
        }

        return null;
    }


    // Auxiliary Functions
    private Map<Integer, Integer> fetchProductLegend(){
        Map<Integer, Integer> legend = new HashMap<>();

{'fraction_non_alphanumeric': 0.0978476352308128, 'fraction_numerical': 0.0032568677428490515, 'mean_word_length': 2.2251141552511418, 'pattern_counts': {'":': 0, '<': 29, '<?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': '937260', 'n_tokens_mistral': 1865, 'n_tokens_neox': 1735, 'n_words': 402}