package com.ctrip.ops.sysdev.filters;

import java.util.*;

import com.ctrip.ops.sysdev.baseplugin.BaseFilter;

import org.apache.log4j.Logger;

public class Metric extends BaseFilter {
    private static final Logger logger = Logger.getLogger(Metric.class.getName());

    int windowSize;
    String key;
    String value;
    Map<String, Object> metric;
     Map<String, Object> metricToEmit;
    long lastEmitTime;

    public Metric(Map config) {
        super(config);
    }

    protected void prepare() {
        this.key = (String) config.get("key");
        this.value = (String) config.get("value");
        this.windowSize = (int) config.get("windowSize") * 1000;
        this.processExtraEventsFunc = true;
        this.metric = new HashMap();
        this.metricToEmit = new HashMap();

        this.lastEmitTime = System.currentTimeMillis();
    }

    @Override
    protected Map filter(final Map event) {
        if (System.currentTimeMillis() >= this.windowSize + this.lastEmitTime) {
            this.metricToEmit = this.metric;
            this.metric = new HashMap();
        }
        if (event.containsKey(this.key) && event.containsKey(this.value)) {
            String keyValue = (String) event.get(this.key);
            Object valueValue = event.get(this.value);
            HashMap set = (HashMap) this.metric.get(keyValue);
            if (set == null) {
                set = new HashMap();
                set.put(valueValue, 1);
            } else {
                if (set.containsKey(valueValue)) {
                    int count = (int) set.get(valueValue);
                    set.put(valueValue, count + 1);
                } else {
                    set.put(valueValue, 1);
                }
            }
            this.metric.put(keyValue, set);
        }
        return event;
    }

    @Override
    public List<Map<String, Object>> filterExtraEvents(Map event) {
        if (metricToEmit.size() == 0) {
            return null;
        }
        List<Map<String, Object>> events = new ArrayList<Map<String, Object>>();
        this.metricToEmit.put("@timestamp", this.lastEmitTime);
        this.postProcess(this.metricToEmit, true);
        events.add(this.metricToEmit);
        this.metricToEmit = new HashMap();
        this.lastEmitTime = System.currentTimeMillis();
        return events;
    }
}

@@ -353,17 +353,16 @@
 ic;%0A    
- 
 Map%3CStri
@@ -1195,25 +1195,16 @@
 yValue =
- (String)
  event.g
@@ -1215,16 +1215,27 @@
 his.key)
+.toString()
 ;%0A      

keyvalue must be String