Search in sources :

Example 46 with Duration

use of org.joda.time.Duration in project service-proxy by membrane.

the class AMRateLimiter method fillPolicyCleanupTimes.

private void fillPolicyCleanupTimes() {
    policyRateLimits.clear();
    for (Policy policy : amc.getPolicies().values()) {
        String name = policy.getName();
        int requests = policy.getRateLimit().getRequests();
        Duration interval = Duration.standardSeconds(policy.getRateLimit().getInterval());
        HashSet<String> services = new HashSet<String>(policy.getServiceProxies());
        PolicyRateLimit prl = new PolicyRateLimit();
        prl.setName(name);
        prl.setRequests(requests);
        prl.setInterval(interval);
        prl.setServices(services);
        prl.incrementNextCleanup();
        policyRateLimits.put(name, prl);
    }
}
Also used : Policy(com.predic8.membrane.core.interceptor.apimanagement.policy.Policy) Duration(org.joda.time.Duration) HashSet(java.util.HashSet)

Example 47 with Duration

use of org.joda.time.Duration in project substitution-schedule-parser by vertretungsplanme.

the class ParserUtils method parseDate.

static LocalDate parseDate(String string) {
    if (string == null)
        return null;
    reinitIfNeeded();
    string = string.replace("Stand:", "").replace("Import:", "").replaceAll(", Woche [A-Z]", "").trim();
    int i = 0;
    for (DateTimeFormatter f : dateFormatters) {
        try {
            LocalDate d = f.parseLocalDate(string);
            if (dateFormats[i].contains("yyyy")) {
                return d;
            } else {
                Duration currentYearDifference = abs(new Duration(DateTime.now(), d.toDateTimeAtCurrentTime()));
                Duration lastYearDifference = abs(new Duration(DateTime.now(), d.minusYears(1).toDateTimeAtCurrentTime()));
                Duration nextYearDifference = abs(new Duration(DateTime.now(), d.plusYears(1).toDateTimeAtCurrentTime()));
                if (lastYearDifference.isShorterThan(currentYearDifference)) {
                    return DateTimeFormat.forPattern(dateFormats[i]).withLocale(Locale.GERMAN).withDefaultYear(f.getDefaultYear() - 1).parseLocalDate(string);
                } else if (nextYearDifference.isShorterThan(currentYearDifference)) {
                    return DateTimeFormat.forPattern(dateFormats[i]).withLocale(Locale.GERMAN).withDefaultYear(f.getDefaultYear() + 1).parseLocalDate(string);
                } else {
                    return d;
                }
            }
        } catch (IllegalArgumentException e) {
        // Does not match this format, try the next one
        }
        i++;
    }
    // Does not match any known format :(
    return null;
}
Also used : Duration(org.joda.time.Duration) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) LocalDate(org.joda.time.LocalDate)

Example 48 with Duration

use of org.joda.time.Duration in project watchdog by TestRoots.

the class IntervalBase method getDurationString.

/**
 * @return A human-readable duration.
 */
public String getDurationString() {
    Duration duration = getDuration();
    Period period = duration.toPeriod();
    return PeriodFormat.getDefault().print(period);
}
Also used : Period(org.joda.time.Period) Duration(org.joda.time.Duration)

Example 49 with Duration

use of org.joda.time.Duration in project molgenis by molgenis.

the class ProgressImpl method success.

@Override
public void success() {
    jobExecution.setEndDate(Instant.now());
    jobExecution.setStatus(SUCCESS);
    jobExecution.setProgressInt(jobExecution.getProgressMax());
    Duration yourDuration = Duration.millis(timeRunning());
    Period period = yourDuration.toPeriod();
    PeriodFormatter periodFormatter = new PeriodFormatterBuilder().appendDays().appendSuffix("d ").appendHours().appendSuffix("h ").appendMinutes().appendSuffix("m ").appendSeconds().appendSuffix("s ").appendMillis().appendSuffix("ms ").toFormatter();
    String timeSpent = periodFormatter.print(period);
    JOB_EXECUTION_LOG.info("Execution successful. Time spent: {}", timeSpent);
    sendEmail(jobExecution.getSuccessEmail(), jobExecution.getType() + " job succeeded.", jobExecution.getLog());
    update();
    JobExecutionContext.unset();
}
Also used : PeriodFormatterBuilder(org.joda.time.format.PeriodFormatterBuilder) PeriodFormatter(org.joda.time.format.PeriodFormatter) Period(org.joda.time.Period) Duration(org.joda.time.Duration)

Example 50 with Duration

use of org.joda.time.Duration in project graylog2-server by Graylog2.

the class AdditionExpression method evaluateUnsafe.

@Nullable
@Override
public Object evaluateUnsafe(EvaluationContext context) {
    final Object leftValue = left.evaluateUnsafe(context);
    final Object rightValue = right.evaluateUnsafe(context);
    // special case for date arithmetic
    final boolean leftDate = DateTime.class.equals(leftValue.getClass());
    final boolean leftPeriod = Period.class.equals(leftValue.getClass());
    final boolean rightDate = DateTime.class.equals(rightValue.getClass());
    final boolean rightPeriod = Period.class.equals(rightValue.getClass());
    if (leftDate && rightPeriod) {
        final DateTime date = (DateTime) leftValue;
        final Period period = (Period) rightValue;
        return isPlus() ? date.plus(period) : date.minus(period);
    } else if (leftPeriod && rightDate) {
        final DateTime date = (DateTime) rightValue;
        final Period period = (Period) leftValue;
        return isPlus() ? date.plus(period) : date.minus(period);
    } else if (leftPeriod && rightPeriod) {
        final Period period1 = (Period) leftValue;
        final Period period2 = (Period) rightValue;
        return isPlus() ? period1.plus(period2) : period1.minus(period2);
    } else if (leftDate && rightDate) {
        // because adding two dates makes no sense
        if (isPlus()) {
            // makes no sense to compute and should be handles in the parser already
            return null;
        }
        final DateTime left = (DateTime) leftValue;
        final DateTime right = (DateTime) rightValue;
        if (left.isBefore(right)) {
            return new Duration(left, right);
        } else {
            return new Duration(right, left);
        }
    } else if (String.class.equals(type)) {
        if (!isPlus) {
            return null;
        }
        final String left = String.valueOf(leftValue);
        final String right = String.valueOf(rightValue);
        return left + right;
    }
    if (isIntegral()) {
        final long l = (long) leftValue;
        final long r = (long) rightValue;
        if (isPlus) {
            return l + r;
        } else {
            return l - r;
        }
    } else {
        final double l = (double) leftValue;
        final double r = (double) rightValue;
        if (isPlus) {
            return l + r;
        } else {
            return l - r;
        }
    }
}
Also used : Period(org.joda.time.Period) Duration(org.joda.time.Duration) DateTime(org.joda.time.DateTime) Nullable(javax.annotation.Nullable)

Aggregations

Duration (org.joda.time.Duration)272 Test (org.junit.Test)148 Instant (org.joda.time.Instant)66 DateTime (org.joda.time.DateTime)32 Period (org.joda.time.Period)27 IntervalWindow (org.apache.beam.sdk.transforms.windowing.IntervalWindow)24 TestDruidCoordinatorConfig (org.apache.druid.server.coordinator.TestDruidCoordinatorConfig)22 HashMap (java.util.HashMap)18 IOException (java.io.IOException)17 Category (org.junit.experimental.categories.Category)16 ArrayList (java.util.ArrayList)15 Map (java.util.Map)15 KV (org.apache.beam.sdk.values.KV)15 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 IndexSpec (org.apache.druid.segment.IndexSpec)12 Set (java.util.Set)10 GlobalWindows (org.apache.beam.sdk.transforms.windowing.GlobalWindows)10 DynamicPartitionsSpec (org.apache.druid.indexer.partitions.DynamicPartitionsSpec)10 Interval (org.joda.time.Interval)10 Request (com.metamx.http.client.Request)9