22
22
*/
23
23
package org .opengrok .indexer .web ;
24
24
25
+ import com .fasterxml .jackson .annotation .JsonIgnore ;
26
+ import com .fasterxml .jackson .annotation .JsonProperty ;
27
+ import com .fasterxml .jackson .core .JsonProcessingException ;
28
+ import com .fasterxml .jackson .databind .ObjectMapper ;
29
+
30
+ import java .io .IOException ;
25
31
import java .util .Calendar ;
26
32
import java .util .Map ;
27
33
import java .util .TreeMap ;
28
34
import java .util .stream .Collectors ;
29
35
import java .util .stream .LongStream ;
30
- import org .json .simple .JSONArray ;
31
- import org .json .simple .JSONObject ;
32
36
33
37
/**
34
38
* Framework for statistics gathering. So far used only by the webapp.
@@ -51,18 +55,35 @@ public class Statistics {
51
55
protected static final String STATISTIC_DAY_HISTOGRAM = "day_histogram" ;
52
56
protected static final String STATISTIC_MONTH_HISTOGRAM = "month_histogram" ;
53
57
58
+ @ JsonProperty (STATISTIC_REQUEST_CATEGORIES )
54
59
private Map <String , Long > requestCategories = new TreeMap <>();
60
+ @ JsonProperty (STATISTIC_TIMING )
55
61
private Map <String , Long > timing = new TreeMap <>();
62
+ @ JsonProperty (STATISTIC_TIMING_MIN )
56
63
private Map <String , Long > timingMin = new TreeMap <>();
64
+ @ JsonProperty (STATISTIC_TIMING_MAX )
57
65
private Map <String , Long > timingMax = new TreeMap <>();
66
+ @ JsonProperty (STATISTIC_TIMING_AVG )
67
+ private Map <String , Double > timingAvg = new TreeMap <>();
68
+ @ JsonProperty (STATISTIC_DAY_HISTOGRAM )
58
69
private long [] dayHistogram = new long [24 ];
70
+ @ JsonProperty (STATISTIC_MONTH_HISTOGRAM )
59
71
private long [] monthHistogram = new long [31 ];
60
- private long timeStart = System . currentTimeMillis ();
72
+ @ JsonProperty ( STATISTIC_REQUESTS )
61
73
private long requests = 0 ;
74
+ @ JsonProperty (STATISTIC_MINUTES )
62
75
private long minutes = 1 ;
76
+ @ JsonProperty (STATISTIC_REQUESTS_PER_MINUTE )
63
77
private long requestsPerMinute = 0 ;
78
+ @ JsonProperty (STATISTIC_REQUESTS_PER_MINUTE_MIN )
64
79
private long requestsPerMinuteMin = Long .MAX_VALUE ;
80
+ @ JsonProperty (STATISTIC_REQUESTS_PER_MINUTE_MAX )
65
81
private long requestsPerMinuteMax = Long .MIN_VALUE ;
82
+ @ JsonProperty (STATISTIC_REQUESTS_PER_MINUTE_AVG )
83
+ private double requestsPerMinuteAvg = 0 ;
84
+
85
+ @ JsonIgnore
86
+ private long timeStart = System .currentTimeMillis ();
66
87
67
88
/**
68
89
* Adds a single request into all requests.
@@ -72,6 +93,7 @@ synchronized public void addRequest() {
72
93
73
94
requestsPerMinute ++;
74
95
requests ++;
96
+ requestsPerMinuteAvg = requests / (double ) minutes ;
75
97
76
98
if (requestsPerMinute > requestsPerMinuteMax ) {
77
99
requestsPerMinuteMax = requestsPerMinute ;
@@ -102,6 +124,7 @@ synchronized protected void maybeRefresh() {
102
124
* @param category category
103
125
*/
104
126
synchronized public void addRequest (String category ) {
127
+ maybeRefresh ();
105
128
Long val = requestCategories .get (category );
106
129
if (val == null ) {
107
130
val = 0L ;
@@ -146,6 +169,10 @@ synchronized public void addRequestTime(String category, long v) {
146
169
timing .put (category , val );
147
170
timingMin .put (category , min );
148
171
timingMax .put (category , max );
172
+
173
+ // Recompute the average for given category.
174
+ timingAvg .put (category ,
175
+ timing .get (category ).doubleValue () / requestCategories .get (category ));
149
176
}
150
177
151
178
public Map <String , Long > getRequestCategories () {
@@ -171,11 +198,6 @@ public Map<String, Long> getTimingMax() {
171
198
* @return map of averages for each category
172
199
*/
173
200
public Map <String , Double > getTimingAvg () {
174
- Map <String , Double > timingAvg = new TreeMap <>();
175
- for (Map .Entry <String , Long > entry : timing .entrySet ()) {
176
- timingAvg .put (entry .getKey (), entry .getValue ().doubleValue ()
177
- / requestCategories .get (entry .getKey ()));
178
- }
179
201
return timingAvg ;
180
202
}
181
203
@@ -195,6 +217,10 @@ synchronized public void setTimingMax(Map<String, Long> timing_max) {
195
217
this .timingMax = timing_max ;
196
218
}
197
219
220
+ synchronized public void setTimingAvg (Map <String , Double > timing_avg ) {
221
+ this .timingAvg = timing_avg ;
222
+ }
223
+
198
224
public long getTimeStart () {
199
225
return timeStart ;
200
226
}
@@ -212,7 +238,6 @@ synchronized public void setRequests(long requests) {
212
238
}
213
239
214
240
public long getMinutes () {
215
- maybeRefresh ();
216
241
return minutes ;
217
242
}
218
243
@@ -221,7 +246,6 @@ synchronized public void setMinutes(long minutes) {
221
246
}
222
247
223
248
public long getRequestsPerMinute () {
224
- maybeRefresh ();
225
249
return requestsPerMinute ;
226
250
}
227
251
@@ -252,8 +276,11 @@ synchronized public void setRequestsPerMinuteMax(long requestsPerMinuteMax) {
252
276
}
253
277
254
278
public double getRequestsPerMinuteAvg () {
255
- maybeRefresh ();
256
- return requests / (double ) minutes ;
279
+ return this .requestsPerMinuteAvg ;
280
+ }
281
+
282
+ synchronized public void setRequestsPerMinuteAvg (double value ) {
283
+ this .requestsPerMinuteAvg = value ;
257
284
}
258
285
259
286
public long [] getDayHistogram () {
@@ -273,116 +300,41 @@ synchronized public void setMonthHistogram(long[] monthHistogram) {
273
300
}
274
301
275
302
/**
276
- * Convert this statistics object into JSONObject.
303
+ * Convert this {@code Statistics} object into JSON
277
304
*
278
- * @return the json object
305
+ * @return the JSON string
279
306
*/
280
- public JSONObject toJson () {
307
+ public String toJson () throws JsonProcessingException {
281
308
return toJson (this );
282
309
}
283
310
284
311
/**
285
- * Convert JSONObject object into statistics .
312
+ * Convert JSON into {@code Statistics} object .
286
313
*
287
- * @param input object containing statistics
288
- * @return the statistics object
314
+ * @param jsonString String with JSON
315
+ * @return the {@code Statistics} object
289
316
*/
290
317
@ SuppressWarnings ("unchecked" )
291
- public static Statistics from (JSONObject input ) {
292
- Statistics stats = new Statistics ();
293
- Object o ;
294
- if ((o = input .get (STATISTIC_REQUEST_CATEGORIES )) != null ) {
295
- stats .setRequestCategories ((Map <String , Long >) o );
296
- }
297
- if ((o = input .get (STATISTIC_TIMING )) != null ) {
298
- stats .setTiming ((Map <String , Long >) o );
299
- }
300
- if ((o = input .get (STATISTIC_TIMING_MIN )) != null ) {
301
- stats .setTimingMin ((Map <String , Long >) o );
302
- }
303
- if ((o = input .get (STATISTIC_TIMING_MAX )) != null ) {
304
- stats .setTimingMax ((Map <String , Long >) o );
305
- }
306
- if ((o = input .get (STATISTIC_REQUESTS )) != null ) {
307
- stats .setRequests ((long ) o );
308
- }
309
- if ((o = input .get (STATISTIC_MINUTES )) != null ) {
310
- stats .setMinutes ((long ) o );
311
- }
312
- if ((o = input .get (STATISTIC_REQUESTS_PER_MINUTE )) != null ) {
313
- stats .setRequestsPerMinute ((long ) o );
314
- }
315
- if ((o = input .get (STATISTIC_REQUESTS_PER_MINUTE_MIN )) != null ) {
316
- stats .setRequestsPerMinuteMin ((long ) o );
317
- }
318
- if ((o = input .get (STATISTIC_REQUESTS_PER_MINUTE_MAX )) != null ) {
319
- stats .setRequestsPerMinuteMax ((long ) o );
320
- }
321
- if ((o = input .get (STATISTIC_DAY_HISTOGRAM )) != null ) {
322
- stats .setDayHistogram (convertJSONArrayToArray ((JSONArray ) o , stats .getDayHistogram ()));
323
- }
324
- if ((o = input .get (STATISTIC_MONTH_HISTOGRAM )) != null ) {
325
- stats .setMonthHistogram (convertJSONArrayToArray ((JSONArray ) o , stats .getMonthHistogram ()));
326
- }
318
+ public static Statistics fromJson (String jsonString ) throws IOException {
319
+ ObjectMapper mapper = new ObjectMapper ();
320
+ Statistics stats = mapper .readValue (jsonString , Statistics .class );
327
321
stats .setTimeStart (System .currentTimeMillis ());
322
+
328
323
return stats ;
329
324
}
330
325
331
326
/**
332
- * Convert statistics object into JSONObject .
327
+ * Convert statistics object into JSON .
333
328
*
334
329
* @param stats the statistics object
335
- * @return the json object or empty json object if there was no request
336
- */
337
- @ SuppressWarnings ("unchecked" )
338
- public static JSONObject toJson (Statistics stats ) {
339
- JSONObject output = new JSONObject ();
340
- if (stats .getRequests () == 0 ) {
341
- return output ;
342
- }
343
- output .put (STATISTIC_REQUEST_CATEGORIES , new JSONObject (stats .getRequestCategories ()));
344
- output .put (STATISTIC_TIMING , new JSONObject (stats .getTiming ()));
345
- output .put (STATISTIC_TIMING_MIN , new JSONObject (stats .getTimingMin ()));
346
- output .put (STATISTIC_TIMING_MAX , new JSONObject (stats .getTimingMax ()));
347
- output .put (STATISTIC_TIMING_AVG , new JSONObject (stats .getTimingAvg ()));
348
- output .put (STATISTIC_MINUTES , stats .getMinutes ());
349
- output .put (STATISTIC_REQUESTS , stats .getRequests ());
350
- output .put (STATISTIC_REQUESTS_PER_MINUTE , stats .getRequestsPerMinute ());
351
- output .put (STATISTIC_REQUESTS_PER_MINUTE_MIN , stats .getRequestsPerMinuteMin ());
352
- output .put (STATISTIC_REQUESTS_PER_MINUTE_MAX , stats .getRequestsPerMinuteMax ());
353
- output .put (STATISTIC_REQUESTS_PER_MINUTE_AVG , stats .getRequestsPerMinuteAvg ());
354
- output .put (STATISTIC_DAY_HISTOGRAM , convertArrayToJSONArray (stats .getDayHistogram ()));
355
- output .put (STATISTIC_MONTH_HISTOGRAM , convertArrayToJSONArray (stats .getMonthHistogram ()));
356
- return output ;
357
- }
358
-
359
- /**
360
- * Converts an array into json array.
361
- *
362
- * @param array the input array
363
- * @return the output json array
330
+ * @return String with JSON
364
331
*/
365
332
@ SuppressWarnings ("unchecked" )
366
- private static JSONArray convertArrayToJSONArray (long [] array ) {
367
- JSONArray ret = new JSONArray ();
368
- for (long o : array ) {
369
- ret .add (o );
370
- }
371
- return ret ;
372
- }
333
+ public static String toJson (Statistics stats ) throws JsonProcessingException {
334
+ ObjectMapper mapper = new ObjectMapper ();
335
+ String jsonStr = mapper .writeValueAsString (stats );
373
336
374
- /**
375
- * Converts an json array into an array.
376
- *
377
- * @param dest the input json array
378
- * @param target the output array
379
- * @return target
380
- */
381
- private static long [] convertJSONArrayToArray (JSONArray dest , long [] target ) {
382
- for (int i = 0 ; i < target .length && i < dest .size (); i ++) {
383
- target [i ] = (long ) dest .get (i );
384
- }
385
- return target ;
337
+ return jsonStr ;
386
338
}
387
339
388
340
@ Override
@@ -400,6 +352,5 @@ public String toString() {
400
352
+ "\n requestsPerMinuteAvg = " + getRequestsPerMinuteAvg ()
401
353
+ "\n dayHistogram = " + LongStream .of (getDayHistogram ()).mapToObj (a -> Long .toString (a )).map (a -> a .toString ()).collect (Collectors .joining (", " ))
402
354
+ "\n monthHistogram = " + LongStream .of (getMonthHistogram ()).mapToObj (a -> Long .toString (a )).map (a -> a .toString ()).collect (Collectors .joining (", " ));
403
-
404
355
}
405
356
}
0 commit comments