Use average of scores in the interval

pull/30/head
Alinson S. Xavier 10 years ago
parent 2a750704d9
commit 7493291ade

@ -121,13 +121,16 @@ public class ScoreList
return lastScore; return lastScore;
} }
public int[] getAllValues(Long fromTimestamp, Long toTimestamp, Integer divisor, Long offset) public int[] getAllValues(Long fromTimestamp, Long toTimestamp, Integer divisor)
{ {
String query = "select score from Score where habit = ? and timestamp > ? and " + Long offset = toTimestamp - (divisor - 1) * DateHelper.millisecondsInOneDay;
"timestamp <= ? and (timestamp - ?) % ? = 0 order by timestamp desc";
String params[] = { habit.getId().toString(), fromTimestamp.toString(), String query = "select ((timestamp - ?) / ?) as time, avg(score) from Score " +
toTimestamp.toString(), offset.toString(), divisor.toString()}; "where habit = ? and timestamp > ? and timestamp <= ? " +
"group by time order by time desc";
String params[] = { offset.toString(), divisor.toString(), habit.getId().toString(),
fromTimestamp.toString(), toTimestamp.toString()};
SQLiteDatabase db = Cache.openDatabase(); SQLiteDatabase db = Cache.openDatabase();
Cursor cursor = db.rawQuery(query, params); Cursor cursor = db.rawQuery(query, params);
@ -139,7 +142,7 @@ public class ScoreList
do do
{ {
scores[k++] = cursor.getInt(0); scores[k++] = (int) cursor.getLong(1);
} }
while (cursor.moveToNext()); while (cursor.moveToNext());
@ -155,6 +158,6 @@ public class ScoreList
long fromTimestamp = oldestRep.timestamp; long fromTimestamp = oldestRep.timestamp;
long toTimestamp = DateHelper.getStartOfToday(); long toTimestamp = DateHelper.getStartOfToday();
return getAllValues(fromTimestamp, toTimestamp, divisor, toTimestamp); return getAllValues(fromTimestamp, toTimestamp, divisor);
} }
} }

Loading…
Cancel
Save