reworked to use DBHelper

This commit is contained in:
Roumen Georgiev 2019-05-13 14:18:10 +03:00
parent 62b2d83a18
commit 6e63d340cd
3 changed files with 63 additions and 57 deletions

View file

@ -7,6 +7,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import info.nightscout.androidaps.Constants;
@ -18,8 +19,8 @@ import info.nightscout.androidaps.plugins.general.nsclient.data.NSSgv;
import info.nightscout.androidaps.plugins.general.overview.OverviewPlugin;
import info.nightscout.androidaps.plugins.general.overview.graphExtensions.DataPointWithLabelInterface;
import info.nightscout.androidaps.plugins.general.overview.graphExtensions.PointsWithLabelGraphSeries;
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus;
import info.nightscout.androidaps.utils.DecimalFormatter;
import info.nightscout.androidaps.utils.T;
@DatabaseTable(tableName = DatabaseHelper.DATABASE_BGREADINGS)
public class BgReading implements DataPointWithLabelInterface {
@ -249,19 +250,29 @@ public class BgReading implements DataPointWithLabelInterface {
// Copied from xDrip+
String calculateDirection() {
GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData();
double slope;
if (glucoseStatus == null || glucoseStatus.prev_glucose == 0)
// Rework to get bgreaings from internal DB and calculate on that base
List<BgReading> bgReadingsList = MainApp.getDbHelper().getAllBgreadingsDataFromTime(this.date - T.mins(10).msecs(), false);
if (bgReadingsList == null || bgReadingsList.size() < 2)
return "NONE";
BgReading current = bgReadingsList.get(1);
BgReading previous = bgReadingsList.get(0);
if (bgReadingsList.get(1).date < bgReadingsList.get(0).date) {
current = bgReadingsList.get(0);
previous = bgReadingsList.get(1);
}
double slope;
// Avoid division by 0
if (glucoseStatus.date == glucoseStatus.previous_date)
if (current.date == previous.date)
slope = 0;
else
slope = (glucoseStatus.prev_glucose - glucoseStatus.glucose) / (glucoseStatus.previous_date - glucoseStatus.date);
slope = (previous.value - current.value) / (previous.date - current.date);
if (L.isEnabled(L.GLUCOSE))
log.debug("Slope is :" + slope + " delta " + glucoseStatus.delta + " date difference " + (glucoseStatus.date - glucoseStatus.previous_date));
log.debug("Slope is :" + slope + " delta " + (previous.value - current.value) + " date difference " + (current.date - previous.date));
double slope_by_minute = slope * 60000;
String arrow = "NONE";

View file

@ -26,8 +26,6 @@ public class GlucoseStatus {
public double short_avgdelta = 0d;
public double long_avgdelta = 0d;
public long date = 0L;
public long previous_date = 0L;
public double prev_glucose = 0d;
public String log() {
@ -60,8 +58,6 @@ public class GlucoseStatus {
// load 45min
//long fromtime = DateUtil.now() - 60 * 1000L * 45;
//List<BgReading> data = MainApp.getDbHelper().getBgreadingsDataFromTime(fromtime, false);
long prevDate = 0;
double prevValue = 0;
synchronized (IobCobCalculatorPlugin.getPlugin().getDataLock()) {
@ -98,9 +94,6 @@ public class GlucoseStatus {
status.long_avgdelta = 0d;
status.avgdelta = 0d; // for OpenAPS MA
status.date = now_date;
status.previous_date = 0; // setting the previous value date for slope calculation
status.prev_glucose = 0;
if (L.isEnabled(L.GLUCOSE))
log.debug("sizeRecords==1");
return status.round();
@ -125,11 +118,6 @@ public class GlucoseStatus {
// multiply by 5 to get the same units as delta, i.e. mg/dL/5m
change = now.value - then.value;
avgdelta = change / minutesago * 5;
// save the value of date if it was 5 min ago or less than 10 min
if( minutesago >= 5 && minutesago < 10 ) {
prevDate = then_date;
prevValue = then.value;
}
if (L.isEnabled(L.GLUCOSE))
log.debug(then.toString() + " minutesago=" + minutesago + " avgdelta=" + avgdelta);
@ -171,8 +159,6 @@ public class GlucoseStatus {
status.long_avgdelta = average(long_deltas);
status.avgdelta = status.short_avgdelta; // for OpenAPS MA
status.previous_date = prevDate; // setting the previous value date for slope calculation
status.prev_glucose = prevValue;
if (L.isEnabled(L.GLUCOSE))
log.debug(status.log());

View file

@ -3,15 +3,16 @@ package info.nightscout.androidaps.db;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;
import edu.emory.mathcs.backport.java.util.Arrays;
import info.AAPSMocker;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
@ -22,6 +23,8 @@ import info.nightscout.androidaps.utils.SP;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.when;
@RunWith(PowerMockRunner.class)
@ -110,41 +113,32 @@ public class BgReadingTest {
@Test
public void calculateDirection() {
List<BgReading> bgReadingsList = null;
AAPSMocker.mockDatabaseHelper();
when(MainApp.getDbHelper().getAllBgreadingsDataFromTime(anyLong(),anyBoolean())).thenReturn(bgReadingsList);
assertEquals("NONE", bgReading.calculateDirection());
glucoseStatus = new GlucoseStatus();
glucoseStatus.glucose = 0;
glucoseStatus.prev_glucose = 0;
glucoseStatus.date = 1000L * 60 * 12;
glucoseStatus.previous_date = 1000L * 60 * 6;
BgReading newReading = new BgReading();
PowerMockito.mockStatic(GlucoseStatus.class);
when(GlucoseStatus.getGlucoseStatusData()).thenReturn(glucoseStatus);
assertEquals("NONE", newReading.calculateDirection());
glucoseStatus.glucose = 72;
glucoseStatus.prev_glucose = 10;
assertEquals("DoubleUp", newReading.calculateDirection());
glucoseStatus.glucose = 72;
glucoseStatus.prev_glucose = 55;
assertEquals("SingleUp", newReading.calculateDirection());
glucoseStatus.glucose = 72;
glucoseStatus.prev_glucose = 65;
assertEquals("FortyFiveUp", newReading.calculateDirection());
glucoseStatus.glucose = 72;
glucoseStatus.prev_glucose = 70;
assertEquals("Flat", newReading.calculateDirection());
glucoseStatus.glucose = 10;
glucoseStatus.prev_glucose = 72;
assertEquals("DoubleDown", newReading.calculateDirection());
glucoseStatus.glucose = 55;
glucoseStatus.prev_glucose = 72;
assertEquals("SingleDown", newReading.calculateDirection());
glucoseStatus.glucose = 65;
glucoseStatus.prev_glucose = 72;
assertEquals("FortyFiveDown", newReading.calculateDirection());
bgReadingsList = setReadings(72,0);
when(MainApp.getDbHelper().getAllBgreadingsDataFromTime(anyLong(),anyBoolean())).thenReturn(bgReadingsList);
assertEquals("DoubleUp", bgReading.calculateDirection());
bgReadingsList = setReadings(76,60);
when(MainApp.getDbHelper().getAllBgreadingsDataFromTime(anyLong(),anyBoolean())).thenReturn(bgReadingsList);
assertEquals("SingleUp", bgReading.calculateDirection());
bgReadingsList = setReadings(74,65);
when(MainApp.getDbHelper().getAllBgreadingsDataFromTime(anyLong(),anyBoolean())).thenReturn(bgReadingsList);
assertEquals("FortyFiveUp", bgReading.calculateDirection());
bgReadingsList = setReadings(72,72);
when(MainApp.getDbHelper().getAllBgreadingsDataFromTime(anyLong(),anyBoolean())).thenReturn(bgReadingsList);
assertEquals("Flat", bgReading.calculateDirection());
bgReadingsList = setReadings(0,72);
when(MainApp.getDbHelper().getAllBgreadingsDataFromTime(anyLong(),anyBoolean())).thenReturn(bgReadingsList);
assertEquals("DoubleDown", bgReading.calculateDirection());
bgReadingsList = setReadings(60,76);
when(MainApp.getDbHelper().getAllBgreadingsDataFromTime(anyLong(),anyBoolean())).thenReturn(bgReadingsList);
assertEquals("SingleDown", bgReading.calculateDirection());
bgReadingsList = setReadings(65,74);
when(MainApp.getDbHelper().getAllBgreadingsDataFromTime(anyLong(),anyBoolean())).thenReturn(bgReadingsList);
assertEquals("FortyFiveDown", bgReading.calculateDirection());
}
@ -155,5 +149,20 @@ public class BgReadingTest {
AAPSMocker.mockApplicationContext();
AAPSMocker.mockSP();
AAPSMocker.mockL();
AAPSMocker.mockDatabaseHelper();
}
public List<BgReading> setReadings(int current_value, int previous_value){
BgReading now = new BgReading();
now.value = current_value;
now.date = System.currentTimeMillis();
BgReading previous = new BgReading();
previous.value = previous_value;
previous.date = System.currentTimeMillis() - ( 6 * 60 * 1000L);
List<BgReading> bgReadings = new ArrayList() {{
add(now);
add(previous);
}};
return bgReadings;
}
}