fix history iob calculation

This commit is contained in:
Milos Kozak 2017-04-25 10:48:30 +02:00
parent 5a3eb0cc25
commit 0232229020
10 changed files with 50 additions and 34 deletions

View file

@ -53,8 +53,8 @@ public class TempBasal {
public boolean isAbsolute = false; // true if if set as absolute value in U public boolean isAbsolute = false; // true if if set as absolute value in U
public IobTotal iobCalc(Date time) { public IobTotal iobCalc(long time) {
IobTotal result = new IobTotal(time.getTime()); IobTotal result = new IobTotal(time);
NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile(); NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
InsulinInterface insulinInterface = MainApp.getConfigBuilder().getActiveInsulin(); InsulinInterface insulinInterface = MainApp.getConfigBuilder().getActiveInsulin();
@ -66,7 +66,7 @@ public class TempBasal {
if (basalRate == null) if (basalRate == null)
return result; return result;
int realDuration = getRealDuration(); int realDuration = getDurationToTime(time);
if (realDuration > 0) { if (realDuration > 0) {
Double netBasalRate = 0d; Double netBasalRate = 0d;
@ -103,8 +103,8 @@ public class TempBasal {
Iob aIOB = insulinInterface.iobCalc(tempBolusPart, time, profile.getDia()); Iob aIOB = insulinInterface.iobCalc(tempBolusPart, time, profile.getDia());
result.basaliob += aIOB.iobContrib; result.basaliob += aIOB.iobContrib;
result.activity += aIOB.activityContrib; result.activity += aIOB.activityContrib;
Double dia_ago = time.getTime() - profile.getDia() * 60 * 60 * 1000; Double dia_ago = time - profile.getDia() * 60 * 60 * 1000;
if (date > dia_ago && date <= time.getTime()) { if (date > dia_ago && date <= time) {
result.netbasalinsulin += tempBolusPart.insulin; result.netbasalinsulin += tempBolusPart.insulin;
if (tempBolusPart.insulin > 0) { if (tempBolusPart.insulin > 0) {
result.hightempinsulin += tempBolusPart.insulin; result.hightempinsulin += tempBolusPart.insulin;
@ -117,26 +117,32 @@ public class TempBasal {
} }
// Determine end of basal // Determine end of basal
public Date getTimeEnd() { public long getTimeEnd() {
Date tempBasalTimePlannedEnd = getPlannedTimeEnd(); long tempBasalTimePlannedEnd = getPlannedTimeEnd();
Date now = new Date(); long now = new Date().getTime();
if (timeEnd != null && timeEnd.getTime() < tempBasalTimePlannedEnd.getTime()) { if (timeEnd != null && timeEnd.getTime() < tempBasalTimePlannedEnd) {
tempBasalTimePlannedEnd = timeEnd; tempBasalTimePlannedEnd = timeEnd.getTime();
} }
if (now.getTime() < tempBasalTimePlannedEnd.getTime()) if (now < tempBasalTimePlannedEnd)
tempBasalTimePlannedEnd = now; tempBasalTimePlannedEnd = now;
return tempBasalTimePlannedEnd; return tempBasalTimePlannedEnd;
} }
public Date getPlannedTimeEnd() { public long getPlannedTimeEnd() {
return new Date(timeStart.getTime() + 60 * 1_000 * duration); return timeStart.getTime() + 60 * 1_000 * duration;
} }
public int getRealDuration() { public int getRealDuration() {
Long msecs = getTimeEnd().getTime() - timeStart.getTime(); long msecs = getTimeEnd() - timeStart.getTime();
return Math.round(msecs / 60f / 1000);
}
public int getDurationToTime(long time) {
long endTime = Math.min(time, getTimeEnd());
long msecs = endTime - timeStart.getTime();
return Math.round(msecs / 60f / 1000); return Math.round(msecs / 60f / 1000);
} }
@ -146,7 +152,7 @@ public class TempBasal {
public int getPlannedRemainingMinutes() { public int getPlannedRemainingMinutes() {
if (timeEnd != null) return 0; if (timeEnd != null) return 0;
float remainingMin = (getPlannedTimeEnd().getTime() - new Date().getTime()) / 1000f / 60; float remainingMin = (getPlannedTimeEnd() - new Date().getTime()) / 1000f / 60;
return (remainingMin < 0) ? 0 : Math.round(remainingMin); return (remainingMin < 0) ? 0 : Math.round(remainingMin);
} }
@ -172,7 +178,7 @@ public class TempBasal {
public boolean isInProgress(Date time) { public boolean isInProgress(Date time) {
if (timeStart.getTime() > time.getTime()) return false; // in the future if (timeStart.getTime() > time.getTime()) return false; // in the future
if (timeEnd == null) { // open end if (timeEnd == null) { // open end
if (timeStart.getTime() < time.getTime() && getPlannedTimeEnd().getTime() > time.getTime()) if (timeStart.getTime() < time.getTime() && getPlannedTimeEnd() > time.getTime())
return true; // in interval return true; // in interval
return false; return false;
} }

View file

@ -17,5 +17,5 @@ public interface InsulinInterface {
String getFriendlyName(); String getFriendlyName();
String getComment(); String getComment();
double getDia(); double getDia();
public Iob iobCalc(Treatment treatment, Date time, Double dia); public Iob iobCalc(Treatment treatment, long time, Double dia);
} }

View file

@ -50,7 +50,7 @@ public class ActivityGraph extends GraphView {
List<DataPoint> iobArray = new ArrayList<DataPoint>(); List<DataPoint> iobArray = new ArrayList<DataPoint>();
for (long time = 0; time <= hours * 60 * 60 * 1000; time += 5 * 60 * 1000L) { for (long time = 0; time <= hours * 60 * 60 * 1000; time += 5 * 60 * 1000L) {
Iob iob = insulin.iobCalc(t, new Date(time), dia); Iob iob = insulin.iobCalc(t, time, dia);
activityArray.add(new DataPoint(time / 60 / 1000, iob.activityContrib)); activityArray.add(new DataPoint(time / 60 / 1000, iob.activityContrib));
iobArray.add(new DataPoint(time / 60 / 1000, iob.iobContrib)); iobArray.add(new DataPoint(time / 60 / 1000, iob.iobContrib));
} }

View file

@ -90,7 +90,7 @@ public class InsulinFastactingPlugin implements PluginBase, InsulinInterface {
} }
@Override @Override
public Iob iobCalc(Treatment treatment, Date time, Double dia) { public Iob iobCalc(Treatment treatment, long time, Double dia) {
Iob result = new Iob(); Iob result = new Iob();
Double scaleFactor = 3.0 / dia; Double scaleFactor = 3.0 / dia;
@ -99,7 +99,7 @@ public class InsulinFastactingPlugin implements PluginBase, InsulinInterface {
if (treatment.insulin != 0d) { if (treatment.insulin != 0d) {
Long bolusTime = treatment.created_at.getTime(); Long bolusTime = treatment.created_at.getTime();
Double minAgo = scaleFactor * (time.getTime() - bolusTime) / 1000d / 60d; Double minAgo = scaleFactor * (time - bolusTime) / 1000d / 60d;
if (minAgo < peak) { if (minAgo < peak) {
Double x1 = minAgo / 5d + 1; Double x1 = minAgo / 5d + 1;

View file

@ -90,7 +90,7 @@ public class InsulinFastactingProlongedPlugin implements PluginBase, InsulinInte
} }
@Override @Override
public Iob iobCalc(Treatment treatment, Date time, Double dia) { public Iob iobCalc(Treatment treatment, long time, Double dia) {
Iob result = new Iob(); Iob result = new Iob();
//Double scaleFactor = 3.0 / dia; //Double scaleFactor = 3.0 / dia;
@ -101,7 +101,7 @@ public class InsulinFastactingProlongedPlugin implements PluginBase, InsulinInte
if (treatment.insulin != 0d) { if (treatment.insulin != 0d) {
Long bolusTime = treatment.created_at.getTime(); Long bolusTime = treatment.created_at.getTime();
Double minAgo = (time.getTime() - bolusTime) / 1000d / 60d; Double minAgo = (time - bolusTime) / 1000d / 60d;
if (minAgo < peak) { if (minAgo < peak) {
Double x1 = 6 / dia * minAgo / 5d + 1; Double x1 = 6 / dia * minAgo / 5d + 1;

View file

@ -461,6 +461,17 @@ public class NSProfile {
return (int) (passed / 1000); return (int) (passed / 1000);
} }
public static int secondsFromMidnight(long date) {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(date);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
long passed = date - c.getTimeInMillis();
return (int) (passed / 1000);
}
public static Double toMgdl(Double value, String units) { public static Double toMgdl(Double value, String units) {
if (units.equals(Constants.MGDL)) return value; if (units.equals(Constants.MGDL)) return value;
else return value * Constants.MMOLL_TO_MGDL; else return value * Constants.MMOLL_TO_MGDL;

View file

@ -76,7 +76,7 @@ public class TempBasalsFragment extends Fragment {
holder.percent.setText(DecimalFormatter.to0Decimal(tempBasal.percent) + "%"); holder.percent.setText(DecimalFormatter.to0Decimal(tempBasal.percent) + "%");
} }
holder.realDuration.setText(DecimalFormatter.to0Decimal(tempBasal.getRealDuration()) + " min"); holder.realDuration.setText(DecimalFormatter.to0Decimal(tempBasal.getRealDuration()) + " min");
IobTotal iob = tempBasal.iobCalc(new Date()); IobTotal iob = tempBasal.iobCalc(new Date().getTime());
holder.iob.setText(DecimalFormatter.to2Decimal(iob.basaliob) + " U"); holder.iob.setText(DecimalFormatter.to2Decimal(iob.basaliob) + " U");
holder.netInsulin.setText(DecimalFormatter.to2Decimal(iob.netInsulin) + " U"); holder.netInsulin.setText(DecimalFormatter.to2Decimal(iob.netInsulin) + " U");
holder.netRatio.setText(DecimalFormatter.to2Decimal(iob.netRatio) + " U/h"); holder.netRatio.setText(DecimalFormatter.to2Decimal(iob.netRatio) + " U/h");
@ -85,7 +85,7 @@ public class TempBasalsFragment extends Fragment {
holder.dateLinearLayout.setBackgroundColor(ContextCompat.getColor(MainApp.instance(), R.color.colorInProgress)); holder.dateLinearLayout.setBackgroundColor(ContextCompat.getColor(MainApp.instance(), R.color.colorInProgress));
else if (tempBasal.timeEnd == null) else if (tempBasal.timeEnd == null)
holder.dateLinearLayout.setBackgroundColor(ContextCompat.getColor(MainApp.instance(), R.color.colorNotEnded)); holder.dateLinearLayout.setBackgroundColor(ContextCompat.getColor(MainApp.instance(), R.color.colorNotEnded));
else if (tempBasal.iobCalc(new Date()).basaliob != 0) else if (tempBasal.iobCalc(new Date().getTime()).basaliob != 0)
holder.dateLinearLayout.setBackgroundColor(ContextCompat.getColor(MainApp.instance(), R.color.colorAffectingIOB)); holder.dateLinearLayout.setBackgroundColor(ContextCompat.getColor(MainApp.instance(), R.color.colorAffectingIOB));
else else
holder.dateLinearLayout.setBackgroundColor(ContextCompat.getColor(MainApp.instance(), R.color.cardColorBackground)); holder.dateLinearLayout.setBackgroundColor(ContextCompat.getColor(MainApp.instance(), R.color.cardColorBackground));

View file

@ -128,8 +128,8 @@ public class TempBasalsPlugin implements PluginBase, TempBasalsInterface {
for (int position = list.size() - 1; position >= 0; position--) { for (int position = list.size() - 1; position >= 0; position--) {
TempBasal t = list.get(position); TempBasal t = list.get(position);
boolean update = false; boolean update = false;
if (t.timeEnd == null && t.getPlannedTimeEnd().getTime() < now) { if (t.timeEnd == null && t.getPlannedTimeEnd() < now) {
t.timeEnd = new Date(t.getPlannedTimeEnd().getTime()); t.timeEnd = new Date(t.getPlannedTimeEnd());
if (Config.logTempBasalsCut) if (Config.logTempBasalsCut)
log.debug("Add timeEnd to old record"); log.debug("Add timeEnd to old record");
update = true; update = true;
@ -137,7 +137,7 @@ public class TempBasalsPlugin implements PluginBase, TempBasalsInterface {
if (position > 0) { if (position > 0) {
Date startofnewer = list.get(position - 1).timeStart; Date startofnewer = list.get(position - 1).timeStart;
if (t.timeEnd == null) { if (t.timeEnd == null) {
t.timeEnd = new Date(Math.min(startofnewer.getTime(), t.getPlannedTimeEnd().getTime())); t.timeEnd = new Date(Math.min(startofnewer.getTime(), t.getPlannedTimeEnd()));
if (Config.logTempBasalsCut) if (Config.logTempBasalsCut)
log.debug("Add timeEnd to old record"); log.debug("Add timeEnd to old record");
update = true; update = true;
@ -180,19 +180,19 @@ public class TempBasalsPlugin implements PluginBase, TempBasalsInterface {
public IobTotal getCalculationToTime(long time) { public IobTotal getCalculationToTime(long time) {
checkForExpired(tempBasals); checkForExpired(tempBasals);
checkForExpired(extendedBoluses); checkForExpired(extendedBoluses);
Date now = new Date(time);
IobTotal total = new IobTotal(time); IobTotal total = new IobTotal(time);
for (Integer pos = 0; pos < tempBasals.size(); pos++) { for (Integer pos = 0; pos < tempBasals.size(); pos++) {
TempBasal t = tempBasals.get(pos); TempBasal t = tempBasals.get(pos);
if (t.timeStart.getTime() > time) continue; if (t.timeStart.getTime() > time) continue;
IobTotal calc = t.iobCalc(now); IobTotal calc = t.iobCalc(time);
log.debug("BasalIOB " + new Date(time) + " >>> " + calc.basaliob);
total.plus(calc); total.plus(calc);
} }
if (useExtendedBoluses) { if (useExtendedBoluses) {
for (Integer pos = 0; pos < extendedBoluses.size(); pos++) { for (Integer pos = 0; pos < extendedBoluses.size(); pos++) {
TempBasal t = extendedBoluses.get(pos); TempBasal t = extendedBoluses.get(pos);
if (t.timeStart.getTime() > time) continue; if (t.timeStart.getTime() > time) continue;
IobTotal calc = t.iobCalc(now); IobTotal calc = t.iobCalc(time);
total.plus(calc); total.plus(calc);
} }
} }

View file

@ -87,7 +87,7 @@ public class TreatmentsFragment extends Fragment implements View.OnClickListener
holder.date.setText(DateUtil.dateAndTimeString(treatments.get(position).created_at)); holder.date.setText(DateUtil.dateAndTimeString(treatments.get(position).created_at));
holder.insulin.setText(DecimalFormatter.to2Decimal(treatments.get(position).insulin) + " U"); holder.insulin.setText(DecimalFormatter.to2Decimal(treatments.get(position).insulin) + " U");
holder.carbs.setText(DecimalFormatter.to0Decimal(treatments.get(position).carbs) + " g"); holder.carbs.setText(DecimalFormatter.to0Decimal(treatments.get(position).carbs) + " g");
Iob iob = insulinInterface.iobCalc(treatments.get(position), new Date(), profile.getDia()); Iob iob = insulinInterface.iobCalc(treatments.get(position), new Date().getTime(), profile.getDia());
holder.iob.setText(DecimalFormatter.to2Decimal(iob.iobContrib) + " U"); holder.iob.setText(DecimalFormatter.to2Decimal(iob.iobContrib) + " U");
holder.activity.setText(DecimalFormatter.to3Decimal(iob.activityContrib) + " U"); holder.activity.setText(DecimalFormatter.to3Decimal(iob.activityContrib) + " U");
holder.mealOrCorrection.setText(treatments.get(position).mealBolus ? MainApp.sResources.getString(R.string.mealbolus) : MainApp.sResources.getString(R.string.correctionbous)); holder.mealOrCorrection.setText(treatments.get(position).mealBolus ? MainApp.sResources.getString(R.string.mealbolus) : MainApp.sResources.getString(R.string.correctionbous));

View file

@ -128,14 +128,13 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface {
Double dia = profile.getDia(); Double dia = profile.getDia();
Date now = new Date(time);
for (Integer pos = 0; pos < treatments.size(); pos++) { for (Integer pos = 0; pos < treatments.size(); pos++) {
Treatment t = treatments.get(pos); Treatment t = treatments.get(pos);
if (t.created_at.getTime() > time) continue; if (t.created_at.getTime() > time) continue;
Iob tIOB = insulinInterface.iobCalc(t, now, dia); Iob tIOB = insulinInterface.iobCalc(t, time, dia);
total.iob += tIOB.iobContrib; total.iob += tIOB.iobContrib;
total.activity += tIOB.activityContrib; total.activity += tIOB.activityContrib;
Iob bIOB = insulinInterface.iobCalc(t, now, dia / SP.getInt("openapsama_bolussnooze_dia_divisor", 2)); Iob bIOB = insulinInterface.iobCalc(t, time, dia / SP.getInt("openapsama_bolussnooze_dia_divisor", 2));
total.bolussnooze += bIOB.iobContrib; total.bolussnooze += bIOB.iobContrib;
} }
return total; return total;