PersistentHigh { public static boolean dataQualityCheck(final long since, final double highMarkMgDl) { final Sensor sensor = Sensor.currentSensor(); if (sensor == null) { Log.e(TAG, "Cannot raise persistent high alert as no active sensor!"); return false; } if (since < sensor.started_at) { Log.e(TAG, "Cannot raise persistent high alert as high time pre-dates sensor start"); return false; } final long duration = msSince(since); if (duration > Constants.DAY_IN_MS || duration < 0) { Log.e(TAG, "Cannot raise persistent high alert as duration doesn't make sense: " + JoH.niceTimeScalar(duration)); return false; } final List<BgReading> readings = BgReading.latestForSensorAsc(2000, since, JoH.tsl(), Home.get_follower()); if (readings == null) { Log.e(TAG, "Cannot raise persistent high alert as there are no readings for this sensor!"); return false; } final int numberOfReadings = readings.size(); if (numberOfReadings == 0) { Log.e(TAG, "Cannot raise persistent high alert as there are 0 readings for this sensor!"); return false; } final long frequency = duration / numberOfReadings; if (frequency > MINUTE_IN_MS * 15) { Log.e(TAG, "Cannot raise persistent high alert as readings frequency is: " + niceTimeScalar(frequency)); return false; } for (final BgReading bgr : readings) { if (bgr.getDg_mgdl() < highMarkMgDl) { Log.e(TAG, "High not persistent as reading at: " + JoH.dateTimeText(bgr.timestamp) + " does not exceed " + JoH.qs(highMarkMgDl) + " mgdl / high mark"); return false; } } return true; }  static boolean checkForPersistentHigh(); static boolean dataQualityCheck(final long since, final double highMarkMgDl);  }
@Test public void dataQualityCheckTest() { assertWithMessage("No sensor invalid time should fail").that(PersistentHigh.dataQualityCheck(1000, HIGH_MARK)).isFalse(); assertWithMessage("No sensor ok time should fail").that(PersistentHigh.dataQualityCheck(START_TIME, HIGH_MARK)).isFalse(); assertWithMessage("No sensor ok time should fail 2").that(PersistentHigh.dataQualityCheck(JoH.tsl(), HIGH_MARK)).isFalse(); Sensor.create(1005); assertWithMessage("Predating sensor should fail").that(PersistentHigh.dataQualityCheck(1000, HIGH_MARK)).isFalse(); assertWithMessage("Post sensor start no data should fail").that(PersistentHigh.dataQualityCheck(JoH.tsl(), HIGH_MARK)).isFalse(); Sensor.create(START_TIME); for (int i = 0; i < (12 * 4); i++) { final long timestamp = START_TIME + Constants.MINUTE_IN_MS * 5 * i; final BgReading bgr = BgReading.bgReadingInsertFromG5(400, timestamp); assertWithMessage("Test result A: " + i + " " + JoH.dateTimeText(timestamp) + " different to expected") .that(PersistentHigh.dataQualityCheck(START_TIME + Constants.MINUTE_IN_MS * 10, HIGH_MARK)) .isEqualTo(i > 16); } START_TIME++; Sensor.create(START_TIME); for (int i = 0; i < (12 * 4); i++) { final long timestamp = START_TIME + Constants.MINUTE_IN_MS * 5 * i; final BgReading bgr = BgReading.bgReadingInsertFromG5(i == 20 ? 100 : 400, timestamp); assertWithMessage("Test result C: " + i + " " + JoH.dateTimeText(timestamp) + " different to expected") .that(PersistentHigh.dataQualityCheck(START_TIME + Constants.MINUTE_IN_MS * 10, HIGH_MARK)) .isEqualTo(i > 16 && i < 20); } START_TIME++; Sensor.create(START_TIME); for (int i = 0; i < (12 * 4); i++) { final long timestamp = START_TIME + Constants.MINUTE_IN_MS * 5 * i; final BgReading bgr = BgReading.bgReadingInsertFromG5(i == 8 ? 100 : 400, timestamp); assertWithMessage("Test result D: " + i + " " + JoH.dateTimeText(timestamp) + " different to expected") .that(PersistentHigh.dataQualityCheck(START_TIME + Constants.MINUTE_IN_MS * 10, HIGH_MARK)) .isFalse(); } List<String> logMessages = ShadowLog.getLogsForTag("PersistentHigh").stream() .map(log -> log.msg) .collect(Collectors.toList()); assertThat(logMessages).contains("Cannot raise persistent high alert as readings frequency is: 3 hours"); printLogs("PersistentHigh"); }