All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fi.evolver.basics.spring.status.component.CommunicationStatusReporter Maven / Gradle / Ivy

package fi.evolver.basics.spring.status.component;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import fi.evolver.basics.spring.log.MessageLogStatisticRepository;
import fi.evolver.basics.spring.log.entity.MessageLogStatistic;
import fi.evolver.basics.spring.status.model.ComponentStatus;
import fi.evolver.basics.spring.status.model.Reportable;


@Component
public class CommunicationStatusReporter implements Reportable {

    private final MessageLogStatisticRepository messageLogStatisticRepository;


    @Autowired
    public CommunicationStatusReporter(MessageLogStatisticRepository messageLogStatisticRepository) {
        this.messageLogStatisticRepository = messageLogStatisticRepository;
    }


    @Override
    public List getStatus() {
        return messageLogStatisticRepository.findAll().stream()
                .map(CommunicationStatusReporter::getStatus)
                .collect(Collectors.toList());
    }


    private static ComponentStatus getStatus(MessageLogStatistic statistic) {
        LocalDateTime now = LocalDateTime.now();
        Map properties = new LinkedHashMap<>();
        properties.put("SuccessRate", Math.round(statistic.getSuccessRate() * 100));
        if (statistic.getLastFailureStart() != null)
            properties.put("TimeSinceFailureMs", Math.max(0L, ChronoUnit.MILLIS.between(statistic.getLastFailureStart(), now)));
        if (statistic.getLastSuccessStart() != null)
            properties.put("TimeSinceSuccessMs", Math.max(0L, ChronoUnit.MILLIS.between(statistic.getLastSuccessStart(), now)));
        properties.put("SuccessDurationMs", Math.round(statistic.getSuccessDurationHt()));
        properties.put("FailureDurationMs", Math.round(statistic.getFailureDurationHt()));
        properties.put("MessageType", statistic.getMessageType().replaceAll("\\W", "-"));
        properties.put("SourceSystem", statistic.getSourceSystem().replaceAll("\\W", "-"));
        properties.put("TargetSystem", statistic.getTargetSystem().replaceAll("\\W", "-"));
        return new ComponentStatus(
                "Communication",
                String.format("%s_%s_%s", statistic.getMessageType().replaceAll("\\W", "-"), statistic.getSourceSystem().replaceAll("\\W", "-"), statistic.getTargetSystem().replaceAll("\\W", "-")),
                properties);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy