|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 Graylog, Inc. |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the Server Side Public License, version 1, |
| 6 | + * as published by MongoDB, Inc. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, |
| 9 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + * Server Side Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the Server Side Public License |
| 14 | + * along with this program. If not, see |
| 15 | + * <http://www.mongodb.com/licensing/server-side-public-license>. |
| 16 | + */ |
| 17 | +package org.graylog.plugins.pipelineprocessor.functions.dates; |
| 18 | + |
| 19 | +import com.google.common.collect.ImmutableList; |
| 20 | +import com.google.common.collect.ImmutableMap; |
| 21 | +import org.graylog.plugins.pipelineprocessor.EvaluationContext; |
| 22 | +import org.graylog.plugins.pipelineprocessor.ast.functions.AbstractFunction; |
| 23 | +import org.graylog.plugins.pipelineprocessor.ast.functions.FunctionArgs; |
| 24 | +import org.graylog.plugins.pipelineprocessor.ast.functions.FunctionDescriptor; |
| 25 | +import org.graylog.plugins.pipelineprocessor.ast.functions.ParameterDescriptor; |
| 26 | +import org.graylog.plugins.pipelineprocessor.rulebuilder.RuleBuilderFunctionGroup; |
| 27 | +import org.joda.time.DateTime; |
| 28 | +import org.joda.time.Duration; |
| 29 | + |
| 30 | +import java.util.Map; |
| 31 | + |
| 32 | +public class DateDiff extends AbstractFunction<Map<String, Object>> { |
| 33 | + |
| 34 | + public static final String NAME = "date_diff"; |
| 35 | + |
| 36 | + private static final String LEFT = "left"; |
| 37 | + private static final String RIGHT = "right"; |
| 38 | + private static final String ABSOLUTE = "absolute"; |
| 39 | + |
| 40 | + private static final long MS_PER_SECOND = 1000L; |
| 41 | + private static final long MS_PER_MINUTE = 60L * MS_PER_SECOND; |
| 42 | + private static final long MS_PER_HOUR = 60L * MS_PER_MINUTE; |
| 43 | + private static final long MS_PER_DAY = 24L * MS_PER_HOUR; |
| 44 | + private static final long MS_PER_WEEK = 7L * MS_PER_DAY; |
| 45 | + |
| 46 | + private final ParameterDescriptor<DateTime, DateTime> left; |
| 47 | + private final ParameterDescriptor<DateTime, DateTime> right; |
| 48 | + private final ParameterDescriptor<Boolean, Boolean> absolute; |
| 49 | + |
| 50 | + public DateDiff() { |
| 51 | + left = ParameterDescriptor.type(LEFT, DateTime.class) |
| 52 | + .description("Start of the interval. May be before or after the end; the result is signed by default (end - start).") |
| 53 | + .ruleBuilderVariable() |
| 54 | + .build(); |
| 55 | + right = ParameterDescriptor.type(RIGHT, DateTime.class) |
| 56 | + .description("End of the interval. May be before or after the start.") |
| 57 | + .build(); |
| 58 | + absolute = ParameterDescriptor.bool(ABSOLUTE) |
| 59 | + .optional() |
| 60 | + .description("If true, return absolute values; otherwise the result is signed (end - start). Defaults to false.") |
| 61 | + .build(); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public Map<String, Object> evaluate(FunctionArgs args, EvaluationContext context) { |
| 66 | + final DateTime leftValue = left.required(args, context); |
| 67 | + final DateTime rightValue = right.required(args, context); |
| 68 | + if (leftValue == null || rightValue == null) { |
| 69 | + return null; |
| 70 | + } |
| 71 | + final boolean abs = absolute.optional(args, context).orElse(false); |
| 72 | + |
| 73 | + final long signedMillis = new Duration(leftValue, rightValue).getMillis(); |
| 74 | + final long value = (abs && signedMillis < 0) ? -signedMillis : signedMillis; |
| 75 | + |
| 76 | + return ImmutableMap.<String, Object>builder() |
| 77 | + .put("millis", value) |
| 78 | + .put("seconds", roundDiv(value, MS_PER_SECOND)) |
| 79 | + .put("minutes", roundDiv(value, MS_PER_MINUTE)) |
| 80 | + .put("hours", roundDiv(value, MS_PER_HOUR)) |
| 81 | + .put("days", roundDiv(value, MS_PER_DAY)) |
| 82 | + .put("weeks", roundDiv(value, MS_PER_WEEK)) |
| 83 | + .put("direction", direction(signedMillis)) |
| 84 | + .put("friendly", friendly(value)) |
| 85 | + .build(); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Divide {@code value} by {@code divisor} with half-away-from-zero rounding, symmetric |
| 90 | + * across positive and negative values. e.g. 2350000ms ÷ 60000 = 39.17 → 39 minutes; |
| 91 | + * 2370000ms ÷ 60000 = 39.5 → 40 minutes; -2370000ms → -40 minutes. |
| 92 | + */ |
| 93 | + private static long roundDiv(long value, long divisor) { |
| 94 | + final long half = divisor / 2; |
| 95 | + return value >= 0 ? (value + half) / divisor : (value - half) / divisor; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Describes {@code right} relative to {@code left}. Computed from the signed millis, |
| 100 | + * so direction is preserved even when {@code absolute=true} strips the sign from the |
| 101 | + * numeric components. |
| 102 | + */ |
| 103 | + private static String direction(long signedMillis) { |
| 104 | + if (signedMillis > 0) { |
| 105 | + return "ahead"; |
| 106 | + } |
| 107 | + if (signedMillis < 0) { |
| 108 | + return "behind"; |
| 109 | + } |
| 110 | + return "equal"; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Human-readable rendering of the (possibly signed) interval. Zero-valued components are |
| 115 | + * omitted. Sub-second remainder is included as a "ms" component only when the total |
| 116 | + * interval is below one minute, so long intervals aren't cluttered with millisecond noise; |
| 117 | + * the raw {@code millis} field always carries the exact value. |
| 118 | + */ |
| 119 | + private static String friendly(long signedMillis) { |
| 120 | + if (signedMillis == 0) { |
| 121 | + return "0 ms"; |
| 122 | + } |
| 123 | + final boolean neg = signedMillis < 0; |
| 124 | + final long m = neg ? -signedMillis : signedMillis; |
| 125 | + final StringBuilder sb = new StringBuilder(); |
| 126 | + if (neg) { |
| 127 | + sb.append('-'); |
| 128 | + } |
| 129 | + final long weeks = m / MS_PER_WEEK; |
| 130 | + final long days = (m / MS_PER_DAY) % 7; |
| 131 | + final long hours = (m / MS_PER_HOUR) % 24; |
| 132 | + final long minutes = (m / MS_PER_MINUTE) % 60; |
| 133 | + final long seconds = (m / MS_PER_SECOND) % 60; |
| 134 | + final long millis = m % MS_PER_SECOND; |
| 135 | + appendPart(sb, weeks, "week", "weeks"); |
| 136 | + appendPart(sb, days, "day", "days"); |
| 137 | + appendPart(sb, hours, "hour", "hours"); |
| 138 | + appendPart(sb, minutes, "minute", "minutes"); |
| 139 | + appendPart(sb, seconds, "second", "seconds"); |
| 140 | + // Include sub-second remainder when the interval is below a minute, so callers see |
| 141 | + // precision for short deltas without "2 weeks ... 47 ms" noise on long ones. |
| 142 | + if (millis > 0 && m < MS_PER_MINUTE) { |
| 143 | + appendPart(sb, millis, "ms", "ms"); |
| 144 | + } |
| 145 | + return sb.toString(); |
| 146 | + } |
| 147 | + |
| 148 | + private static void appendPart(StringBuilder sb, long value, String singular, String plural) { |
| 149 | + if (value == 0) { |
| 150 | + return; |
| 151 | + } |
| 152 | + if (sb.length() > 0 && sb.charAt(sb.length() - 1) != '-') { |
| 153 | + sb.append(' '); |
| 154 | + } |
| 155 | + sb.append(value).append(' ').append(value == 1 ? singular : plural); |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public FunctionDescriptor<Map<String, Object>> descriptor() { |
| 160 | + @SuppressWarnings({"unchecked", "rawtypes"}) |
| 161 | + final Class<? extends Map<String, Object>> returnType = (Class) Map.class; |
| 162 | + return FunctionDescriptor.<Map<String, Object>>builder() |
| 163 | + .name(NAME) |
| 164 | + .returnType(returnType) |
| 165 | + .params(ImmutableList.of(left, right, absolute)) |
| 166 | + .description("Returns the difference between two dates as a map. The numeric units " + |
| 167 | + "(millis, seconds, minutes, hours, days, weeks) are rounded to the nearest whole " + |
| 168 | + "unit. The map also contains 'direction', which describes the end relative to the " + |
| 169 | + "start as \"ahead\", \"behind\", or \"equal\", and 'friendly', a human-readable " + |
| 170 | + "breakdown of the interval. Numeric values are signed by default (end - start). " + |
| 171 | + "Pass absolute=true to return absolute values; direction is always derived from " + |
| 172 | + "the signed result and is preserved.") |
| 173 | + .ruleBuilderEnabled() |
| 174 | + .ruleBuilderName("Date difference") |
| 175 | + .ruleBuilderTitle("Difference between '${left}' and '${right}'") |
| 176 | + .ruleBuilderFunctionGroup(RuleBuilderFunctionGroup.DATE) |
| 177 | + .build(); |
| 178 | + } |
| 179 | +} |
0 commit comments