1 |
torben |
711 |
package dk.thoerup.traininfoservice; |
2 |
|
|
|
3 |
torben |
811 |
import java.sql.Connection; |
4 |
|
|
import java.sql.PreparedStatement; |
5 |
|
|
import java.sql.SQLException; |
6 |
torben |
711 |
import java.util.Date; |
7 |
|
|
import java.util.concurrent.atomic.AtomicInteger; |
8 |
|
|
|
9 |
|
|
import org.apache.commons.lang.time.DurationFormatUtils; |
10 |
|
|
|
11 |
|
|
public class Statistics { |
12 |
|
|
|
13 |
|
|
private AtomicInteger stationLookupsLocation = new AtomicInteger(0); |
14 |
|
|
private AtomicInteger stationLookupsName = new AtomicInteger(0); |
15 |
|
|
private AtomicInteger stationLookupsFavorites = new AtomicInteger(0); |
16 |
|
|
private AtomicInteger departureLookups = new AtomicInteger(0); |
17 |
torben |
716 |
private AtomicInteger timetableLookups = new AtomicInteger(0); |
18 |
torben |
711 |
private AtomicInteger departureCacheHits = new AtomicInteger(0); |
19 |
|
|
private AtomicInteger timetableCacheHits = new AtomicInteger(0); |
20 |
torben |
716 |
private AtomicInteger departureErrors = new AtomicInteger(0); |
21 |
|
|
private AtomicInteger timetableErrors = new AtomicInteger(0); |
22 |
torben |
711 |
|
23 |
torben |
811 |
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 |
torben |
711 |
private Date lastReset = new Date(); |
34 |
|
|
|
35 |
|
|
public void incrementStationLookupsLocation() { |
36 |
|
|
stationLookupsLocation.incrementAndGet(); |
37 |
|
|
} |
38 |
|
|
|
39 |
|
|
public void incrementStationLookupsName() { |
40 |
|
|
stationLookupsName.incrementAndGet(); |
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
public void incrementStationLookupsFavorites() { |
44 |
|
|
stationLookupsFavorites.incrementAndGet(); |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
public void incrementDepartureLookups() { |
48 |
|
|
departureLookups.incrementAndGet(); |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
public void incrementTimetableLookups() { |
52 |
|
|
timetableLookups.incrementAndGet(); |
53 |
|
|
} |
54 |
|
|
|
55 |
|
|
public void incrementDepartureCacheHits() { |
56 |
|
|
departureCacheHits.incrementAndGet(); |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
public void incrementTimetableCacheHits() { |
60 |
|
|
timetableCacheHits.incrementAndGet(); |
61 |
|
|
} |
62 |
|
|
|
63 |
torben |
716 |
public void incrementDepartureErrors() { |
64 |
|
|
departureErrors.incrementAndGet(); |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
public void incrementTimetableErrors() { |
68 |
|
|
timetableErrors.incrementAndGet(); |
69 |
|
|
} |
70 |
|
|
|
71 |
torben |
711 |
///////// |
72 |
|
|
|
73 |
|
|
public int getStationTotals() { |
74 |
|
|
return stationLookupsLocation.get() + stationLookupsName.get() + stationLookupsFavorites.get(); |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
public int getStationLookupsLocation() { |
78 |
|
|
return stationLookupsLocation.get(); |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
public int getStationLookupsName() { |
82 |
|
|
return stationLookupsName.get(); |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
public int getStationLookupsFavorites() { |
86 |
|
|
return stationLookupsFavorites.get(); |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
public int getDepartureLookups() { |
90 |
|
|
return departureLookups.get(); |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
public int getTimetableLookups() { |
94 |
|
|
return timetableLookups.get(); |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
public int getDepartureCacheHits() { |
98 |
|
|
return departureCacheHits.get(); |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
public int getTimetableCacheHits() { |
102 |
|
|
return timetableCacheHits.get(); |
103 |
|
|
} |
104 |
|
|
|
105 |
torben |
716 |
public int getDepartureErrors() { |
106 |
|
|
return departureErrors.get(); |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
public int getTimetableErrors() { |
110 |
|
|
return timetableErrors.get(); |
111 |
|
|
} |
112 |
|
|
|
113 |
torben |
711 |
public Date getLastReset() { |
114 |
|
|
return lastReset; |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
|
118 |
|
|
/* helper functions */ |
119 |
|
|
|
120 |
|
|
public String getElapsedAsString() { |
121 |
|
|
Date now = new Date(); |
122 |
|
|
long duration = (now.getTime() - lastReset.getTime() ); |
123 |
|
|
|
124 |
|
|
return DurationFormatUtils.formatDuration(duration, "d, HH:mm:ss"); |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
public double getElapsedDays() { |
128 |
|
|
Date now = new Date(); |
129 |
|
|
long elapsedMs = now.getTime() - lastReset.getTime(); |
130 |
|
|
long elapsedHour = elapsedMs / (60*60*1000); |
131 |
|
|
|
132 |
|
|
double elapsedDay = ((double)elapsedHour) / 24.0; |
133 |
|
|
|
134 |
|
|
if (elapsedDay < 1.0) |
135 |
|
|
elapsedDay = 1.0; |
136 |
|
|
|
137 |
|
|
return elapsedDay; |
138 |
|
|
} |
139 |
|
|
|
140 |
|
|
|
141 |
torben |
811 |
/******* |
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 |
torben |
711 |
* Singleton stuff |
237 |
|
|
*/ |
238 |
|
|
|
239 |
|
|
private static Statistics instance = null; |
240 |
|
|
private Statistics() { |
241 |
|
|
// Exists only to defeat instantiation. |
242 |
|
|
} |
243 |
|
|
public static Statistics getInstance() { |
244 |
|
|
if(instance == null) { |
245 |
|
|
instance = new Statistics(); |
246 |
|
|
} |
247 |
|
|
return instance; |
248 |
|
|
} |
249 |
|
|
} |