aboutsummaryrefslogtreecommitdiff
path: root/impl_core/src/main/java/io/opencensus/implcore/stats/IntervalBucket.java
blob: 12cb319ed9970281eb46995107463ffcccf5dd3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * Copyright 2017, OpenCensus Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.opencensus.implcore.stats;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.collect.Maps;
import io.opencensus.common.Duration;
import io.opencensus.common.Timestamp;
import io.opencensus.stats.Aggregation;
import io.opencensus.tags.TagValue;
import java.util.List;
import java.util.Map;

/*>>>
import org.checkerframework.checker.nullness.qual.Nullable;
*/

/** The bucket with aggregated {@code MeasureValue}s used for {@code IntervalViewData}. */
final class IntervalBucket {

  private static final Duration ZERO = Duration.create(0, 0);

  private final Timestamp start;
  private final Duration duration;
  private final Aggregation aggregation;
  private final Map<List</*@Nullable*/ TagValue>, MutableAggregation> tagValueAggregationMap =
      Maps.newHashMap();

  IntervalBucket(Timestamp start, Duration duration, Aggregation aggregation) {
    checkNotNull(start, "Start");
    checkNotNull(duration, "Duration");
    checkArgument(duration.compareTo(ZERO) > 0, "Duration must be positive");
    checkNotNull(aggregation, "Aggregation");
    this.start = start;
    this.duration = duration;
    this.aggregation = aggregation;
  }

  Map<List</*@Nullable*/ TagValue>, MutableAggregation> getTagValueAggregationMap() {
    return tagValueAggregationMap;
  }

  Timestamp getStart() {
    return start;
  }

  // Puts a new value into the internal MutableAggregations, based on the TagValues.
  void record(
      List</*@Nullable*/ TagValue> tagValues,
      double value,
      Map<String, String> attachments,
      Timestamp timestamp) {
    if (!tagValueAggregationMap.containsKey(tagValues)) {
      tagValueAggregationMap.put(tagValues, MutableViewData.createMutableAggregation(aggregation));
    }
    tagValueAggregationMap.get(tagValues).add(value, attachments, timestamp);
  }

  /*
   * Returns how much fraction of duration has passed in this IntervalBucket. For example, if this
   * bucket starts at 10s and has a duration of 20s, and now is 15s, then getFraction() should
   * return (15 - 10) / 20 = 0.25.
   *
   * This IntervalBucket must be current, i.e. the current timestamp must be within
   * [this.start, this.start + this.duration).
   */
  double getFraction(Timestamp now) {
    Duration elapsedTime = now.subtractTimestamp(start);
    checkArgument(
        elapsedTime.compareTo(ZERO) >= 0 && elapsedTime.compareTo(duration) < 0,
        "This bucket must be current.");
    return ((double) elapsedTime.toMillis()) / duration.toMillis();
  }

  void clearStats() {
    tagValueAggregationMap.clear();
  }
}