synchronized Stat createStat(String name, TimeWindow[] windows) { if (statistics.get(name) != null) { throw new RuntimeException("Stat with name "+ name + " is already defined"); } Map<TimeWindow, TimeStat> timeStats = new LinkedHashMap<TimeWindow, TimeStat>(); for (TimeWindow window : windows) { StatUpdater collector = updaters.get(window); if (collector == null) { if(SINCE_START.equals(window)) { collector = new StatUpdater(); } else { collector = new TimeWindowStatUpdater(window, period); } updaters.put(window, collector); } TimeStat timeStat = new TimeStat(); collector.addTimeStat(name, timeStat); timeStats.put(window, timeStat); } Stat stat = new Stat(name, timeStats); statistics.put(name, stat); return stat; }
synchronized void update() { for (TimeStat stat : statToCollect.values()) { stat.updates++; if (stat.updates == updatesPerBucket) { stat.addBucket(); stat.updates = 0; } if (stat.buckets.size() > collectBuckets) { stat.removeBucket(); } } }
private Stat(String name, Map<TimeWindow, TimeStat> timeStats) { this.name = name; this.timeStats = timeStats; }
public synchronized void inc(int incr) { for (TimeStat ts : timeStats.values()) { ts.inc(incr); } }
public synchronized Map<TimeWindow, TimeStat> getValues() { return Collections.unmodifiableMap(timeStats); }
synchronized void addTimeStat(String name, TimeStat s) { statToCollect.put(name, s); }
synchronized TimeStat removeTimeStat(String name) { return statToCollect.remove(name); }
synchronized void update() { for (TimeStat stat : statToCollect.values()) { stat.setValueToCurrent(); } }