/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.service.MetricsBin
 *  com.l7tech.gateway.common.service.MetricsSummaryBin
 *  com.l7tech.gateway.common.service.PublishedService
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.objectmodel.PersistentEntity
 *  com.l7tech.util.TimeUnit
 *  jakarta.annotation.PostConstruct
 *  jakarta.inject.Inject
 */
package com.l7tech.server.wsdm;

import com.l7tech.gateway.common.service.MetricsBin;
import com.l7tech.gateway.common.service.MetricsSummaryBin;
import com.l7tech.gateway.common.service.PublishedService;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.Goid;
import com.l7tech.objectmodel.PersistentEntity;
import com.l7tech.server.service.ServiceManager;
import com.l7tech.server.service.ServiceMetricsManager;
import com.l7tech.server.service.ServiceMetricsServices;
import com.l7tech.server.wsdm.subscription.ServiceStateMonitor;
import com.l7tech.util.TimeUnit;
import jakarta.annotation.PostConstruct;
import jakarta.inject.Inject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Aggregator
implements ServiceStateMonitor {
    private final Logger a = Logger.getLogger(Aggregator.class.getName());
    private final ReadWriteLock b = new ReentrantReadWriteLock();
    private final Map<Goid, MetricsSummaryBin> c = new HashMap<Goid, MetricsSummaryBin>();
    private Map<Goid, MetricsSummaryBin> d;
    private long e;
    private long f = 0L;
    @Inject
    private ServiceManager serviceManager;
    @Inject
    private ServiceMetricsManager metricsManager;
    @Inject
    private ServiceMetricsServices metricsServices;
    private static final String[] g;

    @PostConstruct
    public void start() {
        this.a();
    }

    @Override
    public void onServiceDisabled(Goid goid) {
        this.a(goid);
    }

    @Override
    public void onServiceEnabled(Goid goid) {
        this.a(goid);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @Override
    public void onServiceCreated(Goid goid) {
        this.b.writeLock().lock();
        try {
            MetricsBin metricsBin = new MetricsBin();
            metricsBin.setServiceGoid(goid);
            ArrayList<MetricsBin> arrayList = new ArrayList<MetricsBin>(1);
            arrayList.add(metricsBin);
            MetricsSummaryBin metricsSummaryBin = new MetricsSummaryBin(arrayList);
            this.c.put(goid, metricsSummaryBin);
        }
        finally {
            this.b.writeLock().unlock();
        }
    }

    @Override
    public void onServiceDeleted(Goid goid) {
        this.b.writeLock().lock();
        try {
            this.c.remove(goid);
        }
        finally {
            this.b.writeLock().unlock();
        }
    }

    public Map<Goid, MetricsSummaryBin> getMetricsForServices() {
        this.b.writeLock().lock();
        try {
            Map<Goid, MetricsSummaryBin> map = this.a(false);
            return map;
        }
        finally {
            this.b.writeLock().unlock();
        }
    }

    /*
     * Loose catch block
     */
    private Map<Goid, MetricsSummaryBin> a(boolean bl2) {
        try {
            HashMap<Goid, MetricsSummaryBin> hashMap;
            block17: {
                Goid[] goidArray;
                long l2;
                block16: {
                    hashMap = new HashMap<Goid, MetricsSummaryBin>();
                    l2 = MetricsBin.periodStartFor((int)0, (int)this.metricsServices.getFineInterval(), (long)this.f);
                    goidArray = this.c.size() == 0 ? new Goid[]{} : this.c.keySet().toArray(new Goid[this.c.size()]);
                    if (System.currentTimeMillis() - l2 <= TimeUnit.HOURS.toMillis(1L)) break block16;
                    this.a();
                    for (Map.Entry<Goid, MetricsSummaryBin> entry : this.c.entrySet()) {
                        try {
                            if (!Goid.isDefault((Goid)entry.getKey())) {
                                this.f = Math.max(this.f, entry.getValue().getPeriodEnd());
                            }
                        }
                        catch (FindException findException) {
                            throw Aggregator.a(findException);
                        }
                        hashMap.put(entry.getKey(), entry.getValue());
                    }
                    break block17;
                }
                Map<Goid, MetricsSummaryBin> map = this.metricsManager.summarizeByService(null, 0, l2, null, goidArray, bl2);
                for (Map.Entry<Goid, MetricsSummaryBin> entry : map.entrySet()) {
                    MetricsSummaryBin metricsSummaryBin;
                    block14: {
                        block15: {
                            metricsSummaryBin = this.c.get(entry.getKey());
                            if (metricsSummaryBin != null) break block14;
                            try {
                                block18: {
                                    if (Goid.isDefault((Goid)entry.getKey())) break block15;
                                    break block18;
                                    catch (FindException findException) {
                                        throw Aggregator.a(findException);
                                    }
                                }
                                this.c.put(entry.getKey(), entry.getValue());
                                this.f = Math.max(this.f, entry.getValue().getPeriodEnd());
                            }
                            catch (FindException findException) {
                                throw Aggregator.a(findException);
                            }
                        }
                        hashMap.put(entry.getKey(), entry.getValue());
                        continue;
                    }
                    ArrayList<Object> arrayList = new ArrayList<Object>(2);
                    arrayList.add(metricsSummaryBin);
                    arrayList.add((MetricsBin)entry.getValue());
                    MetricsSummaryBin metricsSummaryBin2 = new MetricsSummaryBin(arrayList);
                    try {
                        if (!Goid.isDefault((Goid)entry.getKey())) {
                            this.c.put(entry.getKey(), metricsSummaryBin2);
                            this.f = Math.max(this.f, entry.getValue().getPeriodEnd());
                        }
                    }
                    catch (FindException findException) {
                        throw Aggregator.a(findException);
                    }
                    hashMap.put(entry.getKey(), metricsSummaryBin2);
                }
            }
            return hashMap;
        }
        catch (FindException findException) {
            this.a.log(Level.WARNING, g[3], findException);
            return new HashMap<Goid, MetricsSummaryBin>();
        }
    }

    private void a(Goid goid) {
        this.metricsServices.trackServiceMetrics(goid);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public MetricsSummaryBin getMetricsForService(Goid goid) {
        this.b.writeLock().lock();
        try {
            if (this.c.containsKey(goid)) {
                if (System.currentTimeMillis() - this.f <= (long)this.metricsServices.getFineInterval()) {
                    MetricsSummaryBin metricsSummaryBin = this.c.get(goid);
                    return metricsSummaryBin;
                }
                this.a(true);
                MetricsSummaryBin metricsSummaryBin = this.c.get(goid);
                return metricsSummaryBin;
            }
            if (this.serviceManager.findByPrimaryKey(goid) != null) {
                Map<Goid, MetricsSummaryBin> map = this.a(true);
                if (map.containsKey(goid)) {
                    MetricsSummaryBin metricsSummaryBin = map.get(goid);
                    return metricsSummaryBin;
                }
                MetricsSummaryBin metricsSummaryBin = map.get(PersistentEntity.DEFAULT_GOID);
                return metricsSummaryBin;
            }
            MetricsSummaryBin metricsSummaryBin = null;
            return metricsSummaryBin;
        }
        catch (FindException findException) {
            this.a.log(Level.WARNING, g[1] + String.valueOf(goid), findException);
            MetricsSummaryBin metricsSummaryBin = null;
            return metricsSummaryBin;
        }
        finally {
            this.b.writeLock().unlock();
        }
    }

    /*
     * Unable to fully structure code
     */
    private void a() {
        try {
            if (this.metricsManager == null) {
                this.a.log(Level.WARNING, Aggregator.g[0]);
                return;
            }
        }
        catch (FindException v0) {
            throw Aggregator.a(v0);
        }
        try {
            block29: {
                if (this.d == null) ** GOTO lbl16
                try {
                    block33: {
                        if (System.currentTimeMillis() - this.e < TimeUnit.DAYS.toMillis(1L)) break block29;
                        break block33;
                        catch (FindException v1) {
                            throw Aggregator.a(v1);
                        }
                    }
                    this.d = this.metricsManager.summarizeByService(null, 2, null, null, null, false);
                }
                catch (FindException v2) {
                    throw Aggregator.a(v2);
                }
            }
            var1_1 = this.serviceManager.findAll();
            for (PublishedService var3_4 : var1_1) {
                block35: {
                    block30: {
                        block32: {
                            block31: {
                                var4_5 = this.d.get(var3_4.getGoid());
                                if (var4_5 == null) {
                                    var5_6 = this.metricsManager.summarizeByService(null, 1, null, null, new Goid[]{var3_4.getGoid()}, false).get(var3_4.getGoid());
                                } else {
                                    var5_6 = this.metricsManager.summarizeByService(null, 1, var4_5.getPeriodEnd(), null, new Goid[]{var3_4.getGoid()}, false).get(var3_4.getGoid());
                                    try {
                                        v3 = this;
                                        v4 = this.e >= var4_5.getPeriodEnd() ? this.e : var4_5.getPeriodEnd();
                                    }
                                    catch (FindException v5) {
                                        throw Aggregator.a(v5);
                                    }
                                    v3.e = v4;
                                }
                                if (var5_6 != null) break block30;
                                try {
                                    block34: {
                                        v6 = this.metricsManager;
                                        v7 = null;
                                        v8 = 0;
                                        if (var4_5 != null) break block31;
                                        break block34;
                                        catch (FindException v9) {
                                            throw Aggregator.a(v9);
                                        }
                                    }
                                    v10 = null;
                                    break block32;
                                }
                                catch (FindException v11) {
                                    throw Aggregator.a(v11);
                                }
                            }
                            v10 = var4_5.getPeriodEnd();
                        }
                        var6_7 = v6.summarizeByService(v7, v8, v10, null, new Goid[]{var3_4.getGoid()}, false).get(var3_4.getGoid());
                        break block35;
                    }
                    var6_7 = this.metricsManager.summarizeByService(null, 0, var5_6.getPeriodEnd(), null, new Goid[]{var3_4.getGoid()}, false).get(var3_4.getGoid());
                }
                var7_8 = new ArrayList<MetricsSummaryBin>(3);
                try {
                    if (var4_5 != null) {
                        var7_8.add(var4_5);
                    }
                }
                catch (FindException v12) {
                    throw Aggregator.a(v12);
                }
                try {
                    if (var5_6 != null) {
                        var7_8.add(var5_6);
                    }
                }
                catch (FindException v13) {
                    throw Aggregator.a(v13);
                }
                try {
                    if (var6_7 != null) {
                        var7_8.add(var6_7);
                    }
                }
                catch (FindException v14) {
                    throw Aggregator.a(v14);
                }
                if (var7_8.size() > 0) {
                    var8_9 = new MetricsSummaryBin(var7_8);
                } else {
                    var9_10 = new MetricsBin();
                    var9_10.setServiceGoid(var3_4.getGoid());
                    var10_11 = new ArrayList<MetricsBin>(1);
                    var10_11.add(var9_10);
                    var8_9 = new MetricsSummaryBin(var10_11);
                }
                this.c.put(var3_4.getGoid(), var8_9);
            }
            this.f = System.currentTimeMillis();
        }
        catch (FindException var1_2) {
            this.a.log(Level.WARNING, Aggregator.g[2], var1_2);
        }
    }

    private static FindException a(FindException findException) {
        return findException;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[4];
                var3_1 = 0;
                var2_2 = "u\f\u001b7e@<T\rZ'lGiI\u000e\u001eud@hR\u000b\u0019&)\b1\u0000\f\u0015ud@hR\u000b\u0019&DDrA\u0005\u001f')FsN\u0004\u00132|WyD2c\u0003\u0014;fQ<R\u0007\u000e'`@jEB\u00170}WuC\u0011Vuz@nV\u000b\u00190)AsE\u0011Z;fQ<E\u001a\u0013&}\u001f<\u0003";
                var4_3 = "u\f\u001b7e@<T\rZ'lGiI\u000e\u001eud@hR\u000b\u0019&)\b1\u0000\f\u0015ud@hR\u000b\u0019&DDrA\u0005\u001f')FsN\u0004\u00132|WyD2c\u0003\u0014;fQ<R\u0007\u000e'`@jEB\u00170}WuC\u0011Vuz@nV\u000b\u00190)AsE\u0011Z;fQ<E\u001a\u0013&}\u001f<\u0003".length();
                var1_4 = 57;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = Aggregator.a(v0, Aggregator.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "v\u0003\u001b)h\u0016}V\u0010\r/tQ/@\u0014\u001b0sUj@\"v\u0003\u001b)h\u0016}V\u0010\r/tQ/^\u0014\u001d4sU|\u0013\u0017\u00064:EjA\u0007\u0000%\u007fE";
                    var4_3 = "v\u0003\u001b)h\u0016}V\u0010\r/tQ/@\u0014\u001b0sUj@\"v\u0003\u001b)h\u0016}V\u0010\r/tQ/^\u0014\u001d4sU|\u0013\u0017\u00064:EjA\u0007\u0000%\u007fE".length();
                    var1_4 = 22;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 85;
                        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++] = Aggregator.a(v0, Aggregator.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;
            }
        }
        Aggregator.g = var5;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x5A);
        }
        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 -> 102;
                    case 1 -> 36;
                    case 2 -> 60;
                    case 3 -> 19;
                    case 4 -> 79;
                    case 5 -> 99;
                    default -> 90;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

