ResponseStatistics.java 6.74 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.scene7.is.provider.ProcessingStatus
 *  com.scene7.is.provider.Response
 *  com.scene7.is.provider.ResponseData
 *  com.scene7.is.util.MimeTypeEnum
 *  com.scene7.is.util.ServerCacheUseEnum
 *  com.scene7.is.util.Statistics
 *  com.scene7.is.util.StatisticsSnapshot
 *  com.scene7.is.util.collections.CollectionUtil
 *  org.jetbrains.annotations.NotNull
 *  org.jetbrains.annotations.Nullable
 */
package com.scene7.is.ps.provider;

import com.scene7.is.provider.ProcessingStatus;
import com.scene7.is.provider.Response;
import com.scene7.is.provider.ResponseData;
import com.scene7.is.util.MimeTypeEnum;
import com.scene7.is.util.ServerCacheUseEnum;
import com.scene7.is.util.Statistics;
import com.scene7.is.util.StatisticsSnapshot;
import com.scene7.is.util.collections.CollectionUtil;
import java.util.HashMap;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class ResponseStatistics
implements Statistics {
    private static final long STARTUP_TIME = System.currentTimeMillis();
    private long idleStartTime;
    private long startTime;
    private int enterCount;
    private long totalIdleTime;
    private int totalRequestsServed;
    private long maxRequestTime;
    private long totalRequestTime;
    private long totalBytesServed;
    private long maxBytesServed;
    private int maxRequestOverlap;
    @NotNull
    private final Map<Integer, Long> statusCounters = new HashMap<Integer, Long>();
    @NotNull
    private final Map<MimeTypeEnum, Long> mimeTypeCounters = CollectionUtil.enumMap(MimeTypeEnum.class);
    @NotNull
    private final Map<ServerCacheUseEnum, Long> cacheUseCounters = CollectionUtil.enumMap(ServerCacheUseEnum.class);
    @NotNull
    private final String name;
    @NotNull
    private final String version;
    @NotNull
    private final String revision;
    @NotNull
    private final String buildNumber;
    @NotNull
    private final String buildDate;

    public ResponseStatistics(@NotNull String name, @NotNull String version, @NotNull String revision, @NotNull String buildNumber, @NotNull String buildDate) {
        this.name = name;
        this.version = version;
        this.revision = revision;
        this.buildNumber = buildNumber;
        this.buildDate = buildDate;
        this.reset();
    }

    public static long getStartupTime() {
        return STARTUP_TIME;
    }

    public static long getUpTime() {
        return System.currentTimeMillis() - STARTUP_TIME;
    }

    public synchronized void enter() {
        if (this.enterCount == 0) {
            this.totalIdleTime += System.currentTimeMillis() - this.idleStartTime;
        }
        ++this.enterCount;
        if (this.enterCount > this.maxRequestOverlap) {
            this.maxRequestOverlap = this.enterCount;
        }
    }

    public synchronized void leave(@Nullable Response response, @Nullable ProcessingStatus status) {
        --this.enterCount;
        if (this.enterCount == 0) {
            this.idleStartTime = System.currentTimeMillis();
        }
        ++this.totalRequestsServed;
        if (status != null && status.getFetchedTime() != 0) {
            this.countRequestTime(status.getFetchedTime() - status.getCreatedTime());
        }
        if (response != null) {
            this.countBytesServed(response.getData().getSize());
            ResponseStatistics.updateDistribution(this.statusCounters, response.getStatus());
            MimeTypeEnum mimeType = response.getData().getMimeType();
            if (mimeType != null) {
                ResponseStatistics.updateDistribution(this.mimeTypeCounters, mimeType);
            }
            ResponseStatistics.updateDistribution(this.cacheUseCounters, response.getServerCacheUse());
        }
    }

    public synchronized void leave(long bytesServed, @Nullable ProcessingStatus status) {
        --this.enterCount;
        if (this.enterCount == 0) {
            this.idleStartTime = System.currentTimeMillis();
        }
        ++this.totalRequestsServed;
        if (status != null && status.getFetchedTime() != 0) {
            this.countRequestTime(status.getFetchedTime() - status.getCreatedTime());
        }
        this.countBytesServed(bytesServed);
    }

    @NotNull
    public String getName() {
        return this.name;
    }

    @NotNull
    public String getVersion() {
        return this.version;
    }

    @NotNull
    public String getRevision() {
        return this.revision;
    }

    @NotNull
    public String getBuildNumber() {
        return this.buildNumber;
    }

    @NotNull
    public String getBuildDate() {
        return this.buildDate;
    }

    public synchronized int getRequestOverlap() {
        return this.enterCount;
    }

    @NotNull
    public StatisticsSnapshot getSnapshot(boolean resetStats) {
        return this.getSnapshot(System.currentTimeMillis(), resetStats);
    }

    @NotNull
    public synchronized StatisticsSnapshot getSnapshot(long currentTime, boolean resetStats) {
        long upTime = currentTime - STARTUP_TIME;
        long elapsedTime = currentTime - this.startTime;
        long totalIdleTime = this.totalIdleTime;
        if (this.enterCount == 0) {
            totalIdleTime += currentTime - this.idleStartTime;
        }
        StatisticsSnapshot snapshot = new StatisticsSnapshot(upTime, elapsedTime, this.maxBytesServed, this.totalBytesServed, this.maxRequestTime, this.totalRequestTime, totalIdleTime, this.totalRequestsServed, this.maxRequestOverlap, this.statusCounters, this.mimeTypeCounters, this.cacheUseCounters);
        if (resetStats) {
            this.reset();
        }
        return snapshot;
    }

    private void reset() {
        this.idleStartTime = this.startTime = System.currentTimeMillis();
        this.totalIdleTime = 0;
        this.totalRequestsServed = 0;
        this.maxRequestTime = 0;
        this.totalRequestTime = 0;
        this.totalBytesServed = 0;
        this.maxBytesServed = 0;
        this.maxRequestOverlap = 0;
        this.statusCounters.clear();
        this.mimeTypeCounters.clear();
        this.cacheUseCounters.clear();
    }

    private void countRequestTime(long time) {
        if (time > this.maxRequestTime) {
            this.maxRequestTime = time;
        }
        this.totalRequestTime += time;
    }

    private void countBytesServed(long bytesServed) {
        if (this.maxBytesServed < bytesServed) {
            this.maxBytesServed = bytesServed;
        }
        this.totalBytesServed += bytesServed;
    }

    private static <T> void updateDistribution(@NotNull Map<T, Long> counters, @NotNull T object) {
        Long counter = counters.get(object);
        counter = counter == null ? Long.valueOf(1) : Long.valueOf(counter + 1);
        counters.put(object, counter);
    }
}