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

fi.evolver.basics.spring.status.StatusReportService Maven / Gradle / Ivy

package fi.evolver.basics.spring.status;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.util.ProxyUtils;
import org.springframework.stereotype.Service;

import fi.evolver.basics.spring.status.model.ComponentStatus;
import fi.evolver.basics.spring.status.model.Reportable;
import fi.evolver.utils.timing.TimingUtils;
import fi.evolver.utils.timing.TimingUtils.AutoCloser;


@Service
public class StatusReportService {
	private static final Logger LOG = LoggerFactory.getLogger(StatusReportService.class);


	private final List reportables;


	@Autowired
	public StatusReportService(List reportables) {
		this.reportables = reportables;
	}


	public List getStatus() {
		List results = new ArrayList<>();

		for (Reportable reportable: reportables) {
			try (AutoCloser t = TimingUtils.begin(getCleanName(reportable))) {
				results.addAll(reportable.getStatus());
			}
			catch (RuntimeException e) {
				LOG.warn("Querying the status of {} failed", reportable.getClass().getSimpleName(), e);
			}
		}

		return results;
	}


	public List getStatusByReportable(String reportable) {
		return reportables.stream()
				.filter(r -> ProxyUtils.getUserClass(r).getSimpleName().equals(reportable))
				.findFirst()
				.map(Reportable::getStatus)
				.orElseGet(Collections::emptyList);
	}


	public List listReportables() {
		return reportables.stream()
				.map(ProxyUtils::getUserClass)
				.map(Class::getSimpleName)
				.collect(Collectors.toList());
	}


	private static String getCleanName(Reportable reportable) {
		return ProxyUtils.getUserClass(reportable).getSimpleName();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy