1 |
package dk.thoerup.traininfoservice; |
2 |
|
3 |
import java.util.Date; |
4 |
import java.util.concurrent.atomic.AtomicInteger; |
5 |
|
6 |
import org.apache.commons.lang.time.DurationFormatUtils; |
7 |
|
8 |
public class Statistics { |
9 |
|
10 |
private AtomicInteger stationLookupsLocation = new AtomicInteger(0); |
11 |
private AtomicInteger stationLookupsName = new AtomicInteger(0); |
12 |
private AtomicInteger stationLookupsFavorites = new AtomicInteger(0); |
13 |
private AtomicInteger departureLookups = new AtomicInteger(0); |
14 |
private AtomicInteger timetableLookups = new AtomicInteger(0); |
15 |
private AtomicInteger departureCacheHits = new AtomicInteger(0); |
16 |
private AtomicInteger timetableCacheHits = new AtomicInteger(0); |
17 |
|
18 |
private Date lastReset = new Date(); |
19 |
|
20 |
public void incrementStationLookupsLocation() { |
21 |
stationLookupsLocation.incrementAndGet(); |
22 |
} |
23 |
|
24 |
public void incrementStationLookupsName() { |
25 |
stationLookupsName.incrementAndGet(); |
26 |
} |
27 |
|
28 |
public void incrementStationLookupsFavorites() { |
29 |
stationLookupsFavorites.incrementAndGet(); |
30 |
} |
31 |
|
32 |
public void incrementDepartureLookups() { |
33 |
departureLookups.incrementAndGet(); |
34 |
} |
35 |
|
36 |
public void incrementTimetableLookups() { |
37 |
timetableLookups.incrementAndGet(); |
38 |
} |
39 |
|
40 |
public void incrementDepartureCacheHits() { |
41 |
departureCacheHits.incrementAndGet(); |
42 |
} |
43 |
|
44 |
public void incrementTimetableCacheHits() { |
45 |
timetableCacheHits.incrementAndGet(); |
46 |
} |
47 |
|
48 |
///////// |
49 |
|
50 |
public int getStationTotals() { |
51 |
return stationLookupsLocation.get() + stationLookupsName.get() + stationLookupsFavorites.get(); |
52 |
} |
53 |
|
54 |
public int getStationLookupsLocation() { |
55 |
return stationLookupsLocation.get(); |
56 |
} |
57 |
|
58 |
public int getStationLookupsName() { |
59 |
return stationLookupsName.get(); |
60 |
} |
61 |
|
62 |
public int getStationLookupsFavorites() { |
63 |
return stationLookupsFavorites.get(); |
64 |
} |
65 |
|
66 |
public int getDepartureLookups() { |
67 |
return departureLookups.get(); |
68 |
} |
69 |
|
70 |
public int getTimetableLookups() { |
71 |
return timetableLookups.get(); |
72 |
} |
73 |
|
74 |
public int getDepartureCacheHits() { |
75 |
return departureCacheHits.get(); |
76 |
} |
77 |
|
78 |
public int getTimetableCacheHits() { |
79 |
return timetableCacheHits.get(); |
80 |
} |
81 |
|
82 |
public Date getLastReset() { |
83 |
return lastReset; |
84 |
} |
85 |
|
86 |
|
87 |
/* helper functions */ |
88 |
|
89 |
public String getElapsedAsString() { |
90 |
Date now = new Date(); |
91 |
long duration = (now.getTime() - lastReset.getTime() ); |
92 |
|
93 |
return DurationFormatUtils.formatDuration(duration, "d, HH:mm:ss"); |
94 |
} |
95 |
|
96 |
public double getElapsedDays() { |
97 |
Date now = new Date(); |
98 |
long elapsedMs = now.getTime() - lastReset.getTime(); |
99 |
long elapsedHour = elapsedMs / (60*60*1000); |
100 |
|
101 |
double elapsedDay = ((double)elapsedHour) / 24.0; |
102 |
|
103 |
if (elapsedDay < 1.0) |
104 |
elapsedDay = 1.0; |
105 |
|
106 |
return elapsedDay; |
107 |
} |
108 |
|
109 |
|
110 |
/**** |
111 |
* Singleton stuff |
112 |
*/ |
113 |
|
114 |
private static Statistics instance = null; |
115 |
private Statistics() { |
116 |
// Exists only to defeat instantiation. |
117 |
} |
118 |
public static Statistics getInstance() { |
119 |
if(instance == null) { |
120 |
instance = new Statistics(); |
121 |
} |
122 |
return instance; |
123 |
} |
124 |
} |