DefaultProfileBuilder implements ProfileBuilder, Serializable { @Override public Optional<ProfileMeasurement> flush() { Optional<ProfileMeasurement> result; ProfilePeriod period = ProfilePeriod.fromTimestamp(maxTimestamp, periodDurationMillis, TimeUnit.MILLISECONDS); try { String profileExpression = definition .getResult() .getProfileExpressions() .getExpression(); Object profileValue = execute(profileExpression, "result/profile"); Map<String, Object> triageValues = definition .getResult() .getTriageExpressions() .getExpressions() .entrySet() .stream() .collect(Collectors.toMap( e -> e.getKey(), e -> execute(e.getValue(), "result/triage"))); Map<String, Object> state = new HashMap<>(); state.put("profile", profileName); state.put("entity", entity); state.put("start", period.getStartTimeMillis()); state.put("end", period.getEndTimeMillis()); state.put("duration", period.getDurationMillis()); state.put("result", profileValue); List<Object> groups = execute(definition.getGroupBy(), state, "groupBy"); result = Optional.of(new ProfileMeasurement() .withProfileName(profileName) .withEntity(entity) .withGroups(groups) .withPeriod(period) .withProfileValue(profileValue) .withTriageValues(triageValues) .withDefinition(definition)); } catch(Throwable e) { LOG.error(format("Unable to flush profile: error=%s", e.getMessage()), e); result = Optional.empty(); } LOG.debug("Flushed profile: profile={}, entity={}, maxTime={}, period={}, start={}, end={}, duration={}", profileName, entity, maxTimestamp, period.getPeriod(), period.getStartTimeMillis(), period.getEndTimeMillis(), period.getDurationMillis()); isInitialized = false; return result; } private  DefaultProfileBuilder(ProfileConfig definition,
                                String entity,
                                long periodDurationMillis,
                                Context stellarContext); @Override void apply(JSONObject message, long timestamp); @Override Optional<ProfileMeasurement> flush(); @Override Object valueOf(String variable); @Override boolean isInitialized(); @Override ProfileConfig getDefinition(); @Override String getEntity();  }
@Test public void testInitWithNoMessage() throws Exception { long timestamp = 100; definition = JSONUtils.INSTANCE.load(testInitProfile, ProfileConfig.class); builder = new DefaultProfileBuilder.Builder() .withDefinition(definition) .withEntity("10.0.0.1") .withPeriodDuration(10, TimeUnit.MINUTES) .withContext(Context.EMPTY_CONTEXT()) .build(); Optional<ProfileMeasurement> m = builder.flush(); assertTrue(m.isPresent()); assertEquals(0, (int) convert(m.get().getProfileValue(), Integer.class)); }