<commit_before>package com.yiji.framework.watcher.metrics;

import com.alibaba.fastjson.serializer.SerializeConfig;
import com.codahale.metrics.*;
import com.google.common.collect.Maps;
import com.yiji.framework.watcher.MonitorMetrics;
import com.yiji.framework.watcher.metrics.serilization.*;
import com.yjf.common.metrics.MetricsHolder;

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

/**
 * @author <EMAIL>
 */
public class MetricRegistryMetrics implements MonitorMetrics{
    private MetricRegistry metricRegistry;

    public enum MetricType {
        TIMER,
        GAUGE,
        COUNTER,
        HISTOGRAM,
        METER,
        ALL
    }

    public MetricRegistryMetrics() {
        this.metricRegistry = MetricsHolder.metricRegistry();
        SerializeConfig.getGlobalInstance().put(Counter.class, CounterSerializer.INSTANCE);
        SerializeConfig.getGlobalInstance().put(Gauge.class, GaugeSerializer.INSTANCE);
        SerializeConfig.getGlobalInstance().put(Histogram.class, HistogramSerializer.INSTANCE);
        SerializeConfig.getGlobalInstance().put(Meter.class, MeterSerializer.INSTANCE);
        SerializeConfig.getGlobalInstance().put(Timer.class, TimerSerializer.INSTANCE);
    }

    @Override
    public Object monitor(Map<String, Object> params) {
        Map<String, Metric> metrics = metricRegistry.getMetrics();
        Map<String, Metric> ret = Maps.newHashMap();
        for (Map.Entry<String, Metric> metricEntry : metrics.entrySet()) {
            if (matches(params, metricEntry.getKey(), metricEntry.getValue())) {
                ret.put(metricEntry.getKey(), metricEntry.getValue());
            }

        }
        return ret;
    }

    public boolean matches(Map<String, Object> param, String metricName, Metric metric) {
        if (param == null) {
            return true;
        }
        String name = (String) param.get(MonitorMetrics.KEY);
        String type = (String) param.get("type");
        if (metricName != null && !metricName.startsWith(name)) {
            return false;
        }
        if (type != null) {
            MetricType chosen = null;
            try {
                chosen = MetricType.valueOf(type);
            } catch (Exception e) {
                chosen = MetricType.ALL;
            }
            return chosen.equals(MetricType.ALL) || type2MetricClass(chosen).isInstance(metric);
        } else {
            return true;
        }
    }

    private Class<? extends Metric> type2MetricClass(MetricType type) {
        switch (type) {
            case TIMER:
                return Timer.class;
            case GAUGE:
                return Gauge.class;
            case COUNTER:<commit_msg>修改bug<commit_after>package com.yiji.framework.watcher.metrics;

import com.alibaba.fastjson.serializer.SerializeConfig;
import com.codahale.metrics.*;
import com.google.common.collect.Maps;
import com.yiji.framework.watcher.MonitorMetrics;
import com.yiji.framework.watcher.metrics.serilization.*;
import com.yjf.common.metrics.MetricsHolder;

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

/**
 * @author <EMAIL>
 */
public class MetricRegistryMetrics implements MonitorMetrics{
    private MetricRegistry metricRegistry;

    public enum MetricType {
        TIMER,
        GAUGE,
        COUNTER,
        HISTOGRAM,
        METER,
        ALL
    }

    public MetricRegistryMetrics() {
        this.metricRegistry = MetricsHolder.metricRegistry();
        SerializeConfig.getGlobalInstance().put(Counter.class, CounterSerializer.INSTANCE);
        SerializeConfig.getGlobalInstance().put(Gauge.class, GaugeSerializer.INSTANCE);
        SerializeConfig.getGlobalInstance().put(Histogram.class, HistogramSerializer.INSTANCE);
        SerializeConfig.getGlobalInstance().put(Meter.class, MeterSerializer.INSTANCE);
        SerializeConfig.getGlobalInstance().put(Timer.class, TimerSerializer.INSTANCE);
    }

    @Override
    public Object monitor(Map<String, Object> params) {
        Map<String, Metric> metrics = metricRegistry.getMetrics();
        Map<String, Metric> ret = Maps.newHashMap();
        for (Map.Entry<String, Metric> metricEntry : metrics.entrySet()) {
            if (matches(params, metricEntry.getKey(), metricEntry.getValue())) {
                ret.put(metricEntry.getKey(), metricEntry.getValue());
            }

        }
        return ret;
    }

    public boolean matches(Map<String, Object> param, String metricName, Metric metric) {
        if (param == null) {
            return true;
        }
        String name = (String) param.get(MonitorMetrics.KEY);
        String type = (String) param.get("type");
        if (metricName != null && name != null && !metricName.startsWith(name)) {
            return false;
        }
        if (type != null) {
            MetricType chosen = null;
            try {
                chosen = MetricType.valueOf(type);
            } catch (Exception e) {
                chosen = MetricType.ALL;
            }
            return chosen.equals(MetricType.ALL) || type2MetricClass(chosen).isInstance(metric);
        } else {
            return true;
        }
    }

    private Class<? extends Metric> type2MetricClass(MetricType type) {
        switch (type) {
            case TIMER:
                return Timer.class;
            case GAUGE:
                return Gauge.class;
            case COUNTER:
{'fraction_non_alphanumeric': 0.10726256983240223, 'fraction_numerical': 0.0007448789571694599, 'mean_word_length': 2.4763754045307445, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '2017902', 'n_tokens_mistral': 1483, 'n_tokens_neox': 1465, 'n_words': 289}