package com.yihu.hos.monitor.controller;

import com.yihu.hos.monitor.service.ServerMonitorService;
import com.yihu.hos.web.framework.model.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author HZY
 * @vsrsion 1.0
 * Created at 2016/10/26.
 */
@Controller("ServerMonitorController")
@RequestMapping("/monitor/server")
public class ServerMonitorController {

    @Resource(name = ServerMonitorService.BEAN_ID)
    private ServerMonitorService monitorService;


    //跳转到列表页
    @RequestMapping("/initial")
    public String initial(Model model) {
        model.addAttribute("contentPage", "/monitor/server/sEnvManage");
        return "partView";
    }

    @RequestMapping(value = "/usage", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取服务器使用率", produces = "application/json", notes = "获取服务器硬件使用率")
    public Result usage(
            HttpServletRequest request,
            @ApiParam(name = "host", value = "服务器IP", required = true)
            @RequestParam(value = "host") String host,
            @ApiParam(name = "type", value = "类型", required = true)
            @RequestParam(value = "type") String type,
            @ApiParam(name = "beginTime", value = "开始时间", required = true)
            @RequestParam(value = "beginTime") String beginTime,
            @ApiParam(name = "endTime", value = "结束时间", required = true)
            @RequestParam(value = "endTime") String endTime) {
        HttpSession session = request.getSession();
        Result result = monitorService.getMonitorList(session, host, type, beginTime, endTime);
        return result;
    }


    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取服务器详情信息", produces = "application/json", notes = "获取服务器详情信息")
    public Result detail(
            HttpServletRequest request,
            @ApiParam(name = "host", value = "服务器IP", required = true)
            @RequestParam(value = "host") String host,
            @ApiParam(name = "type", value = "类型", required = true)
            @RequestParam(value = "type") String type,
            @ApiParam(name = "date", value = "时间点", required = true)
            @RequestParam(value = "date") String date) {
        HttpSession session = request.getSession();
        return monitorService.getMonitorDetail(session, host, type, date);
    }

    @RequestMapping(value = "/hosts", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取服务器列表", produces = "application/json", notes = "获取服务器列表")
    public Result hosts(HttpServletRequest request) {
        HttpSession session = request.getSession();
        return monitorService.getHosts();
    }


    @RequestMapping(value = "/trees", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取服务器树列表", produces = "application/json", notes = "获取服务器树列表")
    public Result trees(HttpServletRequest request) {
        try {
            HttpSession session = request.getSession();
            return monitorService.getServerTreeList(session);
        } catch (Exception e) {
            return Result.error("获取服务树列表失败");
        }
    }


}
