Skip to content

Commit

Permalink
PebbleMovement: Improve data storage efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
jamorham committed Nov 18, 2016
1 parent b855bb8 commit 9c17653
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;
import com.activeandroid.query.Delete;
import com.activeandroid.query.Select;
import com.activeandroid.util.SQLiteUtils;
import com.eveningoutpost.dexdrip.UtilityModels.BgGraphBuilder;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose;
Expand All @@ -24,6 +26,7 @@ public class PebbleMovement extends Model {

private static boolean patched = false;
private final static String TAG = "PebbleMovement";
private final static boolean d = false;

@Expose
@Column(name = "timestamp", unique = true, onUniqueConflicts = Column.ConflictAction.IGNORE)
Expand All @@ -50,6 +53,23 @@ public String toS() {

// static methods

public static PebbleMovement createEfficientRecord(long timestamp_ms, int data)
{
PebbleMovement pm = last();
if ((pm == null) || (data < pm.metric) || ((timestamp_ms - pm.timestamp) > (1000 * 30 * 5))) {
pm = new PebbleMovement();
pm.timestamp = timestamp_ms;
if (d) UserError.Log.d(TAG,"Creating new record for timestamp: "+JoH.dateTimeText(timestamp_ms));
} else {
if (d) UserError.Log.d(TAG,"Merging pebble movement record: "+JoH.dateTimeText(timestamp_ms)+" vs old "+JoH.dateTimeText(pm.timestamp));
}

pm.metric = (int) (long) data;
if(d) UserError.Log.d(TAG, "Saving Movement: " + pm.toS());
pm.saveit();
return pm;
}

public static PebbleMovement last() {
try {
return new Select()
Expand Down Expand Up @@ -108,6 +128,13 @@ public static List<PebbleMovement> deltaListFromMovementList(List<PebbleMovement
return mList;
}

public static List<PebbleMovement> cleanup(int retention_days) {
return new Delete()
.from(PebbleMovement.class)
.where("timestamp < ?", JoH.tsl() - (retention_days * 86400000L))
.execute();
}


// create the table ourselves without worrying about model versioning and downgrading
private static void fixUpTable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import android.content.Intent;
import android.os.PowerManager;

import com.eveningoutpost.dexdrip.Home;
import com.eveningoutpost.dexdrip.Models.JoH;
import com.eveningoutpost.dexdrip.Models.PebbleMovement;
import com.eveningoutpost.dexdrip.Models.UserError;
import com.eveningoutpost.dexdrip.UtilityModels.BgSendQueue;
import com.eveningoutpost.dexdrip.UtilityModels.CalibrationSendQueue;
Expand Down Expand Up @@ -44,7 +46,11 @@ protected void onHandleIntent(Intent intent) {
} catch (Exception e) {
//
}

try {
PebbleMovement.cleanup(Home.getPreferencesInt("retention_pebble_movement", 180));
} catch (Exception e) {
//
}
try {
checkForAnUpdate(getApplicationContext());
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,8 @@ public void receiveData(Context context, UUID logUuid, Long timestamp,
} else {
if (data > 0) {
if (last_movement_timestamp > 0) {
final PebbleMovement pm = new PebbleMovement();
pm.timestamp = last_movement_timestamp * 1000;
pm.metric = (int) (long) data;
final PebbleMovement pm = PebbleMovement.createEfficientRecord(last_movement_timestamp * 1000, (int)(long) data);
Log.d(TAG, "Saving Movement: " + pm.toS());
pm.saveit();
last_movement_timestamp = 0; // reset state
} else {
Log.e(TAG, "Out of sequence movement value received!");
Expand Down
Binary file modified app/src/main/res/raw/xdrip_pebble_classic_trend.bin
Binary file not shown.

0 comments on commit 9c17653

Please sign in to comment.