optimize rendering

This commit is contained in:
Milos Kozak 2017-06-10 21:51:58 +02:00
parent e6f173f305
commit 723c90c32d
3 changed files with 93 additions and 31 deletions

View file

@ -22,10 +22,12 @@ import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.data.IobTotal;
import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.db.BgReading;
import info.nightscout.androidaps.db.TemporaryBasal;
import info.nightscout.androidaps.db.Treatment; import info.nightscout.androidaps.db.Treatment;
import info.nightscout.androidaps.events.EventNewBG; import info.nightscout.androidaps.events.EventNewBG;
import info.nightscout.androidaps.events.EventNewBasalProfile; import info.nightscout.androidaps.events.EventNewBasalProfile;
import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.plugins.IobCobCalculator.events.BasalData;
import info.nightscout.androidaps.plugins.IobCobCalculator.events.EventAutosensCalculationFinished; import info.nightscout.androidaps.plugins.IobCobCalculator.events.EventAutosensCalculationFinished;
import info.nightscout.androidaps.plugins.IobCobCalculator.events.EventNewHistoryData; import info.nightscout.androidaps.plugins.IobCobCalculator.events.EventNewHistoryData;
import info.nightscout.utils.Round; import info.nightscout.utils.Round;
@ -41,6 +43,7 @@ public class IobCobCalculatorPlugin implements PluginBase {
private static LongSparseArray<IobTotal> iobTable = new LongSparseArray<>(); // oldest at index 0 private static LongSparseArray<IobTotal> iobTable = new LongSparseArray<>(); // oldest at index 0
private static LongSparseArray<AutosensData> autosensDataTable = new LongSparseArray<>(); // oldest at index 0 private static LongSparseArray<AutosensData> autosensDataTable = new LongSparseArray<>(); // oldest at index 0
private static LongSparseArray<BasalData> basalDataTable = new LongSparseArray<>(); // oldest at index 0
private static volatile List<BgReading> bgReadings = null; // newest at index 0 private static volatile List<BgReading> bgReadings = null; // newest at index 0
private static volatile List<BgReading> bucketed_data = null; private static volatile List<BgReading> bucketed_data = null;
@ -352,6 +355,31 @@ public class IobCobCalculatorPlugin implements PluginBase {
return null; return null;
} }
public static BasalData getBasalData(long time) {
long now = new Date().getTime();
time = roundUpTime(time);
BasalData retval = basalDataTable.get(time);
if (retval == null) {
retval = new BasalData();
TemporaryBasal tb = MainApp.getConfigBuilder().getTempBasalFromHistory(time);
retval.basal = MainApp.getConfigBuilder().getProfile(time).getBasal(time);
if (tb != null) {
retval.isTempBasalRunning = true;
retval.tempBasalAbsolute = tb.tempBasalConvertedToAbsolute(time);
} else {
retval.isTempBasalRunning = false;
retval.tempBasalAbsolute = retval.basal;
}
if (time < now) {
basalDataTable.append(time, retval);
}
//log.debug(">>> Cache miss " + new Date(time).toLocaleString());
} else {
//log.debug(">>> Cache hit " + new Date(time).toLocaleString());
}
return retval;
}
public static AutosensData getAutosensData(long time) { public static AutosensData getAutosensData(long time) {
long now = new Date().getTime(); long now = new Date().getTime();
if (time > now) if (time > now)
@ -563,6 +591,15 @@ public class IobCobCalculatorPlugin implements PluginBase {
break; break;
} }
} }
for (int index = basalDataTable.size() - 1; index >= 0; index--) {
if (basalDataTable.keyAt(index) > time) {
if (Config.logAutosensData)
log.debug("Removing from basalDataTable: " + new Date(basalDataTable.keyAt(index)).toLocaleString());
basalDataTable.removeAt(index);
} else {
break;
}
}
} }
sHandler.post(new Runnable() { sHandler.post(new Runnable() {
@Override @Override

View file

@ -0,0 +1,11 @@
package info.nightscout.androidaps.plugins.IobCobCalculator.events;
/**
* Created by mike on 10.06.2017.
*/
public class BasalData {
public double basal;
public double tempBasalAbsolute;
public boolean isTempBasalRunning;
}

View file

@ -81,8 +81,6 @@ import info.nightscout.androidaps.db.Treatment;
import info.nightscout.androidaps.events.EventCareportalEventChange; import info.nightscout.androidaps.events.EventCareportalEventChange;
import info.nightscout.androidaps.events.EventExtendedBolusChange; import info.nightscout.androidaps.events.EventExtendedBolusChange;
import info.nightscout.androidaps.events.EventInitializationChanged; import info.nightscout.androidaps.events.EventInitializationChanged;
import info.nightscout.androidaps.events.EventNewBG;
import info.nightscout.androidaps.events.EventNewBasalProfile;
import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.events.EventPreferenceChange;
import info.nightscout.androidaps.events.EventPumpStatusChanged; import info.nightscout.androidaps.events.EventPumpStatusChanged;
import info.nightscout.androidaps.events.EventRefreshGui; import info.nightscout.androidaps.events.EventRefreshGui;
@ -98,6 +96,7 @@ import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesPlugin; import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesPlugin;
import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensData; import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensData;
import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin;
import info.nightscout.androidaps.plugins.IobCobCalculator.events.BasalData;
import info.nightscout.androidaps.plugins.IobCobCalculator.events.EventAutosensCalculationFinished; import info.nightscout.androidaps.plugins.IobCobCalculator.events.EventAutosensCalculationFinished;
import info.nightscout.androidaps.plugins.Loop.LoopPlugin; import info.nightscout.androidaps.plugins.Loop.LoopPlugin;
import info.nightscout.androidaps.plugins.Loop.events.EventNewOpenLoopNotification; import info.nightscout.androidaps.plugins.Loop.events.EventNewOpenLoopNotification;
@ -119,6 +118,7 @@ import info.nightscout.utils.BolusWizard;
import info.nightscout.utils.DateUtil; import info.nightscout.utils.DateUtil;
import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.DecimalFormatter;
import info.nightscout.utils.NSUpload; import info.nightscout.utils.NSUpload;
import info.nightscout.utils.Profiler;
import info.nightscout.utils.Round; import info.nightscout.utils.Round;
import info.nightscout.utils.SP; import info.nightscout.utils.SP;
import info.nightscout.utils.ToastUtils; import info.nightscout.utils.ToastUtils;
@ -359,15 +359,15 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
switch (buttonView.getId()) { switch (buttonView.getId()) {
case R.id.overview_showprediction: case R.id.overview_showprediction:
SP.putBoolean("showprediction", showPredictionView.isChecked()); SP.putBoolean("showprediction", showPredictionView.isChecked());
scheduleUpdateGUI("onPredictionCheckedChanged"); updateGUI("onPredictionCheckedChanged");
break; break;
case R.id.overview_showbasals: case R.id.overview_showbasals:
SP.putBoolean("showbasals", showBasalsView.isChecked()); SP.putBoolean("showbasals", showBasalsView.isChecked());
scheduleUpdateGUI("onBasalsCheckedChanged"); updateGUI("onBasalsCheckedChanged");
break; break;
case R.id.overview_showiob: case R.id.overview_showiob:
SP.putBoolean("showiob", showIobView.isChecked()); SP.putBoolean("showiob", showIobView.isChecked());
scheduleUpdateGUI("onIobCheckedChanged"); updateGUI("onIobCheckedChanged");
break; break;
case R.id.overview_showcob: case R.id.overview_showcob:
showDeviationsView.setOnCheckedChangeListener(null); showDeviationsView.setOnCheckedChangeListener(null);
@ -375,7 +375,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
showDeviationsView.setOnCheckedChangeListener(this); showDeviationsView.setOnCheckedChangeListener(this);
SP.putBoolean("showcob", showCobView.isChecked()); SP.putBoolean("showcob", showCobView.isChecked());
SP.putBoolean("showdeviations", showDeviationsView.isChecked()); SP.putBoolean("showdeviations", showDeviationsView.isChecked());
scheduleUpdateGUI("onCobCheckedChanged"); updateGUI("onCobCheckedChanged");
break; break;
case R.id.overview_showdeviations: case R.id.overview_showdeviations:
showCobView.setOnCheckedChangeListener(null); showCobView.setOnCheckedChangeListener(null);
@ -383,7 +383,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
showCobView.setOnCheckedChangeListener(this); showCobView.setOnCheckedChangeListener(this);
SP.putBoolean("showcob", showCobView.isChecked()); SP.putBoolean("showcob", showCobView.isChecked());
SP.putBoolean("showdeviations", showDeviationsView.isChecked()); SP.putBoolean("showdeviations", showDeviationsView.isChecked());
scheduleUpdateGUI("onDeviationsCheckedChanged"); updateGUI("onDeviationsCheckedChanged");
break; break;
} }
} }
@ -764,7 +764,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
@Subscribe @Subscribe
public void onStatusEvent(final EventAutosensCalculationFinished ev) { public void onStatusEvent(final EventAutosensCalculationFinished ev) {
scheduleUpdateGUI("EventRefreshGui"); scheduleUpdateGUI("EventAutosensCalculationFinished");
} }
@Subscribe @Subscribe
@ -787,20 +787,22 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
scheduleUpdateGUI("EventExtendedBolusChange"); scheduleUpdateGUI("EventExtendedBolusChange");
} }
@Subscribe // Handled by EventAutosensCalculationFinished
public void onStatusEvent(final EventNewBG ev) { // @Subscribe
scheduleUpdateGUI("EventNewBG"); // public void onStatusEvent(final EventNewBG ev) {
} // scheduleUpdateGUI("EventNewBG");
// }
@Subscribe @Subscribe
public void onStatusEvent(final EventNewOpenLoopNotification ev) { public void onStatusEvent(final EventNewOpenLoopNotification ev) {
scheduleUpdateGUI("EventNewOpenLoopNotification"); scheduleUpdateGUI("EventNewOpenLoopNotification");
} }
@Subscribe // Handled by EventAutosensCalculationFinished
public void onStatusEvent(final EventNewBasalProfile ev) { // @Subscribe
scheduleUpdateGUI("EventNewBasalProfile"); // public void onStatusEvent(final EventNewBasalProfile ev) {
} // scheduleUpdateGUI("EventNewBasalProfile");
// }
@Subscribe @Subscribe
public void onStatusEvent(final EventTempTargetChange ev) { public void onStatusEvent(final EventTempTargetChange ev) {
@ -876,13 +878,14 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
if (scheduledUpdate != null) if (scheduledUpdate != null)
scheduledUpdate.cancel(false); scheduledUpdate.cancel(false);
Runnable task = new UpdateRunnable(); Runnable task = new UpdateRunnable();
final int msec = 400; final int msec = 2000;
scheduledUpdate = worker.schedule(task, msec, TimeUnit.MILLISECONDS); scheduledUpdate = worker.schedule(task, msec, TimeUnit.MILLISECONDS);
} }
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
public void updateGUI(String from) { public void updateGUI(String from) {
log.debug("updateGUI entered from: " + from); log.debug("updateGUI entered from: " + from);
Date updateGUIStart = new Date();
if (getActivity() == null) if (getActivity() == null)
return; return;
@ -1152,6 +1155,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
} }
// ****** GRAPH ******* // ****** GRAPH *******
//log.debug("updateGUI checkpoint 1");
// allign to hours // allign to hours
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
@ -1170,12 +1174,12 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
predHours = Math.min(2, predHours); predHours = Math.min(2, predHours);
predHours = Math.max(0, predHours); predHours = Math.max(0, predHours);
hoursToFetch = rangeToDisplay - predHours; hoursToFetch = rangeToDisplay - predHours;
toTime = calendar.getTimeInMillis() + 100000; // little bit more to avoid wrong rounding toTime = calendar.getTimeInMillis() + 60000; // little bit more to avoid wrong rounding
fromTime = toTime - hoursToFetch * 60 * 60 * 1000L; fromTime = toTime - hoursToFetch * 60 * 60 * 1000L;
endTime = toTime + predHours * 60 * 60 * 1000L; endTime = toTime + predHours * 60 * 60 * 1000L;
} else { } else {
hoursToFetch = rangeToDisplay; hoursToFetch = rangeToDisplay;
toTime = calendar.getTimeInMillis() + 100000; // little bit more to avoid wrong rounding toTime = calendar.getTimeInMillis() + 600000; // little bit more to avoid wrong rounding
fromTime = toTime - hoursToFetch * 60 * 60 * 1000L; fromTime = toTime - hoursToFetch * 60 * 60 * 1000L;
endTime = toTime; endTime = toTime;
} }
@ -1200,15 +1204,14 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
double lastAbsoluteLineBasal = 0; double lastAbsoluteLineBasal = 0;
double lastBaseBasal = 0; double lastBaseBasal = 0;
double lastTempBasal = 0; double lastTempBasal = 0;
for (long time = fromTime; time < now; time += 1 * 60 * 1000L) { for (long time = fromTime; time < now; time += 60 * 1000L) {
TemporaryBasal tb = MainApp.getConfigBuilder().getTempBasalFromHistory(time); BasalData basalData = IobCobCalculatorPlugin.getBasalData(time);
double baseBasalValue = MainApp.getConfigBuilder().getProfile(time).getBasal(Profile.secondsFromMidnight(time)); double baseBasalValue = basalData.basal;
double baseLineValue = baseBasalValue;
double absoluteLineValue = baseBasalValue; double absoluteLineValue = baseBasalValue;
double tempBasalValue = 0; double tempBasalValue = 0;
double basal = 0d; double basal = 0d;
if (tb != null) { if (basalData.isTempBasalRunning) {
absoluteLineValue = tempBasalValue = tb.tempBasalConvertedToAbsolute(new Date(time).getTime()); absoluteLineValue = tempBasalValue = basalData.tempBasalAbsolute;
if (tempBasalValue != lastTempBasal) { if (tempBasalValue != lastTempBasal) {
tempBasalArray.add(new DataPoint(time, lastTempBasal)); tempBasalArray.add(new DataPoint(time, lastTempBasal));
tempBasalArray.add(new DataPoint(time, basal = tempBasalValue)); tempBasalArray.add(new DataPoint(time, basal = tempBasalValue));
@ -1230,9 +1233,9 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
} }
} }
if (baseLineValue != lastLineBasal) { if (baseBasalValue != lastLineBasal) {
basalLineArray.add(new DataPoint(time, lastLineBasal)); basalLineArray.add(new DataPoint(time, lastLineBasal));
basalLineArray.add(new DataPoint(time, baseLineValue)); basalLineArray.add(new DataPoint(time, baseBasalValue));
} }
if (absoluteLineValue != lastAbsoluteLineBasal) { if (absoluteLineValue != lastAbsoluteLineBasal) {
absoluteBasalLineArray.add(new DataPoint(time, lastAbsoluteLineBasal)); absoluteBasalLineArray.add(new DataPoint(time, lastAbsoluteLineBasal));
@ -1240,7 +1243,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
} }
lastAbsoluteLineBasal = absoluteLineValue; lastAbsoluteLineBasal = absoluteLineValue;
lastLineBasal = baseLineValue; lastLineBasal = baseBasalValue;
lastTempBasal = tempBasalValue; lastTempBasal = tempBasalValue;
maxBasalValueFound = Math.max(maxBasalValueFound, basal); maxBasalValueFound = Math.max(maxBasalValueFound, basal);
} }
@ -1283,6 +1286,8 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
absoluteBasalsLineSeries.setCustomPaint(absolutePaint); absoluteBasalsLineSeries.setCustomPaint(absolutePaint);
} }
//log.debug("updateGUI checkpoint 2");
// **** IOB COB DEV graph **** // **** IOB COB DEV graph ****
class DeviationDataPoint extends DataPoint { class DeviationDataPoint extends DataPoint {
public int color; public int color;
@ -1399,10 +1404,12 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
} else { } else {
iobGraph.setVisibility(View.GONE); iobGraph.setVisibility(View.GONE);
} }
//log.debug("updateGUI checkpoint 3");
// remove old data from graph // remove old data from graph
bgGraph.getSecondScale().getSeries().clear(); bgGraph.getSecondScale().getSeries().clear();
bgGraph.getSeries().clear(); bgGraph.getSeries().clear();
//log.debug("updateGUI checkpoint 4");
// **** Area **** // **** Area ****
DoubleDataPoint[] areaDataPoints = new DoubleDataPoint[]{ DoubleDataPoint[] areaDataPoints = new DoubleDataPoint[]{
@ -1427,6 +1434,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
iobGraph.getGridLabelRenderer().setLabelFormatter(new TimeAsXAxisLabelFormatter(getActivity(), "HH")); iobGraph.getGridLabelRenderer().setLabelFormatter(new TimeAsXAxisLabelFormatter(getActivity(), "HH"));
iobGraph.getGridLabelRenderer().setNumHorizontalLabels(7); // only 7 because of the space iobGraph.getGridLabelRenderer().setNumHorizontalLabels(7); // only 7 because of the space
//log.debug("updateGUI checkpoint 5");
// **** BG graph **** // **** BG graph ****
List<BgReading> bgReadingsArray = MainApp.getDbHelper().getBgreadingsDataFromTime(fromTime, true); List<BgReading> bgReadingsArray = MainApp.getDbHelper().getBgreadingsDataFromTime(fromTime, true);
List<DataPointWithLabelInterface> bgListArray = new ArrayList<>(); List<DataPointWithLabelInterface> bgListArray = new ArrayList<>();
@ -1462,6 +1470,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
addSeriesWithoutInvalidate(new PointsWithLabelGraphSeries<>(bg), bgGraph); addSeriesWithoutInvalidate(new PointsWithLabelGraphSeries<>(bg), bgGraph);
} }
//log.debug("updateGUI checkpoint 6");
// Treatments // Treatments
List<DataPointWithLabelInterface> filteredTreatments = new ArrayList<>(); List<DataPointWithLabelInterface> filteredTreatments = new ArrayList<>();
@ -1474,6 +1483,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
filteredTreatments.add(t); filteredTreatments.add(t);
} }
//log.debug("updateGUI checkpoint 7");
// ProfileSwitch // ProfileSwitch
List<ProfileSwitch> profileSwitches = MainApp.getConfigBuilder().getProfileSwitchesFromHistory().getList(); List<ProfileSwitch> profileSwitches = MainApp.getConfigBuilder().getProfileSwitchesFromHistory().getList();
@ -1483,6 +1493,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
filteredTreatments.add(t); filteredTreatments.add(t);
} }
//log.debug("updateGUI checkpoint 8");
// Extended bolus // Extended bolus
if (!pump.isFakingTempsByExtendedBoluses()) { if (!pump.isFakingTempsByExtendedBoluses()) {
List<ExtendedBolus> extendedBoluses = MainApp.getConfigBuilder().getExtendedBolusesFromHistory().getList(); List<ExtendedBolus> extendedBoluses = MainApp.getConfigBuilder().getExtendedBolusesFromHistory().getList();
@ -1496,6 +1507,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
} }
} }
//log.debug("updateGUI checkpoint 9");
// Careportal // Careportal
List<CareportalEvent> careportalEvents = MainApp.getDbHelper().getCareportalEventsFromTime(fromTime, true); List<CareportalEvent> careportalEvents = MainApp.getDbHelper().getCareportalEventsFromTime(fromTime, true);
@ -1511,6 +1523,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
if (treatmentsArray.length > 0) { if (treatmentsArray.length > 0) {
addSeriesWithoutInvalidate(new PointsWithLabelGraphSeries<>(treatmentsArray), bgGraph); addSeriesWithoutInvalidate(new PointsWithLabelGraphSeries<>(treatmentsArray), bgGraph);
} }
//log.debug("updateGUI checkpoint 10");
// set manual y bounds to have nice steps // set manual y bounds to have nice steps
bgGraph.getViewport().setMaxY(maxBgValue); bgGraph.getViewport().setMaxY(maxBgValue);
@ -1520,12 +1533,12 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
// set second scale // set second scale
if (pump.getPumpDescription().isTempBasalCapable && showBasalsView.isChecked()) { if (pump.getPumpDescription().isTempBasalCapable && showBasalsView.isChecked()) {
bgGraph.getSecondScale().setMinY(0);
bgGraph.getSecondScale().setMaxY(maxBgValue / lowLine * maxBasalValueFound * 1.2d);
bgGraph.getSecondScale().addSeries(baseBasalsSeries); bgGraph.getSecondScale().addSeries(baseBasalsSeries);
bgGraph.getSecondScale().addSeries(tempBasalsSeries); bgGraph.getSecondScale().addSeries(tempBasalsSeries);
bgGraph.getSecondScale().addSeries(basalsLineSeries); bgGraph.getSecondScale().addSeries(basalsLineSeries);
bgGraph.getSecondScale().addSeries(absoluteBasalsLineSeries); bgGraph.getSecondScale().addSeries(absoluteBasalsLineSeries);
bgGraph.getSecondScale().setMinY(0);
bgGraph.getSecondScale().setMaxY(maxBgValue / lowLine * maxBasalValueFound * 1.2d);
} }
bgGraph.getSecondScale().setLabelFormatter(new LabelFormatter() { bgGraph.getSecondScale().setLabelFormatter(new LabelFormatter() {
@Override @Override
@ -1539,6 +1552,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
} }
}); });
//log.debug("updateGUI checkpoint 11");
// **** NOW line **** // **** NOW line ****
DataPoint[] nowPoints = new DataPoint[]{ DataPoint[] nowPoints = new DataPoint[]{
new DataPoint(now, 0), new DataPoint(now, 0),
@ -1566,7 +1580,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
if (updating != null) if (updating != null)
updating.setVisibility(View.GONE); updating.setVisibility(View.GONE);
log.debug("updateGUI finshed"); Profiler.log(log, from, updateGUIStart);
} }
public double getNearestBg(long date, List<BgReading> bgReadingsArray) { public double getNearestBg(long date, List<BgReading> bgReadingsArray) {