All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.tblsoft.solr.log.parser.SolrRequestCounter Maven / Gradle / Ivy

The newest version!
package de.tblsoft.solr.log.parser;

import com.google.common.base.Strings;
import com.google.common.util.concurrent.AtomicLongMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.*;

public class SolrRequestCounter extends SolrQueryLogParser {

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

    private AtomicLongMap count = AtomicLongMap.create();

    private static Map aggregationOptions =  new HashMap();
    static {
        aggregationOptions.put("second", "yyyy-MM-dd-kk:mm:ss");
        aggregationOptions.put("minute", "yyyy-MM-dd-kk:mm");
        aggregationOptions.put("hour", "yyyy-MM-dd-kk");
        aggregationOptions.put("day", "yyyy-MM-dd");
        aggregationOptions.put("month", "yyyy-MM");
        aggregationOptions.put("year", "yyyy");
    }

    private String aggregationOption = "second";

    private long maxOutput = 150;



    public SolrRequestCounter(String file) {
        super(file);
    }

    @Override
    protected void logRow(SolrLogRow solrLogRow) {


        SimpleDateFormat sdf = new SimpleDateFormat(
               aggregationOptions.get(aggregationOption));
        String aggregatedDate = sdf.format(solrLogRow.getTimestamp());

        count.incrementAndGet(aggregatedDate);

    }

    @Override
    protected void logRowError(SolrLogRow solrLogRow, Exception e) {
        LOG.error("error");
    }

    public void print() {
        SortedSet keys = new TreeSet(count.asMap().keySet());

        long maxValue = Collections.max(count.asMap().values());
        LOG.info("max: " + maxValue);

        for (String key : keys) {
            long value = count.get(key);
            long scaledValue = scale(value,maxOutput, maxValue);
            LOG.info(key + " : " + value + print(scaledValue));

        }
    }

    private String print(long n) {
        int i = 0;
        StringBuilder buffer = new StringBuilder(" ");
        while (i++ < n) {
            //http://www.key-shortcut.com/zeichentabellen/utf-8-unicode-tabelle-2/
            //buffer.append('\u2588');
            //'\u2B1B'
            char icon = '\u2B1B';
            buffer.append(icon);
        }
        return buffer.toString();
    }

    private long scale(long value, long scale, long max) {
        long f = (value * scale) / max;
        return f;
    }

    public void setAggregationOption(String aggregationOption) {
        if(!Strings.isNullOrEmpty(aggregationOption)) {
            this.aggregationOption = aggregationOption;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy