package dk.thoerup.traininfoservice; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Date; import java.util.concurrent.atomic.AtomicInteger; import org.apache.commons.lang.time.DurationFormatUtils; import dk.thoerup.traininfoservice.db.DBConnection; public class Statistics { private AtomicInteger stationLookupsLocation = new AtomicInteger(0); private AtomicInteger stationLookupsName = new AtomicInteger(0); private AtomicInteger stationLookupsFavorites = new AtomicInteger(0); private AtomicInteger departureLookups = new AtomicInteger(0); private AtomicInteger timetableLookups = new AtomicInteger(0); private AtomicInteger departureCacheHits = new AtomicInteger(0); private AtomicInteger timetableCacheHits = new AtomicInteger(0); private AtomicInteger departureErrors = new AtomicInteger(0); private AtomicInteger timetableErrors = new AtomicInteger(0); private int old_stationLookupsLocation = 0; private int old_stationLookupsName = 0; private int old_stationLookupsFavorites = 0; private int old_departureLookups = 0; private int old_timetableLookups = 0; private int old_departureCacheHits = 0; private int old_timetableCacheHits = 0; private int old_departureErrors = 0; private int old_timetableErrors = 0; private Date lastReset = new Date(); public void incrementStationLookupsLocation() { stationLookupsLocation.incrementAndGet(); } public void incrementStationLookupsName() { stationLookupsName.incrementAndGet(); } public void incrementStationLookupsFavorites() { stationLookupsFavorites.incrementAndGet(); } public void incrementDepartureLookups() { departureLookups.incrementAndGet(); } public void incrementTimetableLookups() { timetableLookups.incrementAndGet(); } public void incrementDepartureCacheHits() { departureCacheHits.incrementAndGet(); } public void incrementTimetableCacheHits() { timetableCacheHits.incrementAndGet(); } public void incrementDepartureErrors() { departureErrors.incrementAndGet(); } public void incrementTimetableErrors() { timetableErrors.incrementAndGet(); } ///////// public int getStationTotals() { return stationLookupsLocation.get() + stationLookupsName.get() + stationLookupsFavorites.get(); } public int getStationLookupsLocation() { return stationLookupsLocation.get(); } public int getStationLookupsName() { return stationLookupsName.get(); } public int getStationLookupsFavorites() { return stationLookupsFavorites.get(); } public int getDepartureLookups() { return departureLookups.get(); } public int getTimetableLookups() { return timetableLookups.get(); } public int getDepartureCacheHits() { return departureCacheHits.get(); } public int getTimetableCacheHits() { return timetableCacheHits.get(); } public int getDepartureErrors() { return departureErrors.get(); } public int getTimetableErrors() { return timetableErrors.get(); } public Date getLastReset() { return lastReset; } /* helper functions */ public String getElapsedAsString() { Date now = new Date(); long duration = (now.getTime() - lastReset.getTime() ); return DurationFormatUtils.formatDuration(duration, "d, HH:mm:ss"); } public double getElapsedDays() { Date now = new Date(); long elapsedMs = now.getTime() - lastReset.getTime(); long elapsedHour = elapsedMs / (60*60*1000); double elapsedDay = ((double)elapsedHour) / 24.0; if (elapsedDay < 1.0) elapsedDay = 1.0; return elapsedDay; } /******* * Save stats to DB */ public void saveStats() { synchronized(this) { int loc = stationLookupsLocation.get(); int name = stationLookupsName.get(); int fav = stationLookupsFavorites.get(); int departure = departureLookups.get(); int depcache = departureCacheHits.get(); int deperror = departureErrors.get(); int timetable = timetableLookups.get(); int timecache = timetableCacheHits.get(); int timeerror = timetableErrors.get(); int diff_loc = loc - old_stationLookupsLocation; int diff_name = name - old_stationLookupsName; int diff_fav = fav - old_stationLookupsFavorites; int diff_departure = departure - old_departureLookups; int diff_depcache = depcache - old_departureCacheHits; int diff_deperror = deperror - old_departureErrors; int diff_timetable = timetable - old_timetableLookups; int diff_timecache = timecache - old_timetableCacheHits; int diff_timeerror = timeerror - old_timetableErrors; if ( diff_loc != 0 || diff_name != 0 || diff_fav != 0 || diff_departure != 0 || diff_timetable != 0 || diff_depcache != 0 || diff_timecache != 0 || diff_deperror != 0 || diff_timeerror != 0) { try { //System.out.println("Updating ..."); Connection conn = DBConnection.getConnection(); PreparedStatement stmt = conn.prepareStatement("UPDATE trainstatistics SET location=location+?, name=name+?, favorites=favorites+?, departure=departure+?, " + "depcache=depcache+?, deperror=deperror+?, timetable=timetable+?, timecache=timecache+?, timeerror=timeerror+? " + "WHERE statisticsdate=now()::date"); stmt.setInt(1, diff_loc); stmt.setInt(2, diff_name); stmt.setInt(3, diff_fav); stmt.setInt(4, diff_departure); stmt.setInt(5, diff_depcache); stmt.setInt(6, diff_deperror); stmt.setInt(7, diff_timetable); stmt.setInt(8, diff_timecache); stmt.setInt(9, diff_timeerror); int rowcount = stmt.executeUpdate(); stmt.close(); if (rowcount == 0) { //System.out.println("inserting new row ..."); stmt = conn.prepareStatement("INSERT INTO trainstatistics(statisticsdate,location,name,favorites,departure,depcache,deperror,timetable,timecache,timeerror) " + "values (now()::date, ?,?,?,?,?,?,?,?,?)" ); stmt.setInt(1, diff_loc); stmt.setInt(2, diff_name); stmt.setInt(3, diff_fav); stmt.setInt(4, diff_departure); stmt.setInt(5, diff_depcache); stmt.setInt(6, diff_deperror); stmt.setInt(7, diff_timetable); stmt.setInt(8, diff_timecache); stmt.setInt(9, diff_timeerror); stmt.executeUpdate(); } old_stationLookupsLocation = loc; old_stationLookupsName = name; old_stationLookupsFavorites = fav; old_departureLookups = departure; old_departureCacheHits = depcache; old_departureErrors = deperror; old_timetableLookups = timetable; old_timetableCacheHits = timecache; old_timetableErrors = timeerror; conn.close(); } catch (SQLException sqle) { } } } } /******* * Singleton stuff */ private static Statistics instance = null; private Statistics() { // Exists only to defeat instantiation. } public static Statistics getInstance() { if(instance == null) { instance = new Statistics(); } return instance; } }