Authored by FengRuwei

查询javaapi状态

package com.monitor.influxdb.mapper;
import com.alibaba.fastjson.JSONObject;
import com.monitor.model.domain.JavaApiStaticsModel;
import com.monitor.model.response.JavaApiStatusRep;
import java.util.List;
import java.util.Map;
/**
* Created by fruwei on 2016/6/21.
*/
public interface IJavaApiStaticsMapper {
public void insertJavaApiStatics(String influxDBName,JSONObject statics);
public void insertJavaApiStatics(String influxDBName, JSONObject statics);
public JavaApiStaticsModel selectlatestJavaApiStatics(String influxDBName, Map<String, Integer> query);
public List<JavaApiStaticsModel> selectlatestJavaApiStaticsList(String influxDBName, List<Map<String, Integer>> query);
}
... ...
... ... @@ -4,11 +4,20 @@ import com.alibaba.fastjson.JSONObject;
import com.monitor.common.contants.InfluxDBContants;
import com.monitor.influxdb.InluxDBSingle;
import com.monitor.influxdb.mapper.IJavaApiStaticsMapper;
import com.monitor.model.domain.JavaApiStaticsModel;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Point;
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;
/**
* Created by fruwei on 2016/6/21.
*/
... ... @@ -18,6 +27,8 @@ public class JavaApiStaticsMapper implements IJavaApiStaticsMapper {
@Autowired
private InluxDBSingle inluxDBSingle;
Random random = new Random();
@Override
public void insertJavaApiStatics(String influxDBName, JSONObject statics) {
... ... @@ -25,7 +36,9 @@ public class JavaApiStaticsMapper implements IJavaApiStaticsMapper {
BatchPoints batchPoints = BatchPoints
.database(InfluxDBContants.APP_ALARM).retentionPolicy("default")
.build();
Point point = Point.measurement(InfluxDBContants.YOMO_TB_JAVAAPI)
Point.Builder pointBuilder = Point.measurement(InfluxDBContants.YOMO_TB_JAVAAPI)
.tag("api_id", "api_id")
.tag("mobj_id", "mobj_id")
.addField("api_id", statics.getIntValue("api_id"))
.addField("api_name", statics.getString("api_name"))
.addField("api_type", statics.getIntValue("api_type"))
... ... @@ -37,14 +50,75 @@ public class JavaApiStaticsMapper implements IJavaApiStaticsMapper {
.addField("start", statics.getLongValue("end"))
.addField("end", statics.getLongValue("end"))
.addField("is_exception", statics.getBooleanValue("is_exception"))
.addField("exception", statics.getString("exception"))
.build();
.time(System.currentTimeMillis() * 1000000 + random.nextInt(999999), TimeUnit.NANOSECONDS);
if (statics.getBooleanValue("is_exception") == true) {
pointBuilder.addField("exception", statics.getString("exception"));
} else {
pointBuilder.addField("response", statics.getString("response"));
}
Point point = pointBuilder.build();
batchPoints.point(point);
inluxDBSingle.getInfluxDBByName(InfluxDBContants.AWS).getInfluxDB()
.write(batchPoints);
}
@Override
public JavaApiStaticsModel selectlatestJavaApiStatics(String influxDBName, Map<String, Integer> param) {
int api_id = param.get("api_id");
int mobj_id = param.get("mobj_id");
String sql = "select * from " + InfluxDBContants.YOMO_TB_JAVAAPI + " where time > now() - 1h ";
sql += " and api_id=" + param.get("api_id");
sql += " and mobj_id=" + param.get("mobj_id");
sql += " order by time desc limit 1";
Query query = new Query(sql, InfluxDBContants.APP_ALARM);
QueryResult result = inluxDBSingle.getInfluxDBByName(InfluxDBContants.AWS).getInfluxDB().query(query);
JavaApiStaticsModel javaApiStaticsModel = new JavaApiStaticsModel();
javaApiStaticsModel.setServiceId(api_id);
javaApiStaticsModel.setMObjectId(mobj_id);
javaApiStaticsModel.setStatus(0);
QueryResult.Result rel = result.getResults().get(0);
try {
QueryResult.Series series = rel.getSeries().get(0);
Boolean status = (Boolean) series.getValues().get(0).get(series.getColumns().indexOf("is_exception"));
if (status == true) {
javaApiStaticsModel.setStatus(1);
} else
javaApiStaticsModel.setStatus(0);
}catch (Exception e){
e.printStackTrace();
}
return javaApiStaticsModel;
}
@Override
public List<JavaApiStaticsModel> selectlatestJavaApiStaticsList(String influxDBName, List<Map<String, Integer>> paramList) {
//TODO 后期改批量查询
List<JavaApiStaticsModel> javaApiStaticsModels = new ArrayList<>();
for (Map<String, Integer> param : paramList) {
JavaApiStaticsModel model = selectlatestJavaApiStatics(influxDBName, param);
javaApiStaticsModels.add(model);
}
return javaApiStaticsModels;
}
}
... ...
... ... @@ -43,18 +43,11 @@ public class ZkMapper extends InfluxDBQuery implements IZkMapper {
@Override
public int selectCountByCodition(PageBean page) {
String command = "SELECT COUNT(id) FROM "+InfluxDBContants.ZOOKEEPER_ALARM;
return query(influxDBName, command, InfluxDBContants.ZOOKEEPER_ALARM,
"ZkMapper", "selectCountByCodition");
return 1;
}
@Override
public List<ZkInfo> selectZkInfosByCodition(PageBean page) {
String command="SELECT id, hostIp, isLive from "+InfluxDBContants.ZOOKEEPER_ALARM+
" ORDER BY time DESC LIMIT " + page.getParams().get("pageSize") +
" OFFSET " + page.getParams().get("startIndex");
return query(influxDBName, command, InfluxDBContants.ZOOKEEPER_ALARM,
"ZkMapper", "selectZkInfosByCodition");
return null;
}
}
... ...
... ... @@ -47,8 +47,12 @@ public class InfluxDBJavaApiHandler implements IJavaApiHadnler {
jsonObject.put("end", javaApiStatics.getEndTime());
jsonObject.put("is_exception", javaApiStatics.isHasException());
jsonObject.put("exception", javaApiStatics.getException());
if (javaApiStatics.isHasException()) {
jsonObject.put("exception", javaApiStatics.getException());
} else {
JSONObject rep = javaApiStatics.getResponse();
jsonObject.put("response", javaApiStatics.getResponse());
}
javaApiStaticsMapper.insertJavaApiStatics(null, jsonObject);
... ...
package com.monitor.model.domain;
import lombok.Data;
/**
* Created by fruwei on 2016/6/22.
*/
@Data
public class JavaApiStaticsModel {
private Integer serviceId;
private Integer serviceType;
private Integer mObjectId;
private Integer status;
@Override
public String toString() {
return "JavaApiStaticsModel{" +
"mObjectId=" + mObjectId +
", serviceId=" + serviceId +
", serviceType=" + serviceType +
", status=" + status +
'}';
}
}
... ...
package com.monitor;
import com.monitor.influxdb.mapper.impl.JavaApiStaticsMapper;
import com.monitor.javaserver.common.JavaApiStatics;
import com.monitor.model.domain.JavaApiStaticsModel;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by fruwei on 2016/6/22.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath*:META-INF/spring/spring*.xml"})
@PropertySource({"classpath:jdbc.properties",
"classpath:*.properties"})
public class JavaApiInfluxDBTest {
Logger logger = LoggerFactory.getLogger("test");
@Autowired
JavaApiStaticsMapper javaApiStaticsMapper;
@Test
public void testQuery() {
Map<String, Integer> param = new HashMap<>();
param.put("api_id", 21);
param.put("mobj_id", 43);
JavaApiStaticsModel rel = javaApiStaticsMapper.selectlatestJavaApiStatics(null, param);
logger.info(rel.toString());
}
@Test
public void testBatchQuery() {
List<Map<String, Integer> > listParam=new ArrayList<>();
Map<String, Integer> param1 = new HashMap<>();
param1.put("api_id", 21);
param1.put("mobj_id", 43);
listParam.add(param1);
Map<String, Integer> param2 = new HashMap<>();
param2.put("api_id", 19);
param2.put("mobj_id", 134);
listParam.add(param2);
List<JavaApiStaticsModel> rel = javaApiStaticsMapper.selectlatestJavaApiStaticsList(null, listParam);
logger.info(rel.toString());
}
}
... ...