--- android/TrainInfoService/src/dk/thoerup/traininfoservice/Statistics.java 2010/06/09 14:20:50 810 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/Statistics.java 2010/06/09 20:16:01 811 @@ -1,5 +1,8 @@ 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; @@ -17,6 +20,16 @@ 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() { @@ -125,7 +138,101 @@ } - /**** + /******* + * 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 */