UNPKG

2.98 kBJavaScriptView Raw
1"use strict";
2/**
3 * Copyright 2018, OpenCensus Authors
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.BucketBoundaries = void 0;
19const defaultLogger = require("../common/console-logger");
20class BucketBoundaries {
21 constructor(boundaries, logger = defaultLogger) {
22 this.logger = logger.logger();
23 this.buckets = this.dropNegativeBucketBounds(boundaries);
24 this.bucketCounts = this.getBucketCounts(this.buckets);
25 }
26 /**
27 * Gets bucket boundaries
28 */
29 getBoundaries() {
30 return this.buckets;
31 }
32 /**
33 * Gets initial bucket counts
34 */
35 getCounts() {
36 return this.bucketCounts;
37 }
38 /**
39 * Drops negative (BucketBounds) are currently not supported by
40 * any of the backends that OC supports
41 * @param bucketBoundaries a list with the bucket boundaries
42 */
43 dropNegativeBucketBounds(bucketBoundaries) {
44 let negative = 0;
45 if (!bucketBoundaries)
46 return [];
47 const result = bucketBoundaries.reduce((accumulator, boundary, index) => {
48 if (boundary > 0) {
49 const nextBoundary = bucketBoundaries[index + 1];
50 this.validateBoundary(boundary, nextBoundary);
51 accumulator.push(boundary);
52 }
53 else {
54 negative++;
55 }
56 return accumulator;
57 }, []);
58 if (negative) {
59 this.logger.warn(`Dropping ${negative} negative bucket boundaries, the values must be strictly > 0.`);
60 }
61 return result;
62 }
63 /**
64 * Gets initial list of bucket counters
65 * @param buckets Bucket boundaries
66 */
67 getBucketCounts(buckets) {
68 if (!buckets)
69 return [];
70 const bucketsCount = new Array(buckets.length + 1);
71 bucketsCount.fill(0);
72 return bucketsCount;
73 }
74 /**
75 * Checks boundaries order and duplicates
76 * @param current Boundary
77 * @param next Next boundary
78 */
79 validateBoundary(current, next) {
80 if (next) {
81 if (current > next) {
82 this.logger.error('Bucket boundaries not sorted.');
83 }
84 if (current === next) {
85 this.logger.error('Bucket boundaries not unique.');
86 }
87 }
88 }
89}
90exports.BucketBoundaries = BucketBoundaries;
91//# sourceMappingURL=bucket-boundaries.js.map
\No newline at end of file