From e266f5dc8a061dbaaf312a788df8f685ed51444c Mon Sep 17 00:00:00 2001 From: insanity Date: Thu, 9 Nov 2017 17:41:50 +0900 Subject: [PATCH] model's arranging --- .../service/SensorConfigGenerator.java | 1 - .../module/infra/service/InfraService.java | 18 +++++--- .../service/NoAuthProbeService.java | 4 +- .../overflow/module/probe/model/Probe.java | 45 ++++++++++++------- .../module/probe/service/ProbeService.java | 13 +++++- .../overflow/module/sensor/dao/SensorDAO.java | 1 + .../overflow/module/sensor/model/Sensor.java | 9 ++++ .../overflow/module/target/model/Target.java | 13 ++++++ src/main/resources/local/init.sql | 28 ++++++------ .../probe/service/ProbeServiceTest.java | 3 -- 10 files changed, 91 insertions(+), 44 deletions(-) diff --git a/src/main/java/com/loafle/overflow/module/generator/service/SensorConfigGenerator.java b/src/main/java/com/loafle/overflow/module/generator/service/SensorConfigGenerator.java index 03af878..17c4dd7 100644 --- a/src/main/java/com/loafle/overflow/module/generator/service/SensorConfigGenerator.java +++ b/src/main/java/com/loafle/overflow/module/generator/service/SensorConfigGenerator.java @@ -62,5 +62,4 @@ public class SensorConfigGenerator { - } diff --git a/src/main/java/com/loafle/overflow/module/infra/service/InfraService.java b/src/main/java/com/loafle/overflow/module/infra/service/InfraService.java index 693d9db..d52ba7e 100644 --- a/src/main/java/com/loafle/overflow/module/infra/service/InfraService.java +++ b/src/main/java/com/loafle/overflow/module/infra/service/InfraService.java @@ -8,6 +8,7 @@ import com.loafle.overflow.module.infra.model.Infra; import com.loafle.overflow.module.probe.exception.ProbeNotFoundException; import com.loafle.overflow.module.probe.model.Probe; import com.loafle.overflow.module.probe.service.ProbeService; +import com.loafle.overflow.module.sensor.dao.SensorDAO; import com.loafle.overflow.module.target.model.Target; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; @@ -25,6 +26,9 @@ public class InfraService { @Autowired InfraDAO infraDAO; + @Autowired + SensorDAO sensorDAO; + @Autowired private ProbeService probeService; @@ -33,7 +37,9 @@ public class InfraService { } public Infra read(long id) { - return this.infraDAO.findOne(id); + Infra infra = this.infraDAO.findOne(id); + infra.getTarget().setSensors(this.sensorDAO.findAllByTarget(infra.getTarget())); + return infra; } public Page readAllByProbe(Probe probe, PageParams pageParams) { @@ -47,12 +53,13 @@ public class InfraService { throw new ProbeNotFoundException(); } - return this.infraDAO.findAllByProbeList(probeList, PageUtil.getPageRequest(pageParams)); + Page infraList = this.infraDAO.findAllByProbeList(probeList, PageUtil.getPageRequest(pageParams)); + for (Infra infra: infraList) { + infra.getTarget().setSensors(this.sensorDAO.findAllByTarget(infra.getTarget())); + } + return infraList; } -// public List readAllByProbeList(List probeList) { -// return this.infraDAO.findAllByProbeList(probeList); -// } public List readAllTargetByDomain(Domain domain) { @@ -74,5 +81,4 @@ public class InfraService { return this.infraDAO.findByTarget(target); } - } diff --git a/src/main/java/com/loafle/overflow/module/noauthprobe/service/NoAuthProbeService.java b/src/main/java/com/loafle/overflow/module/noauthprobe/service/NoAuthProbeService.java index 54c6b74..c9f9c6b 100644 --- a/src/main/java/com/loafle/overflow/module/noauthprobe/service/NoAuthProbeService.java +++ b/src/main/java/com/loafle/overflow/module/noauthprobe/service/NoAuthProbeService.java @@ -1,5 +1,6 @@ package com.loafle.overflow.module.noauthprobe.service; +import com.loafle.overflow.commons.service.MessagePublisher; import com.loafle.overflow.module.apikey.model.ApiKey; import com.loafle.overflow.module.apikey.service.ApiKeyService; import com.loafle.overflow.module.domain.model.Domain; @@ -10,7 +11,6 @@ import com.loafle.overflow.module.noauthprobe.dao.NoAuthProbeDAO; import com.loafle.overflow.module.noauthprobe.model.NoAuthProbe; import com.loafle.overflow.module.probe.model.Probe; import com.loafle.overflow.module.probe.service.ProbeService; -import com.loafle.overflow.commons.service.MessagePublisher; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.type.TypeReference; import org.springframework.beans.factory.annotation.Autowired; @@ -81,8 +81,6 @@ public class NoAuthProbeService { probe.setDomain(new Domain(1)); probe.setAuthorizeMember(new Member(1)); probe.setStatus(new MetaProbeStatus((short)1)); - probe.setTargetCount(0); - probe.setSensorCount(0); Map objMap = this.objectMapper.readValue(noAuthProbe.getDescription(), new TypeReference>() {}); Map hostMap = (Map)objMap.get("host"); diff --git a/src/main/java/com/loafle/overflow/module/probe/model/Probe.java b/src/main/java/com/loafle/overflow/module/probe/model/Probe.java index 59c84d8..ddd774d 100644 --- a/src/main/java/com/loafle/overflow/module/probe/model/Probe.java +++ b/src/main/java/com/loafle/overflow/module/probe/model/Probe.java @@ -1,11 +1,13 @@ package com.loafle.overflow.module.probe.model; import com.loafle.overflow.module.domain.model.Domain; +import com.loafle.overflow.module.infra.model.Infra; import com.loafle.overflow.module.member.model.Member; import com.loafle.overflow.module.meta.model.MetaProbeStatus; import javax.persistence.*; import java.util.Date; +import java.util.List; /** * Created by root on 17. 6. 22. @@ -21,13 +23,16 @@ public class Probe { private Domain domain; private String probeKey; private String encryptionKey; - private int targetCount; - private int sensorCount; private String displayName; private String cidr; private Date authorizeDate; private Member authorizeMember; + + private List targets; + // private InfraHost host; +// private int targetCount; +// private int sensorCount; public Probe() { @@ -107,23 +112,33 @@ public class Probe { this.encryptionKey = encryptionKey; } - @Column(name = "TARGET_COUNT", nullable = false) - public int getTargetCount() { - return targetCount; + + @Transient + public List getTargets() { + return targets; } - public void setTargetCount(int targetCount) { - this.targetCount = targetCount; + public void setTargets(List targets) { + this.targets = targets; } - @Column(name = "SENSOR_COUNT", nullable = false) - public int getSensorCount() { - return sensorCount; - } - - public void setSensorCount(int sensorCount) { - this.sensorCount = sensorCount; - } + // @Column(name = "TARGET_COUNT", nullable = false) +// public int getTargetCount() { +// return targetCount; +// } +// +// public void setTargetCount(int targetCount) { +// this.targetCount = targetCount; +// } +// +// @Column(name = "SENSOR_COUNT", nullable = false) +// public int getSensorCount() { +// return sensorCount; +// } +// +// public void setSensorCount(int sensorCount) { +// this.sensorCount = sensorCount; +// } @Column(name = "DISPLAY_NAME") public String getDisplayName() { diff --git a/src/main/java/com/loafle/overflow/module/probe/service/ProbeService.java b/src/main/java/com/loafle/overflow/module/probe/service/ProbeService.java index 7954d18..052b91f 100644 --- a/src/main/java/com/loafle/overflow/module/probe/service/ProbeService.java +++ b/src/main/java/com/loafle/overflow/module/probe/service/ProbeService.java @@ -1,6 +1,7 @@ package com.loafle.overflow.module.probe.service; import com.loafle.overflow.module.domain.model.Domain; +import com.loafle.overflow.module.infra.dao.InfraDAO; import com.loafle.overflow.module.probe.dao.ProbeDAO; import com.loafle.overflow.module.probe.model.Probe; import org.springframework.beans.factory.annotation.Autowired; @@ -16,6 +17,8 @@ public class ProbeService { @Autowired private ProbeDAO probeDAO; + @Autowired + private InfraDAO infraDAO; public Probe regist(Probe probe) { return this.probeDAO.save(probe); @@ -26,11 +29,17 @@ public class ProbeService { } public List readAllByDomain(Domain domain) { - return this.probeDAO.findAllByDomainOrderByIdDesc(domain); + List probes = this.probeDAO.findAllByDomainOrderByIdDesc(domain); + for(Probe probe : probes) { + probe.setTargets(this.infraDAO.findAllByProbe(probe)); + } + return probes; } public Probe read(long id) { - return this.probeDAO.findOne(id); + Probe probe = this.probeDAO.findOne(id); + probe.setTargets(this.infraDAO.findAllByProbe(probe)); + return probe; } public Probe readByProbeKey(String probeKey) { diff --git a/src/main/java/com/loafle/overflow/module/sensor/dao/SensorDAO.java b/src/main/java/com/loafle/overflow/module/sensor/dao/SensorDAO.java index fd48353..83aef28 100644 --- a/src/main/java/com/loafle/overflow/module/sensor/dao/SensorDAO.java +++ b/src/main/java/com/loafle/overflow/module/sensor/dao/SensorDAO.java @@ -20,6 +20,7 @@ public interface SensorDAO extends JpaRepository { Page findAllByTarget(Target target, Pageable pageable); + List findAllByTarget(Target target); @Query("SELECT s from Sensor s WHERE s.target in (:targetList)") Page findAllByTargetList(@Param("targetList") List targetList, Pageable pageable); diff --git a/src/main/java/com/loafle/overflow/module/sensor/model/Sensor.java b/src/main/java/com/loafle/overflow/module/sensor/model/Sensor.java index 176a9f8..73c8fe7 100644 --- a/src/main/java/com/loafle/overflow/module/sensor/model/Sensor.java +++ b/src/main/java/com/loafle/overflow/module/sensor/model/Sensor.java @@ -22,6 +22,7 @@ public class Sensor { private Target target; private MetaCrawler crawler; private String crawlerInputItems; + private short itemCount = 0; @Id @GeneratedValue(strategy= GenerationType.IDENTITY) @@ -92,4 +93,12 @@ public class Sensor { this.crawlerInputItems = crawlerInputItems; } + @Column(name = "ITEM_COUNT", nullable = false) + public short getItemCount() { + return itemCount; + } + + public void setItemCount(short itemCount) { + this.itemCount = itemCount; + } } diff --git a/src/main/java/com/loafle/overflow/module/target/model/Target.java b/src/main/java/com/loafle/overflow/module/target/model/Target.java index 1dde87c..2a0ae5c 100644 --- a/src/main/java/com/loafle/overflow/module/target/model/Target.java +++ b/src/main/java/com/loafle/overflow/module/target/model/Target.java @@ -1,7 +1,10 @@ package com.loafle.overflow.module.target.model; +import com.loafle.overflow.module.sensor.model.Sensor; + import javax.persistence.*; import java.util.Date; +import java.util.List; /** * Created by root on 17. 6. 22. @@ -15,6 +18,8 @@ public class Target { private String displayName; private String description; + private List sensors; + /* private long id; private Date createDate; @@ -82,4 +87,12 @@ public class Target { // this.infra = infra; // } + @Transient + public List getSensors() { + return sensors; + } + + public void setSensors(List sensors) { + this.sensors = sensors; + } } diff --git a/src/main/resources/local/init.sql b/src/main/resources/local/init.sql index 2112ba1..3203b96 100644 --- a/src/main/resources/local/init.sql +++ b/src/main/resources/local/init.sql @@ -1309,11 +1309,11 @@ INSERT INTO public.domain_member (create_date,domain_id,member_id) VALUES ( INSERT INTO public.api_key (api_key,create_date,domain_id) VALUES ( '52abd6fd57e511e7ac52080027658d13','2017-06-26 13:02:28.347',1); -INSERT INTO public.probe (authorize_date,cidr,create_date,description,display_name,encryption_key,probe_key,sensor_count,target_count,authorize_member_id,domain_id,status) VALUES ( -'2017-08-21 14:48:31.563','192.168.1.0/24','2017-08-21 14:48:31.563','snoop probe','test probe 111111','8c51fa9c5bcc11e7980a080027658d13','899fdd145bcc11e7b611080027658d13',0,0,1,1,1); +INSERT INTO public.probe (authorize_date,cidr,create_date,description,display_name,encryption_key,probe_key,authorize_member_id,domain_id,status) VALUES ( +'2017-08-21 14:48:31.563','192.168.1.0/24','2017-08-21 14:48:31.563','snoop probe','test probe 111111','8c51fa9c5bcc11e7980a080027658d13','899fdd145bcc11e7b611080027658d13',1,1,1); -INSERT INTO public.probe (authorize_date,cidr,create_date,description,display_name,encryption_key,probe_key,sensor_count,target_count,authorize_member_id,domain_id,status) VALUES ( -'2017-08-21 15:00:09.691','192.168.2.0/24','2017-08-21 15:00:09.691','insanity probe','test probe 22222','f1f72c8d2a814580bb7c4615ec43a6a8','95d8bcdc739741dca74c4a0e489e0774',0,0,1,1,1); +INSERT INTO public.probe (authorize_date,cidr,create_date,description,display_name,encryption_key,probe_key,authorize_member_id,domain_id,status) VALUES ( +'2017-08-21 15:00:09.691','192.168.2.0/24','2017-08-21 15:00:09.691','insanity probe','test probe 22222','f1f72c8d2a814580bb7c4615ec43a6a8','95d8bcdc739741dca74c4a0e489e0774',1,1,1); INSERT INTO public.target (create_date,description,display_name) VALUES ( '2017-07-27 20:18:09.555','Machine Target','ghost target'); @@ -1369,16 +1369,16 @@ INSERT INTO public.infra_os_daemon ("name",id,os_id) VALUES ( insert into public.PROBE_INFRAHOST (HOST_ID, PROBE_ID) values (3, 1); -INSERT INTO public.sensor (crawler_input_items,create_date,description,crawler_id,status,target_id) VALUES ( -NULL,'2017-06-26 20:19:07.009','My sensor 11111',1,1,1); -INSERT INTO public.sensor (crawler_input_items,create_date,description,crawler_id,status,target_id) VALUES ( -NULL,'2017-06-26 20:19:07.074','My sensor 22222',1,2,1); -INSERT INTO public.sensor (crawler_input_items,create_date,description,crawler_id,status,target_id) VALUES ( -NULL,'2017-08-28 16:17:05.506','OS_SERVICE-MYSQL_CRAWLER',11,1,4); -INSERT INTO public.sensor (crawler_input_items,create_date,description,crawler_id,status,target_id) VALUES ( -NULL,'2017-09-05 12:26:02.738','HOST-WMI_CRAWLER',23,1,3); -INSERT INTO public.sensor (crawler_input_items,create_date,description,crawler_id,status,target_id) VALUES ( -NULL,'2017-09-11 12:26:02.738','SERVICE-JMX_CRAWLER',17,1,4); +INSERT INTO public.sensor (crawler_input_items,create_date,description,crawler_id,status,target_id,item_count) VALUES ( +NULL,'2017-06-26 20:19:07.009','My sensor 11111',1,1,1,3); +INSERT INTO public.sensor (crawler_input_items,create_date,description,crawler_id,status,target_id,item_count) VALUES ( +NULL,'2017-06-26 20:19:07.074','My sensor 22222',1,2,1,1); +INSERT INTO public.sensor (crawler_input_items,create_date,description,crawler_id,status,target_id,item_count) VALUES ( +NULL,'2017-08-28 16:17:05.506','OS_SERVICE-MYSQL_CRAWLER',11,1,4,2); +INSERT INTO public.sensor (crawler_input_items,create_date,description,crawler_id,status,target_id,item_count) VALUES ( +NULL,'2017-09-05 12:26:02.738','HOST-WMI_CRAWLER',23,1,3,1); +INSERT INTO public.sensor (crawler_input_items,create_date,description,crawler_id,status,target_id,item_count) VALUES ( +NULL,'2017-09-11 12:26:02.738','SERVICE-JMX_CRAWLER',17,1,4,4); --INSERT INTO public.sensor_item (create_date,item_id,sensor_id) VALUES ( --'2017-06-26 20:21:16.626',1,1); diff --git a/src/test/java/com/loafle/overflow/module/probe/service/ProbeServiceTest.java b/src/test/java/com/loafle/overflow/module/probe/service/ProbeServiceTest.java index a6b9730..3f02a8f 100644 --- a/src/test/java/com/loafle/overflow/module/probe/service/ProbeServiceTest.java +++ b/src/test/java/com/loafle/overflow/module/probe/service/ProbeServiceTest.java @@ -1,7 +1,6 @@ package com.loafle.overflow.module.probe.service; import com.loafle.overflow.module.domain.model.Domain; -import com.loafle.overflow.module.infra.model.InfraHost; import com.loafle.overflow.module.member.model.Member; import com.loafle.overflow.module.meta.model.MetaProbeStatus; import com.loafle.overflow.module.probe.model.Probe; @@ -49,8 +48,6 @@ public class ProbeServiceTest { probe.setStatus(status); - probe.setTargetCount(0); - probe.setSensorCount(0); probe.setDisplayName("test probe 22222"); probe.setCidr("192.168.1.0/24"); // probe.setAuthorizedDate(new Date());