package com.unidev.proxymanager;

import com.google.common.collect.Lists;
import com.unidev.proxymanager.data.ProxyHistory;
import com.unidev.proxymanager.data.ProxyHistoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * Proxy history service
 */
@Service
public class ProxyHistoryService {

    @Autowired
    private ProxyHistoryRepository proxyHistoryRepository;

    /**
     * Add new proxy for history tracking
     * @param ip
     * @param port
     */
    public void addProxy(String ip, Integer port) {

        ProxyHistory proxyHistory = new ProxyHistory();
        proxyHistory.setId((ip + ":" + port) + "");
        proxyHistory.setIp(ip);
        proxyHistory.setPort(port);
        proxyHistory.setAddDate(new Date());

        proxyHistoryRepository.save(proxyHistory);

    }

    /**
     * Remove proxy from indexing
     * @param ip
     * @param port
     */
    public void removeProxy(String ip, Integer port) {
        proxyHistoryRepository.delete( (ip + ":" + port)+ "");
    }

    /**
     * List all available proxies
     * @return
     */
    public Collection<ProxyHistory> listProxies() {
        Iterable<ProxyHistory> proxyHistory = proxyHistoryRepository.findAll();
        return Lists.newArrayList(proxyHistory);
    }


}

@@ -173,24 +173,80 @@
 Repository;%0A
+import com.unidev.proxymanager.domain.ProxyCheckResult;%0A
 import org.s
@@ -296,16 +296,16 @@
 owired;%0A
-
 import o
@@ -554,24 +554,116 @@
 yService %7B%0A%0A
+    public static final int MAX_SCORE = 10;%0A    private static final int MIN_SCORE = -10;%0A%0A%0A
     @Autowir
@@ -1557,24 +1557,24 @@
 .findAll();%0A
-
         retu
@@ -1617,11 +1617,1062 @@
 %0A    %7D%0A%0A
+    /**%0A     * Update proxy history%0A     * @param proxyCheckResult Proxy check result%0A     */%0A    public void updateProxyHistory(ProxyHistory proxyHistory, ProxyCheckResult proxyCheckResult) %7B%0A%0A        proxyCheckResult.setCheckStatus(proxyCheckResult.getCheckStatus());%0A        proxyHistory.setLastUpdate(new Date());%0A        proxyHistory.setRequestTime(proxyCheckResult.getRequestTime() / 1000D);%0A        proxyHistory.addRequestCount(1);%0A        proxyHistory.setRemoteIp(proxyCheckResult.getRemoteIp());%0A%0A        if (proxyCheckResult.getCheckStatus() == ProxyCheckResult.CheckStatus.OK) %7B%0A            proxyHistory.addScore(1);%0A        %7D%0A%0A        if (proxyCheckResult.getCheckStatus() == ProxyCheckResult.CheckStatus.FAILED) %7B%0A            proxyHistory.addScore(-1);%0A        %7D%0A%0A        if (proxyHistory.getScore() %3E MAX_SCORE) %7B%0A            proxyHistory.setScore(MAX_SCORE);%0A        %7D%0A%0A        if (proxyHistory.getScore() %3C MIN_SCORE) %7B%0A            proxyHistory.setScore(MIN_SCORE);%0A        %7D%0A%0A        proxyHistoryRepository.save(proxyHistory);%0A    %7D%0A%0A
 %0A%7D%0A

Update proxy history moved in history service