File tree Expand file tree Collapse file tree
server/src/main/java/swim/tutorial Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package swim .tutorial ;
22
3+ import java .util .Iterator ;
34import swim .api .SwimLane ;
45import swim .api .agent .AbstractAgent ;
56import 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 )
Original file line number Diff line number Diff line change 101101 . strokeWidth ( 2 ) ;
102102chart . 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 */
111116const valueLink = swim . downlinkValue ( )
112117 . hostUri ( "warp://localhost:9001" )
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 >
You can’t perform that action at this time.
0 commit comments