/** * @param t an iterator of non-descending timestamps * @return a timeSeries identical to this but relocated to t */ public TimeSeries<T, V> relocate(Iterator<T> t) { return new TimeSeries<>(t, this); }
@Override public boolean equals(Object x) { if (this == x) { return true; } if (!(x instanceof TimeSeries)) { return false; } TimeSeries that = (TimeSeries) x; BiFunction<V, V, Boolean> f = WeakLogic::weakEquals; TimeSeries aux = merge(this, that, f); return Boolean.TRUE.equals(aux.apply(null)) && (aux.size() == 1); }
/** * @param x the argument * @return the value of this at x */ public V apply(T x) { int i = binarySearch(times, x, cmp); i = (0 <= i) ? i : -i - 2; return get(i).getSecond(); }
/** * @param tv the first time series * @param tw the second time series * @param f a function mapping V x V -> U (e.g. x, y -> x <= y) * @param <T> type of timestamps * @param <V> type of values * @param <U> return type of f * @return a new time series unioning all time stamps given, * the value being the result of f (e.g. lessThan, equals) */ public static <T extends Comparable<T>, V, U> TimeSeries<T, U> merge(TimeSeries<T, V> tv, TimeSeries<T, V> tw, BiFunction<V, V, U> f) { Iterator<Pair<T, List<V>>> aux1 = asIterator(merge(asList(tv, tw))); Iterator<Pair<T, U>> aux2 = map(aux1, (Pair<T, List<V>> p) -> pairOf(p.getFirst(), f.apply(p.getSecond().get(0), p.getSecond().get(1)))); return new TimeSeries<>(aux2); }
/** * @param a left border a <= b * @param b right border * @return returns a time series identical with this on [a, b) and undefined otherwise */ public TimeSeries<T, V> subSeries(T a, T b) { if (cmp.compare(a, b) >= 0) { throw new IllegalArgumentException(); } int i = binarySearch(times, a, cmp); i = (0 <= i) ? i : -i - 2; int j = binarySearch(times, b, cmp); j = (0 <= j) ? j - 1 : -j - 2; Iterator<Pair<T, V>> result = fluent(of(pairOf(a, this.apply(a)))) .concat(zip(pairOf(times.subList(i, j + 1).iterator(), values.subList(i, j + 1).iterator()), true), of(pairOf(b, null))); return new TimeSeries<>(result); }
private MetricTimeSeries map(TimeSeries<Long, Double> timeSeries) { MetricTimeSeries.Builder builder = new MetricTimeSeries.Builder(timeSeries.getAttribute("metric").toString()); //add points Iterator<Pair<Long, Double>> it = timeSeries.iterator(); //ignore the first element if (it.hasNext()) { it.next(); } while (it.hasNext()) { Pair<Long, Double> pair = it.next(); builder.point(pair.getFirst(), pair.getSecond()); } //add attributes timeSeries.getAttributes().forEachRemaining(attribute -> builder.attribute(attribute.getKey(), attribute.getValue())); return builder.build(); } }
@Override public BinaryTimeSeries to(TimeSeries<Long, Double> timeSeries) { //-oo is represented through the first element that is null, hence if the size is one the time series is empty if (timeSeries.size() == 1) { LOGGER.info("Empty time series detected. {}", timeSeries); //Create a builder with the minimal required fields BinaryTimeSeries.Builder builder = new BinaryTimeSeries.Builder() .data(new byte[]{}) .start(0) .end(0); return builder.build(); } else { return new MetricTimeSeriesConverter().to(map(timeSeries)); } }
/** * @param tv the first time series * @param tw the second time series * @param f a function mapping V x V -> U (e.g. x, y -> x <= y) * @param <T> type of timestamps * @param <V> type of values * @param <U> return type of f * @return a new time series unioning all time stamps given, * the value being the result of f (e.g. lessThan, equals) */ public static <T extends Comparable<T>, V, U> TimeSeries<T, U> merge(TimeSeries<T, V> tv, TimeSeries<T, V> tw, BiFunction<V, V, U> f) { Iterator<Pair<T, List<V>>> aux1 = asIterator(merge(asList(tv, tw))); Iterator<Pair<T, U>> aux2 = map(aux1, (Pair<T, List<V>> p) -> pairOf(p.getFirst(), f.apply(p.getSecond().get(0), p.getSecond().get(1)))); return new TimeSeries<>(aux2); }
/** * @param a left border a <= b * @param b right border * @return returns a time series identical with this on [a, b) and undefined otherwise */ public TimeSeries<T, V> subSeries(T a, T b) { if (cmp.compare(a, b) >= 0) { throw new IllegalArgumentException(); } int i = binarySearch(times, a, cmp); i = (0 <= i) ? i : -i - 2; int j = binarySearch(times, b, cmp); j = (0 <= j) ? j - 1 : -j - 2; Iterator<Pair<T, V>> result = fluent(of(pairOf(a, this.apply(a)))) .concat(zip(pairOf(times.subList(i, j + 1).iterator(), values.subList(i, j + 1).iterator()), true), of(pairOf(b, null))); return new TimeSeries<>(result); }
@Override public boolean equals(Object x) { if (this == x) { return true; } if (!(x instanceof TimeSeries)) { return false; } TimeSeries that = (TimeSeries) x; BiFunction<V, V, Boolean> f = WeakLogic::weakEquals; TimeSeries aux = merge(this, that, f); return Boolean.TRUE.equals(aux.apply(null)) && (aux.size() == 1); }
/** * @param t an iterator of non-descending timestamps * @return a timeSeries identical to this but relocated to t */ public TimeSeries<T, V> relocate(Iterator<T> t) { return new TimeSeries<>(t, this); }
/** * @param x the argument * @return the value of this at x */ public V apply(T x) { int i = binarySearch(times, x, cmp); i = (0 <= i) ? i : -i - 2; return get(i).getSecond(); }
/** * @param ts a time series * @param <T> the time type * @param <V> the value type * @return a new time series unioning all time stamps given, * the value being the list of all values valid at that time */ public static <T extends Comparable<T>, V> TimeSeries<T, List<V>> merge(Iterable<TimeSeries<T, V>> ts) { Iterator<Pair<T, List<V>>> aux = new TimeSeriesMerge<>(map(asIterator(ts), Iterable::iterator)); return new TimeSeries<>(aux); }
/** * @param ts a time series * @param <T> the time type * @param <V> the value type * @return a new time series unioning all time stamps given, * the value being the list of all values valid at that time */ public static <T extends Comparable<T>, V> TimeSeries<T, List<V>> merge(Iterable<TimeSeries<T, V>> ts) { Iterator<Pair<T, List<V>>> aux = new TimeSeriesMerge<>(map(asIterator(ts), Iterable::iterator)); return new TimeSeries<>(aux); }
@Override public TimeSeries<Long, Double> from(BinaryTimeSeries binaryTimeSeries, long queryStart, long queryEnd) { //This is a hack MetricTimeSeries metricTimeSeries = new MetricTimeSeriesConverter().from(binaryTimeSeries, queryStart, queryEnd); TimeSeries<Long, Double> timeSeries = new TimeSeries<>(map(metricTimeSeries.points())); metricTimeSeries.getAttributesReference().forEach(timeSeries::addAttribute); return timeSeries; }
/** * @param ts a time series * @param <T> the time type * @param <V> the value type * @param op an operator reducing the value list * @return a new time series unioning all time stamps given, * the value being the result of the reducing by op (e.g. min, max, avg) */ public static <T extends Comparable<T>, V> TimeSeries<T, V> merge(Iterable<TimeSeries<T, V>> ts, BinaryOperator<V> op) { BinaryOperator<V> wop = weakBinaryOperator(op); Iterator<Pair<T, List<V>>> aux1 = new TimeSeriesMerge<>(map(asIterator(ts), Iterable::iterator)); Iterator<Pair<T, V>> aux2 = map(aux1, (Pair<T, List<V>> p) -> pairOf(p.getFirst(), reduce(asIterator(p.getSecond()), wop))); return new TimeSeries<>(aux2); }
/** * @param ts a time series * @param <T> the time type * @param <V> the value type * @param op an operator reducing the value list * @return a new time series unioning all time stamps given, * the value being the result of the reducing by op (e.g. min, max, avg) */ public static <T extends Comparable<T>, V> TimeSeries<T, V> merge(Iterable<TimeSeries<T, V>> ts, BinaryOperator<V> op) { BinaryOperator<V> wop = weakBinaryOperator(op); Iterator<Pair<T, List<V>>> aux1 = new TimeSeriesMerge<>(map(asIterator(ts), Iterable::iterator)); Iterator<Pair<T, V>> aux2 = map(aux1, (Pair<T, List<V>> p) -> pairOf(p.getFirst(), reduce(asIterator(p.getSecond()), wop))); return new TimeSeries<>(aux2); }