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 |
public class Statistics { |
10 |
|
11 |
private AtomicInteger stationLookupsLocation = new AtomicInteger(0); |
12 |
private AtomicInteger stationLookupsName = new AtomicInteger(0); |
13 |
private AtomicInteger stationLookupsFavorites = new AtomicInteger(0); |
14 |
private AtomicInteger departureLookups = new AtomicInteger(0); |
15 |
private AtomicInteger timetableLookups = new AtomicInteger(0); |
16 |
private AtomicInteger departureCacheHits = new AtomicInteger(0); |
17 |
private AtomicInteger timetableCacheHits = new AtomicInteger(0); |
18 |
private AtomicInteger departureErrors = new AtomicInteger(0); |
19 |
private AtomicInteger timetableErrors = new AtomicInteger(0); |
20 |
|
21 |
private int old_stationLookupsLocation = 0; |
22 |
private int old_stationLookupsName = 0; |
23 |
private int old_stationLookupsFavorites = 0; |
24 |
private int old_departureLookups = 0; |
25 |
private int old_timetableLookups = 0; |
26 |
private int old_departureCacheHits = 0; |
27 |
private int old_timetableCacheHits = 0; |
28 |
private int old_departureErrors = 0; |
29 |
private int old_timetableErrors = 0; |
30 |
|
31 |
private Date lastReset = new Date(); |
32 |
|
33 |
public void incrementStationLookupsLocation() { |
34 |
stationLookupsLocation.incrementAndGet(); |
35 |
} |
36 |
|
37 |
public void incrementStationLookupsName() { |
38 |
stationLookupsName.incrementAndGet(); |
39 |
} |
40 |
|
41 |
public void incrementStationLookupsFavorites() { |
42 |
stationLookupsFavorites.incrementAndGet(); |
43 |
} |
44 |
|
45 |
public void incrementDepartureLookups() { |
46 |
departureLookups.incrementAndGet(); |
47 |
} |
48 |
|
49 |
public void incrementTimetableLookups() { |
50 |
timetableLookups.incrementAndGet(); |
51 |
} |
52 |
|
53 |
public void incrementDepartureCacheHits() { |
54 |
departureCacheHits.incrementAndGet(); |
55 |
} |
56 |
|
57 |
public void incrementTimetableCacheHits() { |
58 |
timetableCacheHits.incrementAndGet(); |
59 |
} |
60 |
|
61 |
public void incrementDepartureErrors() { |
62 |
departureErrors.incrementAndGet(); |
63 |
} |
64 |
|
65 |
public void incrementTimetableErrors() { |
66 |
timetableErrors.incrementAndGet(); |
67 |
} |
68 |
|
69 |
///////// |
70 |
|
71 |
public int getStationTotals() { |
72 |
return stationLookupsLocation.get() + stationLookupsName.get() + stationLookupsFavorites.get(); |
73 |
} |
74 |
|
75 |
public int getStationLookupsLocation() { |
76 |
return stationLookupsLocation.get(); |
77 |
} |
78 |
|
79 |
public int getStationLookupsName() { |
80 |
return stationLookupsName.get(); |
81 |
} |
82 |
|
83 |
public int getStationLookupsFavorites() { |
84 |
return stationLookupsFavorites.get(); |
85 |
} |
86 |
|
87 |
public int getDepartureLookups() { |
88 |
return departureLookups.get(); |
89 |
} |
90 |
|
91 |
public int getTimetableLookups() { |
92 |
return timetableLookups.get(); |
93 |
} |
94 |
|
95 |
public int getDepartureCacheHits() { |
96 |
return departureCacheHits.get(); |
97 |
} |
98 |
|
99 |
public int getTimetableCacheHits() { |
100 |
return timetableCacheHits.get(); |
101 |
} |
102 |
|
103 |
public int getDepartureErrors() { |
104 |
return departureErrors.get(); |
105 |
} |
106 |
|
107 |
public int getTimetableErrors() { |
108 |
return timetableErrors.get(); |
109 |
} |
110 |
|
111 |
public Date getLastReset() { |
112 |
return lastReset; |
113 |
} |
114 |
|
115 |
|
116 |
/* helper functions */ |
117 |
|
118 |
public String getElapsedAsString() { |
119 |
Date now = new Date(); |
120 |
long duration = (now.getTime() - lastReset.getTime() ); |
121 |
|
122 |
return "" + duration; |
123 |
//TODO fix this |
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 |
/******* |
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 |
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 |
} |