package com.tsi.comm.server.repository;

import com.tsi.app.common.app.AppUtils;
import com.tsi.app.common.utils.Counter;
import com.tsi.app.common.xnet.NetUtils;
import com.tsi.comm.server.mybatis.vo.AbstractDbmsVo;
import com.tsi.comm.server.mybatis.vo.CommStatusVo;
import com.tsi.comm.server.mybatis.vo.KafkaTransVo;
import com.tsi.comm.server.process.dbms.TsiCvimDbmsProcess;
import com.tsi.comm.server.protocol.TsiCpuPacket;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TsiTpmsManager {

    private static TsiTpmsManager _instance = null;

    public static TsiTpmsManager getInstance() {
        if (_instance == null) {
            synchronized (TsiTpmsManager.class) {
                if (_instance == null)
                    _instance = new TsiTpmsManager();
            }
        }
        return _instance;
    }

    private TsiTpmsManager() {
        this.readTPS = new Counter();
        this.readTPM = new Counter();
        this.readTPD = new Counter();
        this.readBPS = new Counter();
        this.readBPM = new Counter();
        this.readBPD = new Counter();
        this.kafkaTransVo = new KafkaTransVo(AbstractDbmsVo.DBMS_KAFKA_TRANS_HS);
        this.kafkaTransVo.setHostName(NetUtils.getHostName());
        this.tsiCvimDbmsService = (TsiCvimDbmsProcess) AppUtils.getBean(TsiCvimDbmsProcess.class);
    }

    // DB Process Service
    private TsiCvimDbmsProcess tsiCvimDbmsService;

    // 패킷 수
    private Counter readTPS;    // read transaction per seconds
    private Counter readTPM;    // read transaction per minute
    private Counter readTPD;    // read transaction per day

    // 패킷 바이트수
    private Counter readBPS;    // read bytes per seconds
    private Counter readBPM;    // read bytes per minute
    private Counter readBPD;    // read bytes per day
    private KafkaTransVo kafkaTransVo;

    public KafkaTransVo getKafkaTransVo() {
        return kafkaTransVo;
    }

    public void readPacket(TsiCpuPacket packet) {
        this.readTPS.increment();
        this.readBPS.add(packet.getBuf().length);

        this.readTPM.increment();
        this.readBPM.add(packet.getBuf().length);

        this.readTPD.increment();
        this.readBPD.add(packet.getBuf().length);
    }

    public void resetSecond() {
        CommStatusVo stat = new CommStatusVo(AbstractDbmsVo.DBMS_TPMS_STAT_1S);
        long trans = this.readTPS.reset();
        long bytes = this.readBPS.reset();
        stat.setTrans(trans);
        stat.setBytes(bytes);
        stat.setSessions(TsiSessionManager.getInstance().get());
        this.tsiCvimDbmsService.add(stat, (int)Thread.currentThread().getId());

        /*log.info("Second Statistics: {} sessions, {} TRX, {}. {}",
                TsiSessionManager.getInstance().get(),
                String.format("%,d", trans),
                Converter.getSize(bytes), Thread.currentThread().getName());*/
    }
    public void resetMinute() {
        long trans = this.readTPM.reset();
        long bytes = this.readBPM.reset();
        /*log.info("Minute Statistics: {} sessions, {} TRX, {}. {}",
                TsiSessionManager.getInstance().get(),
                String.format("%,d", trans),
                Converter.getSize(bytes), Thread.currentThread().getName());*/
    }
    public void resetDay() {
        long trans = this.readTPD.reset();
        long bytes = this.readBPD.reset();
        /*log.info("   Day Statistics: {} sessions, {} TRX, {}. {}",
                TsiSessionManager.getInstance().get(),
                String.format("%,d", trans),
                Converter.getSize(bytes), Thread.currentThread().getName());*/
    }

}
