From 5931abc860673f489787155ea4734af48de255fd Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 6 Jun 2017 17:14:17 +0200 Subject: [PATCH] BgReading optimization --- .../androidaps/Services/DataService.java | 56 ++---------------- .../nightscout/androidaps/db/BgReading.java | 58 ++++++++++++++++--- .../androidaps/db/DatabaseHelper.java | 23 ++++++-- .../plugins/Overview/OverviewFragment.java | 2 - 4 files changed, 75 insertions(+), 64 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java index 3c81d5c84c..1a3bb2fd66 100644 --- a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java +++ b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java @@ -166,16 +166,7 @@ public class DataService extends IntentService { bgReading.date = bundle.getLong(Intents.EXTRA_TIMESTAMP); bgReading.raw = bundle.getDouble(Intents.EXTRA_RAW); - if (bgReading.date < new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000L) { - if (Config.logIncommingBG) - log.debug("Ignoring old XDRIPREC BG " + bgReading.toString()); - return; - } - - if (Config.logIncommingBG) - log.debug("XDRIPREC BG " + bgReading.toString()); - - MainApp.getDbHelper().createIfNotExists(bgReading); + MainApp.getDbHelper().createIfNotExists(bgReading, "XDRIP"); } private void handleNewDataFromGlimp(Intent intent) { @@ -189,11 +180,7 @@ public class DataService extends IntentService { bgReading.date = bundle.getLong("myTimestamp"); bgReading.raw = 0; - if (Config.logIncommingBG) - log.debug(bundle.toString()); - log.debug("GLIMP BG " + bgReading.toString()); - - MainApp.getDbHelper().createIfNotExists(bgReading); + MainApp.getDbHelper().createIfNotExists(bgReading, "GLIMP"); } private void handleNewDataFromMM640g(Intent intent) { @@ -221,16 +208,7 @@ public class DataService extends IntentService { bgReading.date = json_object.getLong("date"); bgReading.raw = json_object.getDouble("sgv"); - if (bgReading.date < new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000L) { - if (Config.logIncommingBG) - log.debug("Ignoring old MM640g BG " + bgReading.toString()); - return; - } - - if (Config.logIncommingBG) - log.debug("MM640g BG " + bgReading.toString()); - - MainApp.getDbHelper().createIfNotExists(bgReading); + MainApp.getDbHelper().createIfNotExists(bgReading, "MM640g"); break; default: log.debug("Unknown entries type: " + type); @@ -402,14 +380,7 @@ public class DataService extends IntentService { JSONObject sgvJson = new JSONObject(sgvstring); NSSgv nsSgv = new NSSgv(sgvJson); BgReading bgReading = new BgReading(nsSgv); - if (bgReading.date < new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000l) { - if (Config.logIncommingData) - log.debug("Ignoring old BG: " + bgReading.toString()); - return; - } - MainApp.getDbHelper().createIfNotExists(bgReading); - if (Config.logIncommingData) - log.debug("ADD: Stored new BG: " + bgReading.toString()); + MainApp.getDbHelper().createIfNotExists(bgReading, "NS"); } if (bundles.containsKey("sgvs")) { @@ -419,14 +390,7 @@ public class DataService extends IntentService { JSONObject sgvJson = jsonArray.getJSONObject(i); NSSgv nsSgv = new NSSgv(sgvJson); BgReading bgReading = new BgReading(nsSgv); - if (bgReading.date < new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000l) { - if (Config.logIncommingData) - log.debug("Ignoring old BG: " + bgReading.toString()); - } else { - MainApp.getDbHelper().createIfNotExists(bgReading); - if (Config.logIncommingData) - log.debug("ADD: Stored new BG: " + bgReading.toString()); - } + MainApp.getDbHelper().createIfNotExists(bgReading, "NS"); } } } catch (Exception e) { @@ -441,11 +405,6 @@ public class DataService extends IntentService { JSONObject mbgJson = new JSONObject(mbgstring); NSMbg nsMbg = new NSMbg(mbgJson); CareportalEvent careportalEvent = new CareportalEvent(nsMbg); - if (careportalEvent.date < new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000l) { - if (Config.logIncommingData) - log.debug("Ignoring old MBG: " + careportalEvent.log()); - return; - } MainApp.getDbHelper().createOrUpdate(careportalEvent); if (Config.logIncommingData) log.debug("Adding/Updating new MBG: " + careportalEvent.log()); @@ -458,11 +417,6 @@ public class DataService extends IntentService { JSONObject mbgJson = jsonArray.getJSONObject(i); NSMbg nsMbg = new NSMbg(mbgJson); CareportalEvent careportalEvent = new CareportalEvent(nsMbg); - if (careportalEvent.date < new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000l) { - if (Config.logIncommingData) - log.debug("Ignoring old MBG: " + careportalEvent.log()); - return; - } MainApp.getDbHelper().createOrUpdate(careportalEvent); if (Config.logIncommingData) log.debug("Adding/Updating new MBG: " + careportalEvent.log()); diff --git a/app/src/main/java/info/nightscout/androidaps/db/BgReading.java b/app/src/main/java/info/nightscout/androidaps/db/BgReading.java index 2a58157f91..b49fe5375d 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/BgReading.java +++ b/app/src/main/java/info/nightscout/androidaps/db/BgReading.java @@ -6,6 +6,8 @@ import com.j256.ormlite.table.DatabaseTable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Date; + import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; @@ -40,12 +42,10 @@ public class BgReading implements DataPointWithLabelInterface { @DatabaseField public String _id = null; // NS _id - - public static String units = Constants.MGDL; - public boolean isPrediction = false; // true when drawing predictions as bg points - public BgReading() {} + public BgReading() { + } public BgReading(NSSgv sgv) { date = sgv.getMills(); @@ -66,7 +66,7 @@ public class BgReading implements DataPointWithLabelInterface { else return DecimalFormatter.to1Decimal(value * Constants.MGDL_TO_MMOLL); } - public String directionToSymbol() { + public String directionToSymbol() { String symbol = ""; if (direction.compareTo("DoubleDown") == 0) { symbol = "\u21ca"; @@ -105,13 +105,56 @@ public class BgReading implements DataPointWithLabelInterface { public String toString() { return "BgReading{" + "date=" + date + - ", date=" + DateUtil.dateAndTimeString(date) + + ", date=" + new Date(date).toLocaleString() + ", value=" + value + ", direction=" + direction + ", raw=" + raw + '}'; } + public boolean isDataChanging(BgReading other) { + if (date != other.date) { + log.error("Comparing different"); + return false; + } + if (value != other.value) + return true; + return false; + } + + public boolean isEqual(BgReading other) { + if (date != other.date) { + log.error("Comparing different"); + return false; + } + if (value != other.value) + return false; + if (raw != other.raw) + return false; + if (!direction.equals(other.direction)) + return false; + if (_id == null && other._id != null) + return false; + else if (_id != null && other._id == null) + return false; + else if (_id == null && other._id == null) + ; + else if (!_id.equals(other._id)) + return false; + return true; + } + + public void copyFrom(BgReading other) { + if (date != other.date) { + log.error("Copying different"); + return; + } + value = other.value; + raw = other.raw; + direction = other.direction; + _id = other._id; + } + // ------------------ DataPointWithLabelInterface ------------------ @Override public double getX() { @@ -120,6 +163,7 @@ public class BgReading implements DataPointWithLabelInterface { @Override public double getY() { + String units = MainApp.getConfigBuilder().getProfile().getUnits(); return valueToUnits(units); } @@ -151,6 +195,7 @@ public class BgReading implements DataPointWithLabelInterface { @Override public int getColor() { + String units = MainApp.getConfigBuilder().getProfile().getUnits(); Double lowLine = SP.getDouble("low_mark", 0d); Double highLine = SP.getDouble("high_mark", 0d); if (lowLine < 1) { @@ -159,7 +204,6 @@ public class BgReading implements DataPointWithLabelInterface { if (highLine < 1) { highLine = Profile.fromMgdlToUnits(OverviewPlugin.bgTargetHigh, units); } - String units = MainApp.getConfigBuilder().getProfile().getUnits(); int color = MainApp.sResources.getColor(R.color.inrange); if (isPrediction) color = MainApp.sResources.getColor(R.color.prediction); diff --git a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java index 87b4050f6a..873c7c93ac 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -320,16 +320,31 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { return getDao(ProfileSwitch.class); } + public long roundDateToSec(long date) { + return date - date % 1000; + } // ------------------- BgReading handling ----------------------- - public void createIfNotExists(BgReading bgReading) { - bgReading.date = bgReading.date - bgReading.date % 1000; + public void createIfNotExists(BgReading bgReading, String from) { try { - getDaoBgReadings().createIfNotExists(bgReading); + bgReading.date = roundDateToSec(bgReading.date); + BgReading old = getDaoBgReadings().queryForId(bgReading.date); + if (old == null) { + getDaoBgReadings().create(bgReading); + log.debug("BG: New record from: " + from + " " + bgReading.toString()); + scheduleBgChange(); + return; + } + if (!old.isEqual(bgReading)) { + old.copyFrom(bgReading); + getDaoBgReadings().update(old); + log.debug("BG: Updating record from: " + from + " " + old.toString()); + scheduleBgChange(); + return; + } } catch (SQLException e) { e.printStackTrace(); } - scheduleBgChange(); } private static void scheduleBgChange() { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java index 9b9d65c541..c2d9dd7766 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java @@ -1097,8 +1097,6 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, deltaView.setText("Δ " + MainApp.sResources.getString(R.string.notavailable)); avgdeltaView.setText(""); } - - BgReading.units = profile.getUnits(); } else { if (updating != null) updating.setVisibility(View.GONE);