package com.angkorteam.mbaas.server.background;

import com.angkorteam.mbaas.model.entity.Tables;
import com.angkorteam.mbaas.model.entity.tables.records.StatisticsRecord;
import com.angkorteam.mbaas.server.MBaaS;
import org.hyperic.sigar.*;
import org.jooq.DSLContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * Created by Khauv Socheat on 4/20/2016.
 */
@Service
public class PerformanceBackground {

    private static final Logger LOGGER = LoggerFactory.getLogger(MBaaS.class);

    @Autowired
    private DSLContext context;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    private boolean error = false;

    @Scheduled(cron = "0 * * * * *")
    public void collect() {
        if (error) {
            return;
        }
        try {
            Sigar sigar = new Sigar();
            long processId = sigar.getPid();
            ProcCpu procCpu = sigar.getProcCpu(processId);
            ProcMem procMem = sigar.getProcMem(processId);
            StatisticsRecord statisticsRecord = context.newRecord(Tables.STATISTICS);
            statisticsRecord.setStatisticsId(UUID.randomUUID().toString());
            statisticsRecord.setMemory(Double.valueOf(procMem.getSize()));
            statisticsRecord.setCpu(procCpu.getPercent());
            statisticsRecord.setDateCreated(new Date());
            statisticsRecord.store();
            sigar.close();
        } catch (Throwable e) {
            error = true;
        }
    }
}

@@ -1624,24 +1624,123 @@
 rd.store();%0A
+            LOGGER.info(%22cpu %7B%7D mem %7B%7D%22, statisticsRecord.getCpu(), statisticsRecord.getMemory());%0A
             
@@ -1782,24 +1782,57 @@
 owable e) %7B%0A
+            e.printStackTrace();%0A
             

add performance collector