1 |
package dk.thoerup.traininfoservice; |
2 |
|
3 |
import java.sql.Connection; |
4 |
import java.sql.PreparedStatement; |
5 |
import java.sql.SQLException; |
6 |
import java.util.Date; |
7 |
import java.util.concurrent.atomic.AtomicInteger; |
8 |
|
9 |
import org.apache.commons.lang.time.DurationFormatUtils; |
10 |
|
11 |
import dk.thoerup.traininfoservice.db.DBConnection; |
12 |
|
13 |
public class Statistics { |
14 |
|
15 |
private AtomicInteger stationLookupsLocation = new AtomicInteger(0); |
16 |
private AtomicInteger stationLookupsName = new AtomicInteger(0); |
17 |
private AtomicInteger stationLookupsFavorites = new AtomicInteger(0); |
18 |
private AtomicInteger departureLookups = new AtomicInteger(0); |
19 |
private AtomicInteger timetableLookups = new AtomicInteger(0); |
20 |
private AtomicInteger departureCacheHits = new AtomicInteger(0); |
21 |
private AtomicInteger timetableCacheHits = new AtomicInteger(0); |
22 |
private AtomicInteger departureErrors = new AtomicInteger(0); |
23 |
private AtomicInteger timetableErrors = new AtomicInteger(0); |
24 |
|
25 |
private int old_stationLookupsLocation = 0; |
26 |
private int old_stationLookupsName = 0; |
27 |
private int old_stationLookupsFavorites = 0; |
28 |
private int old_departureLookups = 0; |
29 |
private int old_timetableLookups = 0; |
30 |
private int old_departureCacheHits = 0; |
31 |
private int old_timetableCacheHits = 0; |
32 |
private int old_departureErrors = 0; |
33 |
private int old_timetableErrors = 0; |
34 |
|
35 |
private Date lastReset = new Date(); |
36 |
|
37 |
public void incrementStationLookupsLocation() { |
38 |
stationLookupsLocation.incrementAndGet(); |
39 |
} |
40 |
|
41 |
public void incrementStationLookupsName() { |
42 |
stationLookupsName.incrementAndGet(); |
43 |
} |
44 |
|
45 |
public void incrementStationLookupsFavorites() { |
46 |
stationLookupsFavorites.incrementAndGet(); |
47 |
} |
48 |
|
49 |
public void incrementDepartureLookups() { |
50 |
departureLookups.incrementAndGet(); |
51 |
} |
52 |
|
53 |
public void incrementTimetableLookups() { |
54 |
timetableLookups.incrementAndGet(); |
55 |
} |
56 |
|
57 |
public void incrementDepartureCacheHits() { |
58 |
departureCacheHits.incrementAndGet(); |
59 |
} |
60 |
|
61 |
public void incrementTimetableCacheHits() { |
62 |
timetableCacheHits.incrementAndGet(); |
63 |
} |
64 |
|
65 |
public void incrementDepartureErrors() { |
66 |
departureErrors.incrementAndGet(); |
67 |
} |
68 |
|
69 |
public void incrementTimetableErrors() { |
70 |
timetableErrors.incrementAndGet(); |
71 |
} |
72 |
|
73 |
///////// |
74 |
|
75 |
public int getStationTotals() { |
76 |
return stationLookupsLocation.get() + stationLookupsName.get() + stationLookupsFavorites.get(); |
77 |
} |
78 |
|
79 |
public int getStationLookupsLocation() { |
80 |
return stationLookupsLocation.get(); |
81 |
} |
82 |
|
83 |
public int getStationLookupsName() { |
84 |
return stationLookupsName.get(); |
85 |
} |
86 |
|
87 |
public int getStationLookupsFavorites() { |
88 |
return stationLookupsFavorites.get(); |
89 |
} |
90 |
|
91 |
public int getDepartureLookups() { |
92 |
return departureLookups.get(); |
93 |
} |
94 |
|
95 |
public int getTimetableLookups() { |
96 |
return timetableLookups.get(); |
97 |
} |
98 |
|
99 |
public int getDepartureCacheHits() { |
100 |
return departureCacheHits.get(); |
101 |
} |
102 |
|
103 |
public int getTimetableCacheHits() { |
104 |
return timetableCacheHits.get(); |
105 |
} |
106 |
|
107 |
public int getDepartureErrors() { |
108 |
return departureErrors.get(); |
109 |
} |
110 |
|
111 |
public int getTimetableErrors() { |
112 |
return timetableErrors.get(); |
113 |
} |
114 |
|
115 |
public Date getLastReset() { |
116 |
return lastReset; |
117 |
} |
118 |
|
119 |
|
120 |
/* helper functions */ |
121 |
|
122 |
public String getElapsedAsString() { |
123 |
Date now = new Date(); |
124 |
long duration = (now.getTime() - lastReset.getTime() ); |
125 |
|
126 |
return DurationFormatUtils.formatDuration(duration, "d, HH:mm:ss"); |
127 |
} |
128 |
|
129 |
public double getElapsedDays() { |
130 |
Date now = new Date(); |
131 |
long elapsedMs = now.getTime() - lastReset.getTime(); |
132 |
long elapsedHour = elapsedMs / (60*60*1000); |
133 |
|
134 |
double elapsedDay = ((double)elapsedHour) / 24.0; |
135 |
|
136 |
if (elapsedDay < 1.0) |
137 |
elapsedDay = 1.0; |
138 |
|
139 |
return elapsedDay; |
140 |
} |
141 |
|
142 |
|
143 |
/******* |
144 |
* Save stats to DB |
145 |
*/ |
146 |
|
147 |
public void saveStats() { |
148 |
synchronized(this) { |
149 |
int loc = stationLookupsLocation.get(); |
150 |
int name = stationLookupsName.get(); |
151 |
int fav = stationLookupsFavorites.get(); |
152 |
int departure = departureLookups.get(); |
153 |
int depcache = departureCacheHits.get(); |
154 |
int deperror = departureErrors.get(); |
155 |
int timetable = timetableLookups.get(); |
156 |
int timecache = timetableCacheHits.get(); |
157 |
int timeerror = timetableErrors.get(); |
158 |
|
159 |
int diff_loc = loc - old_stationLookupsLocation; |
160 |
int diff_name = name - old_stationLookupsName; |
161 |
int diff_fav = fav - old_stationLookupsFavorites; |
162 |
|
163 |
int diff_departure = departure - old_departureLookups; |
164 |
int diff_depcache = depcache - old_departureCacheHits; |
165 |
int diff_deperror = deperror - old_departureErrors; |
166 |
|
167 |
int diff_timetable = timetable - old_timetableLookups; |
168 |
int diff_timecache = timecache - old_timetableCacheHits; |
169 |
int diff_timeerror = timeerror - old_timetableErrors; |
170 |
|
171 |
if ( diff_loc != 0 || diff_name != 0 || diff_fav != 0 || |
172 |
diff_departure != 0 || diff_timetable != 0 || diff_depcache != 0 || diff_timecache != 0 || |
173 |
diff_deperror != 0 || diff_timeerror != 0) { |
174 |
try { |
175 |
|
176 |
//System.out.println("Updating ..."); |
177 |
Connection conn = DBConnection.getConnection(); |
178 |
PreparedStatement stmt = conn.prepareStatement("UPDATE trainstatistics SET location=location+?, name=name+?, favorites=favorites+?, departure=departure+?, " + |
179 |
"depcache=depcache+?, deperror=deperror+?, timetable=timetable+?, timecache=timecache+?, timeerror=timeerror+? " + |
180 |
"WHERE statisticsdate=now()::date"); |
181 |
|
182 |
stmt.setInt(1, diff_loc); |
183 |
stmt.setInt(2, diff_name); |
184 |
stmt.setInt(3, diff_fav); |
185 |
stmt.setInt(4, diff_departure); |
186 |
stmt.setInt(5, diff_depcache); |
187 |
stmt.setInt(6, diff_deperror); |
188 |
stmt.setInt(7, diff_timetable); |
189 |
stmt.setInt(8, diff_timecache); |
190 |
stmt.setInt(9, diff_timeerror); |
191 |
|
192 |
int rowcount = stmt.executeUpdate(); |
193 |
|
194 |
stmt.close(); |
195 |
|
196 |
if (rowcount == 0) { |
197 |
|
198 |
//System.out.println("inserting new row ..."); |
199 |
stmt = conn.prepareStatement("INSERT INTO trainstatistics(statisticsdate,location,name,favorites,departure,depcache,deperror,timetable,timecache,timeerror) " + |
200 |
"values (now()::date, ?,?,?,?,?,?,?,?,?)" ); |
201 |
|
202 |
stmt.setInt(1, diff_loc); |
203 |
stmt.setInt(2, diff_name); |
204 |
stmt.setInt(3, diff_fav); |
205 |
stmt.setInt(4, diff_departure); |
206 |
stmt.setInt(5, diff_depcache); |
207 |
stmt.setInt(6, diff_deperror); |
208 |
stmt.setInt(7, diff_timetable); |
209 |
stmt.setInt(8, diff_timecache); |
210 |
stmt.setInt(9, diff_timeerror); |
211 |
|
212 |
stmt.executeUpdate(); |
213 |
} |
214 |
|
215 |
|
216 |
old_stationLookupsLocation = loc; |
217 |
old_stationLookupsName = name; |
218 |
old_stationLookupsFavorites = fav; |
219 |
old_departureLookups = departure; |
220 |
old_departureCacheHits = depcache; |
221 |
old_departureErrors = deperror; |
222 |
old_timetableLookups = timetable; |
223 |
old_timetableCacheHits = timecache; |
224 |
old_timetableErrors = timeerror; |
225 |
|
226 |
conn.close(); |
227 |
|
228 |
|
229 |
} catch (SQLException sqle) { |
230 |
|
231 |
} |
232 |
} |
233 |
|
234 |
} |
235 |
} |
236 |
|
237 |
/******* |
238 |
* Singleton stuff |
239 |
*/ |
240 |
|
241 |
private static Statistics instance = null; |
242 |
private Statistics() { |
243 |
// Exists only to defeat instantiation. |
244 |
} |
245 |
public static Statistics getInstance() { |
246 |
if(instance == null) { |
247 |
instance = new Statistics(); |
248 |
} |
249 |
return instance; |
250 |
} |
251 |
} |