--- android/TrainInfoService/src/dk/thoerup/traininfoservice/Statistics.java 2010/05/05 20:11:03 711 +++ android/TrainInfoService/src/main/java/dk/thoerup/traininfoservice/Statistics.java 2015/03/20 21:18:33 2466 @@ -1,19 +1,36 @@ 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 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(); @@ -45,6 +62,14 @@ timetableCacheHits.incrementAndGet(); } + public void incrementDepartureErrors() { + departureErrors.incrementAndGet(); + } + + public void incrementTimetableErrors() { + timetableErrors.incrementAndGet(); + } + ///////// public int getStationTotals() { @@ -79,6 +104,14 @@ return timetableCacheHits.get(); } + public int getDepartureErrors() { + return departureErrors.get(); + } + + public int getTimetableErrors() { + return timetableErrors.get(); + } + public Date getLastReset() { return lastReset; } @@ -107,7 +140,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 */