<commit_before>package org.kuali.kfs.sys.businessobject;


import net.sf.ehcache.statistics.StatisticsGateway;
import org.kuali.rice.core.api.config.property.ConfigContext;
import org.kuali.rice.core.api.util.RiceConstants;
import org.kuali.rice.core.impl.services.CoreImplServiceLocator;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;

import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.sql.DataSource;
import java.lang.management.ManagementFactory;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;

public class HealthReport {

    public String status = "";
    public String message = "";
    public List<Metric> metrics;

    public HealthReport() {
        metrics = new ArrayList();
    }

    public HealthReport checkHealth() {

        long startTime = System.currentTimeMillis();
        checkMemory();
        checkDatabase();
        checkSessions();
        checkCache();
        if (getMessage().isEmpty()) {
            status = "OK";
            message = "System checks out";
        }
        Metric metric = new Metric("Health", "executionTime", (System.currentTimeMillis()-startTime)+"ms");
        metrics.add(metric);
        return this;

    }

    protected void checkCache() {

        try {
            doCheckCache();
        } catch (Exception e) {
            status = "Failed";
            message += "Failed to assess Cache health.\n";
        }
    }

    protected void doCheckCache() {
        String cacheName = "http://rice.kuali.org/kim/v2_0/PermissionType";
        CacheManager cm = CoreImplServiceLocator.getCacheManagerRegistry().getCacheManagerByCacheName(cacheName);
        Cache cache = cm.getCache(cacheName);

        net.sf.ehcache.Cache nativeCache = (net.sf.ehcache.Cache) cache.getNativeCache();
        Metric metric = new Metric("Cache", "size", "" + nativeCache.getSize());
        metrics.add(metric);
        StatisticsGateway sg = nativeCache.getStatistics();
        metric = new Metric("Cache", "hitRatio", "" + sg.cacheHitRatio());
        metrics.add(metric);
        metric = new Metric("Cache", "expiredCount", "" + sg.cacheExpiredCount());
        metrics.add(metric);
        metric = new Metric("Cache", "missCount", "" + sg.cacheMissCount());
        metrics.add(metric);
    }

    protected void checkSessions() {
        try {
            doCheckSessions();

        } catch (Exception e) {
            status = "Failed";
            message += "Failed to assess Session health.\n";
        }
    }

    protected void doCheckSessions() throws MalformedObjectNameException, MBeanException, AttributeNotFoundException, InstanceNotFoundException, ReflectionException {
        String context = ConfigContext.getCurrentContextConfig().getProperty("app.context.name");

        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
        ObjectName objectName = new ObjectName("Catalina:type=Manager,context=/"+context+",host=localhost");
        Object activeSessions = mBeanServer.getAttribute(objectName, "activeSessions");
        Metric metric = new Metric("Session", "active", activeSessions.toString());
        metrics.add(metric);
        Object expiredSessions = mBeanServer.getAttribute(objectName, "expiredSessions");
        metric = new Metric("Session", "expired", expiredSessions.toString());
        metrics.add(metric);
        Object maxActive = mBeanServer.getAttribute(objectName, "maxActive");
        metric = new Metric("Session", "maxActive", maxActive.toString());
        metrics.add(metric);
    }

    protected void checkDatabase() {

        try {
            doCheckDatabase();
        } catch (Exception e) {
            status = "Failed";
            message += "Failed to assess Database health.\n";
        }


    }

