/** * Construct a ProfileBuilder. */ public ProfileBuilder build() { if(definition == null) { throw new IllegalArgumentException("missing profiler definition; got null"); } if(StringUtils.isEmpty(entity)) { throw new IllegalArgumentException(format("missing entity name; got '%s'", entity)); } if(periodDurationMillis == null) { throw new IllegalArgumentException("missing period duration"); } return new DefaultProfileBuilder(definition, entity, periodDurationMillis, context); } }
/** * Executes an expression contained within the profile definition. * * @param expression The expression to execute. * @param expressionType The type of expression; init, update, result. Provides additional context if expression execution fails. * @return The result of executing the expression. */ private Object execute(String expression, String expressionType) { return execute(expression, Collections.emptyMap(), expressionType); }
/** * Apply a message to the profile. * * @param message The message to apply. * @param timestamp The timestamp of the message. */ @Override public void apply(JSONObject message, long timestamp) { LOG.debug("Applying message to profile; profile={}, entity={}, timestamp={}", profileName, entity, timestamp); try { if (!isInitialized()) { LOG.debug("Initializing profile; profile={}, entity={}, timestamp={}", profileName, entity, timestamp); // execute each 'init' expression assign(definition.getInit(), message, "init"); isInitialized = true; } // execute each 'update' expression assign(definition.getUpdate(), message, "update"); // keep track of the 'latest' timestamp seen for use when flushing the profile if(timestamp > maxTimestamp) { maxTimestamp = timestamp; } } catch(Throwable e) { LOG.error(format("Unable to apply message to profile: %s", e.getMessage()), e); } }
/** * Executes an expression contained within the profile definition. * * @param expression The expression to execute. * @param transientState Additional transient state provided to the expression. * @param expressionType The type of expression; init, update, result. Provides additional context if expression execution fails. * @return The result of executing the expression. */ private Object execute(String expression, Map<String, Object> transientState, String expressionType) { Object result = null; List<Object> allResults = execute(Collections.singletonList(expression), transientState, expressionType); if(allResults.size() > 0) { result = allResults.get(0); } return result; }