<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The SF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package org.apache.sling.hc.samples.impl;

import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.ConfigurationPolicy;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.PropertyUnbounded;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.apache.sling.hc.api.HealthCheck;
import org.apache.sling.hc.api.Result;
import org.apache.sling.hc.util.FormattingResultLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Sample Health Check that takes N msec to execute,
 *  used to demonstrate execution timeouts and caching.
 */
@Component(
        configurationFactory=true,
        policy=ConfigurationPolicy.REQUIRE,
        metatype=true)
@Properties({
    @Property(name=HealthCheck.NAME),
    @Property(name=HealthCheck.TAGS, unbounded=PropertyUnbounded.ARRAY),
    @Property(name=HealthCheck.MBEAN_NAME),
})
@Service(value=HealthCheck.class)
public class SlowHealthCheckSample implements HealthCheck{

    private final Logger log = LoggerFactory.getLogger(getClass());
    private final AtomicInteger counter = new AtomicInteger();
    private long minExecutionTime;
    private long maxExecutionTime;
    
    @Property(label="Minimum execution time",
            description="Shortest execution time in milliseconds",
            intValue=SlowHealthCheckSample.DEFAULT_MIN_EXEC_TIME)
    private static final String PROP_MIN_EXEC_TIME = "execution.time.min.msec";
    private static final int DEFAULT_MIN_EXEC_TIME = 1000;
    
    @Property(label="Maximum execution time",
            description="Longest execution time in milliseconds",
            intValue=SlowHealthCheckSample.DEFAULT_MAX_EXEC_TIME)
    private static final String PROP_MAX_EXEC_TIME = "execution.time.max.msec";
    private static final int DEFAULT_MAX_EXEC_TIME = 5000;
    
    @Override<commit_msg>SLING-3278 - make SlowHealthCheckSample immediate to have a (somewhat) persistent counter<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The SF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package org.apache.sling.hc.samples.impl;

import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.ConfigurationPolicy;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.PropertyUnbounded;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.apache.sling.hc.api.HealthCheck;
import org.apache.sling.hc.api.Result;
import org.apache.sling.hc.util.FormattingResultLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Sample Health Check that takes N msec to execute,
 *  used to demonstrate execution timeouts and caching.
 */

// Need to make the component immediate to make sure a single 
// instance is used, otherwise the lazy DS activation policy
// might cause a different instance to be used for every
// execution. In this sample this is just done to allow the
// counter to persist as long as this service's bundle is active.
@Component(
        configurationFactory=true,
        policy=ConfigurationPolicy.REQUIRE,
        metatype=true,
        immediate=true)
@Properties({
    @Property(name=HealthCheck.NAME),
    @Property(name=HealthCheck.TAGS, unbounded=PropertyUnbounded.ARRAY),
    @Property(name=HealthCheck.MBEAN_NAME),
})
@Service(value=HealthCheck.class)
public class SlowHealthCheckSample implements HealthCheck{

    private final Logger log = LoggerFactory.getLogger(getClass());
    private final AtomicInteger counter = new AtomicInteger();
    private long minExecutionTime;
    private long maxExecutionTime;
    
    @Property(label="Minimum execution time",
            description="Shortest execution time in milliseconds",
            intValue=SlowHealthCheckSample.DEFAULT_MIN_EXEC_TIME)
    private static final String PROP_MIN_EXEC_TIME = "execution.time.min.msec";
    private static final int DEFAULT_MIN_EXEC_TIME = 1000;
    
    @Property(label="Maximum execution time",
            description="Longest execution time in milliseconds",
            intValue=SlowHealthCheckSample.DEFAULT_MAX_EXEC_TIME)
    private static final String PROP_MAX_EXEC_TIME = "execution.time.max.msec";
    private static final int DEFAULT_MAX_EXEC_TIME = 5000;
    
    @Override
{'fraction_non_alphanumeric': 0.08149779735682819, 'fraction_numerical': 0.005034612964128383, 'mean_word_length': 4.401019541206457, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2503809', 'n_tokens_mistral': 1725, 'n_tokens_neox': 1671, 'n_words': 537}