// pass now for olderThanTime to ensure all existing baselines are deleted
// pass 30000 for amountOfData to only include the youngest in the baseline calculation
long computeTime = baselineManager.calculateAutoBaselines(3000, System.currentTimeMillis());
assert computeTime > 0;
MeasurementBaseline bl1;
MeasurementBaseline bl2;
int bl1Id;
int bl2Id;
Date bl1ComputeTime;
Date bl2ComputeTime;
begin();
bl1 = em.find(MeasurementSchedule.class, measSched.getId()).getBaseline();
assert bl1 != null : "Baseline for measSched should have been inserted";
assert bl1.getSchedule().getId() == measSched.getId();
assert !bl1.isUserEntered();
assertEquals(30.0, bl1.getMin());
assertEquals(50.0, bl1.getMax());
assertEquals(40.0, bl1.getMean());
bl2 = em.find(MeasurementSchedule.class, measSched2.getId()).getBaseline();
assert bl2 != null : "Baseline for measSched2 should have been inserted";
assert bl2.getSchedule().getId() == measSched2.getId();
assert !bl2.isUserEntered();
assertEquals(500.0, bl2.getMin());
assertEquals(2500.0, bl2.getMax());
assertEquals(1500.0, bl2.getMean());
// remember these, the next time we calculate, they will be deleted and new ones created
bl1Id = bl1.getId();
bl2Id = bl2.getId();
bl1ComputeTime = bl1.getComputeTime();
bl2ComputeTime = bl2.getComputeTime();
commit();
// calculate them again, the values will be the same, but the delete query will be triggered
// wait a bit so our compute time will be assured to be different
Thread.sleep(1000L);
computeTime = baselineManager.calculateAutoBaselines(30000, System.currentTimeMillis());
assert computeTime > 0;
begin();
bl1 = em.find(MeasurementSchedule.class, measSched.getId()).getBaseline();
assert bl1 != null : "Baseline for measSched should have been inserted";
assert bl1.getSchedule().getId() == measSched.getId();
assert !bl1.isUserEntered();
assertEquals(30.0, bl1.getMin());
assertEquals(50.0, bl1.getMax());
assertEquals(40.0, bl1.getMean());
bl2 = em.find(MeasurementSchedule.class, measSched2.getId()).getBaseline();
assert bl2 != null : "Baseline for measSched2 should have been inserted";
assert bl2.getSchedule().getId() == measSched2.getId();
assert !bl2.isUserEntered();
assertEquals(500.0, bl2.getMin());
assertEquals(2500.0, bl2.getMax());
assertEquals(1500.0, bl2.getMean());
// check the new IDs with the old ones - they should be different due to the delete query
assert bl1.getId() != bl1Id : "bl1.getId() was " + bl1.getId() + ", bl1Id was " + bl1Id;
assert bl2.getId() != bl2Id : "bl2.getId() was " + bl2.getId() + ", bl2Id was " + bl2Id;
assert bl1.getComputeTime().after(bl1ComputeTime);
assert bl2.getComputeTime().after(bl2ComputeTime);
commit();
// calculate them again using wider ranges to include more (all available) 1H data in the calculation
Thread.sleep(1000L);
computeTime = baselineManager.calculateAutoBaselines(240000, System.currentTimeMillis());
assert computeTime > 0;
begin();
bl1 = em.find(MeasurementSchedule.class, measSched.getId()).getBaseline();
assert bl1 != null : "Baseline for measSched should have been inserted";
assert bl1.getSchedule().getId() == measSched.getId();
assert !bl1.isUserEntered();
assertEquals(2.0, bl1.getMin());
assertEquals(50.00, bl1.getMax());
assertEquals(20.25, bl1.getMean());
bl2 = em.find(MeasurementSchedule.class, measSched2.getId()).getBaseline();
assert bl2 != null : "Baseline for measSched2 should have been inserted";
assert bl2.getSchedule().getId() == measSched2.getId();
assert !bl2.isUserEntered();
assertEquals(500.0, bl2.getMin());
assertEquals(7000.0, bl2.getMax());
assertEquals(3375.0, bl2.getMean());
// check the new IDs with the old ones - they should be different due to the delete query
assert bl1.getId() != bl1Id;
assert bl2.getId() != bl2Id;
assert bl1.getComputeTime().after(bl1ComputeTime);
assert bl2.getComputeTime().after(bl2ComputeTime);
commit();
} catch (Throwable t) {
System.out.println("TEST FAILURE STACK TRACE FOLLOWS:");
t.printStackTrace();