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 |
|
|
20 |
private AtomicInteger departureErrors = new AtomicInteger(0); |
private AtomicInteger departureErrors = new AtomicInteger(0); |
21 |
private AtomicInteger timetableErrors = new AtomicInteger(0); |
private AtomicInteger timetableErrors = new AtomicInteger(0); |
22 |
|
|
23 |
|
private int old_stationLookupsLocation = 0; |
24 |
|
private int old_stationLookupsName = 0; |
25 |
|
private int old_stationLookupsFavorites = 0; |
26 |
|
private int old_departureLookups = 0; |
27 |
|
private int old_timetableLookups = 0; |
28 |
|
private int old_departureCacheHits = 0; |
29 |
|
private int old_timetableCacheHits = 0; |
30 |
|
private int old_departureErrors = 0; |
31 |
|
private int old_timetableErrors = 0; |
32 |
|
|
33 |
private Date lastReset = new Date(); |
private Date lastReset = new Date(); |
34 |
|
|
35 |
public void incrementStationLookupsLocation() { |
public void incrementStationLookupsLocation() { |
138 |
} |
} |
139 |
|
|
140 |
|
|
141 |
/**** |
/******* |
142 |
|
* Save stats to DB |
143 |
|
*/ |
144 |
|
|
145 |
|
public void saveStats() { |
146 |
|
synchronized(this) { |
147 |
|
int loc = stationLookupsLocation.get(); |
148 |
|
int name = stationLookupsName.get(); |
149 |
|
int fav = stationLookupsFavorites.get(); |
150 |
|
int departure = departureLookups.get(); |
151 |
|
int depcache = departureCacheHits.get(); |
152 |
|
int deperror = departureErrors.get(); |
153 |
|
int timetable = timetableLookups.get(); |
154 |
|
int timecache = timetableCacheHits.get(); |
155 |
|
int timeerror = timetableErrors.get(); |
156 |
|
|
157 |
|
int diff_loc = loc - old_stationLookupsLocation; |
158 |
|
int diff_name = name - old_stationLookupsName; |
159 |
|
int diff_fav = fav - old_stationLookupsFavorites; |
160 |
|
|
161 |
|
int diff_departure = departure - old_departureLookups; |
162 |
|
int diff_depcache = depcache - old_departureCacheHits; |
163 |
|
int diff_deperror = deperror - old_departureErrors; |
164 |
|
|
165 |
|
int diff_timetable = timetable - old_timetableLookups; |
166 |
|
int diff_timecache = timecache - old_timetableCacheHits; |
167 |
|
int diff_timeerror = timeerror - old_timetableErrors; |
168 |
|
|
169 |
|
if ( diff_loc != 0 || diff_name != 0 || diff_fav != 0 || |
170 |
|
diff_departure != 0 || diff_timetable != 0 || diff_depcache != 0 || diff_timecache != 0 || |
171 |
|
diff_deperror != 0 || diff_timeerror != 0) { |
172 |
|
try { |
173 |
|
|
174 |
|
//System.out.println("Updating ..."); |
175 |
|
Connection conn = DBConnection.getConnection(); |
176 |
|
PreparedStatement stmt = conn.prepareStatement("UPDATE trainstatistics SET location=location+?, name=name+?, favorites=favorites+?, departure=departure+?, " + |
177 |
|
"depcache=depcache+?, deperror=deperror+?, timetable=timetable+?, timecache=timecache+?, timeerror=timeerror+? " + |
178 |
|
"WHERE statisticsdate=now()::date"); |
179 |
|
|
180 |
|
stmt.setInt(1, diff_loc); |
181 |
|
stmt.setInt(2, diff_name); |
182 |
|
stmt.setInt(3, diff_fav); |
183 |
|
stmt.setInt(4, diff_departure); |
184 |
|
stmt.setInt(5, diff_depcache); |
185 |
|
stmt.setInt(6, diff_deperror); |
186 |
|
stmt.setInt(7, diff_timetable); |
187 |
|
stmt.setInt(8, diff_timecache); |
188 |
|
stmt.setInt(9, diff_timeerror); |
189 |
|
|
190 |
|
int rowcount = stmt.executeUpdate(); |
191 |
|
|
192 |
|
stmt.close(); |
193 |
|
|
194 |
|
if (rowcount == 0) { |
195 |
|
|
196 |
|
//System.out.println("inserting new row ..."); |
197 |
|
stmt = conn.prepareStatement("INSERT INTO trainstatistics(statisticsdate,location,name,favorites,departure,depcache,deperror,timetable,timecache,timeerror) " + |
198 |
|
"values (now()::date, ?,?,?,?,?,?,?,?,?)" ); |
199 |
|
|
200 |
|
stmt.setInt(1, diff_loc); |
201 |
|
stmt.setInt(2, diff_name); |
202 |
|
stmt.setInt(3, diff_fav); |
203 |
|
stmt.setInt(4, diff_departure); |
204 |
|
stmt.setInt(5, diff_depcache); |
205 |
|
stmt.setInt(6, diff_deperror); |
206 |
|
stmt.setInt(7, diff_timetable); |
207 |
|
stmt.setInt(8, diff_timecache); |
208 |
|
stmt.setInt(9, diff_timeerror); |
209 |
|
|
210 |
|
stmt.executeUpdate(); |
211 |
|
} |
212 |
|
|
213 |
|
|
214 |
|
old_stationLookupsLocation = loc; |
215 |
|
old_stationLookupsName = name; |
216 |
|
old_stationLookupsFavorites = fav; |
217 |
|
old_departureLookups = departure; |
218 |
|
old_departureCacheHits = depcache; |
219 |
|
old_departureErrors = deperror; |
220 |
|
old_timetableLookups = timetable; |
221 |
|
old_timetableCacheHits = timecache; |
222 |
|
old_timetableErrors = timeerror; |
223 |
|
|
224 |
|
conn.close(); |
225 |
|
|
226 |
|
|
227 |
|
} catch (SQLException sqle) { |
228 |
|
|
229 |
|
} |
230 |
|
} |
231 |
|
|
232 |
|
} |
233 |
|
} |
234 |
|
|
235 |
|
/******* |
236 |
* Singleton stuff |
* Singleton stuff |
237 |
*/ |
*/ |
238 |
|
|