@@ -60,17 +60,14 @@ void updateTrafficAndReadPerDay() {
6060 try {
6161 final DateTime today = getDayBucket (now );
6262
63- // Record traffic for each 10-minute interval of the current day
64- // At 08:20, we have completed 50 intervals (0-49) and are in the 51st interval
65- // 8 hours * 6 (intervals/hour) + 2 intervals (10, 20) = 50 intervals total
66- int currentInterval = (now .hourOfDay ().get () * 6 ) + (now .minuteOfHour ().get () / 10 );
67- IntStream .rangeClosed (0 , currentInterval ).forEach (interval ->
68- service .updateTraffic (today .plusMinutes (interval * 10 ), nodeId , 1 , 1 , 1 ));
63+ // Record traffic for each hour of the current day - 9 hours (it's 08:20, so we are in the 9th hour of the day)
64+ IntStream .rangeClosed (0 , now .hourOfDay ().get ()).forEach (hour ->
65+ service .updateTraffic (today .plusHours (hour ), nodeId , 1 , 1 , 1 ));
6966
70- // Record traffic for all previous days - 30 days x 144 ten-minute intervals per day
67+ // Record traffic for all previous days - 30 x 24 hours
7168 IntStream .rangeClosed (1 , 30 ).forEach (day ->
72- IntStream .rangeClosed (0 , 143 ).forEach (interval ->
73- service .updateTraffic (today .minusDays (day ).plusMinutes ( interval * 10 ), nodeId , 1 , 1 , 1 )));
69+ IntStream .rangeClosed (0 , 23 ).forEach (hour ->
70+ service .updateTraffic (today .minusDays (day ).plusHours ( hour ), nodeId , 1 , 1 , 1 )));
7471
7572 // Verify that today is included from the histogram.
7673 final TrafficCounterService .TrafficHistogram trafficHistogramIncludesToday =
@@ -104,68 +101,23 @@ private static void verifyDayTrafficVolume(TrafficCounterService.TrafficHistogra
104101 final ImmutableList <Long > outputValues = ImmutableList .copyOf (histogram .values ());
105102
106103 // Check that we got the expected count for each of the previous days. We should get the full counter
107- // for the complete day. (144 ten-minute intervals per day )
104+ // for the complete day. (24 )
108105 for (int i = 0 ; i < 30 ; i ++) {
109106 assertThat (outputValues .get (i ))
110107 .withFailMessage ("Value <%s> is not the expected value - expected=%s but got=%s" ,
111- i , 144 , outputValues .get (i ))
112- .isEqualTo (144 );
108+ i , 24 , outputValues .get (i ))
109+ .isEqualTo (24 );
113110 }
114111
115- // Check that we got the correct count for the current day. At 08:20, we have 51 ten-minute intervals
116- // (8 hours * 6 intervals/hour + 2 intervals for 00, 10, 20 = 51 intervals)
112+ // Check that we got the correct count for the current day. The current day is only in its 9th hour,
113+ // so we should only get a value of 9.
117114 assertThat (outputValues .get (30 ))
118115 .withFailMessage ("Value <%s> is not the expected value - expected=%s but got=%s" ,
119- 30 , 51 , outputValues .get (30 ))
120- .isEqualTo (51 );
116+ 30 , 9 , outputValues .get (30 ))
117+ .isEqualTo (9 );
121118 });
122119 }
123120
124- @ Test
125- void updateTrafficAndReadPerTenMinutes () {
126- // Make sure we use a fixed time for the test
127- final DateTime now = DateTime .parse ("2017-10-29T08:20:00.000Z" );
128- DateTimeUtils .setCurrentMillisProvider (new InstantMillisProvider (now ));
129-
130- try {
131- final DateTime today = getDayBucket (now );
132-
133- // Record traffic for each 10-minute interval of the current day
134- int currentInterval = (now .hourOfDay ().get () * 6 ) + (now .minuteOfHour ().get () / 10 );
135- IntStream .rangeClosed (0 , currentInterval ).forEach (interval ->
136- service .updateTraffic (today .plusMinutes (interval * 10 ), nodeId , 1 , 1 , 1 ));
137-
138- // Record traffic for all previous days - 30 days x 144 ten-minute intervals per day
139- IntStream .rangeClosed (1 , 30 ).forEach (day ->
140- IntStream .rangeClosed (0 , 143 ).forEach (interval ->
141- service .updateTraffic (today .minusDays (day ).plusMinutes (interval * 10 ), nodeId , 1 , 1 , 1 )));
142-
143- final TrafficCounterService .TrafficHistogram histogramIncludesToday =
144- service .clusterTrafficOfLastDays (Duration .standardDays (30 ), TrafficCounterService .Interval .TEN_MINUTE , true );
145- assertThat (histogramIncludesToday .from ()).isEqualTo (getDayBucket (now ).minusDays (30 ));
146- assertThat (histogramIncludesToday .to ()).isEqualTo (now );
147-
148- // We should have 4371 entries: 30 days (30x144 = 4320) + current day (51 ten-minute intervals)
149- assertThat (histogramIncludesToday .input ()).hasSize (4371 );
150- assertThat (histogramIncludesToday .decoded ()).hasSize (4371 );
151- assertThat (histogramIncludesToday .output ()).hasSize (4371 );
152- verifyTenMinuteTraffic (histogramIncludesToday , 4371 );
153-
154- final TrafficCounterService .TrafficHistogram histogramExcludesToday =
155- service .clusterTrafficOfLastDays (Duration .standardDays (30 ), TrafficCounterService .Interval .TEN_MINUTE , false );
156- assertThat (histogramExcludesToday .from ()).isEqualTo (getDayBucket (now ).minusDays (30 ));
157- assertThat (histogramExcludesToday .to ()).isEqualTo (getDayBucket (now ).minusMillis (1 ));
158-
159- // We should have 4320 entries: 30 days x 144 ten-minute intervals per day (no current day)
160- assertThat (histogramExcludesToday .input ()).hasSize (4320 );
161- assertThat (histogramExcludesToday .decoded ()).hasSize (4320 );
162- assertThat (histogramExcludesToday .output ()).hasSize (4320 );
163- verifyTenMinuteTraffic (histogramExcludesToday , 4320 );
164- } finally {
165- DateTimeUtils .setCurrentMillisSystem ();
166- }
167- }
168-
169121 @ Test
170122 void updateTrafficAndReadPerHour () {
171123 // Make sure we use a fixed time for the test
@@ -175,50 +127,47 @@ void updateTrafficAndReadPerHour() {
175127 try {
176128 final DateTime today = getDayBucket (now );
177129
178- // Record traffic for each 10-minute interval of the current day
179- int currentInterval = (now .hourOfDay ().get () * 6 ) + (now .minuteOfHour ().get () / 10 );
180- IntStream .rangeClosed (0 , currentInterval ).forEach (interval ->
181- service .updateTraffic (today .plusMinutes (interval * 10 ), nodeId , 1 , 1 , 1 ));
130+ // Record traffic for each hour of the current day (now)
131+ IntStream .rangeClosed (0 , now .hourOfDay ().get ()).forEach (hour ->
132+ service .updateTraffic (today .plusHours (hour ), nodeId , 1 , 1 , 1 ));
182133
183- // Record traffic for all previous days - 30 days x 144 ten-minute intervals per day
134+ // Record traffic for all previous days
184135 IntStream .rangeClosed (1 , 30 ).forEach (day ->
185- IntStream .rangeClosed (0 , 143 ).forEach (interval ->
186- service .updateTraffic (today .minusDays (day ).plusMinutes ( interval * 10 ), nodeId , 1 , 1 , 1 )));
136+ IntStream .rangeClosed (0 , 23 ).forEach (hour ->
137+ service .updateTraffic (today .minusDays (day ).plusHours ( hour ), nodeId , 1 , 1 , 1 )));
187138
188- // Test HOURLY aggregation - should aggregate 10-minute buckets into hourly buckets
189139 final TrafficCounterService .TrafficHistogram histogramIncludesToday =
190140 service .clusterTrafficOfLastDays (Duration .standardDays (30 ), TrafficCounterService .Interval .HOURLY , true );
191141 assertThat (histogramIncludesToday .from ()).isEqualTo (getDayBucket (now ).minusDays (30 ));
192142 assertThat (histogramIncludesToday .to ()).isEqualTo (now );
193143
194- // We should have 729 entries: 30 days (30x24 = 720) + current day (9 hours: 0-8)
195- // Each hour aggregates 6 ten-minute intervals
144+ // We should have 729 entries, one for each hour in the 30 days (30x24) history and the current day (9 hours)
196145 assertThat (histogramIncludesToday .input ()).hasSize (729 );
197146 assertThat (histogramIncludesToday .decoded ()).hasSize (729 );
198147 assertThat (histogramIncludesToday .output ()).hasSize (729 );
199- verifyHourlyAggregatedTraffic (histogramIncludesToday , 729 , true );
148+ verifyHourTraffic (histogramIncludesToday , 729 );
200149
201150 final TrafficCounterService .TrafficHistogram histogramExcludesToday =
202151 service .clusterTrafficOfLastDays (Duration .standardDays (30 ), TrafficCounterService .Interval .HOURLY , false );
203152 assertThat (histogramExcludesToday .from ()).isEqualTo (getDayBucket (now ).minusDays (30 ));
204153 assertThat (histogramExcludesToday .to ()).isEqualTo (getDayBucket (now ).minusMillis (1 ));
205154
206- // We should have 720 entries: 30 days x 24 hours per day (no current day)
155+ // We should have 720 entries, one for each hour in the 30 days (30x24) history and none for the current day.
207156 assertThat (histogramExcludesToday .input ()).hasSize (720 );
208157 assertThat (histogramExcludesToday .decoded ()).hasSize (720 );
209158 assertThat (histogramExcludesToday .output ()).hasSize (720 );
210- verifyHourlyAggregatedTraffic (histogramExcludesToday , 720 , false );
159+ verifyHourTraffic (histogramExcludesToday , 720 );
211160 } finally {
212161 DateTimeUtils .setCurrentMillisSystem ();
213162 }
214163 }
215164
216- private static void verifyTenMinuteTraffic (TrafficCounterService .TrafficHistogram trafficHistogram , int bucketCount ) {
165+ private static void verifyHourTraffic (TrafficCounterService .TrafficHistogram trafficHistogram , int bucketCount ) {
217166 // For each type of traffic, check that we got the correct values
218167 ImmutableList .of (trafficHistogram .input (), trafficHistogram .decoded (), trafficHistogram .output ()).forEach (histogram -> {
219168 final ImmutableList <Long > outputValues = ImmutableList .copyOf (histogram .values ());
220169
221- // Check that we got the expected count for each 10-minute interval . We should get one value per interval . (1)
170+ // Check that we got the expected count for each of the previous days . We should get one value per hour . (1)
222171 for (int i = 0 ; i < bucketCount ; i ++) {
223172 assertThat (outputValues .get (i ))
224173 .withFailMessage ("Value <%s> is not the expected value - expected=%s but got=%s" ,
@@ -227,38 +176,4 @@ private static void verifyTenMinuteTraffic(TrafficCounterService.TrafficHistogra
227176 }
228177 });
229178 }
230-
231- private static void verifyHourlyAggregatedTraffic (TrafficCounterService .TrafficHistogram trafficHistogram ,
232- int bucketCount ,
233- boolean includesPartialCurrentHour ) {
234- // For each type of traffic, check that we got the correct values
235- ImmutableList .of (trafficHistogram .input (), trafficHistogram .decoded (), trafficHistogram .output ()).forEach (histogram -> {
236- final ImmutableList <Long > outputValues = ImmutableList .copyOf (histogram .values ());
237-
238- if (includesPartialCurrentHour ) {
239- // Check that we got the expected count for each complete hourly bucket
240- // Each complete hour should have 6 (ten-minute intervals aggregated)
241- for (int i = 0 ; i < bucketCount - 1 ; i ++) {
242- assertThat (outputValues .get (i ))
243- .withFailMessage ("Value <%s> is not the expected value - expected=%s but got=%s" ,
244- i , 6 , outputValues .get (i ))
245- .isEqualTo (6 );
246- }
247-
248- // The last bucket (current hour 08:00-08:59 at 08:20) has only 3 intervals: 08:00, 08:10, 08:20
249- assertThat (outputValues .get (bucketCount - 1 ))
250- .withFailMessage ("Value <%s> is not the expected value - expected=%s but got=%s" ,
251- bucketCount - 1 , 3 , outputValues .get (bucketCount - 1 ))
252- .isEqualTo (3 );
253- } else {
254- // All hours are complete, each should have 6 ten-minute intervals
255- for (int i = 0 ; i < bucketCount ; i ++) {
256- assertThat (outputValues .get (i ))
257- .withFailMessage ("Value <%s> is not the expected value - expected=%s but got=%s" ,
258- i , 6 , outputValues .get (i ))
259- .isEqualTo (6 );
260- }
261- }
262- });
263- }
264179}
0 commit comments