<commit_before>package io.bootique.jdbc.instrumented.hikaricp.healthcheck;

import com.zaxxer.hikari.HikariPoolMXBean;
import com.zaxxer.hikari.pool.HikariPool;
import io.bootique.metrics.health.HealthCheck;
import io.bootique.metrics.health.HealthCheckOutcome;
import io.bootique.metrics.health.check.Threshold;
import io.bootique.metrics.health.check.ThresholdType;
import io.bootique.metrics.health.check.ValueRange;
import io.bootique.value.Duration;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * HikariCP "aliveness" standard check
 */
public class ConnectivityCheck implements HealthCheck {

    private final HikariPoolMXBean pool;
    private final ValueRange<Duration> timeoutThresholds;

    public ConnectivityCheck(HikariPoolMXBean pool, ValueRange<Duration> timeoutThresholds) {
        this.pool = pool;
        this.timeoutThresholds = timeoutThresholds;
    }

    /**
     * Generates a stable qualified name for the {@link ConnectivityCheck}
     *
     * @param dataSourceName Bootique configuration name of the data source being checked.
     * @return qualified name bq.jdbc.[dataSourceName].connectivity
     */
    public static String healthCheckName(String dataSourceName) {
        return "bq.jdbc." + dataSourceName + ".connectivity";
    }

    /**
     * The health check obtains a {@link Connection} from the pool and immediately return it.
     * The standard HikariCP internal "aliveness" check will be run.
     *
     * @return {@link HealthCheckOutcome#ok()} if an "alive" {@link Connection} can be obtained,
     * otherwise {@link HealthCheckOutcome#critical()} if the connection fails or times out
     */
    @Override
    public HealthCheckOutcome check() {

        HealthCheckOutcome warningOutcome = checkThreshold(ThresholdType.WARNING);

        switch (warningOutcome.getStatus()) {
            case WARNING:
                return warningOutcome;
            // this is a case of WARNING threshold messing...
            case UNKNOWN:
                return checkThreshold(ThresholdType.CRITICAL);
        }

        return HealthCheckOutcome.ok();
    }
    
    protected HealthCheckOutcome checkThreshold(ThresholdType type) {

        Threshold<Duration> threshold = timeoutThresholds.getThreshold(type);
        if (threshold == null) {
            return HealthCheckOutcome.unknown();
        }

        long timeout = threshold.getValue().getDuration().toMillis();

        try (Connection connection = ((HikariPool) pool).getConnection(timeout)) {
            return HealthCheckOutcome.ok();
        } catch (SQLException e) {
            return HealthCheckOutcome.critical(e);
        }
    }
}<commit_msg>Revisit the names and formats of Hikari health check configuration #81<commit_after>package io.bootique.jdbc.instrumented.hikaricp.healthcheck;

import com.zaxxer.hikari.HikariPoolMXBean;
import com.zaxxer.hikari.pool.HikariPool;
import io.bootique.metrics.health.HealthCheck;
import io.bootique.metrics.health.HealthCheckOutcome;
import io.bootique.metrics.health.check.Threshold;
import io.bootique.metrics.health.check.ThresholdType;
import io.bootique.metrics.health.check.ValueRange;
import io.bootique.value.Duration;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.function.Function;

/**
 * HikariCP "aliveness" standard check
 */
public class ConnectivityCheck implements HealthCheck {

    private final HikariPoolMXBean pool;
    private final ValueRange<Duration> timeoutThresholds;

    public ConnectivityCheck(HikariPoolMXBean pool, ValueRange<Duration> timeoutThresholds) {
        this.pool = pool;
        this.timeoutThresholds = timeoutThresholds;
    }

    /**
     * Generates a stable qualified name for the {@link ConnectivityCheck}
     *
     * @param dataSourceName Bootique configuration name of the data source being checked.
     * @return qualified name bq.jdbc.[dataSourceName].connectivity
     */
    public static String healthCheckName(String dataSourceName) {
        return "bq.jdbc." + dataSourceName + ".connectivity";
    }

    /**
     * The health check obtains a {@link Connection} from the pool and immediately return it.
     * The standard HikariCP internal "aliveness" check will be run.
     *
     * @return {@link HealthCheckOutcome#ok()} if an "alive" {@link Connection} can be obtained,
     * otherwise {@link HealthCheckOutcome#critical()} if the connection fails or times out
     */
    @Override
    public HealthCheckOutcome check() {

        HealthCheckOutcome warningOutcome = checkThreshold(ThresholdType.WARNING, e -> HealthCheckOutcome.warning(e));

        switch (warningOutcome.getStatus()) {
            case WARNING:
                return warningOutcome;
            case UNKNOWN:
                return checkThreshold(ThresholdType.CRITICAL, e -> HealthCheckOutcome.critical(e));
        }

        return HealthCheckOutcome.ok();
    }

    protected HealthCheckOutcome checkThreshold(ThresholdType type, Function<SQLException, HealthCheckOutcome> onFailure) {

        Threshold<Duration> threshold = timeoutThresholds.getThreshold(type);
        if (threshold == null) {
            return HealthCheckOutcome.unknown();
        }

        long timeout = threshold.getValue().getDuration().toMillis();

        try (Connection connection = ((HikariPool) pool).getConnection(timeout)) {
            return HealthCheckOutcome.ok();
        } catch (SQLException e) {
            return onFailure.apply(e);
        }
    }
}
{'fraction_non_alphanumeric': 0.09077705156136529, 'fraction_numerical': 0.00036310820624546115, 'mean_word_length': 3.300546448087432, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 12, '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': '265964', 'n_tokens_mistral': 1513, 'n_tokens_neox': 1465, 'n_words': 369}