Заготовка под отправку счетчиков

This commit is contained in:
serega6531
2020-03-17 01:21:40 +03:00
parent 658e3255eb
commit b3cc0de450
7 changed files with 70 additions and 11 deletions

View File

@@ -5,15 +5,15 @@ import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import ru.serega6531.packmate.service.StreamSubscriptionService;
import ru.serega6531.packmate.service.SubscriptionService;
@Component
public class WebSocketHandler extends TextWebSocketHandler {
private final StreamSubscriptionService subscriptionService;
private final SubscriptionService subscriptionService;
@Autowired
public WebSocketHandler(StreamSubscriptionService subscriptionService) {
public WebSocketHandler(SubscriptionService subscriptionService) {
this.subscriptionService = subscriptionService;
}

View 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;
}
}

View File

@@ -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());
}
}

View File

@@ -22,13 +22,13 @@ import java.util.stream.Collectors;
public class PatternService {
private final PatternRepository repository;
private final StreamSubscriptionService subscriptionService;
private final SubscriptionService subscriptionService;
private final Map<Integer, Pattern> patterns = new HashMap<>();
@Autowired
public PatternService(PatternRepository repository,
StreamSubscriptionService subscriptionService) {
SubscriptionService subscriptionService) {
this.repository = repository;
this.subscriptionService = subscriptionService;

View File

@@ -22,7 +22,7 @@ import java.util.Optional;
public class ServicesService {
private final ServiceRepository repository;
private final StreamSubscriptionService subscriptionService;
private final SubscriptionService subscriptionService;
private final InetAddress localIp;
@@ -30,7 +30,7 @@ public class ServicesService {
@Autowired
public ServicesService(ServiceRepository repository,
StreamSubscriptionService subscriptionService,
SubscriptionService subscriptionService,
@Value("${local-ip}") String localIpString) throws UnknownHostException {
this.repository = repository;
this.subscriptionService = subscriptionService;

View File

@@ -30,7 +30,8 @@ public class StreamService {
private final StreamRepository repository;
private final PatternService patternService;
private final ServicesService servicesService;
private final StreamSubscriptionService subscriptionService;
private final CountingService countingService;
private final SubscriptionService subscriptionService;
private final boolean ignoreEmptyPackets;
@@ -40,11 +41,13 @@ public class StreamService {
public StreamService(StreamRepository repository,
PatternService patternService,
ServicesService servicesService,
StreamSubscriptionService subscriptionService,
CountingService countingService,
SubscriptionService subscriptionService,
@Value("${ignore-empty-packets}") boolean ignoreEmptyPackets) {
this.repository = repository;
this.patternService = patternService;
this.servicesService = servicesService;
this.countingService = countingService;
this.subscriptionService = subscriptionService;
this.ignoreEmptyPackets = ignoreEmptyPackets;
}
@@ -88,6 +91,8 @@ public class StreamService {
stream.setEndTimestamp(packets.get(packets.size() - 1).getTimestamp());
stream.setService(service.getPort());
countingService.countStream(service.getPort(), packets.size());
new StreamOptimizer(service, packets).optimizeStream();
processUserAgent(packets, stream);

View File

@@ -18,14 +18,14 @@ import java.util.Objects;
@Service
@Slf4j
public class StreamSubscriptionService {
public class SubscriptionService {
private final List<WebSocketSession> subscribers = Collections.synchronizedList(new ArrayList<>());
private final ObjectMapper mapper;
@Autowired
public StreamSubscriptionService(ObjectMapper mapper) {
public SubscriptionService(ObjectMapper mapper) {
this.mapper = mapper;
}