cache midnight time calculation

This commit is contained in:
Milos Kozak 2018-10-28 12:21:35 +01:00
parent 00349527e1
commit d54651e916
4 changed files with 61 additions and 22 deletions

View file

@ -23,6 +23,7 @@ import info.nightscout.androidaps.plugins.Overview.notifications.Notification;
import info.nightscout.utils.DateUtil;
import info.nightscout.utils.DecimalFormatter;
import info.nightscout.utils.FabricPrivacy;
import info.nightscout.utils.MidnightTime;
public class Profile {
private static Logger log = LoggerFactory.getLogger(Profile.class);
@ -381,7 +382,7 @@ public class Profile {
}
public double getIsf() {
return getIsfTimeFromMidnight(secondsFromMidnight(System.currentTimeMillis()));
return getIsfTimeFromMidnight(secondsFromMidnight());
}
public double getIsf(long time) {
@ -401,7 +402,7 @@ public class Profile {
}
public double getIc() {
return getIcTimeFromMidnight(secondsFromMidnight(System.currentTimeMillis()));
return getIcTimeFromMidnight(secondsFromMidnight());
}
public double getIc(long time) {
@ -421,7 +422,7 @@ public class Profile {
}
public double getBasal() {
return getBasalTimeFromMidnight(secondsFromMidnight(System.currentTimeMillis()));
return getBasalTimeFromMidnight(secondsFromMidnight());
}
public double getBasal(long time) {
@ -465,7 +466,7 @@ public class Profile {
}
public double getTarget() {
return getTarget(secondsFromMidnight(System.currentTimeMillis()));
return getTarget(secondsFromMidnight());
}
protected double getTarget(int timeAsSeconds) {
@ -473,7 +474,7 @@ public class Profile {
}
public double getTargetLow() {
return getTargetLowTimeFromMidnight(secondsFromMidnight(System.currentTimeMillis()));
return getTargetLowTimeFromMidnight(secondsFromMidnight());
}
public double getTargetLow(long time) {
@ -487,7 +488,7 @@ public class Profile {
}
public double getTargetHigh() {
return getTargetHighTimeFromMidnight(secondsFromMidnight(System.currentTimeMillis()));
return getTargetHighTimeFromMidnight(secondsFromMidnight());
}
public double getTargetHigh(long time) {
@ -518,24 +519,13 @@ public class Profile {
}
public static int secondsFromMidnight() {
Calendar c = Calendar.getInstance();
long now = c.getTimeInMillis();
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
long passed = now - c.getTimeInMillis();
long passed = DateUtil.now() - MidnightTime.calc();
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();
long midnight = MidnightTime.calc(date);
long passed = date - midnight;
return (int) (passed / 1000);
}

View file

@ -38,6 +38,7 @@ import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin;
import info.nightscout.utils.DateUtil;
import info.nightscout.utils.DecimalFormatter;
import info.nightscout.utils.FabricPrivacy;
import info.nightscout.utils.MidnightTime;
import info.nightscout.utils.Profiler;
import info.nightscout.utils.SP;
@ -395,8 +396,10 @@ public class IobCobOref1Thread extends Thread {
} finally {
mWakeLock.release();
MainApp.bus().post(new EventIobCalculationProgress(""));
if (L.isEnabled(L.AUTOSENS))
if (L.isEnabled(L.AUTOSENS)) {
log.debug("AUTOSENSDATA thread ended: " + from);
log.debug("Midnights: " + MidnightTime.log());
}
Profiler.log(log, "IobCobOref1Thread", start);
}
}

View file

@ -38,6 +38,7 @@ import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin;
import info.nightscout.utils.DateUtil;
import info.nightscout.utils.DecimalFormatter;
import info.nightscout.utils.FabricPrivacy;
import info.nightscout.utils.MidnightTime;
import info.nightscout.utils.Profiler;
import info.nightscout.utils.SP;
@ -322,8 +323,10 @@ public class IobCobThread extends Thread {
} finally {
mWakeLock.release();
MainApp.bus().post(new EventIobCalculationProgress(""));
if (L.isEnabled(L.AUTOSENS))
if (L.isEnabled(L.AUTOSENS)) {
log.debug("AUTOSENSDATA thread ended: " + from);
log.debug("Midnights: " + MidnightTime.log());
}
Profiler.log(log, "IobCobThread", start);
}
}

View file

@ -0,0 +1,43 @@
package info.nightscout.utils;
import android.util.LongSparseArray;
import java.util.Calendar;
public class MidnightTime {
private static LongSparseArray times = new LongSparseArray();
private static long hits = 0;
private static long misses = 0;
public static long calc() {
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return c.getTimeInMillis();
}
public static long calc(long time) {
Long m = (Long) times.get(time);
if (m != null) {
++hits;
return m;
}
Calendar c = Calendar.getInstance();
c.setTimeInMillis(time);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
m = c.getTimeInMillis();
times.append(time, m);
++misses;
return m;
}
public static String log() {
return "Hits: " + hits + " misses: " + misses + " stored: " + times.size();
}
}