@AddinInterfaceMethodDefinition(
userlevel = 9
)
public void aggregateStatistics(GalenaArgsInterface args, String jid) {
jid = jid.toLowerCase();
DBConnection db = this.db.getDBConnection(true);
// First check if user has an entry in the JID table.
int jidID;
{
List<Map<String,Object>> jidentry = db.getListOfMaps("SELECT * FROM goim_stats_jid WHERE jid = ?",jid);
if(jidentry != null && jidentry.size() > 0)
jidID = (Integer)jidentry.get(0).get("id");
else
jidID = db.sendInsert(args.getUser(),"goim_stats_jid","jid",jid);
}
boolean dontlogthisday = false;
// Now check when the last day entry was made.
List<Map<String,Object>> listOfPresenceLog;
{
List<Map<String,Object>> lastDayEntry = db.getListOfMaps("SELECT * FROM goim_stats_day WHERE jid = ? ORDER BY day DESC LIMIT 1",jidID);
if(lastDayEntry != null && lastDayEntry.size() > 0) {
Date lastDay = (Date)lastDayEntry.get(0).get("day");
Integer id = (Integer)lastDayEntry.get(0).get("id");
Calendar cal = Calendar.getInstance();
cal.setTime(lastDay);
// We recalculate the last day ...
db.sendDelete(args.getUser(),"goim_stats_game","day = ?",id);
db.sendDelete(args.getUser(),"goim_stats_presencestatus","day = ?",id);
db.sendDelete(args.getUser(),"goim_stats_day","id = ?",id);
// To recalculate the last day we also need to recalculate
// the day before so we get presences which started there and
// lasted until this day.
cal.add(Calendar.DAY_OF_MONTH,-1);
listOfPresenceLog = db.getListOfMaps("SELECT * FROM goim_presence_log WHERE jid = ? AND start > ? ORDER BY start",jid,cal.getTime());
dontlogthisday = true;
} else {
// No entry yet
listOfPresenceLog = db.getListOfMaps("SELECT * FROM goim_presence_log WHERE jid = ? ORDER BY start",jid);
}
}
Calendar currday = null; // The start of the current day.
Calendar nextday = null; // The start of the next day (ie. the end of the current)
HashMap<String,Long> gamesPlayed = new HashMap<String,Long>();
HashMap<String,Long> presenceModes = new HashMap<String,Long>();
HashMap<String,Long> nextGamesPlayed = new HashMap<String,Long>();
HashMap<String,Long> nextPresenceModes = new HashMap<String,Long>();
/// OK .. really BAD algorithm .. but it works ... somehow .. i guess/hope ?!
for(Map<String,Object> logEntry : listOfPresenceLog) {
Date start = (Date)logEntry.get("start");
Date end = (Date)logEntry.get("end");
String mode = (String)logEntry.get("mode");
if(nextday == null) {
nextday = Calendar.getInstance();
currday = Calendar.getInstance();
nextday.clear();
Calendar startcal = Calendar.getInstance();
startcal.setTime(start);
nextday.set(Calendar.YEAR,startcal.get(Calendar.YEAR));
nextday.set(Calendar.MONTH,startcal.get(Calendar.MONTH));
nextday.set(Calendar.DAY_OF_MONTH,startcal.get(Calendar.DAY_OF_MONTH));
currday = (Calendar)nextday.clone();
nextday.add(Calendar.DAY_OF_MONTH,1);
} else {
if(start.after(nextday.getTime())) {
System.out.println("Day: " + nextday.getTime().toString() + " .... " + start.toString());
if(!dontlogthisday)
logDay(args, jidID, currday, gamesPlayed, presenceModes);
dontlogthisday = false;
gamesPlayed = nextGamesPlayed;
presenceModes = nextPresenceModes;
nextGamesPlayed = new HashMap<String,Long>();
nextPresenceModes = new HashMap<String,Long>();
currday = (Calendar)nextday.clone();
nextday.add(Calendar.DAY_OF_MONTH,1);
}
}
if(start.after(nextday.getTime())) continue;
long durationForNextDay = 0;
long durationInMilliSeconds = end.getTime() - start.getTime();
if(end.after(nextday.getTime())) {
long tmpdur = nextday.getTime().getTime() - start.getTime();
durationForNextDay = (durationInMilliSeconds - tmpdur);
durationInMilliSeconds = tmpdur;
}
{
Long d = presenceModes.get(mode);
if(d == null) d = durationInMilliSeconds;
else d = d + durationInMilliSeconds;
presenceModes.put(mode,d);
if(durationForNextDay > 0) {
d = nextPresenceModes.get(mode);
if(d == null) d = durationForNextDay;
else d = d + durationForNextDay;
nextPresenceModes.put(mode,d);
}
}
List<Map<String,Object>> games = db.getListOfMaps("SELECT * FROM goim_server WHERE presence = ?",logEntry.get("id"));
for(Map<String,Object> game : games) {
String gameName = (String)game.get("game");
Long d = gamesPlayed.get(gameName);
if(d == null) d = durationInMilliSeconds;
else d = d + durationInMilliSeconds;
gamesPlayed.put(gameName,d);
if(durationForNextDay > 0) {
d = nextGamesPlayed.get(gameName);
if(d == null) d = durationForNextDay;
else d = d + durationForNextDay;
nextGamesPlayed.put(gameName,d);
}
}
}
if(currday != null && !dontlogthisday)
logDay(args, jidID, currday, gamesPlayed, presenceModes);
db.close();
}