Showing
6 changed files
with
189 additions
and
15 deletions
1 | package com.monitor.influxdb.mapper; | 1 | package com.monitor.influxdb.mapper; |
2 | 2 | ||
3 | import com.alibaba.fastjson.JSONObject; | 3 | import com.alibaba.fastjson.JSONObject; |
4 | +import com.monitor.model.domain.JavaApiStaticsModel; | ||
5 | +import com.monitor.model.response.JavaApiStatusRep; | ||
6 | + | ||
7 | +import java.util.List; | ||
8 | +import java.util.Map; | ||
4 | 9 | ||
5 | /** | 10 | /** |
6 | * Created by fruwei on 2016/6/21. | 11 | * Created by fruwei on 2016/6/21. |
7 | */ | 12 | */ |
8 | public interface IJavaApiStaticsMapper { | 13 | public interface IJavaApiStaticsMapper { |
9 | 14 | ||
10 | - public void insertJavaApiStatics(String influxDBName,JSONObject statics); | 15 | + public void insertJavaApiStatics(String influxDBName, JSONObject statics); |
16 | + | ||
17 | + | ||
18 | + public JavaApiStaticsModel selectlatestJavaApiStatics(String influxDBName, Map<String, Integer> query); | ||
11 | 19 | ||
20 | + public List<JavaApiStaticsModel> selectlatestJavaApiStaticsList(String influxDBName, List<Map<String, Integer>> query); | ||
12 | 21 | ||
13 | } | 22 | } |
@@ -4,11 +4,20 @@ import com.alibaba.fastjson.JSONObject; | @@ -4,11 +4,20 @@ import com.alibaba.fastjson.JSONObject; | ||
4 | import com.monitor.common.contants.InfluxDBContants; | 4 | import com.monitor.common.contants.InfluxDBContants; |
5 | import com.monitor.influxdb.InluxDBSingle; | 5 | import com.monitor.influxdb.InluxDBSingle; |
6 | import com.monitor.influxdb.mapper.IJavaApiStaticsMapper; | 6 | import com.monitor.influxdb.mapper.IJavaApiStaticsMapper; |
7 | +import com.monitor.model.domain.JavaApiStaticsModel; | ||
7 | import org.influxdb.dto.BatchPoints; | 8 | import org.influxdb.dto.BatchPoints; |
8 | import org.influxdb.dto.Point; | 9 | import org.influxdb.dto.Point; |
10 | +import org.influxdb.dto.Query; | ||
11 | +import org.influxdb.dto.QueryResult; | ||
9 | import org.springframework.beans.factory.annotation.Autowired; | 12 | import org.springframework.beans.factory.annotation.Autowired; |
10 | import org.springframework.stereotype.Component; | 13 | import org.springframework.stereotype.Component; |
11 | 14 | ||
15 | +import java.util.ArrayList; | ||
16 | +import java.util.List; | ||
17 | +import java.util.Map; | ||
18 | +import java.util.Random; | ||
19 | +import java.util.concurrent.TimeUnit; | ||
20 | + | ||
12 | /** | 21 | /** |
13 | * Created by fruwei on 2016/6/21. | 22 | * Created by fruwei on 2016/6/21. |
14 | */ | 23 | */ |
@@ -18,6 +27,8 @@ public class JavaApiStaticsMapper implements IJavaApiStaticsMapper { | @@ -18,6 +27,8 @@ public class JavaApiStaticsMapper implements IJavaApiStaticsMapper { | ||
18 | @Autowired | 27 | @Autowired |
19 | private InluxDBSingle inluxDBSingle; | 28 | private InluxDBSingle inluxDBSingle; |
20 | 29 | ||
30 | + Random random = new Random(); | ||
31 | + | ||
21 | @Override | 32 | @Override |
22 | public void insertJavaApiStatics(String influxDBName, JSONObject statics) { | 33 | public void insertJavaApiStatics(String influxDBName, JSONObject statics) { |
23 | 34 | ||
@@ -25,7 +36,9 @@ public class JavaApiStaticsMapper implements IJavaApiStaticsMapper { | @@ -25,7 +36,9 @@ public class JavaApiStaticsMapper implements IJavaApiStaticsMapper { | ||
25 | BatchPoints batchPoints = BatchPoints | 36 | BatchPoints batchPoints = BatchPoints |
26 | .database(InfluxDBContants.APP_ALARM).retentionPolicy("default") | 37 | .database(InfluxDBContants.APP_ALARM).retentionPolicy("default") |
27 | .build(); | 38 | .build(); |
28 | - Point point = Point.measurement(InfluxDBContants.YOMO_TB_JAVAAPI) | 39 | + Point.Builder pointBuilder = Point.measurement(InfluxDBContants.YOMO_TB_JAVAAPI) |
40 | + .tag("api_id", "api_id") | ||
41 | + .tag("mobj_id", "mobj_id") | ||
29 | .addField("api_id", statics.getIntValue("api_id")) | 42 | .addField("api_id", statics.getIntValue("api_id")) |
30 | .addField("api_name", statics.getString("api_name")) | 43 | .addField("api_name", statics.getString("api_name")) |
31 | .addField("api_type", statics.getIntValue("api_type")) | 44 | .addField("api_type", statics.getIntValue("api_type")) |
@@ -37,14 +50,75 @@ public class JavaApiStaticsMapper implements IJavaApiStaticsMapper { | @@ -37,14 +50,75 @@ public class JavaApiStaticsMapper implements IJavaApiStaticsMapper { | ||
37 | .addField("start", statics.getLongValue("end")) | 50 | .addField("start", statics.getLongValue("end")) |
38 | .addField("end", statics.getLongValue("end")) | 51 | .addField("end", statics.getLongValue("end")) |
39 | .addField("is_exception", statics.getBooleanValue("is_exception")) | 52 | .addField("is_exception", statics.getBooleanValue("is_exception")) |
40 | - .addField("exception", statics.getString("exception")) | ||
41 | - .build(); | 53 | + .time(System.currentTimeMillis() * 1000000 + random.nextInt(999999), TimeUnit.NANOSECONDS); |
54 | + | ||
55 | + if (statics.getBooleanValue("is_exception") == true) { | ||
56 | + pointBuilder.addField("exception", statics.getString("exception")); | ||
57 | + } else { | ||
58 | + pointBuilder.addField("response", statics.getString("response")); | ||
59 | + } | ||
60 | + | ||
61 | + Point point = pointBuilder.build(); | ||
42 | batchPoints.point(point); | 62 | batchPoints.point(point); |
43 | 63 | ||
44 | inluxDBSingle.getInfluxDBByName(InfluxDBContants.AWS).getInfluxDB() | 64 | inluxDBSingle.getInfluxDBByName(InfluxDBContants.AWS).getInfluxDB() |
45 | .write(batchPoints); | 65 | .write(batchPoints); |
46 | 66 | ||
47 | 67 | ||
68 | + } | ||
69 | + | ||
70 | + @Override | ||
71 | + public JavaApiStaticsModel selectlatestJavaApiStatics(String influxDBName, Map<String, Integer> param) { | ||
72 | + int api_id = param.get("api_id"); | ||
73 | + int mobj_id = param.get("mobj_id"); | ||
74 | + String sql = "select * from " + InfluxDBContants.YOMO_TB_JAVAAPI + " where time > now() - 1h "; | ||
75 | + sql += " and api_id=" + param.get("api_id"); | ||
76 | + sql += " and mobj_id=" + param.get("mobj_id"); | ||
77 | + sql += " order by time desc limit 1"; | ||
78 | + Query query = new Query(sql, InfluxDBContants.APP_ALARM); | ||
79 | + | ||
80 | + QueryResult result = inluxDBSingle.getInfluxDBByName(InfluxDBContants.AWS).getInfluxDB().query(query); | ||
81 | + | ||
82 | + JavaApiStaticsModel javaApiStaticsModel = new JavaApiStaticsModel(); | ||
83 | + javaApiStaticsModel.setServiceId(api_id); | ||
84 | + javaApiStaticsModel.setMObjectId(mobj_id); | ||
85 | + javaApiStaticsModel.setStatus(0); | ||
86 | + | ||
87 | + QueryResult.Result rel = result.getResults().get(0); | ||
88 | + | ||
89 | + try { | ||
90 | + QueryResult.Series series = rel.getSeries().get(0); | ||
91 | + | ||
92 | + Boolean status = (Boolean) series.getValues().get(0).get(series.getColumns().indexOf("is_exception")); | ||
93 | + | ||
94 | + if (status == true) { | ||
95 | + javaApiStaticsModel.setStatus(1); | ||
96 | + } else | ||
97 | + javaApiStaticsModel.setStatus(0); | ||
98 | + | ||
99 | + }catch (Exception e){ | ||
100 | + e.printStackTrace(); | ||
101 | + } | ||
102 | + return javaApiStaticsModel; | ||
48 | 103 | ||
49 | } | 104 | } |
105 | + | ||
106 | + | ||
107 | + | ||
108 | + @Override | ||
109 | + public List<JavaApiStaticsModel> selectlatestJavaApiStaticsList(String influxDBName, List<Map<String, Integer>> paramList) { | ||
110 | + | ||
111 | + //TODO 后期改批量查询 | ||
112 | + | ||
113 | + List<JavaApiStaticsModel> javaApiStaticsModels = new ArrayList<>(); | ||
114 | + | ||
115 | + for (Map<String, Integer> param : paramList) { | ||
116 | + JavaApiStaticsModel model = selectlatestJavaApiStatics(influxDBName, param); | ||
117 | + javaApiStaticsModels.add(model); | ||
118 | + } | ||
119 | + | ||
120 | + return javaApiStaticsModels; | ||
121 | + } | ||
122 | + | ||
123 | + | ||
50 | } | 124 | } |
@@ -43,18 +43,11 @@ public class ZkMapper extends InfluxDBQuery implements IZkMapper { | @@ -43,18 +43,11 @@ public class ZkMapper extends InfluxDBQuery implements IZkMapper { | ||
43 | @Override | 43 | @Override |
44 | public int selectCountByCodition(PageBean page) { | 44 | public int selectCountByCodition(PageBean page) { |
45 | String command = "SELECT COUNT(id) FROM "+InfluxDBContants.ZOOKEEPER_ALARM; | 45 | String command = "SELECT COUNT(id) FROM "+InfluxDBContants.ZOOKEEPER_ALARM; |
46 | - return query(influxDBName, command, InfluxDBContants.ZOOKEEPER_ALARM, | ||
47 | - "ZkMapper", "selectCountByCodition"); | ||
48 | - | 46 | + return 1; |
49 | } | 47 | } |
50 | 48 | ||
51 | @Override | 49 | @Override |
52 | public List<ZkInfo> selectZkInfosByCodition(PageBean page) { | 50 | public List<ZkInfo> selectZkInfosByCodition(PageBean page) { |
53 | - String command="SELECT id, hostIp, isLive from "+InfluxDBContants.ZOOKEEPER_ALARM+ | ||
54 | - " ORDER BY time DESC LIMIT " + page.getParams().get("pageSize") + | ||
55 | - " OFFSET " + page.getParams().get("startIndex"); | ||
56 | - | ||
57 | - return query(influxDBName, command, InfluxDBContants.ZOOKEEPER_ALARM, | ||
58 | - "ZkMapper", "selectZkInfosByCodition"); | 51 | +return null; |
59 | } | 52 | } |
60 | } | 53 | } |
@@ -47,8 +47,12 @@ public class InfluxDBJavaApiHandler implements IJavaApiHadnler { | @@ -47,8 +47,12 @@ public class InfluxDBJavaApiHandler implements IJavaApiHadnler { | ||
47 | jsonObject.put("end", javaApiStatics.getEndTime()); | 47 | jsonObject.put("end", javaApiStatics.getEndTime()); |
48 | 48 | ||
49 | jsonObject.put("is_exception", javaApiStatics.isHasException()); | 49 | jsonObject.put("is_exception", javaApiStatics.isHasException()); |
50 | - jsonObject.put("exception", javaApiStatics.getException()); | ||
51 | - | 50 | + if (javaApiStatics.isHasException()) { |
51 | + jsonObject.put("exception", javaApiStatics.getException()); | ||
52 | + } else { | ||
53 | + JSONObject rep = javaApiStatics.getResponse(); | ||
54 | + jsonObject.put("response", javaApiStatics.getResponse()); | ||
55 | + } | ||
52 | 56 | ||
53 | javaApiStaticsMapper.insertJavaApiStatics(null, jsonObject); | 57 | javaApiStaticsMapper.insertJavaApiStatics(null, jsonObject); |
54 | 58 |
1 | +package com.monitor.model.domain; | ||
2 | + | ||
3 | +import lombok.Data; | ||
4 | + | ||
5 | +/** | ||
6 | + * Created by fruwei on 2016/6/22. | ||
7 | + */ | ||
8 | +@Data | ||
9 | +public class JavaApiStaticsModel { | ||
10 | + private Integer serviceId; | ||
11 | + private Integer serviceType; | ||
12 | + private Integer mObjectId; | ||
13 | + private Integer status; | ||
14 | + | ||
15 | + | ||
16 | + @Override | ||
17 | + public String toString() { | ||
18 | + return "JavaApiStaticsModel{" + | ||
19 | + "mObjectId=" + mObjectId + | ||
20 | + ", serviceId=" + serviceId + | ||
21 | + ", serviceType=" + serviceType + | ||
22 | + ", status=" + status + | ||
23 | + '}'; | ||
24 | + } | ||
25 | +} |
1 | +package com.monitor; | ||
2 | + | ||
3 | +import com.monitor.influxdb.mapper.impl.JavaApiStaticsMapper; | ||
4 | +import com.monitor.javaserver.common.JavaApiStatics; | ||
5 | +import com.monitor.model.domain.JavaApiStaticsModel; | ||
6 | +import org.junit.Test; | ||
7 | +import org.junit.runner.RunWith; | ||
8 | +import org.slf4j.Logger; | ||
9 | +import org.slf4j.LoggerFactory; | ||
10 | +import org.springframework.beans.factory.annotation.Autowired; | ||
11 | +import org.springframework.context.annotation.PropertySource; | ||
12 | +import org.springframework.test.context.ContextConfiguration; | ||
13 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
14 | + | ||
15 | +import java.util.ArrayList; | ||
16 | +import java.util.HashMap; | ||
17 | +import java.util.List; | ||
18 | +import java.util.Map; | ||
19 | + | ||
20 | +/** | ||
21 | + * Created by fruwei on 2016/6/22. | ||
22 | + */ | ||
23 | +@RunWith(SpringJUnit4ClassRunner.class) | ||
24 | +@ContextConfiguration(locations = { | ||
25 | + "classpath*:META-INF/spring/spring*.xml"}) | ||
26 | +@PropertySource({"classpath:jdbc.properties", | ||
27 | + "classpath:*.properties"}) | ||
28 | +public class JavaApiInfluxDBTest { | ||
29 | + Logger logger = LoggerFactory.getLogger("test"); | ||
30 | + @Autowired | ||
31 | + JavaApiStaticsMapper javaApiStaticsMapper; | ||
32 | + | ||
33 | + | ||
34 | + @Test | ||
35 | + public void testQuery() { | ||
36 | + | ||
37 | + Map<String, Integer> param = new HashMap<>(); | ||
38 | + param.put("api_id", 21); | ||
39 | + param.put("mobj_id", 43); | ||
40 | + JavaApiStaticsModel rel = javaApiStaticsMapper.selectlatestJavaApiStatics(null, param); | ||
41 | + | ||
42 | + logger.info(rel.toString()); | ||
43 | + | ||
44 | + } | ||
45 | + | ||
46 | + | ||
47 | + @Test | ||
48 | + public void testBatchQuery() { | ||
49 | + | ||
50 | + List<Map<String, Integer> > listParam=new ArrayList<>(); | ||
51 | + Map<String, Integer> param1 = new HashMap<>(); | ||
52 | + param1.put("api_id", 21); | ||
53 | + param1.put("mobj_id", 43); | ||
54 | + listParam.add(param1); | ||
55 | + Map<String, Integer> param2 = new HashMap<>(); | ||
56 | + param2.put("api_id", 19); | ||
57 | + param2.put("mobj_id", 134); | ||
58 | + | ||
59 | + | ||
60 | + listParam.add(param2); | ||
61 | + | ||
62 | + List<JavaApiStaticsModel> rel = javaApiStaticsMapper.selectlatestJavaApiStaticsList(null, listParam); | ||
63 | + | ||
64 | + logger.info(rel.toString()); | ||
65 | + | ||
66 | + } | ||
67 | + | ||
68 | + | ||
69 | +} |
-
Please register or login to post a comment