package com.sohu.cache.web.controller;

import com.sohu.cache.entity.AppDesc;
import com.sohu.cache.entity.InstanceInfo;
import com.sohu.cache.entity.InstanceStats;
import com.sohu.cache.entity.MachineInfo;
import com.sohu.cache.entity.MachineStats;
import com.sohu.cache.machine.MachineDeployCenter;
import com.sohu.cache.util.TypeUtil;
import com.sohu.cache.web.enums.SuccessEnum;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
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.servlet.ModelAndView;

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

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 机器管理
 *
 * @author leifu
 * @Time 2014年10月14日
 */
@Controller
@RequestMapping("manage/machine")
public class MachineManageController extends BaseController{

    @Resource
    private MachineDeployCenter machineDeployCenter;

    @RequestMapping(value = "/list")
    public ModelAndView doMachineList(HttpServletRequest request,
                                HttpServletResponse response, Model model, String ipLike) {
        List<MachineStats> machineList = machineCenter.getMachineStats(ipLike);
        model.addAttribute("list", machineList);
        model.addAttribute("ipLike", ipLike);
        model.addAttribute("machineActive", SuccessEnum.SUCCESS.value());
        return new ModelAndView("manage/machine/list");
    }
    
    /**
     * 机器实例展示
     * @param ip
     * @return
     */
    @RequestMapping(value = "/machineInstances")
    public ModelAndView doMachineInstances(HttpServletRequest request,
                                HttpServletResponse response, Model model, String ip) {
        //机器以及机器下面的实例信息
        MachineInfo machineInfo = machineCenter.getMachineInfoByIp(ip);
        List<InstanceInfo> instanceList = machineCenter.getMachineInstanceInfo(ip);
        List<InstanceStats> instanceStatList = machineCenter.getMachineInstanceStatsByIp(ip);       
        //统计信息
        fillInstanceModel(instanceList, instanceStatList, model);
        
        model.addAttribute("machineInfo", machineInfo);
        model.addAttribute("machineActive", SuccessEnum.SUCCESS.value());
        return new ModelAndView("manage/machine/machineInstances");
    }
    

    @RequestMapping(value = "/add", method = {RequestMethod.POST})
    public ModelAndView doAdd(HttpServletRequest request,
                              HttpServletResponse response, Model model) {
        int id = NumberUtils.toInt(request.getParameter("id"), 0);
        MachineInfo machineInfo = null;
        if (id > 0) {
        } else {
            machineInfo = new MachineInfo();
            machineInfo.setIp(request.getParameter("ip"));
            machineInfo.setRoom(request.getParameter("room"));
            machineInfo.setMem(NumberUtils.toInt(request.getParameter("mem"), 0));
            machineInfo.setCpu(NumberUtils.toInt(request.getParameter("cpu"), 0));
            machineInfo.setVirtual(NumberUtils.toInt(request.getParameter("virtual"), 0));
            machineInfo.setRealIp(request.getParameter("realIp"));
            machineInfo.setServiceTime(new Date());
            boolean isSuccess = machineDeployCenter.addMachine(machineInfo);
            model.addAttribute("result", isSuccess);
        }
        return new ModelAndView("");
    }
    
    @RequestMapping(value = "/delete")
    public ModelAndView doDelete(HttpServletRequest request, HttpServletResponse response, Model model) {
        String machineIp = request.getParameter("machineIp");
        if (StringUtils.isNotBlank(machineIp)) {
            MachineInfo machineInfo = machineCenter.getMachineInfoByIp(machineIp);
            boolean success = machineDeployCenter.removeMachine(machineInfo);
            logger.warn("delete machine {}, result is {}", machineIp, success);
        } else {
            logger.warn("machineIp is empty!");
        }
        return new ModelAndView("manage/machine/list");
    }
    
    
    /**
     * 实例统计信息
     * @param appAudit
     * @param model
     */
    protected void fillInstanceModel(List<InstanceInfo> instanceList, List<InstanceStats> appInstanceStats, Model model) {
        Map<String, MachineStats> machineStatsMap = new HashMap<String, MachineStats>();
        Map<String, Long> machineCanUseMem = new HashMap<String, Long>();
        Map<String, InstanceStats> instanceStatsMap = new HashMap<String, InstanceStats>();
        Map<Long, AppDesc> appInfoMap = new HashMap<Long, AppDesc>();

        for (InstanceStats instanceStats : appInstanceStats) {
            instanceStatsMap.put(instanceStats.getIp() + ":" + instanceStats.getPort(), instanceStats);
            appInfoMap.put(instanceStats.getAppId(), appService.getByAppId(instanceStats.getAppId()));
        }

        for (InstanceInfo instanceInfo : instanceList) {
            if (TypeUtil.isRedisSentinel(instanceInfo.getType())) {
                continue;
            }
            String ip = instanceInfo.getIp();
            if (machineStatsMap.containsKey(ip)) {
                continue;
            }
            List<MachineStats> machineStatsList = machineCenter.getMachineStats(ip);
            MachineStats machineStats = null;
            for (MachineStats stats : machineStatsList) {
                if (stats.getIp().equals(ip)) {
                    machineStats = stats;
                    machineStatsMap.put(ip, machineStats);
                    break;
                }
            }
            MachineStats ms = machineCenter.getMachineMemoryDetail(ip);
            machineCanUseMem.put(ip, ms.getMachineMemInfo().getLockedMem());
        }
        model.addAttribute("appInfoMap", appInfoMap);
        
        model.addAttribute("machineCanUseMem", machineCanUseMem);
        model.addAttribute("machineStatsMap", machineStatsMap);

        model.addAttribute("instanceList", instanceList);
        model.addAttribute("instanceStatsMap", instanceStatsMap);
    }
    
    
}

@@ -3493,45 +3493,122 @@
     
-machineInfo.setServiceTime(new Date()
+Date date = new Date();%0D%0A            machineInfo.setServiceTime(date);%0D%0A            machineInfo.setModifyTime(date
 );%0D%0A

修复 issue 4. cause by mysql dif version