Заготовка под отправку счетчиков
This commit is contained in:
@@ -5,15 +5,15 @@ import org.springframework.stereotype.Component;
|
|||||||
import org.springframework.web.socket.CloseStatus;
|
import org.springframework.web.socket.CloseStatus;
|
||||||
import org.springframework.web.socket.WebSocketSession;
|
import org.springframework.web.socket.WebSocketSession;
|
||||||
import org.springframework.web.socket.handler.TextWebSocketHandler;
|
import org.springframework.web.socket.handler.TextWebSocketHandler;
|
||||||
import ru.serega6531.packmate.service.StreamSubscriptionService;
|
import ru.serega6531.packmate.service.SubscriptionService;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class WebSocketHandler extends TextWebSocketHandler {
|
public class WebSocketHandler extends TextWebSocketHandler {
|
||||||
|
|
||||||
private final StreamSubscriptionService subscriptionService;
|
private final SubscriptionService subscriptionService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public WebSocketHandler(StreamSubscriptionService subscriptionService) {
|
public WebSocketHandler(SubscriptionService subscriptionService) {
|
||||||
this.subscriptionService = subscriptionService;
|
this.subscriptionService = subscriptionService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
18
src/main/java/ru/serega6531/packmate/model/pojo/Counter.java
Normal file
18
src/main/java/ru/serega6531/packmate/model/pojo/Counter.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package ru.serega6531.packmate.model.pojo;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public class Counter {
|
||||||
|
|
||||||
|
private int value = 0;
|
||||||
|
|
||||||
|
public void increment() {
|
||||||
|
value++;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void increment(int num) {
|
||||||
|
value += num;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package ru.serega6531.packmate.service;
|
||||||
|
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import ru.serega6531.packmate.model.pojo.Counter;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class CountingService {
|
||||||
|
|
||||||
|
private Map<Integer, Counter> servicesPackets = new HashMap<>();
|
||||||
|
private Map<Integer, Counter> servicesStreams = new HashMap<>();
|
||||||
|
|
||||||
|
private Counter totalPackets = new Counter();
|
||||||
|
private Counter totalStreams = new Counter();
|
||||||
|
|
||||||
|
void countStream(int serviceId, int packets) {
|
||||||
|
getCounter(servicesPackets, serviceId).increment(packets);
|
||||||
|
getCounter(servicesStreams, serviceId).increment();
|
||||||
|
|
||||||
|
totalPackets.increment(packets);
|
||||||
|
totalStreams.increment();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Scheduled(cron = "0 * * ? * *")
|
||||||
|
public void sendCounters() {
|
||||||
|
//TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
private Counter getCounter(Map<Integer, Counter> counters, int serviceId) {
|
||||||
|
return counters.computeIfAbsent(serviceId, c -> new Counter());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -22,13 +22,13 @@ import java.util.stream.Collectors;
|
|||||||
public class PatternService {
|
public class PatternService {
|
||||||
|
|
||||||
private final PatternRepository repository;
|
private final PatternRepository repository;
|
||||||
private final StreamSubscriptionService subscriptionService;
|
private final SubscriptionService subscriptionService;
|
||||||
|
|
||||||
private final Map<Integer, Pattern> patterns = new HashMap<>();
|
private final Map<Integer, Pattern> patterns = new HashMap<>();
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public PatternService(PatternRepository repository,
|
public PatternService(PatternRepository repository,
|
||||||
StreamSubscriptionService subscriptionService) {
|
SubscriptionService subscriptionService) {
|
||||||
this.repository = repository;
|
this.repository = repository;
|
||||||
this.subscriptionService = subscriptionService;
|
this.subscriptionService = subscriptionService;
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ import java.util.Optional;
|
|||||||
public class ServicesService {
|
public class ServicesService {
|
||||||
|
|
||||||
private final ServiceRepository repository;
|
private final ServiceRepository repository;
|
||||||
private final StreamSubscriptionService subscriptionService;
|
private final SubscriptionService subscriptionService;
|
||||||
|
|
||||||
private final InetAddress localIp;
|
private final InetAddress localIp;
|
||||||
|
|
||||||
@@ -30,7 +30,7 @@ public class ServicesService {
|
|||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public ServicesService(ServiceRepository repository,
|
public ServicesService(ServiceRepository repository,
|
||||||
StreamSubscriptionService subscriptionService,
|
SubscriptionService subscriptionService,
|
||||||
@Value("${local-ip}") String localIpString) throws UnknownHostException {
|
@Value("${local-ip}") String localIpString) throws UnknownHostException {
|
||||||
this.repository = repository;
|
this.repository = repository;
|
||||||
this.subscriptionService = subscriptionService;
|
this.subscriptionService = subscriptionService;
|
||||||
|
|||||||
@@ -30,7 +30,8 @@ public class StreamService {
|
|||||||
private final StreamRepository repository;
|
private final StreamRepository repository;
|
||||||
private final PatternService patternService;
|
private final PatternService patternService;
|
||||||
private final ServicesService servicesService;
|
private final ServicesService servicesService;
|
||||||
private final StreamSubscriptionService subscriptionService;
|
private final CountingService countingService;
|
||||||
|
private final SubscriptionService subscriptionService;
|
||||||
|
|
||||||
private final boolean ignoreEmptyPackets;
|
private final boolean ignoreEmptyPackets;
|
||||||
|
|
||||||
@@ -40,11 +41,13 @@ public class StreamService {
|
|||||||
public StreamService(StreamRepository repository,
|
public StreamService(StreamRepository repository,
|
||||||
PatternService patternService,
|
PatternService patternService,
|
||||||
ServicesService servicesService,
|
ServicesService servicesService,
|
||||||
StreamSubscriptionService subscriptionService,
|
CountingService countingService,
|
||||||
|
SubscriptionService subscriptionService,
|
||||||
@Value("${ignore-empty-packets}") boolean ignoreEmptyPackets) {
|
@Value("${ignore-empty-packets}") boolean ignoreEmptyPackets) {
|
||||||
this.repository = repository;
|
this.repository = repository;
|
||||||
this.patternService = patternService;
|
this.patternService = patternService;
|
||||||
this.servicesService = servicesService;
|
this.servicesService = servicesService;
|
||||||
|
this.countingService = countingService;
|
||||||
this.subscriptionService = subscriptionService;
|
this.subscriptionService = subscriptionService;
|
||||||
this.ignoreEmptyPackets = ignoreEmptyPackets;
|
this.ignoreEmptyPackets = ignoreEmptyPackets;
|
||||||
}
|
}
|
||||||
@@ -88,6 +91,8 @@ public class StreamService {
|
|||||||
stream.setEndTimestamp(packets.get(packets.size() - 1).getTimestamp());
|
stream.setEndTimestamp(packets.get(packets.size() - 1).getTimestamp());
|
||||||
stream.setService(service.getPort());
|
stream.setService(service.getPort());
|
||||||
|
|
||||||
|
countingService.countStream(service.getPort(), packets.size());
|
||||||
|
|
||||||
new StreamOptimizer(service, packets).optimizeStream();
|
new StreamOptimizer(service, packets).optimizeStream();
|
||||||
processUserAgent(packets, stream);
|
processUserAgent(packets, stream);
|
||||||
|
|
||||||
|
|||||||
@@ -18,14 +18,14 @@ import java.util.Objects;
|
|||||||
|
|
||||||
@Service
|
@Service
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class StreamSubscriptionService {
|
public class SubscriptionService {
|
||||||
|
|
||||||
private final List<WebSocketSession> subscribers = Collections.synchronizedList(new ArrayList<>());
|
private final List<WebSocketSession> subscribers = Collections.synchronizedList(new ArrayList<>());
|
||||||
|
|
||||||
private final ObjectMapper mapper;
|
private final ObjectMapper mapper;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public StreamSubscriptionService(ObjectMapper mapper) {
|
public SubscriptionService(ObjectMapper mapper) {
|
||||||
this.mapper = mapper;
|
this.mapper = mapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user