/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.mapping.MessageContextMapping
 *  com.l7tech.gateway.common.mapping.MessageContextMapping$MappingType
 *  com.l7tech.gateway.common.service.ServiceState
 *  com.l7tech.identity.User
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.objectmodel.PersistentEntity
 *  com.l7tech.util.NonObfuscatable
 */
package com.l7tech.server.service;

import com.l7tech.gateway.common.mapping.MessageContextMapping;
import com.l7tech.gateway.common.service.ServiceState;
import com.l7tech.identity.User;
import com.l7tech.objectmodel.Goid;
import com.l7tech.objectmodel.PersistentEntity;
import com.l7tech.util.NonObfuscatable;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.logging.Logger;

class ServiceMetrics {
    private static final Logger a;
    private final ReadWriteLock b = new ReentrantReadWriteLock();
    private final ReadWriteLock c = new ReentrantReadWriteLock();
    private final Goid d;
    private MetricsCollector e;
    private Map<MetricsDetailKey, MetricsCollector> f;
    private volatile ServiceState g;
    private static final String h;

    public ServiceMetrics(Goid goid) {
        if (Goid.isDefault((Goid)goid)) {
            throw new IllegalArgumentException(h);
        }
        this.d = goid;
        this.e = new MetricsCollector(System.currentTimeMillis());
        this.f = new HashMap<MetricsDetailKey, MetricsCollector>();
    }

