package com.tsi.comm.server.repository;

import com.tsi.comm.server.config.TsiCvimServerConfig;
import com.tsi.comm.server.xnet.NetUtils;
import com.tsi.comm.server.vo.mariadb.AbstractDbmsVo;
import com.tsi.comm.server.vo.mariadb.CommStatusVo;
import com.tsi.comm.server.vo.mariadb.KafkaTransVo;
import com.tsi.comm.server.process.dbms.TsiCvimDbmsProcess;
import com.tsi.comm.server.protocol.TsiCpuPacket;
import com.tsi.common.utils.Counter;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Slf4j
@RequiredArgsConstructor
@Component
public class TsiTpmsManager {

    private final TsiCvimDbmsProcess dbmsProcess;
    private final TsiSessionManager sessionManager;

    // 패킷 수
    private final Counter readTPS = new Counter();    // read transaction per seconds
    private final Counter readTPM = new Counter();    // read transaction per minute
    private final Counter readTPD = new Counter();    // read transaction per day

    // 패킷 바이트수
    private final Counter readBPS = new Counter();    // read bytes per seconds
    private final Counter readBPM = new Counter();    // read bytes per minute
    private final Counter readBPD = new Counter();    // read bytes per day

    @Getter
    private final KafkaTransVo kafkaTransVo = new KafkaTransVo(AbstractDbmsVo.DBMS_KAFKA_TRANS_HS);

    @PostConstruct
    void init() {
        this.kafkaTransVo.setHostName(NetUtils.getHostName());
    }

    // DB Process Service

    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(int serverId, String hostName) {
        CommStatusVo stat = new CommStatusVo(AbstractDbmsVo.DBMS_TPMS_STAT_1S);
        long trans = this.readTPS.reset();  // read transaction
        long bytes = this.readBPS.reset();  // read bytes
        stat.setHostName(hostName);
        stat.setId(serverId);
        stat.setTrans(trans);
        stat.setBytes(bytes);
        stat.setSessions(this.sessionManager.getChannelCount());
        this.dbmsProcess.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();  // read transaction per minute
        long bytes = this.readBPM.reset();  // read bytes per minute
        /*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();  // read transaction per day
        long bytes = this.readBPD.reset();  // read bytes per day
        /*log.info("   Day Statistics: {} sessions, {} TRX, {}. {}",
                TsiSessionManager.getInstance().get(),
                String.format("%,d", trans),
                Converter.getSize(bytes), Thread.currentThread().getName());*/
    }

}
