Skip to content

Commit

Permalink
cache midnight time calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
MilosKozak committed Oct 28, 2018
1 parent 0034952 commit d54651e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 22 deletions.
30 changes: 10 additions & 20 deletions app/src/main/java/info/nightscout/androidaps/data/Profile.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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);
Expand Down Expand Up @@ -381,7 +382,7 @@ private String getValuesList(LongSparseArray<Double> array, LongSparseArray<Doub
}

public double getIsf() {
return getIsfTimeFromMidnight(secondsFromMidnight(System.currentTimeMillis()));
return getIsfTimeFromMidnight(secondsFromMidnight());
}

public double getIsf(long time) {
Expand All @@ -401,7 +402,7 @@ public String getIsfList() {
}

public double getIc() {
return getIcTimeFromMidnight(secondsFromMidnight(System.currentTimeMillis()));
return getIcTimeFromMidnight(secondsFromMidnight());
}

public double getIc(long time) {
Expand All @@ -421,7 +422,7 @@ public String getIcList() {
}

public double getBasal() {
return getBasalTimeFromMidnight(secondsFromMidnight(System.currentTimeMillis()));
return getBasalTimeFromMidnight(secondsFromMidnight());
}

public double getBasal(long time) {
Expand Down Expand Up @@ -465,15 +466,15 @@ public synchronized BasalValue[] getBasalValues() {
}

public double getTarget() {
return getTarget(secondsFromMidnight(System.currentTimeMillis()));
return getTarget(secondsFromMidnight());
}

protected double getTarget(int timeAsSeconds) {
return (getTargetLowTimeFromMidnight(timeAsSeconds) + getTargetHighTimeFromMidnight(timeAsSeconds)) / 2;
}

public double getTargetLow() {
return getTargetLowTimeFromMidnight(secondsFromMidnight(System.currentTimeMillis()));
return getTargetLowTimeFromMidnight(secondsFromMidnight());
}

public double getTargetLow(long time) {
Expand All @@ -487,7 +488,7 @@ public double getTargetLowTimeFromMidnight(int timeAsSeconds) {
}

public double getTargetHigh() {
return getTargetHighTimeFromMidnight(secondsFromMidnight(System.currentTimeMillis()));
return getTargetHighTimeFromMidnight(secondsFromMidnight());
}

public double getTargetHigh(long time) {
Expand Down Expand Up @@ -518,24 +519,13 @@ public double getMaxDailyBasal() {
}

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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
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;

Expand Down Expand Up @@ -395,8 +396,10 @@ public final void run() {
} 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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
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;

Expand Down Expand Up @@ -322,8 +323,10 @@ public final void run() {
} 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);
}
}
Expand Down
43 changes: 43 additions & 0 deletions app/src/main/java/info/nightscout/utils/MidnightTime.java
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit d54651e

Please sign in to comment.