package com.microservices.mon.k8s.controller;

import com.microservices.common.core.web.controller.BaseController;
import com.microservices.common.core.web.domain.AjaxResult;
import com.microservices.common.core.web.domain.GenericsAjaxResult;
import com.microservices.common.core.web.page.GenericsTableDataInfo;
import com.microservices.mon.k8s.domain.*;
import com.microservices.mon.k8s.service.IK8sDashboardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("dashboard")
@Api(tags = "微服务大屏")
@RefreshScope
public class DashboardController extends BaseController {
    @Autowired
    private IK8sDashboardService k8sDashboardService;

    /**
     * 获取命名空间列表
     */
    @ApiOperation(value = "获取命名空间列表")
    @GetMapping("/namespace/list")
    public GenericsTableDataInfo<K8sNamespace> namespaceList(@RequestParam(value = "search", required = false) @ApiParam(value = "根据应用名称查找") String search) {
        return new GenericsTableDataInfo<>(k8sDashboardService.getNamespaceDetailList(true,search));
    }

    @ApiOperation(value = "获取集群节点列表")
    @GetMapping("/cluster/Nodelist")
    public GenericsTableDataInfo<K8sClusterNode> clusterNodeList() {
        return new GenericsTableDataInfo<>(k8sDashboardService.getClusterNodeList());
    }

    @ApiOperation(value = "获取应用容器Ip列表")
    @GetMapping("/application/{kind}/{namespace}/{applicationName}/podIdList")
    public GenericsTableDataInfo<String> applicationPodIdList(@PathVariable("kind") String kind, @PathVariable("namespace") String namespace, @PathVariable("applicationName") String applicationName) {
        return new GenericsTableDataInfo<>(k8sDashboardService.getApplicationPodIdList(kind, namespace, applicationName));
    }

    @ApiOperation(value = "获取集群状态数据")
    @GetMapping("/cluster/status")
    public GenericsAjaxResult<K8sClusterStatus> clusterStatus() {
        return GenericsAjaxResult.success(k8sDashboardService.getClusterStatus());
    }

    @ApiOperation(value = "集群每秒请求率（QPS）")
    @GetMapping("/cluster/qps")
    public GenericsTableDataInfo<MetricCommonData> clusterQps() {
        return new GenericsTableDataInfo<>(k8sDashboardService.getClusterQps());
    }

    @ApiOperation(value = "最近24小时应用接口请求次数")
    @GetMapping("/cluster/applicationRequestCount")
    public GenericsTableDataInfo<ApplicationRequestCountData> applicationRequestCount() {
        return new GenericsTableDataInfo<>(k8sDashboardService.getApplicationRequestCount());
    }

    @ApiOperation(value = "获取Pod容器状态")
    @GetMapping("/cluster/podContainerStatus/{podName}")
    public GenericsAjaxResult<K8sPodContainerStatus> podContainerStatus(@PathVariable("podName") String podName) {
        return GenericsAjaxResult.success(k8sDashboardService.getPodContainerStatus(podName));
    }

    @ApiOperation(value = "获取服务器节点状态")
    @GetMapping("/cluster/nodeStatus/{nodeIp}")
    public GenericsAjaxResult<K8sNodeStatus> nodeStatus(@PathVariable("nodeIp") String nodeIp) {
        return GenericsAjaxResult.success(k8sDashboardService.getNodeStatus(nodeIp));
    }

    @ApiOperation(value = "获取微服务应用功能列表")
    @GetMapping("/applicationFunction/{kind}/{namespace}/{applicationName}")
    public GenericsTableDataInfo<ApplicationFunction> applicationFunction(@PathVariable("kind") String kind,
                                                                          @PathVariable("namespace") String namespace,
                                                                          @PathVariable("applicationName") String applicationName,
                                                                          @RequestParam("podIpList") @ApiParam(value = "Pod Ip列表(使用逗号分割的ip列表字符串)") String podIpList) {
        return new GenericsTableDataInfo<>(k8sDashboardService.getApplicationFunction(kind, namespace, applicationName, podIpList));
    }
}
