Commit 0decc262 authored by zhouzihao's avatar zhouzihao

dev-添加数据收集器

parent f75cf751
package com.vandyo.sentry.core; package com.vandyo.sentry.core;
import cn.hutool.json.JSONUtil;
import com.vandyo.sentry.core.cases.*; import com.vandyo.sentry.core.cases.*;
import com.vandyo.sentry.core.collectionMachine.CollectionMachine;
import com.vandyo.sentry.core.dto.ErrStatus;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.Map;
@Component @Component
public class Tmp { public class Tmp {
private static final Logger log = LoggerFactory.getLogger(Tmp.class); private static final Logger log = LoggerFactory.getLogger(Tmp.class);
...@@ -21,16 +26,27 @@ public class Tmp { ...@@ -21,16 +26,27 @@ public class Tmp {
@Value("${vandyo.pwd}") @Value("${vandyo.pwd}")
public String pwd; public String pwd;
@Scheduled(fixedRate = 5000) @Scheduled(fixedRate = 1000)
public void test(){ public void test() {
log.info("we run it {} times",++times); log.info("we run it {} times", ++times);
Case case1 = null; Case case1 = null;
case1 = new LoginCase(mobile,pwd); case1 = new LoginCase(mobile, pwd);
case1 = new CarListDecorator(case1); case1 = new CarListDecorator(case1);
case1 = new CarInfoDecorator(case1); case1 = new CarInfoDecorator(case1);
case1 = new CarDetectDecorator(case1); case1 = new CarDetectDecorator(case1);
case1 = new CarStatusDecorator(case1); case1 = new CarStatusDecorator(case1);
case1.check(); case1.check();
Case case2 = null;
case2 = new TestCase();
case2.check();
log.info("over!");
}
@Scheduled(fixedRate = 5000)
public void check() {
Map<String, Map<ErrStatus, Long>> data = CollectionMachine.checkRule(5);
String info = JSONUtil.parseFromMap(data).toStringPretty();
log.warn(info);
} }
} }
...@@ -4,6 +4,7 @@ import cn.hutool.http.HttpRequest; ...@@ -4,6 +4,7 @@ import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.vandyo.sentry.core.collectionMachine.CollectionMachine;
import com.vandyo.sentry.core.dto.ErrStatus; import com.vandyo.sentry.core.dto.ErrStatus;
import com.vandyo.sentry.core.dto.Res; import com.vandyo.sentry.core.dto.Res;
import com.vandyo.sentry.core.tools.Signature; import com.vandyo.sentry.core.tools.Signature;
...@@ -56,6 +57,7 @@ public class CarInfoDecorator extends CaseDecorator { ...@@ -56,6 +57,7 @@ public class CarInfoDecorator extends CaseDecorator {
newRes.setSuccess(false); newRes.setSuccess(false);
} }
} }
CollectionMachine.post(newRes);
return newRes; return newRes;
}else { }else {
return oldRes; return oldRes;
......
...@@ -5,12 +5,14 @@ import cn.hutool.http.HttpResponse; ...@@ -5,12 +5,14 @@ import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray; import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.vandyo.sentry.core.collectionMachine.CollectionMachine;
import com.vandyo.sentry.core.dto.ErrStatus; import com.vandyo.sentry.core.dto.ErrStatus;
import com.vandyo.sentry.core.dto.Res; import com.vandyo.sentry.core.dto.Res;
import com.vandyo.sentry.core.tools.Signature; import com.vandyo.sentry.core.tools.Signature;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects;
public class CarListDecorator extends CaseDecorator { public class CarListDecorator extends CaseDecorator {
...@@ -36,7 +38,7 @@ public class CarListDecorator extends CaseDecorator { ...@@ -36,7 +38,7 @@ public class CarListDecorator extends CaseDecorator {
if (response.isOk()){ if (response.isOk()){
JSONObject json = JSONUtil.parseObj(response.body()); JSONObject json = JSONUtil.parseObj(response.body());
JSONArray list = json.getJSONArray("result"); JSONArray list = json.getJSONArray("result");
if(list.size() < 1){ if(Objects.isNull(list) || list.size() < 1){
newRes.setSuccess(false); newRes.setSuccess(false);
newRes.setStatus(ErrStatus.ErrUnexpected); newRes.setStatus(ErrStatus.ErrUnexpected);
}else { }else {
...@@ -50,6 +52,7 @@ public class CarListDecorator extends CaseDecorator { ...@@ -50,6 +52,7 @@ public class CarListDecorator extends CaseDecorator {
newRes.setStatus(Signature.matchStatus(response.getStatus())); newRes.setStatus(Signature.matchStatus(response.getStatus()));
newRes.setSuccess(false); newRes.setSuccess(false);
} }
CollectionMachine.post(newRes);
return newRes; return newRes;
} else { } else {
return oldRes; return oldRes;
......
...@@ -4,6 +4,7 @@ import cn.hutool.http.HttpRequest; ...@@ -4,6 +4,7 @@ import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.vandyo.sentry.core.collectionMachine.CollectionMachine;
import com.vandyo.sentry.core.dto.ErrStatus; import com.vandyo.sentry.core.dto.ErrStatus;
import com.vandyo.sentry.core.dto.Res; import com.vandyo.sentry.core.dto.Res;
import com.vandyo.sentry.core.tools.Signature; import com.vandyo.sentry.core.tools.Signature;
...@@ -57,6 +58,7 @@ public class CarStatusDecorator extends CaseDecorator{ ...@@ -57,6 +58,7 @@ public class CarStatusDecorator extends CaseDecorator{
newRes.setSuccess(false); newRes.setSuccess(false);
} }
} }
CollectionMachine.post(newRes);
return newRes; return newRes;
}else { }else {
return oldRes; return oldRes;
......
package com.vandyo.sentry.core.cases; package com.vandyo.sentry.core.cases;
import com.vandyo.sentry.core.collectionMachine.CollectionMachine;
import com.vandyo.sentry.core.dto.Res; import com.vandyo.sentry.core.dto.Res;
import java.util.Map; import java.util.Map;
...@@ -23,6 +24,7 @@ public class EmptyCase extends Case { ...@@ -23,6 +24,7 @@ public class EmptyCase extends Case {
res.setSId(sId); res.setSId(sId);
res.setUId(uid); res.setUId(uid);
res.setSuccess(true); res.setSuccess(true);
CollectionMachine.post(res);
return res; return res;
} }
} }
...@@ -4,6 +4,7 @@ import cn.hutool.http.HttpRequest; ...@@ -4,6 +4,7 @@ import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.vandyo.sentry.core.collectionMachine.CollectionMachine;
import com.vandyo.sentry.core.dto.Res; import com.vandyo.sentry.core.dto.Res;
import com.vandyo.sentry.core.tools.Signature; import com.vandyo.sentry.core.tools.Signature;
...@@ -44,6 +45,7 @@ public class LoginCase extends Case { ...@@ -44,6 +45,7 @@ public class LoginCase extends Case {
res.setSuccess(false); res.setSuccess(false);
res.setStatus(Signature.matchStatus(response.getStatus())); res.setStatus(Signature.matchStatus(response.getStatus()));
} }
CollectionMachine.post(res);
return res; return res;
} }
} }
package com.vandyo.sentry.core.cases;
import com.vandyo.sentry.core.collectionMachine.CollectionMachine;
import com.vandyo.sentry.core.dto.ErrStatus;
import com.vandyo.sentry.core.dto.Res;
import java.util.Map;
public class TestCase extends Case {
@Override
public Res<Map<String, String>> check() {
Res<Map<String, String>> res = new Res<>();
res.setSuccess(false);
res.setStatus(ErrStatus.ErrUnexpected);
res.setKey("test");
CollectionMachine.post(res);
return res;
}
}
package com.vandyo.sentry.core.collectionMachine;
import com.vandyo.sentry.core.Tmp;
import com.vandyo.sentry.core.dto.ErrStatus;
import com.vandyo.sentry.core.dto.Res;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
@Component
public class CollectionMachine {
private static final Logger logger = LoggerFactory.getLogger(Tmp.class);
// 收集进来的数据 进到存储 相当于一个临时的日志
private static Map<Long, Res> temp = new ConcurrentHashMap<>(124);
public static void post(Res<Map<String, String>> log) {
// 应该对于不同的请求统一管理 还是 每个单独管理呢?
logger.info("temp long is {}",temp.size());
if (log.getIgnore() || log.getSuccess()) {
//pass
} else {
if (temp.size() > 100) {
logger.info("try to put data");
checkSize(1);
}
try {
temp.put(System.currentTimeMillis(), log);
} catch (Exception e) {
logger.error(e.getLocalizedMessage());
}
}
if (temp.size() == 100) {
System.out.println("Warning!");
}
}
/**
* 对数据进行归纳
*
* @param min 分钟
* @return
*/
public static Map<String, Map<ErrStatus, Long>> checkRule(int min) {
@Data
class ErrBox {
private ErrStatus errStatus;
private Integer count;
}
Map<String, List<ErrBox>> map = temp.keySet().stream()
.filter(x -> x > System.currentTimeMillis() - 1000 * 60 * min)
.map(x -> temp.get(x))
.collect(Collectors.toMap(Res::getKey, x -> {
List<ErrBox> res = new ArrayList<>();
ErrBox box = new ErrBox();
box.setCount(1);
box.setErrStatus(x.getStatus());
res.add(box);
return res;
}, (errStatus, errStatus2) -> {
errStatus.addAll(errStatus2);
return errStatus;
}));
Map<String, Map<ErrStatus, Long>> data = new HashMap<>();
for (Map.Entry<String, List<ErrBox>> entry : map.entrySet()) {
data.put(entry.getKey(), entry.getValue().stream().collect(Collectors.groupingBy(ErrBox::getErrStatus, Collectors.counting())));
}
logger.info("Get All Err Over!");
return data;
}
public static void checkSize(int min) {
// 对于超过了时间限制的数据进行删除
Iterator<Long> iterator = temp.keySet().iterator();
while (iterator.hasNext()) {
Long key = iterator.next();
if (key < System.currentTimeMillis() - 1000 * 60 * min) {
logger.error("remove key {}",key);
iterator.remove();
temp.remove(key);
} else {
return;
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment