This commit is contained in:
insanity 2018-06-28 17:26:55 +09:00
parent b704e5fa54
commit 6e4405750e
8 changed files with 141 additions and 226 deletions

View File

@ -50,7 +50,7 @@
<dependency> <dependency>
<groupId>com.loafle.overflow</groupId> <groupId>com.loafle.overflow</groupId>
<artifactId>commons-java</artifactId> <artifactId>commons-java</artifactId>
<version>1.0.103-SNAPSHOT</version> <version>1.0.106-SNAPSHOT</version>
</dependency> </dependency>
<dependency> <dependency>

View File

@ -1,7 +1,5 @@
package com.loafle.overflow.central.module.sensor.dao; package com.loafle.overflow.central.module.sensor.dao;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@ -15,11 +13,9 @@ import com.loafle.overflow.model.target.Target;
*/ */
@Repository @Repository
public interface SensorDAO extends JpaRepository<Sensor, Long> { public interface SensorDAO extends JpaRepository<Sensor, Long> {
Page<Sensor> findAllByTargetId(Long targetID, Pageable pageable);
List<Sensor> findAllByTargetId(Long targetID); List<Sensor> findAllByTargetId(Long targetID);
Page<Sensor> findAllByTargetIn(List<Target> targets, Pageable pageable); List<Sensor> findAllByTargetIn(List<Target> targets);
Page<Sensor> findAllByTargetInfraId(Long infraID, Pageable pageable); List<Sensor> findAllByTargetInfraProbeId(Long probeId);
} }

View File

@ -1,9 +1,9 @@
package com.loafle.overflow.central.module.sensor.dao; package com.loafle.overflow.central.module.sensor.dao;
import java.util.List;
import com.loafle.overflow.model.sensor.SensorItem; import com.loafle.overflow.model.sensor.SensorItem;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@ -12,5 +12,5 @@ import org.springframework.stereotype.Repository;
*/ */
@Repository @Repository
public interface SensorItemDAO extends JpaRepository<SensorItem, Long> { public interface SensorItemDAO extends JpaRepository<SensorItem, Long> {
Page<SensorItem> findAllBySensorId(Long sensorId, Pageable pageable); List<SensorItem> findAllBySensorId(Long sensorId);
} }

View File

@ -1,16 +1,14 @@
package com.loafle.overflow.central.module.sensor.service; package com.loafle.overflow.central.module.sensor.service;
import com.loafle.overflow.central.commons.utils.PageUtil;
import com.loafle.overflow.central.module.sensor.dao.SensorDAO;
import com.loafle.overflow.central.module.sensor.dao.SensorItemDAO; import com.loafle.overflow.central.module.sensor.dao.SensorItemDAO;
import com.loafle.overflow.core.exception.OverflowException; import com.loafle.overflow.core.exception.OverflowException;
import com.loafle.overflow.core.model.PageParams; import com.loafle.overflow.model.meta.MetaDisplayItemMapping;
import com.loafle.overflow.model.sensor.Sensor; import com.loafle.overflow.model.sensor.Sensor;
import com.loafle.overflow.model.sensor.SensorItem; import com.loafle.overflow.model.sensor.SensorItem;
import com.loafle.overflow.service.central.sensor.SensorItemService; import com.loafle.overflow.service.central.sensor.SensorItemService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.transaction.Transactional; import javax.transaction.Transactional;
@ -25,38 +23,39 @@ public class CentralSensorItemService implements SensorItemService {
@Autowired @Autowired
private SensorItemDAO sensorItemDAO; private SensorItemDAO sensorItemDAO;
@Autowired @Autowired
private SensorDAO sensorDAO; private CentralSensorService sensorService;
@Transactional public SensorItem regist(Sensor sensor, MetaDisplayItemMapping metaDisplayItemMapping) throws OverflowException {
public SensorItem regist(SensorItem sensorItem) throws OverflowException { SensorItem sensorItem = new SensorItem();
Sensor s = sensorDAO.findById(sensorItem.getSensor().getId()).get(); sensorItem.setMetaDisplayItemMapping(metaDisplayItemMapping);
s.setItemCount(s.getItemCount() + 1); sensorItem.setSensor(sensor);
this.sensorDAO.save(s);
return this.sensorItemDAO.save(sensorItem); return this.sensorItemDAO.save(sensorItem);
} }
@Transactional @Override
public boolean registAll(List<SensorItem> sensorItemList) throws OverflowException { public SensorItem read(Long id) throws OverflowException {
Sensor s = sensorDAO.findById(sensorItemList.get(0).getSensor().getId()).get();
s.setItemCount(sensorItemList.size());
this.sensorDAO.save(s);
this.sensorItemDAO.saveAll(sensorItemList);
return true;
}
public SensorItem read(String id) throws OverflowException {
return this.sensorItemDAO.findById(Long.valueOf(id)).get(); return this.sensorItemDAO.findById(Long.valueOf(id)).get();
} }
public Page<SensorItem> readAllBySensorID(Long sensorID, PageParams pageParams) throws OverflowException { @Override
return this.sensorItemDAO.findAllBySensorId(sensorID, PageUtil.getPageRequest(pageParams)); public List<SensorItem> readAllBySensorID(Long sensorID) throws OverflowException {
return this.sensorItemDAO.findAllBySensorId(sensorID);
} }
@Transactional @Transactional
public void remove(SensorItem sensorItem) throws OverflowException { @Override
Sensor s = sensorItem.getSensor(); public void remove(Long sensorItemID) throws OverflowException {
s.setItemCount(s.getItemCount() - 1); SensorItem sensorItem = this.read(sensorItemID);
this.sensorDAO.save(s); if (null == sensorItem) {
this.sensorItemDAO.delete(sensorItem); throw new OverflowException(String.format("ID[%d] of SensorItem is not valid", sensorItemID));
} }
Sensor sensor = sensorItem.getSensor();
if (null == sensor) {
throw new OverflowException(String.format("ID[%d] of Sensor is not valid", sensorItem.getSensor().getId()));
}
sensor.setItemCount(sensor.getItemCount() - 1);
this.sensorService.regist(sensor);
this.sensorItemDAO.deleteById(sensorItemID);
}
} }

View File

