Example usage for org.joda.time Period toString

List of usage examples for org.joda.time Period toString

Introduction

In this page you can find the example usage for org.joda.time Period toString.

Prototype

@ToString
public String toString() 

Source Link

Document

Gets the value as a String in the ISO8601 duration format.

Usage

From source file:uk.ac.imperial.lsds.seep.infrastructure.monitor.policy.trigger.ActionTrigger.java

License:Open Source License

/**
 * Evaluates the state of the trigger. Both the value and the time threshold
 * need to evaluate to true in order for the trigger state to change from
 * non-fired to fired.// w  w w .j  av  a2  s  .c om
 * @param readings metric readings to evaluate (all those that are within the
 * time threshold need to evaluate to true in terms of their value).
 */
public void evaluate(List<MetricReading> readings, TimeReference time) {

    logger.info(
            "Evaluating trigger for " + metricName.toString() + " - " + readings.size() + " readings provided");

    logger.debug("value threshold: " + valueThreshold.toString());
    logger.debug("time threshold: " + timeThreshold.toString());

    // Determine the new state of the trigger depending on the result of
    // evaluating boh thresholds. Need to mark flag if state changes.
    ActionTriggerState pastTriggerState = triggerState;

    boolean enoughReadings = true;

    // Check that we have enough readings to cover the entire time threshold
    if ((readings != null) && (readings.size() > 0)) {
        MetricReading mostRecentReading = readings.get(readings.size() - 1);
        MetricReading leastRecentReading = readings.get(0);

        logger.info("Most recent reading [" + mostRecentReading.getTimestamp() + "]");
        logger.info("Least recent reading [" + leastRecentReading.getTimestamp() + "]");

        Period readingsPeriod = new Period(leastRecentReading.getTimestamp(), mostRecentReading.getTimestamp());

        int toleranceSeconds = new Double(0.1 * timeThreshold.toPeriod().toStandardSeconds().getSeconds())
                .intValue();

        if (readingsPeriod.toStandardSeconds()
                .isLessThan(timeThreshold.toPeriod().toStandardSeconds().minus(toleranceSeconds))) {

            logger.info("Not enough readings, only for last period[" + readingsPeriod + "]");
            enoughReadings = false;
        }
    }

    // If we have enough readings, then we evaluate in detail
    if (enoughReadings) {
        int i = 0;
        for (MetricReading r : readings) {
            Period metricPeriod = new Period(r.getTimestamp(), time.now());
            MetricValue metricValue = r.getValues().get(metricName);

            logger.info("Evaluating reading[" + i + "] value[" + metricValue.toString() + "] period["
                    + metricPeriod.toString() + "]");

            // We evaluate the time threshold first, simple optimisation to be
            // able to abort the iteration sooner (readings are guaranteed to be
            // sorted by time of reception, from most recent to least recent).
            if (timeThreshold.evaluate(metricPeriod)) {
                triggerState = valueThreshold.evaluate(metricValue) ? ActionTriggerState.FIRED
                        : ActionTriggerState.NON_FIRED;

                // If there is a reading within the time threshold for which
                // the value evaluates to false (trigger is non-fired), then
                // we can break from the evaluation loop.
                if (triggerState.equals(ActionTriggerState.NON_FIRED)) {
                    break;
                }
            }

            i++;
        }
    }

    stateChanged = (triggerState != pastTriggerState);
    logger.info("New trigger state is [" + triggerState.toString() + "] changed[" + stateChanged + "]");
}