• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017, OpenCensus Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.opencensus.stats;
18 
19 import com.google.auto.value.AutoValue;
20 import io.opencensus.common.Function;
21 import io.opencensus.internal.Utils;
22 import javax.annotation.concurrent.Immutable;
23 
24 /**
25  * {@link Aggregation} is the process of combining a certain set of {@code MeasureValue}s for a
26  * given {@code Measure} into an {@link AggregationData}.
27  *
28  * <p>{@link Aggregation} currently supports 4 types of basic aggregation:
29  *
30  * <ul>
31  *   <li>Sum
32  *   <li>Count
33  *   <li>Distribution
34  *   <li>LastValue
35  * </ul>
36  *
37  * <p>When creating a {@link View}, one {@link Aggregation} needs to be specified as how to
38  * aggregate {@code MeasureValue}s.
39  *
40  * @since 0.8
41  */
42 @Immutable
43 public abstract class Aggregation {
44 
Aggregation()45   private Aggregation() {}
46 
47   /**
48    * Applies the given match function to the underlying data type.
49    *
50    * @since 0.13
51    */
match( Function<? super Sum, T> p0, Function<? super Count, T> p1, Function<? super Distribution, T> p2, Function<? super LastValue, T> p3, Function<? super Aggregation, T> defaultFunction)52   public abstract <T> T match(
53       Function<? super Sum, T> p0,
54       Function<? super Count, T> p1,
55       Function<? super Distribution, T> p2,
56       Function<? super LastValue, T> p3,
57       Function<? super Aggregation, T> defaultFunction);
58 
59   /**
60    * Calculate sum on aggregated {@code MeasureValue}s.
61    *
62    * @since 0.8
63    */
64   @Immutable
65   @AutoValue
66   public abstract static class Sum extends Aggregation {
67 
Sum()68     Sum() {}
69 
70     private static final Sum INSTANCE = new AutoValue_Aggregation_Sum();
71 
72     /**
73      * Construct a {@code Sum}.
74      *
75      * @return a new {@code Sum}.
76      * @since 0.8
77      */
create()78     public static Sum create() {
79       return INSTANCE;
80     }
81 
82     @Override
match( Function<? super Sum, T> p0, Function<? super Count, T> p1, Function<? super Distribution, T> p2, Function<? super LastValue, T> p3, Function<? super Aggregation, T> defaultFunction)83     public final <T> T match(
84         Function<? super Sum, T> p0,
85         Function<? super Count, T> p1,
86         Function<? super Distribution, T> p2,
87         Function<? super LastValue, T> p3,
88         Function<? super Aggregation, T> defaultFunction) {
89       return p0.apply(this);
90     }
91   }
92 
93   /**
94    * Calculate count on aggregated {@code MeasureValue}s.
95    *
96    * @since 0.8
97    */
98   @Immutable
99   @AutoValue
100   public abstract static class Count extends Aggregation {
101 
Count()102     Count() {}
103 
104     private static final Count INSTANCE = new AutoValue_Aggregation_Count();
105 
106     /**
107      * Construct a {@code Count}.
108      *
109      * @return a new {@code Count}.
110      * @since 0.8
111      */
create()112     public static Count create() {
113       return INSTANCE;
114     }
115 
116     @Override
match( Function<? super Sum, T> p0, Function<? super Count, T> p1, Function<? super Distribution, T> p2, Function<? super LastValue, T> p3, Function<? super Aggregation, T> defaultFunction)117     public final <T> T match(
118         Function<? super Sum, T> p0,
119         Function<? super Count, T> p1,
120         Function<? super Distribution, T> p2,
121         Function<? super LastValue, T> p3,
122         Function<? super Aggregation, T> defaultFunction) {
123       return p1.apply(this);
124     }
125   }
126 
127   /**
128    * Calculate mean on aggregated {@code MeasureValue}s.
129    *
130    * @since 0.8
131    * @deprecated since 0.13, use {@link Distribution} instead.
132    */
133   @Immutable
134   @AutoValue
135   @Deprecated
136   @AutoValue.CopyAnnotations
137   public abstract static class Mean extends Aggregation {
138 
Mean()139     Mean() {}
140 
141     private static final Mean INSTANCE = new AutoValue_Aggregation_Mean();
142 
143     /**
144      * Construct a {@code Mean}.
145      *
146      * @return a new {@code Mean}.
147      * @since 0.8
148      */
create()149     public static Mean create() {
150       return INSTANCE;
151     }
152 
153     @Override
match( Function<? super Sum, T> p0, Function<? super Count, T> p1, Function<? super Distribution, T> p2, Function<? super LastValue, T> p3, Function<? super Aggregation, T> defaultFunction)154     public final <T> T match(
155         Function<? super Sum, T> p0,
156         Function<? super Count, T> p1,
157         Function<? super Distribution, T> p2,
158         Function<? super LastValue, T> p3,
159         Function<? super Aggregation, T> defaultFunction) {
160       return defaultFunction.apply(this);
161     }
162   }
163 
164   /**
165    * Calculate distribution stats on aggregated {@code MeasureValue}s. Distribution includes mean,
166    * count, histogram, min, max and sum of squared deviations.
167    *
168    * @since 0.8
169    */
170   @Immutable
171   @AutoValue
172   public abstract static class Distribution extends Aggregation {
173 
Distribution()174     Distribution() {}
175 
176     /**
177      * Construct a {@code Distribution}.
178      *
179      * @return a new {@code Distribution}.
180      * @since 0.8
181      */
create(BucketBoundaries bucketBoundaries)182     public static Distribution create(BucketBoundaries bucketBoundaries) {
183       Utils.checkNotNull(bucketBoundaries, "bucketBoundaries");
184       return new AutoValue_Aggregation_Distribution(bucketBoundaries);
185     }
186 
187     /**
188      * Returns the {@code Distribution}'s bucket boundaries.
189      *
190      * @return the {@code Distribution}'s bucket boundaries.
191      * @since 0.8
192      */
getBucketBoundaries()193     public abstract BucketBoundaries getBucketBoundaries();
194 
195     @Override
match( Function<? super Sum, T> p0, Function<? super Count, T> p1, Function<? super Distribution, T> p2, Function<? super LastValue, T> p3, Function<? super Aggregation, T> defaultFunction)196     public final <T> T match(
197         Function<? super Sum, T> p0,
198         Function<? super Count, T> p1,
199         Function<? super Distribution, T> p2,
200         Function<? super LastValue, T> p3,
201         Function<? super Aggregation, T> defaultFunction) {
202       return p2.apply(this);
203     }
204   }
205 
206   /**
207    * Calculate the last value of aggregated {@code MeasureValue}s.
208    *
209    * @since 0.13
210    */
211   @Immutable
212   @AutoValue
213   public abstract static class LastValue extends Aggregation {
214 
LastValue()215     LastValue() {}
216 
217     private static final LastValue INSTANCE = new AutoValue_Aggregation_LastValue();
218 
219     /**
220      * Construct a {@code LastValue}.
221      *
222      * @return a new {@code LastValue}.
223      * @since 0.13
224      */
create()225     public static LastValue create() {
226       return INSTANCE;
227     }
228 
229     @Override
match( Function<? super Sum, T> p0, Function<? super Count, T> p1, Function<? super Distribution, T> p2, Function<? super LastValue, T> p3, Function<? super Aggregation, T> defaultFunction)230     public final <T> T match(
231         Function<? super Sum, T> p0,
232         Function<? super Count, T> p1,
233         Function<? super Distribution, T> p2,
234         Function<? super LastValue, T> p3,
235         Function<? super Aggregation, T> defaultFunction) {
236       return p3.apply(this);
237     }
238   }
239 }
240