/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/

package com.cesgroup.cesmonitor.web;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cesgroup.cesmonitor.bean.Alert;
import com.cesgroup.cesmonitor.bean.DiskUsageInfo;
import com.cesgroup.cesmonitor.bean.InterfaceInfo;
import com.cesgroup.cesmonitor.bean.OneHostBaseInfo;
import com.cesgroup.cesmonitor.bean.OneHostDetailInfo;
import com.cesgroup.cesmonitor.bean.OneHostUsedRateInfo;
import com.cesgroup.cesmonitor.bean.ResourceAlarmInfo;
import com.cesgroup.cesmonitor.bean.TopologicalInfo;
import com.cesgroup.cesmonitor.bean.WebMonitorStatus;
import com.cesgroup.cesmonitor.service.MapService;
import com.cesgroup.cesmonitor.service.OverviewService;

/**
 * 监控总览
 *
 */
@Controller
@RequestMapping(value = "/overview")
public class OverviewController {

    @Autowired
    OverviewService overviewService;

    @Autowired
    MapService mapService;

    /**
     *
     * @param model
     *
     * @return 返回网络拓扑信息及主机组信息
     */
    @RequestMapping(value = "index")
    public String index(final Model model) {
        model.addAttribute("groups", overviewService.listHostGroupsWithHosts());
        model.addAttribute("map", mapService.getMapObject());
        return "overview/index";
    }

    /**
     *
     * @param model
     *
     * @param hostid
     *
     * @return 返回主机的Hostid到主机详细信息界面
     */
    @RequestMapping(value = "host")
    public String host(final Model model, final Integer hostid) {
        model.addAttribute("hostid", hostid);
        return "overview/host";
    }

    /**
     * 保存拓扑图sysmapId到文件中
     *
     * @param sysmapId
     *            拓扑图sysmapId
     */
    @RequestMapping(value = "saveSysmapId")
    @ResponseBody
    public void saveSysmapId(final String sysmapId) {
        mapService.addToOverview(Integer.valueOf(sysmapId));
    }

    /**
     *
     * @param hostid
     *
     * @return 返回主机的详细信息
     */
    @RequestMapping(value = "hostDetailInfo")
    @ResponseBody
    public OneHostDetailInfo hostDetailInfo(final String hostId) {
        return overviewService.oneHostDetailInfo(Long.parseLong(hostId));
    }

    /**
     *
     * @param hostid
     *
     * @return 返回主机的使用率（CPU使用率、内存使用率）
     */
    @RequestMapping(value = "hostUsedRateInfo")
    @ResponseBody
    public OneHostUsedRateInfo hostUsedRateInfo(final String hostId) {
        return overviewService.oneHostUsedRateInfo(Long.parseLong(hostId));
    }

    /**
     *
     * @param hostid
     *
     * @return 返回主机的基本信息（平均响应时间、丢包率）
     */
    @RequestMapping(value = "hostBaseInfo")
    @ResponseBody
    public OneHostBaseInfo hostBaseInfo(final String hostId) {
        return overviewService.oneHostBaseInfo(Long.parseLong(hostId));
    }

    /**
     *
     * @param hostid
     *
     * @return 返回主机的web监控状态信息
     */
    @RequestMapping(value = "webMonitorInfo")
    @ResponseBody
    public List<WebMonitorStatus> listHostWebMonitorInfo(final String hostId) {
        return overviewService.listHostWebMonitorInfo(Long.parseLong(hostId));
    }

    /**
     *
     * @return 返回拓扑图信息（sysmapid,name）
     */
    @RequestMapping(value = "topologicalInfo")
    @ResponseBody
    public List<TopologicalInfo> listTopologicalInfo() {
        return overviewService.listTopologicalInfo();
    }

    /**
     *
     * @param hostid
     *
     * @return 根据hostid返回资源报警信息
     */
    @RequestMapping(value = "resourceAlarmInfo")
    @ResponseBody
    public List<ResourceAlarmInfo> listHostResourceAlarmInfo(final String hostId) {
        Map<Integer, ResourceAlarmInfo> mapResourceAlarmMsg = overviewService
            .listHostResourceAlarmInfo(Long.parseLong(hostId));
        List<ResourceAlarmInfo> list = new ArrayList<ResourceAlarmInfo>(mapResourceAlarmMsg.values());
        Collections.sort(list);
        return list;
    }

    /**
     *
     * @param eventId
     *            事件id
     * @return 资源告警
     */
    @RequestMapping(value = "resourceAlert")
    @ResponseBody
    public List<Alert> resourceAlert(final Integer eventId) {
        return overviewService.resourceAlertService(eventId);
    }

    /**
     *
     * @param hostid
     *
     * @return 根据hostid获取磁盘的使用信息
     */
    @RequestMapping(value = "diskUsageInfo")
    @ResponseBody
    public List<DiskUsageInfo> listHostDiskUsageInfo(final String hostId) {
        Map<Integer, DiskUsageInfo> mapDiskUsageMsg = overviewService.listHostsDiskUsageInfo(Long.parseLong(hostId));
        return new ArrayList<DiskUsageInfo>(mapDiskUsageMsg.values());
    }

    /**
     *
     * @param totalItemid
     *            总容量监控项itemid
     * @param usageItemid
     *            使用量监控项itemid
     * @return 返回graphIds
     */
    @RequestMapping(value = "getGraphidByItemids")
    @ResponseBody
    public List<String> getGraphidByItemids(final String totalItemid, final String usageItemid) {
        List<String> graphIds = overviewService.getGraphidByItemids(totalItemid, usageItemid);
        return graphIds;
    }

    /**
     *
     * @param hostid
     *
     * @return 根据hostid获取接口信息
     */
    @RequestMapping(value = "interfaceInfo")
    @ResponseBody
    public List<InterfaceInfo> listHostInterfaceInfo(final String hostId) {
        //long startTime = System.currentTimeMillis();    //获取开始时间
        Map<Integer, InterfaceInfo> mapInterfaceMsg = overviewService.listHostInterfaceInfo(Long.parseLong(hostId));
        //long endTime = System.currentTimeMillis();    //获取结束时间

        // System.out.println("根据hostid获取listHostInterfaceInfo程序运行时间：" + (endTime - startTime) + "ms");    //输出程序运行时
        return new ArrayList<InterfaceInfo>(mapInterfaceMsg.values());
    }
}
