<commit_before>package de.ahus1.hystrix.util;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.netflix.hystrix.util.HystrixRollingNumberEvent;
import com.quigley.zabbixj.metrics.MetricsException;
import com.quigley.zabbixj.metrics.MetricsKey;
import com.quigley.zabbixj.metrics.MetricsProvider;

public class ZabbixCommandMetricsProvider implements MetricsProvider {

    private static Logger LOG = LoggerFactory
            .getLogger(ZabbixCommandMetricsProvider.class);

    Map<String, HystrixRollingNumberEvent> numberEvents = new HashMap<>();
    {
        numberEvents.put("CollapsedRequests",
                HystrixRollingNumberEvent.COLLAPSED);
        numberEvents.put("ExceptionsThrown",
                HystrixRollingNumberEvent.EXCEPTION_THROWN);
        numberEvents.put("Failure", HystrixRollingNumberEvent.FAILURE);
        numberEvents.put("FallbackFailure",
                HystrixRollingNumberEvent.FALLBACK_FAILURE);
        numberEvents.put("FallbackRejection",
                HystrixRollingNumberEvent.FALLBACK_REJECTION);
        numberEvents.put("FallbackSuccess",
                HystrixRollingNumberEvent.FALLBACK_SUCCESS);
        numberEvents.put("ResponsesFromCache",
                HystrixRollingNumberEvent.RESPONSE_FROM_CACHE);
        numberEvents.put("SemaphoreRejected",
                HystrixRollingNumberEvent.SEMAPHORE_REJECTED);
        numberEvents.put("ShortCircuited",
                HystrixRollingNumberEvent.SHORT_CIRCUITED);
        numberEvents.put("Success", HystrixRollingNumberEvent.SUCCESS);
        numberEvents.put("ThreadPoolRejected",
                HystrixRollingNumberEvent.THREAD_POOL_REJECTED);
        numberEvents.put("Timeout", HystrixRollingNumberEvent.TIMEOUT);
    }

    Map<String, HystrixZabbixMetricsPublisherCommand> map = new HashMap<>();

