<commit_before>package com.comandante;

import com.comandante.http.server.resource.BillHttpGraph;
import com.google.common.base.Optional;
import com.google.common.collect.Maps;
import com.google.common.util.concurrent.AbstractExecutionThreadService;
import org.mapdb.DB;
import org.mapdb.HTreeMap;

import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;

public class BillGraphManager {

    private HTreeMap<String, BillHttpGraph> billHttpGraphs;
    private Map<String, BillGraphRefresher> refresherMap;
    private static final String BILL_HTTP_GRAPH_DB = "billHttpGraphs";
    private final ResizeService resizeService;

    public BillGraphManager(DB db) {
        billHttpGraphs = db.createHashMap(BILL_HTTP_GRAPH_DB)
                .valueSerializer(new BillHttpGraphSerializer())
                .makeOrGet();
        this.refresherMap = Maps.newConcurrentMap();
        this.resizeService = new ResizeService();
        this.resizeService.startAsync();
    }

    public void addNewGraph(BillHttpGraph billHttpGraph) {
        BillGraph billGraph = BillGraph.createBillGraph(billHttpGraph, Optional.<String>absent());
        startGraph(billGraph, billHttpGraph.getRefreshRate());
        billHttpGraphs.put(billGraph.getId(), billHttpGraph);
    }

    public void removeGraph(String id) {
        refresherMap.get(id).stopAsync();
        refresherMap.remove(id);
        billHttpGraphs.remove(id);
    }

    public void resizeGraph(BillResizeEvent billResizeEvent) {
        resizeService.process(billResizeEvent);
    }

    private void startGraph(BillGraph billGraph, int reload) {
        BillGraphRefresher billGraphRefresher = new BillGraphRefresher(this, billGraph, reload);
        billGraphRefresher.startAsync();
        refresherMap.put(billGraph.getId(), billGraphRefresher);
    }

    public void generateAllGraphsFromDisk() {
        for (Map.Entry<String, BillHttpGraph> next : billHttpGraphs.entrySet()) {
            if (next.getValue() != null) {
                startGraph(BillGraph.createBillGraph(next.getValue(), Optional.of(next.getKey())), next.getValue().getRefreshRate());
            }
        }
    }

    class ResizeService extends AbstractExecutionThreadService {
        private final LinkedBlockingQueue<BillResizeEvent> events;

        public ResizeService() {
            this.events = new LinkedBlockingQueue<BillResizeEvent>();
        }

        @Override
        protected void run() throws Exception {
            while (true) {
                BillResizeEvent event = events.take();
                BillHttpGraph billHttpGraph = billHttpGraphs.get(event.getId());
                billHttpGraph.setWidth(event.getWidth());
                billHttpGraph.setHeight(event.getHeight());
                billHttpGraphs.put(event.getId(), billHttpGraph);
                refresherMap.get(event.getId()).getBillGraph().reSize(event.getWidth(), event.getHeight());
            }
        }
<commit_msg>using a cache to deal with when to call home to graphite for a resize when a user is dragging around the resize window tooling<commit_after>package com.comandante;

import com.comandante.http.server.resource.BillHttpGraph;
import com.google.common.base.Optional;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.RemovalCause;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
import com.google.common.collect.Maps;
import com.google.common.util.concurrent.AbstractExecutionThreadService;
import org.mapdb.DB;
import org.mapdb.HTreeMap;

import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class BillGraphManager {

    private HTreeMap<String, BillHttpGraph> billHttpGraphs;
    private Map<String, BillGraphRefresher> refresherMap;
    private static final String BILL_HTTP_GRAPH_DB = "billHttpGraphs";
    private final ResizeService resizeService;

    public BillGraphManager(DB db) {
        billHttpGraphs = db.createHashMap(BILL_HTTP_GRAPH_DB)
                .valueSerializer(new BillHttpGraphSerializer())
                .makeOrGet();
        this.refresherMap = Maps.newConcurrentMap();
        this.resizeService = new ResizeService();
        this.resizeService.startAsync();
    }

    public void addNewGraph(BillHttpGraph billHttpGraph) {
        BillGraph billGraph = BillGraph.createBillGraph(billHttpGraph, Optional.<String>absent());
        startGraph(billGraph, billHttpGraph.getRefreshRate());
        billHttpGraphs.put(billGraph.getId(), billHttpGraph);
    }

    public void removeGraph(String id) {
        refresherMap.get(id).stopAsync();
        refresherMap.remove(id);
        billHttpGraphs.remove(id);
    }

    public void resizeGraph(BillResizeEvent billResizeEvent) {
        resizeService.process(billResizeEvent);
    }

    private void startGraph(BillGraph billGraph, int reload) {
        BillGraphRefresher billGraphRefresher = new BillGraphRefresher(this, billGraph, reload);
        billGraphRefresher.startAsync();
        refresherMap.put(billGraph.getId(), billGraphRefresher);
    }

    public void generateAllGraphsFromDisk() {
        for (Map.Entry<String, BillHttpGraph> next : billHttpGraphs.entrySet()) {
            if (next.getValue() != null) {
                startGraph(BillGraph.createBillGraph(next.getValue(), Optional.of(next.getKey())), next.getValue().getRefreshRate());
            }
        }
    }

    class ResizeService extends AbstractExecutionThreadService {
        private final LinkedBlockingQueue<BillResizeEvent> events;
        RemovalListener<String, BillResizeEvent> removalListener = new RemovalListener<String, BillResizeEvent>() {
            public void onRemoval(RemovalNotification<String, BillResizeEvent> removal) {
                try {
                    System.out.println(removal.getCause());
                    if (removal.getCause().equals(RemovalCause.EXPIRED)) {
                        refresherMap.get(removal.getValue().getId()).runOneIteration();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        Cache<String, BillResizeEvent> eventCache = CacheBuilder.newBuilder()
                .expireAfterWrite(1, TimeUnit.SECONDS)
                .removalListener(removalListener)
                .build();

        ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();

        public ResizeService() {
            this.events = new LinkedBlockingQueue<BillResizeEvent>();
            this.ses.scheduleWithFixedDelay(
                    new Runnable() {
                        public void run() {
                                eventCache.cleanUp();
                        }
                    }, 0, 500, TimeUnit.MILLISECONDS);
        }

        @Override
        protected void run() throws Exception {
            while (true) {
                BillResizeEvent event = events.take();
                BillHttpGraph billHttpGraph = billHttpGraphs.get(event.getId());
                if (billHttpGraph.getWidth() != event.getWidth() || billHttpGraph.getHeight() != event.getHeight()) {
                    System.out.println("found a difference updating resize cache.");
                    billHttpGraph.setWidth(event.getWidth());
                    billHttpGraph.setHeight(event.getHeight());
                    billHttpGraphs.put(event.getId(), billHttpGraph);
                    refresherMap.get(event.getId()).getBillGraph().reSize(event.getWidth(), event.getHeight());
                    eventCache.invalidate(event.getId());
                    eventCache.put(event.getId(), event);
                } else {
                    System.out.println("no difference detected, skipping");
                }
            }
        }

{'fraction_non_alphanumeric': 0.09816410640689396, 'fraction_numerical': 0.0006244536030972898, 'mean_word_length': 2.7438055165965403, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '5319469', 'n_tokens_mistral': 2172, 'n_tokens_neox': 2108, 'n_words': 378}