HostInfoCtrl.java 16.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
package com.ui.ctrl;


import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.ui.common.TagTypeEnum;
import com.ui.contants.HttpUriContants;
import com.ui.http.HttpRestClient;
import com.ui.model.BaseResponse;
import com.ui.model.req.HostInfoReq;
import com.ui.model.req.SetHostTagsReq;
import com.ui.model.req.TagReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * Created by yoho on 2016/6/14.
 * 查询机器信息
 */
@Controller
@RequestMapping("/hostInfoList")
public class HostInfoCtrl {

    Logger log = LoggerFactory.getLogger(HostInfoCtrl.class);

    @Autowired
    HttpRestClient httpRestClient;

    @RequestMapping("/toHostInfos")
    public ModelAndView toHostInfos() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("host/hostInfoList");

        mv.addObject("tagTypeList", TagTypeEnum.getAllType());
        mv.addObject("tagsListJson", this.getHostTagsByType("","groupName").getData());

        return mv;
    }

    @RequestMapping("/toHostTopoList")
    public ModelAndView toHostTopoList() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("host/hostTopoList");

        //初始化数据,先加载两层
        BaseResponse response = httpRestClient.defaultGet(HttpUriContants.TYPE_GET_HOST_TOPO, BaseResponse.class);
        if(response!=null&&response.getCode()==200){
            Gson gson = new Gson();
            String jsonString=gson.toJson( response.getData());
            mv.addObject("antvData",jsonString);
        }

        return mv;
    }

    @RequestMapping("/querySubHostTopo")
    @ResponseBody
    public BaseResponse querySubHostTopo(int typeId) {
        Map<String,Integer> map = new HashMap();
        map.put("typeId",typeId);
        return httpRestClient.defaultGet(HttpUriContants.TYPE_GET_HOST_SUB_TOPO, BaseResponse.class,map);
    }


    //numInRow 第二层的节点一排放几个
   /* private void getJsonAntvData(Map<String,List<HostGroup>> initDataMap,int cols,ModelAndView mv){
        int beginx=60;
        int beginy=60;

        int gap_level_top_h=100;
        int gap_h=50;

        List<AntvNode> nodes=new ArrayList<>();
        List<Map<String,String>> edges=new ArrayList<>();

        int maxSize=0;
        int last_y=0;
        for (Map.Entry<String, List<HostGroup>> entry : initDataMap.entrySet()) {
            String topNode = entry.getKey();
            int top_node_x=beginx;
            int top_node_y;
            if(maxSize==0){
                top_node_y=beginy;
            }else{
                top_node_y=last_y+gap_level_top_h;
            }

            AntvNode nodeLevelTop=new AntvNode();
            nodeLevelTop.setLevel(0);
            nodeLevelTop.setId(topNode);
            nodeLevelTop.setLabel(topNode);
            nodeLevelTop.setShape("rect");
            nodeLevelTop.setX(top_node_x);
            nodeLevelTop.setY(top_node_y);
            nodes.add(nodeLevelTop);
            last_y= nodeLevelTop.getY();

            maxSize++;

            List<HostGroup> ls=entry.getValue();
            if(ls==null||ls.size()<=0){
                nodeLevelTop.setLevel(1);

                continue;
            }else{
                double d=ls.size()*1.0/cols;
                maxSize +=(int)Math.ceil(d);
            }


            int secondCount=1;
            int row=1;
            int GAP_BETWEEN_NODE_X=120;
            for(HostGroup secondeNode:ls){
                String sndNodeID=""+secondeNode.getId();
                AntvNode nodeSnd=new AntvNode();
                nodeSnd.setLevel(1);
                nodeSnd.setId(sndNodeID);
                nodeSnd.setLabel(secondeNode.getGroupName());
                nodeSnd.setShape("rect");
                nodeSnd.setX(top_node_x+row*GAP_BETWEEN_NODE_X);
                nodeSnd.setY(top_node_y+secondCount*gap_h);
                nodes.add(nodeSnd);
                last_y=nodeSnd.getY();

                //连线
                //第二行,不再加连接线
                if(secondCount==1){
                    Map<String,String> edgeMap=new HashMap<>();
                    edgeMap.put("source",topNode);
                    edgeMap.put("target",sndNodeID);
                    edges.add(edgeMap);
                }


                if(row<cols){
                    row++;
                }else{
                    row = 1;
                    secondCount++;
                }

            }
        }

        AntvData data=new AntvData();
        data.setNodes(nodes);
        data.setEdges(edges);
        Gson gson = new Gson();
        String jsonString=gson.toJson( data,AntvData.class);

        mv.addObject("antvData",jsonString);
        mv.addObject("antvHeight",last_y+100);
    }*/

    /*@RequestMapping("/toHostTopoSub")
    public ModelAndView toHostTopoSub(String id) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("host/hostTopo");

        //获取子节点(有可能不存在),比如前台-gateway  ,有可能存在,前台-service
        String parentLabel="前台";
        boolean subNodeFlag=false;
        String currentLabel="gateway";
        if(id!=null&&!id.endsWith("1")){
            subNodeFlag=true;
            currentLabel="service";
        }

        Map<String,List<HostGroup>> initDataMap =new LinkedHashMap<>();
        initDataMap.put(currentLabel,tmpDetailData(subNodeFlag));

        getJsonAntvData(initDataMap,3, mv);

        mv.addObject("parentLabel",parentLabel);
        return mv;
    }*/

   /* private List<HostGroup> tmpDetailData(boolean subFlag){
        List<HostGroup> ls=new ArrayList<>();
        if(subFlag){

            HostGroup group14=new HostGroup();
            group14.setId(1001014);
            group14.setGroupName("erp-gateway");
            ls.add(group14);

            HostGroup group13=new HostGroup();
            group13.setId(1001013);
            group13.setGroupName("wechat");
            ls.add(group13);

            HostGroup group12=new HostGroup();
            group12.setId(1001012);
            group12.setGroupName("risk22");
            ls.add(group12);

            HostGroup group11=new HostGroup();
            group11.setId(1001011);
            group11.setGroupName("risk");
            ls.add(group11);

            HostGroup group10=new HostGroup();
            group10.setId(1001010);
            group10.setGroupName("bigdata");
            ls.add(group10);

            HostGroup group9=new HostGroup();
            group9.setId(1001009);
            group9.setGroupName("brower");
            ls.add(group9);

            HostGroup group8=new HostGroup();
            group8.setId(1001008);
            group8.setGroupName("uic");
            ls.add(group8);

            HostGroup group7=new HostGroup();
            group7.setId(1001007);
            group7.setGroupName("sns");
            ls.add(group7);

            HostGroup group6=new HostGroup();
            group6.setId(1001006);
            group6.setGroupName("message");
            ls.add(group6);

            HostGroup group5=new HostGroup();
            group5.setId(1001005);
            group5.setGroupName("product");
            ls.add(group5);

            HostGroup group4=new HostGroup();
            group4.setId(1001004);
            group4.setGroupName("promotion");
            ls.add(group4);

            HostGroup group3=new HostGroup();
            group3.setId(1001003);
            group3.setGroupName("order");
            ls.add(group3);


            HostGroup group2=new HostGroup();
            group2.setId(1001002);
            group2.setGroupName("resources");
            ls.add(group2);

            HostGroup group1=new HostGroup();
            group1.setId(1001001);
            group1.setGroupName("user");
            ls.add(group1);
        }
        Collections.reverse(ls);
        return ls;

    }


    private Map<String,List<HostGroup>> tmpConstructData(){
        Map<String,List<HostGroup>> initDataMap=new LinkedHashMap<>();
        HostGroup group6=new HostGroup();
        group6.setId(1006);
        group6.setGroupName("search");

        HostGroup group5=new HostGroup();
        group5.setId(1005);
        group5.setGroupName("mysql");

        HostGroup group4=new HostGroup();
        group4.setId(1004);
        group4.setGroupName("中间件");

        HostGroup group3=new HostGroup();
        group3.setId(1003);
        group3.setGroupName("service");

        HostGroup group2=new HostGroup();
        group2.setId(1002);
        group2.setGroupName("gateway");

        HostGroup group1=new HostGroup();
        group1.setId(1001);
        group1.setGroupName("nginx");

        initDataMap.put("前台", Lists.newArrayList(group1,group2,group3,group4,group5,group6));

        HostGroup group26=new HostGroup();
        group26.setId(2006);
        group26.setGroupName("greenplum");

        HostGroup group25=new HostGroup();
        group25.setId(2005);
        group25.setGroupName("mysql");

        HostGroup group24=new HostGroup();
        group24.setId(2004);
        group24.setGroupName("中间件");

        HostGroup group22=new HostGroup();
        group22.setId(2003);
        group22.setGroupName("service");

        HostGroup group21=new HostGroup();
        group21.setId(2001);
        group21.setGroupName("nginx");
        initDataMap.put("crm", Lists.newArrayList(group21,group22,group24,group25,group26));

        HostGroup group35=new HostGroup();
        group35.setId(3005);
        group35.setGroupName("mysql");


        HostGroup group32=new HostGroup();
        group32.setId(3002);
        group32.setGroupName("service");

        HostGroup group31=new HostGroup();
        group31.setId(3001);
        group31.setGroupName("nginx");
        initDataMap.put("线下店", Lists.newArrayList(group31,group32, group35));

        HostGroup group68=new HostGroup();
        group68.setId(68);
        group68.setGroupName("code-deploy");

        HostGroup group67=new HostGroup();
        group67.setId(67);
        group67.setGroupName("health");

        HostGroup group66=new HostGroup();
        group66.setId(66);
        group66.setGroupName("kafka");


        HostGroup group65=new HostGroup();
        group65.setId(65);
        group65.setGroupName("zabbix");

        HostGroup group64=new HostGroup();
        group64.setId(64);
        group64.setGroupName("gitlab");

        HostGroup group63=new HostGroup();
        group63.setId(63);
        group63.setGroupName("influxdb");


        HostGroup group62=new HostGroup();
        group62.setId(62);
        group62.setGroupName("dns");

        HostGroup group61=new HostGroup();
        group61.setId(61);
        group61.setGroupName("nginx");
        initDataMap.put("线下店", Lists.newArrayList(group31,group32, group35));
        initDataMap.put("ops", Lists.newArrayList(group61,group62,group63,group64,group65,group66,group67,group68));


        HostGroup group75=new HostGroup();
        group75.setId(75);
        group75.setGroupName("dns");

        HostGroup group74=new HostGroup();
        group74.setId(74);
        group74.setGroupName("mysql");

        HostGroup group73=new HostGroup();
        group73.setId(73);
        group73.setGroupName("search");


        HostGroup group72=new HostGroup();
        group72.setId(72);
        group72.setGroupName("nginx");

        HostGroup group71=new HostGroup();
        group71.setId(71);
        group71.setGroupName("socail");

        initDataMap.put("web", Lists.newArrayList(group71,group72,group73,group74,group75));


        HostGroup group86=new HostGroup();
        group86.setId(86);
        group86.setGroupName("containers");


        HostGroup group85=new HostGroup();
        group85.setId(85);
        group85.setGroupName("dns");

        HostGroup group84=new HostGroup();
        group84.setId(84);
        group84.setGroupName("mysql");

        HostGroup group83=new HostGroup();
        group83.setId(83);
        group83.setGroupName("search");


        HostGroup group82=new HostGroup();
        group82.setId(82);
        group82.setGroupName("nginx");

        HostGroup group81=new HostGroup();
        group81.setId(81);
        group81.setGroupName("socail");
        initDataMap.put("media", Lists.newArrayList(group81,group82,group83,group84,group85,group86));

        HostGroup group92=new HostGroup();
        group92.setId(92);
        group92.setGroupName("mysql");

        HostGroup group91=new HostGroup();
        group91.setId(91);
        group91.setGroupName("平台端");
        initDataMap.put("后台", Lists.newArrayList(group91,group92));
        return initDataMap;
    }*/



    /**
     * 根据标签类别,查询该类别下的属于tag
     * @param type
     * @return
     */
    @RequestMapping("/getHostTagsByType")
    @ResponseBody
    public BaseResponse getHostTagsByType(String type,String idMark){
        if(type==null){
            type="";
        }
        TagReq req=new TagReq();
        req.setTagTypeContent(type);
        BaseResponse response = httpRestClient.defaultPost(HttpUriContants.HOST_ALL_GROUPS, req, BaseResponse.class);
        List<Map<String, String>> hostGroups = (List<Map<String, String>>) response.getData();
        List<Map<String, String>> tags = new ArrayList<Map<String, String>>();
        if (hostGroups != null && hostGroups.size() > 0) {
            for (Map<String, String> m : hostGroups) {
                HashMap map = new HashMap();
                map.put("text", m.get("groupName"));
                if("groupName".equals(idMark)){
                    map.put("id", m.get("groupName"));
                }else{
                    map.put("id", m.get("id"));
                }
                tags.add(map);
            }
        }
        return new BaseResponse<>(JSON.toJSON(tags));
    }

    @RequestMapping("/toEditHostInfos")
    public ModelAndView toEditHostInfos(Map model) {
        model.put("tagTypeMapJson", JSON.toJSONString(TagTypeEnum.getAllTypeMap()));
        return new ModelAndView("host/editHostInfo",model);
    }


    @RequestMapping("/getHostInfos")
    @ResponseBody
    public BaseResponse getHostInfos(HostInfoReq req) throws Exception {

        List<String> tagsList = JSON.parseArray(req.getTags(), String.class);
        req.setTagsList(tagsList);
        BaseResponse response = httpRestClient.defaultPost(HttpUriContants.GET_HOST_INFOS, req, BaseResponse.class);
        return response;
    }


    @RequestMapping("/saveHostInfo")
    @ResponseBody
    public BaseResponse saveHostInfo(HostInfoReq req) throws Exception {
        BaseResponse response = httpRestClient.defaultPost(HttpUriContants.SAVE_HOST_INFOS, req, BaseResponse.class);
        return response;
    }


    @RequestMapping("/delHostInfo")
    @ResponseBody
    public BaseResponse<Integer> delHostInfo(int id,String hostIp) throws Exception {
        //hostIp ,用作日志
        Map<String, Object> map=new HashMap<>();
        map.put("id",id);
        map.put("hostIp",hostIp);
        BaseResponse response = httpRestClient.defaultGet(HttpUriContants.DEL_HOST_INFOS + "?id=" + id, BaseResponse.class);
        return response;
    }

    @RequestMapping("/getHostInfoById")
    @ResponseBody
    public BaseResponse getHostInfoById(int id) throws Exception {
        BaseResponse response = httpRestClient.defaultGet(HttpUriContants.GET_HOST_INFO_BY_ID + "?id=" + id, BaseResponse.class);
        return response;
    }


    @RequestMapping("/synHostInfo")
    @ResponseBody
    public BaseResponse synHostInfo() throws Exception {

        BaseResponse response = httpRestClient.defaultGet(HttpUriContants.SYN_HOST_INFOS, BaseResponse.class);
        return response;
    }


    @RequestMapping("/toSetHostTags")
    public ModelAndView toSetHostTags(String hostIds) {
        ModelAndView mv = new ModelAndView("host/setHostTags");
        mv.addObject("tagTypeMapJson", JSON.toJSONString(TagTypeEnum.getAllTypeMap()));
        return mv;
    }

    /**
     * 备注--参数传递乱码,所以定义了一个SetHostTagsReq类
     *
     * @param req
     * @return
     */
    @RequestMapping("/saveHostTags")
    @ResponseBody
    public BaseResponse saveHostTags(SetHostTagsReq req) {
        BaseResponse response = httpRestClient.defaultPost(HttpUriContants.SAVE_HOST_TAGS, req, BaseResponse.class);
        return response;
    }

}