优化 JSON 解析和告警规则插入代码
Map<String, Object> map = parseJSON2Map(json);
if (!map.containsKey('data')) {
return;
}
MySqlRuleDelete();
JSONObject aa = (JSONObject) map.get('data');
Map<String, Object> map1 = parseJSON2Map(String.valueOf(aa));
devicename = (String) map1.get('name');
JSONObject bb = (JSONObject) map1.get('deviceProfileData');
Map<String, Object> map2 = parseJSON2Map(String.valueOf(bb));
String bblength = String.valueOf(bb);
if (bblength.length() <= 168) {
jedis.set('Error', 'PassRule');
return;
}
List<Map<String, Object>> alarmList = (List<Map<String, Object>>) map2.get('alarms');
for (Map<String, Object> alarm : alarmList) {
Map<String, Object> createRules = (Map<String, Object>) alarm.get('createRules');
if (createRules == null) {
continue;
}
entity_id = (String) alarm.get('id');
for (String color : Arrays.asList('MAJOR', 'MINOR', 'CRITICAL')) {
JSONObject colorJson = (JSONObject) createRules.get(color);
if (colorJson == null) {
continue;
}
JSONObject conditionJson = (JSONObject) colorJson.get('condition');
Map<String, Object> conditionMap = parseJSON2Map(String.valueOf(conditionJson));
List<Map<String, Object>> conditionList = (List<Map<String, Object>>) conditionMap.get('condition');
int num = countOperator(conditionList);
if (num == 2 || num == 3) {
ForGreaterOrLessOrEqual(conditionList, num, color);
} else {
GreaterOrLessOrEqual(conditionList, 1);
String StrdefaultValue = String.valueOf(defaultValue);
jedis.mset('devicename', devicename, 'entity_id', entity_id, 'model', color, 'operation', operation, 'defaultValue', StrdefaultValue, 'i', String.valueOf(1));
MySqlRuleInset(jedis.get('devicename'), jedis.get('entity_id'), jedis.get('model'), jedis.get('operation'), defaultValue, 1); //插入数据库
}
}
}
private int countOperator(List<Map<String, Object>> conditionList) {
int count = 0;
for (Map<String, Object> condition : conditionList) {
if (condition.containsKey('predicate')) {
count++;
}
}
return count;
}
原文地址: http://www.cveoy.top/t/topic/nbsK 著作权归作者所有。请勿转载和采集!