package iot.lidee.controller.dashBoard;

import iot.lidee.common.constant.LideeConstant;
import iot.lidee.common.core.domain.AjaxResult;
import iot.lidee.common.core.redis.RedisCache;
import iot.lidee.iot.model.dashBoard.DashMqttMetrics;
import iot.lidee.iot.model.dashBoard.DashMqttStat;
import iot.lidee.mqtt.manager.ClientManager;
import iot.lidee.mqtt.manager.RetainMsgManager;
import iot.lidee.base.service.ISessionStore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 首页面板和大屏数据
 *
 * @author gsb
 * @date 2023/3/27 17:00
 */
@Api(tags = "首页面板和大屏数据")
@RestController
@RequestMapping("/bashBoard")
public class DashBoardController {

    @Resource
    private RedisCache redisCache;
    @Resource
    private ISessionStore sessionStore;

    @GetMapping("/stats")
    @ApiOperation("mqtt状态数据")
    public AjaxResult stats() {
        DashMqttStat stat = DashMqttStat.builder()
                .connection_count(sessionStore.getSessionMap().size())
                .connection_total(getTotal(LideeConstant.REDIS.MESSAGE_CONNECT_TOTAL))
                .subscription_count(ClientManager.topicMap.size())
                .subscription_total(getTotal(LideeConstant.REDIS.MESSAGE_SUBSCRIBE_TOTAL))
                .retain_count(RetainMsgManager.getSize())
                .retain_total(getTotal(LideeConstant.REDIS.MESSAGE_RETAIN_TOTAL))
                .session_count(sessionStore.getSessionMap().size())
                .session_total(getTotal(LideeConstant.REDIS.MESSAGE_CONNECT_TOTAL))
                .build();
        return AjaxResult.success(stat);

    }


    @GetMapping("/metrics")
    @ApiOperation("mqtt统计")
    public AjaxResult metrics() {
        DashMqttMetrics metrics = DashMqttMetrics.builder()
                .send_total(getTotal(LideeConstant.REDIS.MESSAGE_SEND_TOTAL))
                .receive_total(getTotal(LideeConstant.REDIS.MESSAGE_RECEIVE_TOTAL))
                .auth_total(getTotal(LideeConstant.REDIS.MESSAGE_AUTH_TOTAL))
                .connect_total(getTotal(LideeConstant.REDIS.MESSAGE_CONNECT_TOTAL))
                .subscribe_total(getTotal(LideeConstant.REDIS.MESSAGE_SUBSCRIBE_TOTAL))
                .today_received(getTotal(LideeConstant.REDIS.MESSAGE_RECEIVE_TODAY))
                .today_send(getTotal(LideeConstant.REDIS.MESSAGE_SEND_TODAY))
                .build();
        return AjaxResult.success(metrics);
    }


    public Integer getTotal(String key) {
        return redisCache.getCacheObject(key) == null ? 0 : redisCache.getCacheObject(key);
    }
}
