Grafana客制化无限下钻
1.使用场景:DB对应InfluxDB
我要用Grafana展示两种DB的数据,一种是Relational Database,另一种是Sap DataBase(如ERP,BW等)。众所周知,RDB结构很理想,DataBase -> schema ->table or view > column ,而Sap DB数据结构相对来说比较复杂,DataBase level对应的就是 BW_System,Sap_System,其他的则对应table level
下面的则对应column level
2.插入infliux逻辑
- 将查出的Database level的数据将parentId设为0,
- 将查出的Schema level 的parentId 设为对应Database的id
- 将查出的Table(View) level的parentId设为对应Schema的id
- 将查出的Column level的parentId设为对应Table的id
3.展示数据
- 第一个Dashboard通过插入时的tag作为筛选条件,将parentId = 0的全部查出
.addField("content", transform(commonData.getContent()))
.addField("contentOld", transform(commonData.getContentOld()))
.addField("updateTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(commonData.getUpdateTime()))
.tag("integratorTag", k)
.tag("pareTagId", commonData.getParentId().intValue() + "")
.build());
将objectId设置超链,点击后获取objectId的值给到url,link到第二个Dashboard
- 第二个Dashboard相对简单,link自己,也是携带objectId作为parentId
问题1:对于Sap DB,table level的下一层还可能是 table level
问题2:因为是先有Dashboard,后有的link,column level还会再跳转一次空Dashboard
此处使用java开发了Router Service
来判断grafana的请求是分配给第二个Dashboard,还是第三个Dashboard
Router Service会解析Url的参数
- 添加了第三个Dashboard
完全复制第二个Dashboard,但是override没有data link
4.Router Service
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Controller
/**
* purpose:解析grafana的请求url进行下钻,路由到对应的url
* date:2021-11-09
*/
@RequestMapping("/parseUrl")
public ModelAndView parseUrl(HttpServletRequest request, HttpServletResponse response) {
//获取所有参数,map的key是你自己定义的
String queryString = request.getQueryString();
String[] split = queryString.split("&");
Map<String,String> resultMap = new HashMap();
for (String element : split) {
String[] every = element.split("=");
resultMap.put(every[0],every[1]);
}
String newUrl = "";
if (resultMap.get("objectType").contains("Field") || resultMap.get("objectType").contains("Column")) {
System.out.println("column level");
newUrl = routerUrl+"d/deltawwsb/showbottom?orgId=1&var-parentId=" + resultMap.get("parentId");
} else {
System.out.println( "table level");
newUrl = routerUrl+"d/deltawwsm/showmiddle?orgId=1&var-parentId=" + resultMap.get("objectId");
}
RedirectView redirectView = new RedirectView(newUrl);
ModelAndView mv = new ModelAndView(redirectView);
return mv;
}
- 同时我用link作为他的返回上一级
- 也是要判断爸爸是table level还是 Database level (针对sap)
上面的url路径 :http://localhost:8087/router/rollback?parentId=${parentId}
/**
* purpose:grafana新增返回按钮,判断是返回到top or middle
* date:2021-11-11
*/
@RequestMapping("/rollback")
public ModelAndView rollback(HttpServletRequest request, HttpServletResponse response) {
String queryString = request.getQueryString();
String[] split = queryString.split("&");
Map<String,String> resultMap = new HashMap();
for (String element : split) {
String[] every = element.split("=");
resultMap.put(every[0],every[1]);
}
String newUrl = "";
String parentId = resultMap.get("parentId");
int grandId = routerService.getGrandId(parentId);
if (grandId == 0){
int deleteGrandId = routerService.getDeleteGrandId(parentId);
if (deleteGrandId == 0){
int deleteSapGrandId = routerService.getDeleteSapGrandId(parentId);
if (deleteSapGrandId == 0){
newUrl = routerUrl+"d/deltawwst/showtop?orgId=1";
}else{
newUrl = routerUrl+"d/deltawwsm/showmiddle?orgId=1&var-parentId=" + deleteSapGrandId;
}
}else{
newUrl = routerUrl+"d/deltawwsm/showmiddle?orgId=1&var-parentId=" + deleteGrandId;
}
}else{
System.out.println("middle level");
newUrl = routerUrl+"d/deltawwsm/showmiddle?orgId=1&var-parentId=" + grandId;
}
RedirectView redirectView = new RedirectView(newUrl);
ModelAndView mv = new ModelAndView(redirectView);
return mv;
}
这个就灵活的去查爷爷的ID吧,连接你自己的DB
int grandId = routerService.getGrandId(parentId);