1 |
package dk.thoerup.traininfoservice; |
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; |
import java.util.Date; |
7 |
import java.util.concurrent.atomic.AtomicInteger; |
import java.util.concurrent.atomic.AtomicInteger; |
8 |
|
|
9 |
import org.apache.commons.lang.time.DurationFormatUtils; |
import org.apache.commons.lang.time.DurationFormatUtils; |
10 |
|
|
11 |
|
import dk.thoerup.traininfoservice.db.DBConnection; |
12 |
|
|
13 |
public class Statistics { |
public class Statistics { |
14 |
|
|
15 |
private AtomicInteger stationLookupsLocation = new AtomicInteger(0); |
private AtomicInteger stationLookupsLocation = new AtomicInteger(0); |
16 |
private AtomicInteger stationLookupsName = new AtomicInteger(0); |
private AtomicInteger stationLookupsName = new AtomicInteger(0); |
17 |
private AtomicInteger stationLookupsFavorites = new AtomicInteger(0); |
private AtomicInteger stationLookupsFavorites = new AtomicInteger(0); |
18 |
private AtomicInteger departureLookups = new AtomicInteger(0); |
private AtomicInteger departureLookups = new AtomicInteger(0); |
19 |
private AtomicInteger timetableLookups = new AtomicInteger(0); |
private AtomicInteger timetableLookups = new AtomicInteger(0); |
20 |
private AtomicInteger departureCacheHits = new AtomicInteger(0); |
private AtomicInteger departureCacheHits = new AtomicInteger(0); |
21 |
private AtomicInteger timetableCacheHits = new AtomicInteger(0); |
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(); |
private Date lastReset = new Date(); |
36 |
|
|
62 |
timetableCacheHits.incrementAndGet(); |
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() { |
public int getStationTotals() { |
104 |
return timetableCacheHits.get(); |
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() { |
public Date getLastReset() { |
116 |
return lastReset; |
return lastReset; |
117 |
} |
} |
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 |
* Singleton stuff |
239 |
*/ |
*/ |
240 |
|
|