@ -1,20 +1,18 @@
package com.loafle.overflow.central.module.sensor.service; package com.loafle.overflow.central.module.sensor.service;
import com.loafle.overflow.central.commons.utils.PageUtil;
import com.loafle.overflow.central.module.sensor.dao.SensorDAO; import com.loafle.overflow.central.module.sensor.dao.SensorDAO;
import com.loafle.overflow.core.exception.OverflowException; import com.loafle.overflow.core.exception.OverflowException;
import com.loafle.overflow.core.model.PageParams; import com.loafle.overflow.model.meta.MetaDisplayItemMapping;
import com.loafle.overflow.model.meta.MetaSensorStatus;
import com.loafle.overflow.model.sensor.Sensor; import com.loafle.overflow.model.sensor.Sensor;
import com.loafle.overflow.model.sensor.SensorItem; import com.loafle.overflow.model.target.Target;
import com.loafle.overflow.service.central.sensor.SensorItemService;
import com.loafle.overflow.service.central.sensor.SensorService; import com.loafle.overflow.service.central.sensor.SensorService;
import com.loafle.overflow.service.central.target.TargetService; import com.loafle.overflow.service.central.target.TargetService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.transaction.Transactional;
import java.util.List; import java.util.List;
/** /**
@ -24,56 +22,96 @@ import java.util.List;
public class CentralSensorService implements SensorService { public class CentralSensorService implements SensorService {
@Autowired @Autowired
SensorDAO sensorDAO; private SensorDAO sensorDAO;
@Autowired @Autowired
private SensorItemService sensorItemService; private CentralSensorItemService sensorItemService;
@Autowired @Autowired
private TargetService targetService; private TargetService targetService;
@Transactional @Transactional
public Sensor regist(Sensor sensor, Long targetID) throws OverflowException { @Override
public Sensor regist(Sensor sensor, List<MetaDisplayItemMapping> metaDisplayItemMappings) throws OverflowException {
Long targetID = sensor.getTarget().getId();
Target t = this.targetService.read(targetID);
if (null == t) {
throw new OverflowException(String.format("ID[%d] of Target is not valid", targetID));
}
if (null == sensor.getName()) {
String crawlerName = sensor.getMetaCrawlerMapping().getMetaCrawler().getName();
String targetTypeName = sensor.getTarget().getMetaTargetType().getName();
sensor.setName(targetTypeName + " Sensor via " + crawlerName);
}
sensor.setMetaSensorStatus(MetaSensorStatus.Enum.RUNNING.to());
Sensor s = this.sensorDAO.save(sensor);
s.setItemCount(0);
this.targetService.increaseSensorCount(targetID); this.targetService.increaseSensorCount(targetID);
return this.sensorDAO.save(sensor);
for (MetaDisplayItemMapping metaDisplayItemMapping : metaDisplayItemMappings) {
this.sensorItemService.regist(s, metaDisplayItemMapping);
}
return s;
} }
@Transactional
@Override
public Sensor modify(Long sensorID, List<MetaDisplayItemMapping> metaDisplayItemMappings) throws OverflowException {
// FIXME : not impl
return null;
}
@Override
public Sensor read(Long id) throws OverflowException { public Sensor read(Long id) throws OverflowException {
return this.sensorDAO.findById(id).get(); return this.sensorDAO.findById(id).get();
} }
@Transactional @Transactional
public void remove(Long id, Long targertID) throws OverflowException { @Override
this.targetService.decreaseSensorCount(targertID); public void remove(Long id) throws OverflowException {
Sensor sensor = this.read(id);
if (null == sensor) {
throw new OverflowException(String.format("ID[%d] of Sensor is not valid", id));
}
this.targetService.decreaseSensorCount(sensor.getTarget().getId());
this.sensorDAO.deleteById(id); this.sensorDAO.deleteById(id);
} }
public Page<Sensor> readAllByProbeID(Long probeID, PageParams pageParams) throws OverflowException { @Override
// FIXME: NOT IMPLEMENTS public List<Sensor> readAll() throws OverflowException {
return null; return this.sensorDAO.findAll();
}
public Page<Sensor> readAllByDomainID(Long domainID, PageParams pageParams) throws OverflowException {
// FIXME: NOT IMPLEMENTS
return null;
}
public Page<Sensor> readAllByInfraID(Long infraID, PageParams pageParams) throws OverflowException {
return this.sensorDAO.findAllByTargetInfraId(infraID, PageUtil.getPageRequest(pageParams));
}
public Page<Sensor> readAllByTargetID(Long targetID, PageParams pageParams) throws OverflowException {
return this.sensorDAO.findAllByTargetId(targetID, PageUtil.getPageRequest(pageParams));
} }
@Override @Override
public List<Sensor> readAllByTargetID(Long targetID) throws OverflowException {
return this.sensorDAO.findAllByTargetId(targetID);
}
@Override
public List<Sensor> readAllByTargetIn(List<Target> targets) throws OverflowException {
return this.sensorDAO.findAllByTargetIn(targets);
}
@Override
public List<Sensor> readAllByTargetInfraProbeID(Long probeID) throws OverflowException {
return this.sensorDAO.findAllByTargetInfraProbeId(probeID);
}
public Sensor regist(Sensor sensor) throws OverflowException {
Long targetID = sensor.getTarget().getId();
Target t = this.targetService.read(targetID);
if (null == t) {
throw new OverflowException(String.format("ID[%d] of Target is not valid", targetID));
}
return this.sensorDAO.save(sensor);
}
public Sensor increaseSensorItemCount(Long id) throws OverflowException { public Sensor increaseSensorItemCount(Long id) throws OverflowException {
Sensor s = this.sensorDAO.findById(id).get(); Sensor s = this.sensorDAO.findById(id).get();
s.setItemCount(s.getItemCount() + 1); s.setItemCount(s.getItemCount() + 1);
return this.sensorDAO.save(s); return this.sensorDAO.save(s);
} }
@Override
public Sensor decreaseSensorItemCount(Long id) throws OverflowException { public Sensor decreaseSensorItemCount(Long id) throws OverflowException {
Sensor s = this.sensorDAO.findById(id).get(); Sensor s = this.sensorDAO.findById(id).get();
int count = s.getItemCount(); int count = s.getItemCount();
@ -83,25 +121,4 @@ public class CentralSensorService implements SensorService {
return this.sensorDAO.save(s); return this.sensorDAO.save(s);
} }
@Transactional
public Sensor registSensorConfig(Sensor sensor, List<SensorItem> sensorItemList, String etcJson)
throws OverflowException {
this.targetService.increaseSensorCount(sensor.getTarget().getId());
this.sensorDAO.save(sensor);
for (SensorItem sensorItem : sensorItemList) {
sensorItem.setSensor(sensor);
}
this.sensorItemService.registAll(sensorItemList);
return sensor;
}
@Override
public String generateSensorConfig(Sensor sensor) throws OverflowException {
return null;
}
} }

View File

@ -5,9 +5,7 @@ import static org.junit.Assert.assertNotNull;
import java.util.List; import java.util.List;
import com.loafle.overflow.central.spring.AppConfigTest; import com.loafle.overflow.central.spring.AppConfigTest;
import com.loafle.overflow.model.domain.Domain;
import com.loafle.overflow.model.infra.InfraHostPort; import com.loafle.overflow.model.infra.InfraHostPort;
import com.loafle.overflow.model.member.Member;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;

View File

@ -6,7 +6,6 @@ import com.loafle.overflow.model.discovery.Host;
import com.loafle.overflow.model.discovery.Port; import com.loafle.overflow.model.discovery.Port;
import com.loafle.overflow.model.discovery.Service; import com.loafle.overflow.model.discovery.Service;
import com.loafle.overflow.model.discovery.Zone; import com.loafle.overflow.model.discovery.Zone;
import com.loafle.overflow.model.infra.Infra;
import com.loafle.overflow.model.meta.MetaCryptoType; import com.loafle.overflow.model.meta.MetaCryptoType;
import com.loafle.overflow.model.meta.MetaIPType; import com.loafle.overflow.model.meta.MetaIPType;
import com.loafle.overflow.model.meta.MetaPortType; import com.loafle.overflow.model.meta.MetaPortType;
@ -23,8 +22,6 @@ import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("test") @ActiveProfiles("test")
@ContextConfiguration(classes = { AppConfigTest.class }) @ContextConfiguration(classes = { AppConfigTest.class })

View File

@ -1,141 +1,49 @@
// package com.loafle.overflow.central.module.sensor.service; package com.loafle.overflow.central.module.sensor.service;
// import com.loafle.overflow.central.module.meta.model.MetaCrawler; import com.loafle.overflow.central.spring.AppConfigTest;
// import com.loafle.overflow.central.module.meta.model.MetaSensorStatus; import com.fasterxml.jackson.databind.ObjectMapper;
// import com.loafle.overflow.central.module.sensor.model.Sensor; import com.loafle.overflow.service.central.sensor.SensorService;
// import com.loafle.overflow.central.module.target.model.Target; import org.junit.Assert;
// import com.loafle.overflow.central.spring.AppConfigTest; import org.junit.Ignore;
// import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.Test;
// import org.junit.Assert; import org.junit.runner.RunWith;
// import org.junit.Ignore; import org.springframework.beans.factory.annotation.Autowired;
// import org.junit.Test; import org.springframework.test.context.ContextConfiguration;
// import org.junit.runner.RunWith; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
// import org.springframework.beans.factory.annotation.Autowired;
// import org.springframework.test.context.ContextConfiguration;
// import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
// import java.io.IOException; import java.io.IOException;
// import java.util.Date; import java.util.Date;
// import java.util.HashMap; import java.util.HashMap;
// import java.util.Map; import java.util.Map;
// /** /**
// * Created by insanity on 17. 6. 28. * Created by insanity on 17. 6. 28.
// */ */
// //@Ignore //@Ignore
// @RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
// @ContextConfiguration(classes = {AppConfigTest.class}) @ContextConfiguration(classes = {AppConfigTest.class})
// public class SensorServiceTest { public class SensorServiceTest {
// @Autowired @Autowired
// SensorService sensorService; SensorService sensorService;
// @Autowired @Autowired
// ObjectMapper objectMapper; ObjectMapper objectMapper;
// @Ignore
// @Test
// public void regist() throws Exception {
// Sensor sensor = new Sensor();
// MetaSensorStatus status = new MetaSensorStatus((short)1);
// sensor.setStatus(status);
// MetaCrawler crawler = new MetaCrawler();
// crawler.setId((short)1);
// sensor.setCrawler(crawler);
// Target target = new Target();
// target.setId(1);
// sensor.setTarget(target);
// sensor.setCreateDate(new Date());
// Sensor res = this.sensorService.regist(sensor); @Test
// Assert.assertNotNull(res); public void regist() throws Exception {
// } Sensor sensor = new Sensor();
MetaSensorStatus status = new MetaSensorStatus((short)1);
sensor.setStatus(status);
MetaCrawler crawler = new MetaCrawler();
crawler.setId((short)1);
sensor.setCrawler(crawler);
Target target = new Target();
target.setId(1);
sensor.setTarget(target);
sensor.setCreateDate(new Date());
// @Test Sensor res = this.sensorService.regist(sensor);
// public void read() throws Exception { Assert.assertNotNull(res);
// Sensor res = this.sensorService.read("1"); }
// Assert.assertNotNull(res); }
// ObjectMapper objectMapper = new ObjectMapper();
// // String json = objectMapper.writeValueAsString(res);
// String json = this.objectMapper.writeValueAsString(res);
// System.out.println(json);
// }
// @Test
// public void generateSensorConfig() throws IOException {
// Sensor sensor = new Sensor();
// sensor.setId(5);
// String result = this.sensorService.generateSensorConfig(sensor);
// System.out.println(result);
// }
// @Test
// public void testJsonToMap() throws IOException {
// String json = "{\"interval\":5}";
// ObjectMapper objectMapper = new ObjectMapper();
// Map<String, Object> mm = new HashMap<>();
// mm = objectMapper.readValue(json, HashMap.class);
// System.out.println(mm.get("interval"));
// }
// @Test
// public void stringbbb() {
// StringBuffer stringBuffer = new StringBuffer();
// stringBuffer.append("222222222222222222222222222");
// System.out.println(stringBuffer.toString());
// stringBuffer.setLength(0);
// System.out.println(stringBuffer.toString());
// stringBuffer.append("3333333333");
// System.out.println(stringBuffer.toString());
// }
// // @Test
// // public void readAllByTarget() throws Exception {
// // Target target = new Target();
// // target.setId(1);
// // List<Sensor> sensors = this.sensorService.readAllByTarget(target);
// //
// // ObjectMapper objectMapper = new ObjectMapper();
// //
// // String json = objectMapper.writeValueAsString(sensors);
// //
// // System.out.println(json);
// // }
// // @Test
// // public void readAllByTargetList() {
// //
// // Domain domain = new Domain();
// // domain.setId(1);
// //
// // List<Sensor> sl = this.sensorService.readAllByDomain(domain);
// //
// // Assert.assertNotEquals(sl.size(), 0);
// //
// // }
// // @Test
// // public void readAllByInfra() {
// //
// // Infra infra = new InfraMachine();
// // infra.setId(1);
// //
// // List<Sensor> sl = this.sensorService.readAllByInfra(1);
// //
// // Assert.assertNotEquals(sl.size(), 0);
// //
// // }
// }