hanahmily commented on a change in pull request #4783:
URL: https://github.com/apache/skywalking/pull/4783#discussion_r433329249



##########
File path: 
oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/AvgPercentileFunction.java
##########
@@ -0,0 +1,328 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.skywalking.oap.server.core.analysis.meter.function;
+
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.stream.IntStream;
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import lombok.Setter;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.skywalking.oap.server.core.Const;
+import org.apache.skywalking.oap.server.core.UnexpectedException;
+import org.apache.skywalking.oap.server.core.analysis.meter.MeterEntity;
+import org.apache.skywalking.oap.server.core.analysis.metrics.DataTable;
+import org.apache.skywalking.oap.server.core.analysis.metrics.IntList;
+import org.apache.skywalking.oap.server.core.analysis.metrics.Metrics;
+import 
org.apache.skywalking.oap.server.core.analysis.metrics.MultiIntValuesHolder;
+import 
org.apache.skywalking.oap.server.core.analysis.metrics.PercentileMetrics;
+import org.apache.skywalking.oap.server.core.remote.grpc.proto.RemoteData;
+import org.apache.skywalking.oap.server.core.storage.StorageBuilder;
+import org.apache.skywalking.oap.server.core.storage.annotation.Column;
+
+/**
+ * PercentileFunction is the implementation of {@link PercentileMetrics} in 
the meter system. The major difference is
+ * the PercentileFunction accepts the {@link AvgPercentileArgument} as input 
rather than every single request.
+ */
+@MeterFunction(functionName = "avgPercentile")
+@Slf4j
+public abstract class AvgPercentileFunction extends Metrics implements 
AcceptableValue<AvgPercentileFunction.AvgPercentileArgument>, 
MultiIntValuesHolder {
+    public static final String DATASET = "dataset";
+    public static final String RANKS = "ranks";
+    public static final String VALUE = "value";
+    protected static final String SUMMATION = "summation";
+    protected static final String COUNT = "count";
+
+    @Setter
+    @Getter
+    @Column(columnName = ENTITY_ID)
+    private String entityId;
+    @Getter
+    @Setter
+    @Column(columnName = VALUE, dataType = Column.ValueDataType.LABELED_VALUE, 
storageOnly = true)
+    private DataTable percentileValues = new DataTable(10);
+    @Getter
+    @Setter
+    @Column(columnName = SUMMATION, storageOnly = true)
+    protected DataTable summation = new DataTable(30);
+    @Getter
+    @Setter
+    @Column(columnName = COUNT, storageOnly = true)
+    protected DataTable count = new DataTable(30);
+    @Getter
+    @Setter
+    @Column(columnName = DATASET, storageOnly = true)
+    private DataTable dataset = new DataTable(30);
+    /**
+     * Rank
+     */
+    @Getter
+    @Setter
+    @Column(columnName = RANKS, storageOnly = true)
+    private IntList ranks = new IntList(10);
+
+    private boolean isCalculated = false;
+
+    @Override
+    public void accept(final MeterEntity entity, final AvgPercentileArgument 
value) {
+        if (dataset.size() > 0) {
+            if (!value.getBucketedValues().isCompatible(dataset)) {
+                throw new IllegalArgumentException(
+                    "Incompatible BucketedValues [" + value + "] for current 
PercentileFunction[" + dataset + "]");
+            }
+        }
+
+        for (final int rank : value.getRanks()) {
+            if (rank <= 0) {
+                throw new IllegalArgumentException("Illegal rank value " + 
rank + ", must be positive");
+            }
+        }
+
+        if (ranks.size() > 0) {
+            if (ranks.size() != value.getRanks().length) {
+                throw new IllegalArgumentException(
+                    "Incompatible ranks size = [" + value.getRanks().length + 
"] for current PercentileFunction[" + ranks
+                        .size() + "]");
+            } else {
+                for (final int rank : value.getRanks()) {
+                    if (!ranks.include(rank)) {
+                        throw new IllegalArgumentException(
+                            "Rank " + rank + " doesn't exist in the previous 
ranks " + ranks);
+                    }
+                }
+            }
+        } else {
+            for (final int rank : value.getRanks()) {
+                ranks.add(rank);
+            }
+        }
+
+        this.entityId = entity.id();
+
+        final long[] values = value.getBucketedValues().getValues();
+        for (int i = 0; i < values.length; i++) {
+            String bucketName = 
String.valueOf(value.getBucketedValues().getBuckets()[i]);
+            summation.valueAccumulation(bucketName, values[i]);
+            count.valueAccumulation(bucketName, 1L);
+        }
+
+        this.isCalculated = false;
+    }
+
+    @Override
+    public void combine(final Metrics metrics) {
+        AvgPercentileFunction percentile = (AvgPercentileFunction) metrics;
+
+        if (!summation.keysEqual(percentile.getSummation())) {
+            log.warn("Incompatible input [{}}] for current 
PercentileFunction[{}], entity {}",
+                     percentile, this, entityId
+            );
+            return;
+        }
+        if (ranks.size() > 0) {
+            if (this.ranks.size() != ranks.size()) {
+                log.warn("Incompatible ranks size = [{}}] for current 
PercentileFunction[{}]",
+                         ranks.size(), this.ranks.size()
+                );
+                return;
+            } else {
+                if (!this.ranks.equals(percentile.getRanks())) {
+                    log.warn("Rank {} doesn't exist in the previous ranks {}", 
percentile.getRanks(), ranks);
+                    return;
+                }
+            }
+        }
+
+        this.summation.append(percentile.summation);
+        this.count.append(percentile.count);
+
+        this.isCalculated = false;
+    }
+
+    @Override
+    public void calculate() {

Review comment:
       Comment on this class.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to