grafana+SimpleJson配置图表

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

package com.cn.hnust.controller;

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

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

@Controller
@RequestMapping("/json")
public class JsonController {

	//设置数据源
	@RequestMapping()
	@ResponseBody
	public Map list(HttpServletRequest request,HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Headers", "accept, content-type");
        response.setHeader("Access-Control-Allow-Methods", "POST");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Accept", "application/json");
        response.setHeader("Content-Type", "application/json");
        response.setHeader("Authorization", "Bearer eyJrIjoidGFIOW41aHRoMXZ5aTkxcDBNYUxteDk3TU9zVUhoSkUiLCJuIjoiZ3JhZmFuYV90ZXN0IiwiaWQiOjF9");
        Map map = new HashMap();
        map.put("result", "200 ok");
        return map;
	}
	
	//设置参数
	@RequestMapping("/search")
	@ResponseBody
	public List search(HttpServletRequest request,HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Headers", "accept, content-type");
        response.setHeader("Access-Control-Allow-Methods", "POST");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Accept", "application/json");
        response.setHeader("Content-Type", "application/json");
        response.setHeader("Authorization", "Bearer eyJrIjoidGFIOW41aHRoMXZ5aTkxcDBNYUxteDk3TU9zVUhoSkUiLCJuIjoiZ3JhZmFuYV90ZXN0IiwiaWQiOjF9");
        
        List result = new ArrayList();
        result.add("CPU");
        result.add("RAM");
        result.add("LOAD");
        result.add("SWAP");
        result.add("DISK");
        result.add("NET");
        return result;
	}
	
	//查询
	@RequestMapping("/query")
    @ResponseBody
    public List query(@RequestBody Map  params, HttpServletRequest request,HttpServletResponse response) {
		List> result = new ArrayList>() ;
        List targetList = (List) params.get("targets");
       for (Map targetMap : targetList){
            String target = (String)targetMap.get("target");
            //Map scopedVars = (Map) params.get("scopedVars");
            //Map IP = (Map) scopedVars.get("IP");
            //String nodeIP = (String) IP.get("text");
            if (target.equals("CPU")){
                result.add(strjson());//放入json数据
            }else if (target.equals("RAM")){
                result.add(null);//放入json数据
            }else if (target.equals("LOAD")) {
                result.add(null);
            }else if (target.equals("SWAP")){
                result.add(null);
            }else if(target.equals("DISK")){
                result.add(null);
            }else if (target.equals("NET")){
            	result.add(null);
            }
        }
        response.setHeader("Access-Control-Allow-Headers", "accept, content-type");
        response.setHeader("Access-Control-Allow-Methods", "POST");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Accept", "application/json");
        response.setHeader("Content-Type", "application/json");
        response.setHeader("Authorization", "Bearer eyJrIjoidGFIOW41aHRoMXZ5aTkxcDBNYUxteDk3TU9zVUhoSkUiLCJuIjoiZ3JhZmFuYV90ZXN0IiwiaWQiOjF9");
        Collections.sort(result, new Comparator>() {
            public int compare(Map o1, Map o2) {
                String name1 = String.valueOf(o1.get("target").toString()) ;
                String name2 = String.valueOf(o2.get("target").toString()) ; 
                return name1.compareTo(name2);
            }
        });
        return result;
    }
	
	
	private Map strjson() {
		//String str = "{\"A\":{\"tables\":[{\"columns\":[{\"text\":\"序列\",\"sort\":true,\"desc\":true,\"title\":\"序列\",\"style\":{\"alias\":\"\",\"colorMode\":null,\"colors\":[\"rgba(245, 54, 54, 0.9)\",\"rgba(237, 129, 40, 0.89)\",\"rgba(50, 172, 45, 0.97)\"],\"decimals\":2,\"pattern\":\"/.*/\",\"thresholds\":[],\"type\":\"number\",\"unit\":\"short\"},\"hidden\":false,\"$$hashKey\":\"object:644\"},{\"text\":\"水果名称12\",\"title\":\"水果名称12\",\"style\":{\"alias\":\"\",\"colorMode\":null,\"colors\":[\"rgba(245, 54, 54, 0.9)\",\"rgba(237, 129, 40, 0.89)\",\"rgba(50, 172, 45, 0.97)\"],\"decimals\":2,\"pattern\":\"/.*/\",\"thresholds\":[],\"type\":\"number\",\"unit\":\"short\"},\"hidden\":false,\"$$hashKey\":\"object:645\"},{\"text\":\"价钱\",\"title\":\"价钱\",\"style\":{\"alias\":\"\",\"colorMode\":null,\"colors\":[\"rgba(245, 54, 54, 0.9)\",\"rgba(237, 129, 40, 0.89)\",\"rgba(50, 172, 45, 0.97)\"],\"decimals\":2,\"pattern\":\"/.*/\",\"thresholds\":[],\"type\":\"number\",\"unit\":\"short\"},\"hidden\":false,\"$$hashKey\":\"object:646\"},{\"text\":\"重量(kg)\",\"title\":\"重量(kg)\",\"style\":{\"alias\":\"\",\"colorMode\":null,\"colors\":[\"rgba(245, 54, 54, 0.9)\",\"rgba(237, 129, 40, 0.89)\",\"rgba(50, 172, 45, 0.97)\"],\"decimals\":2,\"pattern\":\"/.*/\",\"thresholds\":[],\"type\":\"number\",\"unit\":\"short\"},\"hidden\":false,\"$$hashKey\":\"object:647\"},{\"text\":\"总价钱\",\"title\":\"总价钱\",\"style\":{\"alias\":\"\",\"colorMode\":null,\"colors\":[\"rgba(245, 54, 54, 0.9)\",\"rgba(237, 129, 40, 0.89)\",\"rgba(50, 172, 45, 0.97)\"],\"decimals\":2,\"pattern\":\"/.*/\",\"thresholds\":[],\"type\":\"number\",\"unit\":\"short\"},\"hidden\":false,\"$$hashKey\":\"object:648\"}],\"rows\":[[1,\"水果01\",4,2,8],[2,\"水果02\",5,2,10],[3,\"水果03\",6,2,12],[4,\"水果04\",7,2,14],[5,\"水果05\",8,2,16]]}]}}";
		Map map = new HashMap<>();
		JSONArray ja = new JSONArray();
		for (int i = 0; i < 10; i++) {
			JSONObject jb = new JSONObject();
			jb.put("num", 10*i);
			jb.put("createTime", "2019-05-09T"+(11+i)+":01:01");
			ja.add(jb);
		}
		map.put("reasult", ja);
		return map;
	}

	//添加注解
	@RequestMapping("/annotations")
    @ResponseBody
    public Map Annotations() {
        Map map = new HashMap();
        map.put("result", "200 ok");
        return map;
    }
}

 

转载于:https://my.oschina.net/lisc2016/blog/3047585

你可能感兴趣的:(json,python,java)