    public Goid getServiceGoid() {
        return this.d;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     * Loose catch block
     */
    public void addRequest(String string, User user, List<MessageContextMapping> list, boolean bl2, boolean bl3, int n2, int n3) {
        block17: {
            this.b.readLock().lock();
            try {
                MetricsDetailKey metricsDetailKey = new MetricsDetailKey(string, user, list);
                MetricsCollector metricsCollector = null;
                this.c.readLock().lock();
                try {
                    metricsCollector = this.f.get(metricsDetailKey);
                }
                finally {
                    this.c.readLock().unlock();
                }
                if (metricsCollector == null) {
                    this.c.writeLock().lock();
                    try {
                        metricsCollector = this.f.get(metricsDetailKey);
                        if (metricsCollector == null) {
                            metricsCollector = new MetricsCollector(this.e.a);
                            this.f.put(metricsDetailKey, metricsCollector);
                        }
                    }
                    finally {
                        this.c.writeLock().unlock();
                    }
                }
                this.e.addAttemptedRequest(n2);
                metricsCollector.addAttemptedRequest(n2);
                if (!bl2) break block17;
                try {
                    block18: {
                        this.e.addAuthorizedRequest();
                        metricsCollector.addAuthorizedRequest();
                        if (!bl3) break block17;
                        break block18;
                        catch (IllegalArgumentException illegalArgumentException) {
                            throw ServiceMetrics.a(illegalArgumentException);
                        }
                    }
                    this.e.addCompletedRequest(n3);
                    metricsCollector.addCompletedRequest(n3);
                }
                catch (IllegalArgumentException illegalArgumentException) {
                    throw ServiceMetrics.a(illegalArgumentException);
                }
            }
            finally {
                this.b.readLock().unlock();
            }
        }
    }

    MetricsCollectorSet a(ServiceState serviceState) {
        MetricsCollectorSet metricsCollectorSet = null;
        this.b.writeLock().lock();
        try {
            if (serviceState != this.g || this.e.getNumAttemptedRequest() > 0) {
                metricsCollectorSet = new MetricsCollectorSet(this.d, serviceState, this.e, this.f);
            }
            this.e = new MetricsCollector(System.currentTimeMillis());
            this.f = new HashMap<MetricsDetailKey, MetricsCollector>();
        }
        finally {
            this.b.writeLock().unlock();
            this.g = serviceState;
        }
        return metricsCollectorSet;
    }

    static MetricsCollectorSet a(long l2, long l3) {
        MetricsCollector metricsCollector = new MetricsCollector(-1L);
        return new MetricsCollectorSet(PersistentEntity.DEFAULT_GOID, l2, l3, metricsCollector, Collections.emptyMap());
    }

    /*
     * Enabled aggressive block sorting
     */
    static {
        h = ServiceMetrics.a(85, ServiceMetrics.a("Q6\u0013\r\u0019Y1e<\b\u001fPW!Q'A\u0015\u001fNt@6A\u000f\u0018_tF6\u0007\u001a\u0005V "));
        a = Logger.getLogger(ServiceMetrics.class.getName());
    }

    private static IllegalArgumentException a(IllegalArgumentException illegalArgumentException) {
        return illegalArgumentException;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ '\u0001');
        }
        return cArray;
    }

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String a(int n2, char[] cArray) {
        char[] cArray2;
        block11: {
            int n3;
            int n4;
            int n5;
            block10: {
                n5 = n2;
                cArray2 = cArray;
                n4 = cArray.length;
                n3 = 0;
                if (!true) break block10;
                n4 = n4;
                if (n4 <= n3) break block11;
            }
            do {
                n5 = n5;
                cArray2 = cArray2;
                int n6 = n3;
                cArray2[n6] = (char)(cArray2[n6] ^ (n5 ^ (switch (n3 % 7) {
                    case 0 -> 119;
                    case 1 -> 6;
                    case 2 -> 52;
                    case 3 -> 46;
                    case 4 -> 37;
                    case 5 -> 111;
                    default -> 1;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }

    @NonObfuscatable
    static class MetricsCollector {
        private final long a;
        private int b;
        private int c;
        private int d;
        private int e;
        private int f;
        private long g;
        private int h;
        private int i;
        private long j;
        private final Object k = new Object();
        private final Object l = new Object();
        private final Object m = new Object();
        private static final String[] n;

        MetricsCollector(long l2) {
            this.a = l2;
        }

        /*
         * WARNING - Removed try catching itself - possible behaviour change.
         */
        public int getNumAttemptedRequest() {
            Object object = this.k;
            synchronized (object) {
                return this.b;
            }
        }

        /*
         * WARNING - Removed try catching itself - possible behaviour change.
         */
        public int getNumAuthorizedRequest() {
            Object object = this.l;
            synchronized (object) {
                return this.c;
            }
        }

        /*
         * WARNING - Removed try catching itself - possible behaviour change.
         */
        public int getNumCompletedRequest() {
            Object object = this.m;
            synchronized (object) {
                return this.d;
            }
        }

        /*
         * WARNING - Removed try catching itself - possible behaviour change.
         */
        public int getNumSuccess() {
            Object object = this.m;
            synchronized (object) {
                return this.d;
            }
        }

        /*
         * WARNING - Removed try catching itself - possible behaviour change.
         */
        public int getNumPolicyViolation() {
            Object object = this.k;
            synchronized (object) {
                Object object2 = this.l;
                synchronized (object2) {
                    return this.b - this.c;
                }
            }
        }

        /*
         * WARNING - Removed try catching itself - possible behaviour change.
         */
        public int getNumRoutingFailure() {
            Object object = this.l;
            synchronized (object) {
                Object object2 = this.m;
                synchronized (object2) {
                    return this.c - this.d;
                }
            }
        }

        /*
         * WARNING - Removed try catching itself - possible behaviour change.
         */
        public int getNumTotal() {
            Object object = this.k;
            synchronized (object) {
                return this.b;
            }
        }

        /*
         * WARNING - Removed try catching itself - possible behaviour change.
         */
        public int getMinFrontendResponseTime() {
            Object object = this.k;
            synchronized (object) {
                return this.e;
            }
        }

        /*
         * WARNING - Removed try catching itself - possible behaviour change.
         */
        public int getMaxFrontendResponseTime() {
            Object object = this.k;
            synchronized (object) {
                return this.f;
            }
        }

        /*
         * WARNING - Removed try catching itself - possible behaviour change.
         */
        public long getSumFrontendResponseTime() {
            Object object = this.k;
            synchronized (object) {
                return this.g;
            }
        }

        /*
         * WARNING - Removed try catching itself - possible behaviour change.
         */
        public int getMinBackendResponseTime() {
            Object object = this.m;
            synchronized (object) {
                return this.h;
            }
        }

        /*
         * WARNING - Removed try catching itself - possible behaviour change.
         */
        public int getMaxBackendResponseTime() {
            Object object = this.m;
            synchronized (object) {
                return this.i;
            }
        }

        /*
         * WARNING - Removed try catching itself - possible behaviour change.
         */
        public long getSumBackendResponseTime() {
            Object object = this.m;
            synchronized (object) {
                return this.j;
            }
        }

        /*
         * WARNING - Removed try catching itself - possible behaviour change.
         */
        public void setNumAttemptedRequest(int n2) {
            Object object = this.k;
            synchronized (object) {
                this.b = n2;
            }
        }

        /*
         * WARNING - Removed try catching itself - possible behaviour change.
         */
        public void setNumAuthorizedRequest(int n2) {
            Object object = this.l;
            synchronized (object) {
                this.c = n2;
            }
        }

        /*
         * WARNING - Removed try catching itself - possible behaviour change.
         */
        public void setNumCompletedRequest(int n2) {
            Object object = this.m;
            synchronized (object) {
                this.d = n2;
            }
        }

        /*
         * WARNING - Removed try catching itself - possible behaviour change.
         */
        public void setMinFrontendResponseTime(int n2) {
            Object object = this.k;
            synchronized (object) {
                this.e = n2;
            }
        }

        /*
         * WARNING - Removed try catching itself - possible behaviour change.
         */
        public void setMaxFrontendResponseTime(int n2) {
            Object object = this.k;
            synchronized (object) {
                this.f = n2;
            }
        }

        /*
         * WARNING - Removed try catching itself - possible behaviour change.
         */
        public void setSumFrontendResponseTime(long l2) {
            Object object = this.k;
            synchronized (object) {
                this.g = l2;
            }
        }

        /*
         * WARNING - Removed try catching itself - possible behaviour change.
         */
        public void setMinBackendResponseTime(int n2) {
            Object object = this.m;
            synchronized (object) {
                this.h = n2;
            }
        }

        /*
         * WARNING - Removed try catching itself - possible behaviour change.
         */
        public void setMaxBackendResponseTime(int n2) {
            Object object = this.m;
            synchronized (object) {
                this.i = n2;
            }
        }

        /*
         * WARNING - Removed try catching itself - possible behaviour change.
         */
        public void setSumBackendResponseTime(long l2) {
            Object object = this.m;
            synchronized (object) {
                this.j = l2;
            }
        }

        /*
         * WARNING - Removed try catching itself - possible behaviour change.
         */
        public void addAttemptedRequest(int n2) {
            if (n2 < 0) {
                String[] stringArray = n;
                a.warning(stringArray[3] + n2 + stringArray[1]);
                n2 = 0;
            }
            Object object = this.k;
            synchronized (object) {
                if (this.b == 0) {
                    this.e = n2;
                    this.f = n2;
                } else {
                    if (n2 < this.e) {
                        this.e = n2;
                    }
                    if (n2 > this.f) {
                        this.f = n2;
                    }
                }
                this.g += (long)n2;
                ++this.b;
            }
        }

        /*
         * WARNING - Removed try catching itself - possible behaviour change.
         */
        public void addAuthorizedRequest() {
            Object object = this.l;
            synchronized (object) {
                ++this.c;
            }
        }

        /*
         * WARNING - Removed try catching itself - possible behaviour change.
         */
        public void addCompletedRequest(int n2) {
            if (n2 < 0) {
                String[] stringArray = n;
                a.warning(stringArray[2] + n2 + stringArray[0]);
                n2 = 0;
            }
            Object object = this.m;
            synchronized (object) {
                if (this.d == 0) {
                    this.h = n2;
                    this.i = n2;
                } else {
                    if (n2 < this.h) {
                        this.h = n2;
                    }
                    if (n2 > this.i) {
                        this.i = n2;
                    }
                }
                this.j += (long)n2;
                ++this.d;
            }
        }

        /*
         * Unable to fully structure code
         */
        static {
            block10: {
                block9: {
                    var5 = new String[4];
                    var3_1 = 0;
                    var2_2 = "\u0002\u001dgcG\n&R\u0000f/\u0014\n6FP<,\b\u000b0G\u00144>\bY)G\u0002{cI!\u0002\u001dgcG\n&R\u0000f/\u0014\n6FP<,\b\u000b0G\u00144>\bY)G\u0002{cI";
                    var4_3 = "\u0002\u001dgcG\n&R\u0000f/\u0014\n6FP<,\b\u000b0G\u00144>\bY)G\u0002{cI!\u0002\u001dgcG\n&R\u0000f/\u0014\n6FP<,\b\u000b0G\u00144>\bY)G\u0002{cI".length();
                    var1_4 = 33;
                    var0_5 = -1;
lbl7:
                    // 2 sources

                    while (true) {
                        v0 = 45;
                        v1 = ++var0_5;
                        v2 = var2_2.substring(v1, v1 + var1_4);
                        v3 = -1;
                        break block9;
                        break;
                    }
lbl13:
                    // 1 sources

                    while (true) {
                        var5[var3_1++] = MetricsCollector.a(v0, MetricsCollector.a(v2));
                        if ((var0_5 += var1_4) < var4_3) {
                            var1_4 = var2_2.charAt(var0_5);
                            ** continue;
                        }
                        var2_2 = "@9_\u0007?<\tk|Z\u0007(>\u001a`8\u0018\u0014.&\u000fa2K\u0003k!\u0016c9\u0018N!@9_\u0007?<\tk|^\u0014$;\u000bk2\\F90\f~3V\u0015.u\u000bg1]Fc";
                        var4_3 = "@9_\u0007?<\tk|Z\u0007(>\u001a`8\u0018\u0014.&\u000fa2K\u0003k!\u0016c9\u0018N!@9_\u0007?<\tk|^\u0014$;\u000bk2\\F90\f~3V\u0015.u\u000bg1]Fc".length();
                        var1_4 = 32;
                        var0_5 = -1;
lbl22:
                        // 2 sources

                        while (true) {
                            v0 = 1;
                            v4 = ++var0_5;
                            v2 = var2_2.substring(v4, v4 + var1_4);
                            v3 = 0;
                            break block9;
                            break;
                        }
                        break;
                    }
lbl28:
                    // 1 sources

                    while (true) {
                        var5[var3_1++] = MetricsCollector.a(v0, MetricsCollector.a(v2));
                        if ((var0_5 += var1_4) < var4_3) {
                            var1_4 = var2_2.charAt(var0_5);
                            ** continue;
                        }
                        break block10;
                        break;
                    }
                }
                switch (v3) {
                    default: {
                        ** continue;
                    }
                    ** case 0:
lbl39:
                    // 1 sources

                    ** continue;
                }
            }
            MetricsCollector.n = var5;
        }

        private static char[] a(String string) {
            char[] cArray = string.toCharArray();
            if (cArray.length < 2) {
                cArray = cArray;
                cArray[0] = (char)(cArray[0] ^ 0x7E);
            }
            return cArray;
        }

        /*
         * Handled impossible loop by duplicating code
         * Enabled aggressive block sorting
         */
        private static String a(int n2, char[] cArray) {
            char[] cArray2;
            block11: {
                int n3;
                int n4;
                int n5;
                block10: {
                    n5 = n2;
                    cArray2 = cArray;
                    n4 = cArray.length;
                    n3 = 0;
                    if (!true) break block10;
                    n4 = n4;
                    if (n4 <= n3) break block11;
                }
                do {
                    n5 = n5;
                    cArray2 = cArray2;
                    int n6 = n3;
                    cArray2[n6] = (char)(cArray2[n6] ^ (n5 ^ (switch (n3 % 7) {
                        case 0 -> 15;
                        case 1 -> 93;
                        case 2 -> 57;
                        case 3 -> 103;
                        case 4 -> 74;
                        case 5 -> 84;
                        default -> 126;
                    })));
                    ++n3;
                    n4 = n4;
                } while (n4 > n3);
            }
            return new String(cArray2).intern();
        }
    }

    @NonObfuscatable
    static class MetricsDetailKey {
        private final String a;
        private final Goid b;
        private final String c;
        private final Set<MessageContextMapping> d;

        MetricsDetailKey(String string, User user, List<MessageContextMapping> list) {
            this.a = string;
            this.b = user == null ? null : user.getProviderId();
            this.c = user == null ? null : user.getId();
            this.d = list == null ? null : new TreeSet<MessageContextMapping>(list);
        }

        public String getOperation() {
            return this.a;
        }

        public Goid getUserProviderId() {
            return this.b;
        }

        public String getUserId() {
            return this.c;
        }

        public Set<MessageContextMapping> getMappings() {
            return this.d;
        }

        public boolean equals(Object object) {
            if (this == object) {
                return true;
            }
            if (object == null || this.getClass() != object.getClass()) {
                return false;
            }
            MetricsDetailKey metricsDetailKey = (MetricsDetailKey)object;
            if (this.a() && (this.b != null ? !this.b.equals((Object)metricsDetailKey.b) : metricsDetailKey.b != null)) {
                return false;
            }
            if (this.d != null ? !this.d.equals(metricsDetailKey.d) : metricsDetailKey.d != null) {
                return false;
            }
            if (this.a != null ? !this.a.equals(metricsDetailKey.a) : metricsDetailKey.a != null) {
                return false;
            }
            return !this.a() || !(this.c != null ? !this.c.equals(metricsDetailKey.c) : metricsDetailKey.c != null);
        }

        public int hashCode() {
            int n2 = this.a != null ? this.a.hashCode() : 0;
            n2 = 31 * n2 + (this.a() && this.b != null ? this.b.hashCode() : 0);
            n2 = 31 * n2 + (this.a() && this.c != null ? this.c.hashCode() : 0);
            n2 = 31 * n2 + (this.d != null ? this.d.hashCode() : 0);
            return n2;
        }

        private boolean a() {
            boolean bl2 = false;
            if (this.d != null) {
                for (MessageContextMapping messageContextMapping : this.d) {
                    if (messageContextMapping.getMappingType() != MessageContextMapping.MappingType.AUTH_USER) continue;
                    bl2 = true;
                }
            }
            return bl2;
        }
    }

    public static class MetricsCollectorSet {
        private final Goid a;
        private final long b;
        private final long c;
        private final ServiceState d;
        private final MetricsCollector e;
        private final Map<MetricsDetailKey, MetricsCollector> f;

        MetricsCollectorSet(Goid goid, ServiceState serviceState, MetricsCollector metricsCollector, Map<MetricsDetailKey, MetricsCollector> map) {
            this.a = goid;
            this.b = metricsCollector.a;
            this.c = System.currentTimeMillis();
            this.d = serviceState;
            this.e = metricsCollector;
            this.f = map;
        }

        MetricsCollectorSet(Goid goid, long l2, long l3, MetricsCollector metricsCollector, Map<MetricsDetailKey, MetricsCollector> map) {
            this.a = goid;
            this.b = l2;
            this.c = l3;
            this.d = null;
            this.e = metricsCollector;
            this.f = map;
        }

        public Goid getServiceGoid() {
            return this.a;
        }

        public long getStartTime() {
            return this.b;
        }

        public long getEndTime() {
            return this.c;
        }

        public ServiceState getServiceState() {
            return this.d;
        }

        public MetricsCollector getSummaryMetrics() {
            return this.e;
        }

        public Map<MetricsDetailKey, MetricsCollector> getDetailMetrics() {
            return this.f;
        }
    }
}

