From 2198e405151332468c40f01f915e4dfa5da7688e Mon Sep 17 00:00:00 2001 From: "jackdaw@loafle.com" Date: Tue, 25 Apr 2017 19:48:49 +0900 Subject: [PATCH] =?UTF-8?q?ResultSet=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../overflow/crawler/redis/RedisCralwer.java | 44 ++++++++++++------- .../crawler/redis/RedisCralwerTest.java | 21 +++++++-- src/test/resources/config/example.json | 31 +++++++------ 3 files changed, 62 insertions(+), 34 deletions(-) diff --git a/src/main/java/com/loafle/overflow/crawler/redis/RedisCralwer.java b/src/main/java/com/loafle/overflow/crawler/redis/RedisCralwer.java index e9ca118..21c6ff3 100644 --- a/src/main/java/com/loafle/overflow/crawler/redis/RedisCralwer.java +++ b/src/main/java/com/loafle/overflow/crawler/redis/RedisCralwer.java @@ -4,12 +4,10 @@ import com.loafle.overflow.crawler.Crawler; import com.loafle.overflow.crawler.config.Config; import com.loafle.overflow.crawler.config.Item; import com.loafle.overflow.crawler.config.Query; +import com.loafle.overflow.crawler.result.OFResultSet; import redis.clients.jedis.Jedis; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Queue; +import java.util.*; import java.util.logging.Level; import java.util.logging.Logger; @@ -30,7 +28,7 @@ public class RedisCralwer extends Crawler { return collectMetric(c); } - protected Map collectMetric(Config c) { + protected Object collectMetric(Config c) { String targetIP = c.getTarget().getConnection().getIp(); int targetPort = Integer.valueOf(c.getTarget().getConnection().getPort()); @@ -39,7 +37,6 @@ public class RedisCralwer extends Crawler { Jedis jedis = null; List items = c.getItems(); - Map returnMap = new HashMap<>(); try { jedis = new Jedis(targetIP, targetPort,false); @@ -50,20 +47,23 @@ public class RedisCralwer extends Crawler { } } + //Map rr = (Map) parseToMap(jedis.info()); + + List resultSetList = new ArrayList<>(); + for (Item item : items) { + OFResultSet resultSet = OFResultSet.newInstance(item); - List metrics = item.getMetrics(); + Query query = item.getQuery(); + String queryString = (String) query.getQueryInfo().get("query"); + String info = jedis.info(queryString); + Map resultMap = (Map) parseToMap(info).get(queryString); - for (Query query : item.getQueries()) { - - String info = jedis.info(query.getQuery()); - Map resultMap = (Map) parseToMap(info).get(query.getQuery()); - - for (int index =0 ; index < query.getKeys().size() ; ++index) { - returnMap.put(metrics.get(index),resultMap.get(query.getKeys().get(index))); - } - } + resultSet.addRow(parse(resultMap,resultSet.getMeta())); + resultSetList.add(resultSet); } + + return resultSetList; } catch (Exception e) { throw e; } finally { @@ -72,7 +72,17 @@ public class RedisCralwer extends Crawler { } } - return returnMap; + } + + private List parse(Map dataMap, Map meta) { + + List row = new ArrayList<>(Arrays.asList(new String[meta.size()])); + for (Map.Entry info : meta.entrySet()) { + String data = dataMap.get(info.getKey()); + data = data.trim(); + row.set(info.getValue().intValue(),data); + } + return row; } public Map parseToMap(String source) { diff --git a/src/test/java/com/loafle/overflow/crawler/redis/RedisCralwerTest.java b/src/test/java/com/loafle/overflow/crawler/redis/RedisCralwerTest.java index ed9344b..b13c62b 100644 --- a/src/test/java/com/loafle/overflow/crawler/redis/RedisCralwerTest.java +++ b/src/test/java/com/loafle/overflow/crawler/redis/RedisCralwerTest.java @@ -2,11 +2,14 @@ package com.loafle.overflow.crawler.redis; import com.fasterxml.jackson.databind.ObjectMapper; import com.loafle.overflow.crawler.config.Config; +import com.loafle.overflow.crawler.result.OFResultSet; import org.junit.Ignore; import org.junit.Test; import redis.clients.jedis.Jedis; import java.io.File; +import java.util.HashMap; +import java.util.List; import java.util.Map; import static org.junit.Assert.*; @@ -25,10 +28,22 @@ public class RedisCralwerTest { Config c = mapper.readValue(new File(path),Config.class); RedisCralwer rc = new RedisCralwer(); - Map data = (Map) rc.getInternal(c); - - assertEquals(data.size(),7); + List result = (List) rc.getInternal(c); + print("",result); + } + private void print(String s, List mm) { + + System.out.println(s); + + Map m = new HashMap<>(); + for(OFResultSet r : mm) { + m.putAll(r.getData()); + } + + for (Map.Entry item : m.entrySet()) { + System.out.println("key=" + item.getKey() + " ||||||||||| value=" + item.getValue()); + } } } \ No newline at end of file diff --git a/src/test/resources/config/example.json b/src/test/resources/config/example.json index ef8207c..03a3aa5 100644 --- a/src/test/resources/config/example.json +++ b/src/test/resources/config/example.json @@ -26,9 +26,10 @@ "cpu.usage.system_children", "cpu.usage.user_children" ], - "queries":[ - { - "query" : "CPU" , + "query": { + "queryInfo" : { + "query" : "CPU" + }, "keys" : [ "used_cpu_sys", "used_cpu_user", @@ -36,7 +37,6 @@ "used_cpu_user_children" ] } - ] }, { "metrics" : [ @@ -44,16 +44,19 @@ "memory.usage.rss", "memory.usage.reak" ], - "queries":[ - { - "query" : "Memory" , - "keys" : [ - "used_memory", - "used_memory_rss", - "used_memory_peak" - ] - } - ] + "query": + { + "queryInfo" : { + "query" : "Memory" + }, + + "keys" : [ + "used_memory", + "used_memory_rss", + "used_memory_peak" + ] + } } + ] } \ No newline at end of file