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 |
private AtomicInteger departureErrors = new AtomicInteger(0); |
18 |
private AtomicInteger timetableErrors = new AtomicInteger(0); |
19 |
|
20 |
private Date lastReset = new Date(); |
21 |
|
22 |
public void incrementStationLookupsLocation() { |
23 |
stationLookupsLocation.incrementAndGet(); |
24 |
} |
25 |
|
26 |
public void incrementStationLookupsName() { |
27 |
stationLookupsName.incrementAndGet(); |
28 |
} |
29 |
|
30 |
public void incrementStationLookupsFavorites() { |
31 |
stationLookupsFavorites.incrementAndGet(); |
32 |
} |
33 |
|
34 |
public void incrementDepartureLookups() { |
35 |
departureLookups.incrementAndGet(); |
36 |
} |
37 |
|
38 |
public void incrementTimetableLookups() { |
39 |
timetableLookups.incrementAndGet(); |
40 |
} |
41 |
|
42 |
public void incrementDepartureCacheHits() { |
43 |
departureCacheHits.incrementAndGet(); |
44 |
} |
45 |
|
46 |
public void incrementTimetableCacheHits() { |
47 |
timetableCacheHits.incrementAndGet(); |
48 |
} |
49 |
|
50 |
public void incrementDepartureErrors() { |
51 |
departureErrors.incrementAndGet(); |
52 |
} |
53 |
|
54 |
public void incrementTimetableErrors() { |
55 |
timetableErrors.incrementAndGet(); |
56 |
} |
57 |
|
58 |
///////// |
59 |
|
60 |
public int getStationTotals() { |
61 |
return stationLookupsLocation.get() + stationLookupsName.get() + stationLookupsFavorites.get(); |
62 |
} |
63 |
|
64 |
public int getStationLookupsLocation() { |
65 |
return stationLookupsLocation.get(); |
66 |
} |
67 |
|
68 |
public int getStationLookupsName() { |
69 |
return stationLookupsName.get(); |
70 |
} |
71 |
|
72 |
public int getStationLookupsFavorites() { |
73 |
return stationLookupsFavorites.get(); |
74 |
} |
75 |
|
76 |
public int getDepartureLookups() { |
77 |
return departureLookups.get(); |
78 |
} |
79 |
|
80 |
public int getTimetableLookups() { |
81 |
return timetableLookups.get(); |
82 |
} |
83 |
|
84 |
public int getDepartureCacheHits() { |
85 |
return departureCacheHits.get(); |
86 |
} |
87 |
|
88 |
public int getTimetableCacheHits() { |
89 |
return timetableCacheHits.get(); |
90 |
} |
91 |
|
92 |
public int getDepartureErrors() { |
93 |
return departureErrors.get(); |
94 |
} |
95 |
|
96 |
public int getTimetableErrors() { |
97 |
return timetableErrors.get(); |
98 |
} |
99 |
|
100 |
public Date getLastReset() { |
101 |
return lastReset; |
102 |
} |
103 |
|
104 |
|
105 |
/* helper functions */ |
106 |
|
107 |
public String getElapsedAsString() { |
108 |
Date now = new Date(); |
109 |
long duration = (now.getTime() - lastReset.getTime() ); |
110 |
|
111 |
return DurationFormatUtils.formatDuration(duration, "d, HH:mm:ss"); |
112 |
} |
113 |
|
114 |
public double getElapsedDays() { |
115 |
Date now = new Date(); |
116 |
long elapsedMs = now.getTime() - lastReset.getTime(); |
117 |
long elapsedHour = elapsedMs / (60*60*1000); |
118 |
|
119 |
double elapsedDay = ((double)elapsedHour) / 24.0; |
120 |
|
121 |
if (elapsedDay < 1.0) |
122 |
elapsedDay = 1.0; |
123 |
|
124 |
return elapsedDay; |
125 |
} |
126 |
|
127 |
|
128 |
/**** |
129 |
* Singleton stuff |
130 |
*/ |
131 |
|
132 |
private static Statistics instance = null; |
133 |
private Statistics() { |
134 |
// Exists only to defeat instantiation. |
135 |
} |
136 |
public static Statistics getInstance() { |
137 |
if(instance == null) { |
138 |
instance = new Statistics(); |
139 |
} |
140 |
return instance; |
141 |
} |
142 |
} |