    protected void doCheckDatabase() throws SQLException {
        DataSource ds = (DataSource) ConfigContext.getCurrentContextConfig().getObject(RiceConstants.DATASOURCE_OBJ);
        DataSource ntds = (DataSource) ConfigContext.getCurrentContextConfig().getObject(RiceConstants.NON_TRANSACTIONAL_DATASOURCE_OBJ);
        DataSource riceDS = (DataSource) ConfigContext.getCurrentContextConfig().getObject(RiceConstants.SERVER_DATASOURCE_OBJ);

        Connection conn = ds.getConnection();
        Boolean closed = conn.isClosed();
        Metric metric = new Metric("Database", "kfs-xa-closed", closed.toString());
        metrics.add(metric);

        conn = ntds.getConnection();
        closed = conn.isClosed();
        metric = new Metric("Database", "kfs-closed", closed.toString());
        metrics.add(metric);

        conn = riceDS.getConnection();
        closed = conn.isClosed();
        metric = new Metric("Database", "rice-xa-closed", closed.toString());
        metrics.add(metric);<commit_msg>FINI-1571 provide some system metrics to the health check endpoint<commit_after>package org.kuali.kfs.sys.businessobject;


import net.sf.ehcache.statistics.StatisticsGateway;
import org.kuali.rice.core.api.config.property.ConfigContext;
import org.kuali.rice.core.api.util.RiceConstants;
import org.kuali.rice.core.impl.services.CoreImplServiceLocator;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;

import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.sql.DataSource;
import java.lang.management.ManagementFactory;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;

public class HealthReport {

    public String status = "";
    public String message = "";
    public List<Metric> metrics;

    public HealthReport() {
        metrics = new ArrayList();
    }

    public HealthReport checkHealth() {

        long startTime = System.currentTimeMillis();
        checkMemory();
        checkDatabase();
        checkSessions();
        checkCache();
        if (getMessage().isEmpty()) {
            status = "OK";
            message = "System checks out";
        }
        Metric metric = new Metric("Health", "executionTime", (System.currentTimeMillis()-startTime)+"ms");
        metrics.add(metric);
        return this;

    }

    protected void checkCache() {

        try {
            doCheckCache();
        } catch (Exception e) {
            status = "Failed";
            message += "Failed to assess Cache health.\n";
        }
    }

    protected void doCheckCache() {
        String cacheName = "http://rice.kuali.org/kim/v2_0/PermissionType";
        CacheManager cm = CoreImplServiceLocator.getCacheManagerRegistry().getCacheManagerByCacheName(cacheName);
        Cache cache = cm.getCache(cacheName);

        net.sf.ehcache.Cache nativeCache = (net.sf.ehcache.Cache) cache.getNativeCache();
        Metric metric = new Metric("Cache", "size", "" + nativeCache.getSize());
        metrics.add(metric);
        StatisticsGateway sg = nativeCache.getStatistics();
        metric = new Metric("Cache", "hitRatio", "" + sg.cacheHitRatio());
        metrics.add(metric);
        metric = new Metric("Cache", "expiredCount", "" + sg.cacheExpiredCount());
        metrics.add(metric);
        metric = new Metric("Cache", "missCount", "" + sg.cacheMissCount());
        metrics.add(metric);
    }

    protected void checkSessions() {
        try {
            doCheckSessions();

        } catch (Exception e) {
            status = "Failed";
            message += "Failed to assess Session health.\n";
        }
    }

    protected void doCheckSessions() throws MalformedObjectNameException, MBeanException, AttributeNotFoundException, InstanceNotFoundException, ReflectionException {
        String context = ConfigContext.getCurrentContextConfig().getProperty("app.context.name");

        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
        ObjectName objectName = new ObjectName("Catalina:type=Manager,context=/"+context+",host=localhost");
        Object activeSessions = mBeanServer.getAttribute(objectName, "activeSessions");
        Metric metric = new Metric("Session", "active", activeSessions.toString());
        metrics.add(metric);
        Object expiredSessions = mBeanServer.getAttribute(objectName, "expiredSessions");
        metric = new Metric("Session", "expired", expiredSessions.toString());
        metrics.add(metric);
        Object maxActive = mBeanServer.getAttribute(objectName, "maxActive");
        metric = new Metric("Session", "maxActive", maxActive.toString());
        metrics.add(metric);
    }

    protected void checkDatabase() {

        try {
            doCheckDatabase();
        } catch (Exception e) {
            status = "Failed";
            message += "Failed to assess Database health.\n";
        }


    }

    protected void doCheckDatabase() throws SQLException {
        DataSource ds = (DataSource) ConfigContext.getCurrentContextConfig().getObject(RiceConstants.DATASOURCE_OBJ);
        DataSource ntds = (DataSource) ConfigContext.getCurrentContextConfig().getObject(RiceConstants.NON_TRANSACTIONAL_DATASOURCE_OBJ);
        DataSource riceDS = (DataSource) ConfigContext.getCurrentContextConfig().getObject(RiceConstants.SERVER_DATASOURCE_OBJ);

        Connection conn = ds.getConnection();
        Boolean closed = conn.isClosed();
        conn.close();
        Metric metric = new Metric("Database", "kfs-xa-closed", closed.toString());
        metrics.add(metric);

        conn = ntds.getConnection();
        closed = conn.isClosed();
        conn.close();
        metric = new Metric("Database", "kfs-closed", closed.toString());
        metrics.add(metric);

        conn = riceDS.getConnection();
        closed = conn.isClosed();
        conn.close();
        metric = new Metric("Database", "rice-xa-closed", closed.toString());
        metrics.add(metric);
{'fraction_non_alphanumeric': 0.11748345641124006, 'fraction_numerical': 0.00076723889901218, 'mean_word_length': 3.2407482716551446, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5397924', 'n_tokens_mistral': 2768, 'n_tokens_neox': 2697, 'n_words': 567}