All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.swirlds.common.metrics.platform.PlatformStatEntry Maven / Gradle / Ivy

Go to download

Swirlds is a software platform designed to build fully-distributed applications that harness the power of the cloud without servers. Now you can develop applications with fairness in decision making, speed, trust and reliability, at a fraction of the cost of traditional server-based platforms.

There is a newer version: 0.56.6
Show newest version
/*
 * Copyright (C) 2022-2024 Hedera Hashgraph, LLC
 *
 * 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 com.swirlds.common.metrics.platform;

import static com.swirlds.metrics.api.Metric.ValueType.MAX;
import static com.swirlds.metrics.api.Metric.ValueType.MIN;
import static com.swirlds.metrics.api.Metric.ValueType.STD_DEV;
import static com.swirlds.metrics.api.Metric.ValueType.VALUE;

import com.swirlds.base.utility.ToStringBuilder;
import com.swirlds.common.metrics.PlatformMetric;
import com.swirlds.common.metrics.StatEntry;
import com.swirlds.common.metrics.statistics.StatsBuffered;
import com.swirlds.metrics.api.MetricConfig;
import com.swirlds.metrics.api.snapshot.Snapshot.SnapshotEntry;
import com.swirlds.metrics.impl.AbstractMetric;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 * Platform-implementation of {@link StatEntry}
 */
@SuppressWarnings("removal")
public class PlatformStatEntry extends AbstractMetric implements PlatformMetric, StatEntry {

    private final @NonNull DataType dataType;
    /**
     * the statistics object (if it implements StatsBuffered), else null
     */
    private final @Nullable StatsBuffered buffered;
    /**
     * a lambda that resets the statistic, using the given half life
     */
    private final @Nullable Consumer reset;
    /**
     * a lambda that returns the statistic string
     */
    private final @NonNull Supplier statsStringSupplier;
    /**
     * a lambda that returns the statistic string and resets it at the same time
     */
    private final @NonNull Supplier resetStatsStringSupplier;

    /**
     * the half life of the statistic, in seconds
     */
    private final double halfLife;

    @SuppressWarnings("unchecked")
    public PlatformStatEntry(@NonNull final StatEntry.Config config) {
        super(config);
        this.dataType = MetricConfig.mapDataType(config.getType());
        this.buffered = config.getBuffered();
        this.reset = config.getReset();
        this.statsStringSupplier = (Supplier) config.getStatsStringSupplier();
        this.resetStatsStringSupplier = (Supplier) config.getResetStatsStringSupplier();
        halfLife = config.getHalfLife();
        if (config.getInit() != null) {
            config.getInit().apply(halfLife);
        }
    }

    /**
     * {@inheritDoc}
     */
    @NonNull
    @Override
    public DataType getDataType() {
        return dataType;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @Nullable
    public StatsBuffered getBuffered() {
        return buffered;
    }

    /**
     * {@inheritDoc}
     */
    @Nullable
    @Override
    public Consumer getReset() {
        return reset;
    }

    /**
     * {@inheritDoc}
     */
    @NonNull
    @Override
    public Supplier getStatsStringSupplier() {
        return statsStringSupplier;
    }

    /**
     * {@inheritDoc}
     */
    @NonNull
    @Override
    public Supplier getResetStatsStringSupplier() {
        return resetStatsStringSupplier;
    }

    /**
     * {@inheritDoc}
     */
    @NonNull
    @Override
    public List takeSnapshot() {
        if (buffered == null) {
            return List.of(new SnapshotEntry(VALUE, resetStatsStringSupplier.get()));
        }

        final double max = buffered.getMax();
        final double min = buffered.getMin();
        final double stdDev = buffered.getStdDev();
        final Object value = resetStatsStringSupplier.get();
        return List.of(
                new SnapshotEntry(VALUE, value),
                new SnapshotEntry(MAX, max),
                new SnapshotEntry(MIN, min),
                new SnapshotEntry(STD_DEV, stdDev));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void reset() {
        if (reset != null) {
            reset.accept(halfLife);
        } else if (buffered != null) {
            buffered.reset(halfLife);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Nullable
    @SuppressWarnings("removal")
    @Override
    public StatsBuffered getStatsBuffered() {
        return getBuffered();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return new ToStringBuilder(this)
                .appendSuper(super.toString())
                .append("value", statsStringSupplier.get())
                .toString();
    }
}