    @Override
    public Object getValue(MetricsKey key) throws MetricsException {
        if (key.getKey().equals("discovery")) {
            try {
                JSONObject discovery = new JSONObject();
                JSONArray array = new JSONArray();
                discovery.put("data", array);
                for (String command : map.keySet()) {
                    JSONObject element = new JSONObject();
                    element.put("{#COMMAND}", command);
                    array.put(element);
                }
                LOG.info("discovery: {}", discovery);
                return discovery;
            } catch (JSONException e) {
                throw new MetricsException(e);
            }
        } else if (key.getKey().startsWith("count")) {
            HystrixRollingNumberEvent event = numberEvents.get(key.getKey()
                    .replaceFirst("count", ""));
            if (event == null) {
                throw new MetricsException("unknown key " + key);
            }
            HystrixZabbixMetricsPublisherCommand command = map.get(key
                    .getParameters()[0]);
            if (command == null) {
                throw new MetricsException("unknown command referenced in "
                        + key);
            }
            return command.getMetrics().getCumulativeCount(event);
        } else if (key.getKey().equals("latencyExecute")) {
            HystrixZabbixMetricsPublisherCommand command = map.get(key
                    .getParameters()[0]);
            if (command == null) {
                throw new MetricsException("unknown command referenced in "
                        + key);
            }
            String percentile = key.getParameters()[1];
            if (percentile.equalsIgnoreCase("mean")) {
                return command.getMetrics().getTotalTimeMean();
            } else {
                return command.getMetrics().getTotalTimePercentile(
                        Double.parseDouble(percentile));
            }
        } else if (key.getKey().equals("latencyTotal")) {
            HystrixZabbixMetricsPublisherCommand command = map.get(key
                    .getParameters()[0]);
            if (command == null) {
                throw new MetricsException("unknown command referenced in "
                        + key);
            }
            String percentile = key.getParameters()[1];
            if (percentile.equalsIgnoreCase("mean")) {
                return command.getMetrics().getTotalTimeMean();
            } else {
                return command.getMetrics().getTotalTimePercentile(
                        Double.parseDouble(percentile));
            }
        } else if (key.getKey().equals("latencyExecute")) {
            HystrixZabbixMetricsPublisherCommand command = map.get(key
                    .getParameters()[0]);
            if (command == null) {
                throw new MetricsException("unknown command referenced in "
                        + key);
            }
            String percentile = key.getParameters()[1];
            if (percentile.equalsIgnoreCase("mean")) {
                return command.getMetrics().getExecutionTimeMean();
            } else {
                return command.getMetrics().getExecutionTimePercentile(
                        Double.parseDouble(percentile));
            }
        } else {
            throw new MetricsException("unknown key " + key);<commit_msg>return 0 for any yet unknown metric<commit_after>package de.ahus1.hystrix.util;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.netflix.hystrix.util.HystrixRollingNumberEvent;
import com.quigley.zabbixj.metrics.MetricsException;
import com.quigley.zabbixj.metrics.MetricsKey;
import com.quigley.zabbixj.metrics.MetricsProvider;

public class ZabbixCommandMetricsProvider implements MetricsProvider {

    private static Logger LOG = LoggerFactory
            .getLogger(ZabbixCommandMetricsProvider.class);

    Map<String, HystrixRollingNumberEvent> numberEvents = new HashMap<>();
    {
        numberEvents.put("CollapsedRequests",
                HystrixRollingNumberEvent.COLLAPSED);
        numberEvents.put("ExceptionsThrown",
                HystrixRollingNumberEvent.EXCEPTION_THROWN);
        numberEvents.put("Failure", HystrixRollingNumberEvent.FAILURE);
        numberEvents.put("FallbackFailure",
                HystrixRollingNumberEvent.FALLBACK_FAILURE);
        numberEvents.put("FallbackRejection",
                HystrixRollingNumberEvent.FALLBACK_REJECTION);
        numberEvents.put("FallbackSuccess",
                HystrixRollingNumberEvent.FALLBACK_SUCCESS);
        numberEvents.put("ResponsesFromCache",
                HystrixRollingNumberEvent.RESPONSE_FROM_CACHE);
        numberEvents.put("SemaphoreRejected",
                HystrixRollingNumberEvent.SEMAPHORE_REJECTED);
        numberEvents.put("ShortCircuited",
                HystrixRollingNumberEvent.SHORT_CIRCUITED);
        numberEvents.put("Success", HystrixRollingNumberEvent.SUCCESS);
        numberEvents.put("ThreadPoolRejected",
                HystrixRollingNumberEvent.THREAD_POOL_REJECTED);
        numberEvents.put("Timeout", HystrixRollingNumberEvent.TIMEOUT);
    }

    Map<String, HystrixZabbixMetricsPublisherCommand> map = new HashMap<>();

    /**
     * Return a metric for a given key. If the command is unknown, return 0 as a
     * neutral value, as Hystrix will register them only at the first command
     * run.
     */
    @Override
    public Object getValue(MetricsKey key) throws MetricsException {
        if (key.getKey().equals("discovery")) {
            try {
                JSONObject discovery = new JSONObject();
                JSONArray array = new JSONArray();
                discovery.put("data", array);
                for (String command : map.keySet()) {
                    JSONObject element = new JSONObject();
                    element.put("{#COMMAND}", command);
                    array.put(element);
                }
                LOG.info("discovery: {}", discovery);
                return discovery;
            } catch (JSONException e) {
                throw new MetricsException(e);
            }
        } else if (key.getKey().startsWith("count")) {
            HystrixRollingNumberEvent event =
                    numberEvents.get(key.getKey().replaceFirst("count", ""));
            if (event == null) {
                throw new MetricsException("unknown key " + key);
            }
            HystrixZabbixMetricsPublisherCommand command =
                    map.get(key.getParameters()[0]);
            if (command == null) {
                return 0;
            }
            return command.getMetrics().getCumulativeCount(event);
        } else if (key.getKey().equals("latencyExecute")) {
            HystrixZabbixMetricsPublisherCommand command =
                    map.get(key.getParameters()[0]);
            if (command == null) {
                return 0;
            }
            String percentile = key.getParameters()[1];
            if (percentile.equalsIgnoreCase("mean")) {
                return command.getMetrics().getTotalTimeMean();
            } else {
                return command.getMetrics().getTotalTimePercentile(
                        Double.parseDouble(percentile));
            }
        } else if (key.getKey().equals("latencyTotal")) {
            HystrixZabbixMetricsPublisherCommand command =
                    map.get(key.getParameters()[0]);
            if (command == null) {
                return 0;
            }
            String percentile = key.getParameters()[1];
            if (percentile.equalsIgnoreCase("mean")) {
                return command.getMetrics().getTotalTimeMean();
            } else {
                return command.getMetrics().getTotalTimePercentile(
                        Double.parseDouble(percentile));
            }
        } else if (key.getKey().equals("latencyExecute")) {
            HystrixZabbixMetricsPublisherCommand command =
                    map.get(key.getParameters()[0]);
            if (command == null) {
                return 0;
            }
            String percentile = key.getParameters()[1];
            if (percentile.equalsIgnoreCase("mean")) {
                return command.getMetrics().getExecutionTimeMean();
            } else {
                return command.getMetrics().getExecutionTimePercentile(
                        Double.parseDouble(percentile));
            }
        } else {
            throw new MetricsException("unknown key " + key);
{'fraction_non_alphanumeric': 0.09848626424967295, 'fraction_numerical': 0.0024294524387964865, 'mean_word_length': 2.0808865860679333, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '4049252', 'n_tokens_mistral': 2786, 'n_tokens_neox': 2667, 'n_words': 471}