package cn.reghao.devops.web.mgr.machine.service;

import cn.reghao.devops.web.admin.sys.service.NotifyService;
import cn.reghao.devops.web.admin.sys.service.notifier.ding.DingMsg;
import cn.reghao.devops.web.mgr.machine.model.dto.KeepalivedState;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author reghao
 * @date 2024-01-05 09:09:46
 */
@Slf4j
@Service
public class KeepalivedService {
    private final NotifyService notifyService;

    public KeepalivedService(NotifyService notifyService) {
        this.notifyService = notifyService;
    }

    public void stateNotify(KeepalivedState keepalivedState) {
        String host = keepalivedState.getHost();
        String state = keepalivedState.getState();
        String realServer = keepalivedState.getRealServer();

        String title;
        String text;
        if (realServer == null) {
            title = "keepalived 事件通知";
            text = String.format("keepalived 节点 %s 切换为 %s", host, state);
        } else {
            title = "keepalived-realserver 事件通知";
            text = String.format("keepalived 节点 %s 检测到 RealServer %s 状态为 %s", host, realServer, state);
        }

        log.info(text);
        DingMsg dingMsg = new DingMsg(title, text);
        notifyService.notify(dingMsg);
    }
}
