package com.lanyuanxiaoyao.service.configuration.entity.yarn;

import cn.hutool.core.map.MapUtil;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.factory.Maps;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.map.ImmutableMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * Yarn 集群信息
 *
 * @author lanyuanxiaoyao
 * @date 2024-02-26
 */
@Component
public class YarnClusters {
    private static final Logger logger = LoggerFactory.getLogger(YarnClusters.class);
    private final ImmutableMap<String, Cluster> clusters = Maps.immutable.ofAll(
            MapUtil.<String, Cluster>builder()
                    .put("a4", new Cluster("http://132.121.107.91:8088"))
                    .put("b1", new Cluster("http://132.122.98.13:8088"))
                    .put("b5", new Cluster("http://132.122.116.12:8088"))
                    .put("b12", new Cluster("http://132.126.207.125:8088"))
                    .build()
    );

    private final ImmutableList<String> activeClusters;
    private final String defaultSyncCluster;
    private final ImmutableList<String> syncClusters;
    private final String defaultCompactionCluster;
    private final ImmutableList<String> compactionClusters;

    public YarnClusters(YarnClusterProperties yarnClusterProperties) {
        syncClusters = Lists.immutable.of(yarnClusterProperties.getSyncClusters().split(",")).select(clusters::containsKey);
        defaultSyncCluster = syncClusters.getFirstOptional().orElse("");

        compactionClusters = Lists.immutable.of(yarnClusterProperties.getCompactionClusters().split(",")).select(clusters::containsKey);
        defaultCompactionCluster = compactionClusters.getFirstOptional().orElse("");

        activeClusters = syncClusters.newWithAll(compactionClusters).distinct();
    }

    public ImmutableMap<String, Cluster> getClusters() {
        return clusters;
    }

    public Cluster getCluster(String name) {
        return clusters.get(name);
    }

    public String getWebUrl(String name) {
        if (clusters.containsKey(name)) {
            return getCluster(name).getWebUrl();
        }
        return null;
    }

    public ImmutableList<String> getActiveClusters() {
        return activeClusters;
    }

    public String getDefaultSyncCluster() {
        return defaultSyncCluster;
    }

    public ImmutableList<String> getSyncClusters() {
        return syncClusters;
    }

    public String getDefaultCompactionCluster() {
        return defaultCompactionCluster;
    }

    public ImmutableList<String> getCompactionClusters() {
        return compactionClusters;
    }

    public static final class Cluster {
        private String webUrl;

        public Cluster(String webUrl) {
            this.webUrl = webUrl;
        }

        public String getWebUrl() {
            return webUrl;
        }

        public void setWebUrl(String webUrl) {
            this.webUrl = webUrl;
        }

        @Override
        public String toString() {
            return "Cluster{" +
                   "webUrl='" + webUrl + '\'' +
                   '}';
        }
    }
}
