<commit_before>/*
 * The MIT License
 *
 * Copyright (c) 2013-2014, CloudBees, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.codahale.metrics.jenkins.impl;

import com.codahale.metrics.ExponentiallyDecayingReservoir;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Metric;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.MetricSet;
import com.codahale.metrics.Timer;
import com.codahale.metrics.jenkins.MetricProvider;
import com.codahale.metrics.jenkins.Metrics;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.model.Computer;
import hudson.model.Executor;
import hudson.model.Node;
import hudson.model.PeriodicWork;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.model.listeners.RunListener;
import jenkins.model.Jenkins;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static com.codahale.metrics.MetricRegistry.name;

/**
 * @author <NAME>
 */
@Extension
public class JenkinsMetricProviderImpl extends MetricProvider {
    private Histogram jenkinsQueueLength;
    private Histogram jenkinsNodeTotalCount;
    private Histogram jenkinsNodeOnlineCount;
    private Histogram jenkinsExecutorTotalCount;
    private Histogram jenkinsExecutorUsedCount;
    private Timer jenkinsBuildDuration;
    private Map<Computer, Timer> computerBuildDurations = new HashMap<Computer, Timer>();
    private Map<String, Metric> metrics;
    private MetricSet set;

    public JenkinsMetricProviderImpl() {
        metrics = new LinkedHashMap<String, Metric>();
        jenkinsQueueLength = new Histogram(new ExponentiallyDecayingReservoir());
        metrics.put(name("jenkins", "queue", "size"), jenkinsQueueLength);
        jenkinsNodeTotalCount = new Histogram(new ExponentiallyDecayingReservoir());
        metrics.put(name("jenkins", "node", "count"), jenkinsNodeTotalCount);
        jenkinsNodeOnlineCount = new Histogram(new ExponentiallyDecayingReservoir());
        metrics.put(name("jenkins", "node", "online"), jenkinsNodeOnlineCount);
        jenkinsExecutorTotalCount = new Histogram(new ExponentiallyDecayingReservoir());
        metrics.put(name("jenkins", "executor", "count"), jenkinsExecutorTotalCount);
        jenkinsExecutorUsedCount = new Histogram(new ExponentiallyDecayingReservoir());
        metrics.put(name("jenkins", "executor", "in-use"), jenkinsExecutorUsedCount);
        jenkinsBuildDuration = new Timer();
        metrics.put(name("jenkins", "build", "duration"), jenkinsBuildDuration);
        set = new MetricSet() {
            public Map<String, Metric> getMetrics() {
                return metrics;
            }
        };
    }

    public static JenkinsMetricProviderImpl instance() {
        return Jenkins.getInstance().getExtensionList(MetricProvider.class).get(JenkinsMetricProviderImpl.class);
    }

    @NonNull
    @Override
    public MetricSet getMetricSet() {
        return set;
    }

    public Histogram getJenkinsExecutorTotalCount() {
        return jenkinsExecutorTotalCount;
    }

    public Histogram getJenkinsExecutorUsedCount() {
        return jenkinsExecutorUsedCount;
    }

    public Histogram getJenkinsNodeOnlineCount() {
        return jenkinsNodeOnlineCount;
    }

    public Histogram getJenkinsNodeTotalCount() {
        return jenkinsNodeTotalCount;
    }

