package com.lts.web.support;

import com.lts.core.commons.utils.DateUtils;
import com.lts.core.logger.Logger;
import com.lts.core.logger.LoggerFactory;
import com.lts.web.repository.TaskTrackerMonitorDataRepository;
import com.lts.web.request.MonitorDataRequest;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 定时清除 monitor 数据
 *
 * @author Robert HG (254963746@qq.com) on 8/23/15.
 */
@Component
public class MonitorDataCleaner implements InitializingBean {

    private static final Logger LOGGER = LoggerFactory.getLogger(MonitorDataCleaner.class);
    @Autowired
    private TaskTrackerMonitorDataRepository taskTrackerMonitorDataRepository;
    private ScheduledExecutorService cleanExecutor = Executors.newSingleThreadScheduledExecutor();

    private AtomicBoolean start = new AtomicBoolean(false);

    public void start() {
        if (start.compareAndSet(false, true)) {
            cleanExecutor.scheduleAtFixedRate(new Runnable() {
                @Override
                public void run() {
                    try {
                        clean();
                    } catch (Throwable t) {
                        LOGGER.error("Clean monitor data error ", t);
                    }
                }
            }, 1, 24, TimeUnit.HOURS);
        }
    }

    private void clean() {
        //  1. 清除TaskTracker的统计数据(5天之前的)
        Long endTime = DateUtils.addDay(new Date(), -5).getTime();
        MonitorDataRequest request = new MonitorDataRequest();
        request.setEndTime(endTime);
        taskTrackerMonitorDataRepository.delete(request);
        LOGGER.info("Clean monitor data before {} succeed ", DateUtils.formatYMD_HMS(new Date(endTime)));
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        start();
    }
}

@@ -141,24 +141,87 @@
 gerFactory;%0A
+import com.lts.web.repository.JobTrackerMonitorDataRepository;%0A
 import com.l
@@ -264,32 +264,32 @@
 DataRepository;%0A
-
 import com.lts.w
@@ -1038,16 +1038,108 @@
 sitory;%0A
+    @Autowired%0A    private JobTrackerMonitorDataRepository jobTrackerMonitorDataRepository;%0A
     priv
@@ -1814,16 +1814,27 @@
 kTracker
+ JobTracker
 %E7%9A%84%E7%BB%9F%E8%AE%A1%E6%95%B0%E6%8D%AE(5%E5%A4%A9
@@ -2005,16 +2005,16 @@
 dTime);%0A
-
         
@@ -2059,24 +2059,82 @@
 e(request);%0A
+        jobTrackerMonitorDataRepository.delete(request);%0A%0A
         LOGG

添加对JobTracker的监控，在LTS-Admin中可以查看