package cn.axzo.workflow.starter.mq.monitor.console;

import cn.axzo.workflow.starter.WorkflowEngineStarterProperties;
import cn.azxo.framework.common.model.CommonResponse;
import lombok.SneakyThrows;
import org.apache.rocketmq.tools.admin.DefaultMQAdminExt;
import org.springframework.beans.factory.annotation.Value;
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;
import java.util.HashMap;
import java.util.Map;

import static cn.axzo.workflow.starter.StarterRPCInvokeMQConfiguration.DEFAULT_EVENT;

/**
 * Workflow Engine Starter MessageQueue Monitor Controller
 *
 * @author wangli
 * @since 2024/6/6 10:04
 */
@RestController
@RequestMapping("/web/we/starter")
public class WorkflowEngineStarterMQMonitorController {
    @Resource
    private WorkflowEngineStarterProperties starterProperties;
    @Resource
    private DefaultMQAdminExt defaultMQAdminExt;
    @Value("${spring.profiles.active}")
    private String activeProfile;

    @SneakyThrows
    @GetMapping("/monitor")
    public CommonResponse<Map<String, Object>> monitor() {
        String topic = DEFAULT_EVENT + activeProfile;
        String broadcastConsumerGroup = "";
        Map<String, Object> result = new HashMap<>();
        result.put("BrokerClusterInfo", defaultMQAdminExt.examineBrokerClusterInfo());
        result.put("TopicClusterList", defaultMQAdminExt.getTopicClusterList(topic));
        result.put("TopicRouteInfo", defaultMQAdminExt.examineTopicRouteInfo(topic));

        result.put("TopicStats", defaultMQAdminExt.examineTopicStats(topic));

        System.getProperty("");
        if (starterProperties.getJoinContainerGroup()) {

        }

        result.put("gid_senna", defaultMQAdminExt.examineConsumeStats("GID_senna_workflow_engine_debugging_consumer"));
        result.put("gid_senna_starter", defaultMQAdminExt.examineConsumeStats("GID_senna_workflow_engine_starter_debugging_consumer"));
        defaultMQAdminExt.examineTopicConfig(System.getProperty("rocketmq.name-server"), "topic_workflow_engine_dev");
        return CommonResponse.success(result);
    }
}