    private synchronized void updateMetrics() {
        final Jenkins jenkins = Jenkins.getInstance();
        if (jenkinsQueueLength != null) {
            jenkinsQueueLength.update(jenkins.getQueue().getBuildableItems().size());
        }
        if (jenkinsNodeTotalCount != null || jenkinsNodeOnlineCount != null || jenkinsExecutorTotalCount != null
                || jenkinsExecutorUsedCount != null) {
            int nodeTotal = 0;
            int nodeOnline = 0;
            int executorTotal = 0;
            int executorUsed = 0;
            if (jenkins.getNumExecutors() > 0) {
                nodeTotal++;
                Computer computer = jenkins.toComputer();
                if (computer != null) {
                    if (!computer.isOffline()) {
                        nodeOnline++;
                        for (Executor e : computer.getExecutors()) {
                            executorTotal++;
                            if (!e.isIdle()) {
                                executorUsed++;
                            }
                        }
                    }
                }
            }<commit_msg>Need instantaneous metrics as well as averages<commit_after>/*
 * The MIT License
 *
 * Copyright (c) 2013-2014, CloudBees, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.codahale.metrics.jenkins.impl;

import com.codahale.metrics.CachedGauge;
import com.codahale.metrics.ExponentiallyDecayingReservoir;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Metric;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.MetricSet;
import com.codahale.metrics.Timer;
import com.codahale.metrics.jenkins.MetricProvider;
import com.codahale.metrics.jenkins.Metrics;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.model.Computer;
import hudson.model.Executor;
import hudson.model.Node;
import hudson.model.PeriodicWork;
import hudson.model.Queue;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.model.listeners.RunListener;
import jenkins.model.Jenkins;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static com.codahale.metrics.MetricRegistry.name;

/**
 * @author <NAME>
 */
@Extension
public class JenkinsMetricProviderImpl extends MetricProvider {
    private Gauge<Integer> jenkinsQueueLength;
    private Gauge<Integer> jenkinsQueueBlocked;
    private Gauge<Integer> jenkinsQueueBuildable;
    private Gauge<Integer> jenkinsQueueStuck;
    private Gauge<Integer> jenkinsQueuePending;
    private Histogram jenkinsQueueLengthAverage;
    private Histogram jenkinsQueueBlockedAverage;
    private Histogram jenkinsQueueBuildableAverage;
    private Histogram jenkinsQueueStuckAverage;
    private Histogram jenkinsQueuePendingAverage;
    private Histogram jenkinsNodeTotalCount;
    private Histogram jenkinsNodeOnlineCount;
    private Histogram jenkinsExecutorTotalCount;
    private Histogram jenkinsExecutorUsedCount;
    private Timer jenkinsBuildDuration;
    private Map<Computer, Timer> computerBuildDurations = new HashMap<Computer, Timer>();
    private Map<String, Metric> metrics;
    private MetricSet set;

    public JenkinsMetricProviderImpl() {
        metrics = new LinkedHashMap<String, Metric>();
        jenkinsQueueLength = new CachedGauge<Integer>(5, TimeUnit.SECONDS) {
            @Override
            protected Integer loadValue() {
                return Jenkins.getInstance().getQueue().getItems().length;
            }
        };
        metrics.put(name("jenkins", "queue", "size"), jenkinsQueueLength);        
        jenkinsQueueBlocked = new CachedGauge<Integer>(5, TimeUnit.SECONDS) {
            @Override
            protected Integer loadValue() {
                int blocked = 0;
                for (Queue.Item i: Jenkins.getInstance().getQueue().getItems()) {
                    if (i.isBlocked()) blocked++;
                }
                return blocked;
            }
        };
        metrics.put(name("jenkins", "queue", "blocked"), jenkinsQueueBlocked);        
        jenkinsQueueBuildable = new CachedGauge<Integer>(5, TimeUnit.SECONDS) {
            @Override
            protected Integer loadValue() {
                int buildable = 0;
                for (Queue.Item i: Jenkins.getInstance().getQueue().getItems()) {
                    if (i.isBuildable()) buildable++;
                }
                return buildable;
            }
        };
        metrics.put(name("jenkins", "queue", "buildable"), jenkinsQueueBuildable);                
        jenkinsQueueStuck = new CachedGauge<Integer>(5, TimeUnit.SECONDS) {
            @Override
            protected Integer loadValue() {
                int stuck = 0;
                for (Queue.Item i: Jenkins.getInstance().getQueue().getItems()) {
                    if (i.isStuck()) stuck++;
                }
                return stuck;
            }
        };
        metrics.put(name("jenkins", "queue", "stuck"), jenkinsQueueStuck);                
        jenkinsQueuePending = new CachedGauge<Integer>(5, TimeUnit.SECONDS) {
            @Override
            protected Integer loadValue() {
                return Jenkins.getInstance().getQueue().getPendingItems().size();
            }
        };
        metrics.put(name("jenkins", "queue", "pending"), jenkinsQueuePending);                
        jenkinsQueueLengthAverage = new Histogram(new ExponentiallyDecayingReservoir());
        jenkinsQueueBlockedAverage = new Histogram(new ExponentiallyDecayingReservoir());
        jenkinsQueueBuildableAverage = new Histogram(new ExponentiallyDecayingReservoir());
        jenkinsQueueStuckAverage = new Histogram(new ExponentiallyDecayingReservoir());
        jenkinsQueuePendingAverage = new Histogram(new ExponentiallyDecayingReservoir());
        metrics.put(name("jenkins", "queue", "blocked", "average"), jenkinsQueueBlockedAverage);
        metrics.put(name("jenkins", "queue", "buildable", "average"), jenkinsQueueBuildableAverage);
        metrics.put(name("jenkins", "queue", "stuck", "average"), jenkinsQueueStuckAverage);
        metrics.put(name("jenkins", "queue", "pending", "average"), jenkinsQueuePendingAverage);
        jenkinsNodeTotalCount = new Histogram(new ExponentiallyDecayingReservoir());
        metrics.put(name("jenkins", "node", "count"), jenkinsNodeTotalCount);
        jenkinsNodeOnlineCount = new Histogram(new ExponentiallyDecayingReservoir());
        metrics.put(name("jenkins", "node", "online"), jenkinsNodeOnlineCount);
        jenkinsExecutorTotalCount = new Histogram(new ExponentiallyDecayingReservoir());
        metrics.put(name("jenkins", "executor", "count"), jenkinsExecutorTotalCount);
        jenkinsExecutorUsedCount = new Histogram(new ExponentiallyDecayingReservoir());
        metrics.put(name("jenkins", "executor", "in-use"), jenkinsExecutorUsedCount);
        jenkinsBuildDuration = new Timer();
        metrics.put(name("jenkins", "build", "duration"), jenkinsBuildDuration);
        set = new MetricSet() {
            public Map<String, Metric> getMetrics() {
                return metrics;
            }
        };
    }

    public static JenkinsMetricProviderImpl instance() {
        return Jenkins.getInstance().getExtensionList(MetricProvider.class).get(JenkinsMetricProviderImpl.class);
    }

    @NonNull
    @Override
    public MetricSet getMetricSet() {
        return set;
    }

    public Histogram getJenkinsExecutorTotalCount() {
        return jenkinsExecutorTotalCount;
    }

    public Histogram getJenkinsExecutorUsedCount() {
        return jenkinsExecutorUsedCount;
    }

    public Histogram getJenkinsNodeOnlineCount() {
        return jenkinsNodeOnlineCount;
    }

    public Histogram getJenkinsNodeTotalCount() {
        return jenkinsNodeTotalCount;
    }

    private synchronized void updateMetrics() {
        final Jenkins jenkins = Jenkins.getInstance();
        if (jenkinsQueueLengthAverage != null && jenkinsQueueLength != null) {
            jenkinsQueueLengthAverage.update(jenkinsQueueLength.getValue());
        }
        if (jenkinsQueueBlockedAverage != null && jenkinsQueueBlocked  != null) {
            jenkinsQueueBlockedAverage.update(jenkinsQueueBlocked.getValue());
        }
        if (jenkinsQueueBuildableAverage != null && jenkinsQueueBuildable != null) {
            jenkinsQueueBuildableAverage.update(jenkinsQueueBuildable.getValue());
        }
        if (jenkinsQueueStuckAverage != null && jenkinsQueueStuck != null) {
            jenkinsQueueStuckAverage.update(jenkinsQueueStuck.getValue());
        }
        if (jenkinsQueuePendingAverage != null && jenkinsQueuePending != null) {
            jenkinsQueuePendingAverage.update(jenkinsQueuePending.getValue());
        }
        if (jenkinsNodeTotalCount != null || jenkinsNodeOnlineCount != null || jenkinsExecutorTotalCount != null
                || jenkinsExecutorUsedCount != null) {
            int nodeTotal = 0;
            int nodeOnline = 0;
            int executorTotal = 0;
            int executorUsed = 0;
            if (jenkins.getNumExecutors() > 0) {
                nodeTotal++;
                Computer computer = jenkins.toComputer();
                if (computer != null) {
                    if (!computer.isOffline()) {
                        nodeOnline++;
                        for (Executor e : computer.getExecutors()) {
                            executorTotal++;
                            if (!e.isIdle()) {
                                executorUsed++;
                            }
                        }
                    }
                }
            }
{'fraction_non_alphanumeric': 0.09254947613504075, 'fraction_numerical': 0.0021989393351442245, 'mean_word_length': 2.945649400357234, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 27, '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': '4630191', 'n_tokens_mistral': 4273, 'n_tokens_neox': 4019, 'n_words': 1031}