Skip to content

Commit 64d460c

Browse files
committed
Remove items that are older than 2 minutes in the histogram.
Add didRemove callback in the index.html to display a rolling graph.
1 parent a5e52d1 commit 64d460c

2 files changed

Lines changed: 30 additions & 4 deletions

File tree

server/src/main/java/swim/tutorial/UnitAgent.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package swim.tutorial;
22

3+
import java.util.Iterator;
34
import swim.api.SwimLane;
45
import swim.api.agent.AbstractAgent;
56
import swim.api.lane.CommandLane;
@@ -17,9 +18,26 @@ public class UnitAgent extends AbstractAgent {
1718
.isTransient(true)
1819
.didUpdate((k,n,o) -> {
1920
logMessage("histogram: replaced " + k + "'s value to " + Recon.toString(n) + " from " + Recon.toString(o));
20-
this.histogram.drop(Math.max(0, this.histogram.size() - 100));
21+
dropOldData();
2122
});
2223

24+
25+
private void dropOldData() {
26+
final long now = System.currentTimeMillis();
27+
final Iterator<Long> iterator = histogram.keyIterator();
28+
while(iterator.hasNext()) {
29+
long key = iterator.next();
30+
if ((now - key) > 2*60*1000L) {
31+
// remove items that are older than 2 minutes
32+
histogram.remove(key);
33+
} else {
34+
// map is sorted by the sort order of the keys, so break out of the loop on the first
35+
// key that is newer than 2 minutes
36+
break;
37+
}
38+
}
39+
}
40+
2341
@SwimLane("history")
2442
protected final ListLane<Value> history = this.<Value>listLane()
2543
.isTransient(true)

ui/index.html

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,17 @@
101101
.strokeWidth(2);
102102
chart.addPlot(plot);
103103

104-
function updatePlots(key, value) {
104+
function addToPlot(key, value) {
105105
const time = key.numberValue();
106106
const v = value.get("count").numberValue(0);
107107
plot.insertDatum({x: time, y: v, opacity: void 0});
108108
}
109109

110+
function removeFromPlot(key) {
111+
const time = key.numberValue();
112+
plot.removeDatum(time);
113+
}
114+
110115
/* Data Subscriptions */
111116
const valueLink = swim.downlinkValue()
112117
.hostUri("warp://localhost:9001")
@@ -123,12 +128,15 @@
123128
})
124129
.open();
125130

126-
const historyLink = swim.downlinkMap()
131+
const histogramLink = swim.downlinkMap()
127132
.hostUri("warp://localhost:9001")
128133
.nodeUri("/unit/master")
129134
.laneUri("histogram")
130135
.didUpdate(function(key, value) {
131-
updatePlots(key, value);
136+
addToPlot(key, value);
137+
})
138+
.didRemove(function(key) {
139+
removeFromPlot(key);
132140
})
133141
.open();
134142
</script>

0 commit comments

Comments
 (0)