package com.ctrip.platform.dal.dao.datasource.log;

import com.ctrip.platform.dal.cluster.Cluster;
import com.ctrip.platform.dal.cluster.cluster.ClusterType;
import com.ctrip.platform.dal.cluster.config.LocalizationConfig;
import com.ctrip.platform.dal.cluster.config.LocalizationState;
import com.ctrip.platform.dal.cluster.database.DatabaseRole;

import java.util.Map;

/**
 * @author c7ch23en
 */
public class ClusterDbSqlContext extends BaseSqlContext {

    protected static final String CLUSTER = "Cluster";
    protected static final String SHARD = "Shard";
    protected static final String ROLE = "Role";
    protected static final String DB_KEY = "DB";
    protected static final String CLUSTER_TYPE = "ClusterType";
    protected static final String UCS_STRATEGY = "UcsStrategy";

    private final Cluster cluster;
    private Integer shard;
    private DatabaseRole role;

    public ClusterDbSqlContext(Cluster cluster, Integer shard, DatabaseRole role) {
        this(cluster, shard, role, null);
    }

    public ClusterDbSqlContext(Cluster cluster, Integer shard, DatabaseRole role, String dbName) {
        super(dbName);
        this.cluster = cluster;
        this.shard = shard;
        this.role = role;
    }

    public ClusterDbSqlContext(Cluster cluster, Integer shard, DatabaseRole role,
                               String clientVersion, String clientZone, String dbName) {
        super(clientVersion, clientZone, dbName);
        this.cluster = cluster;
        this.shard = shard;
        this.role = role;
    }

    @Override
    protected Map<String, String> toMetricTags() {
        Map<String, String> tags = super.toMetricTags();
        addTag(tags, CLUSTER, cluster.getClusterName());
        addTag(tags, SHARD, shard);
        addTag(tags, ROLE, role);
        addTag(tags, DB_KEY, String.format("%s-%s-%s", cluster, shard, role));
        ClusterType type = cluster.getClusterType();
        if (type == ClusterType.DRC) {
            LocalizationConfig config = cluster.getLocalizationConfig();
            if (config != null && config.getLocalizationState() == LocalizationState.ACTIVE)
                tags.put(CLUSTER_TYPE, type.getValue() + "_" + config.getLocalizationState().getValue());
            else
                tags.put(CLUSTER_TYPE, type.getValue() + "_" + LocalizationState.PREPARED.getValue());
            if (config != null && config.getUnitStrategyId() != null)
                tags.put(UCS_STRATEGY, String.valueOf(config.getUnitStrategyId()));
        } else if (type != null)
            tags.put(CLUSTER_TYPE, type.getValue());
        return tags;
    }

    @Override
    protected void addTag(Map<String, String> tags, String tagName, Object tagValue) {
        super.addTag(tags, tagName, tagValue);
        if (tagValue instanceof Integer)
            tags.put(tagName, tagValue.toString());
        else if (tagValue instanceof DatabaseRole)
            tags.put(tagName, ((DatabaseRole) tagValue).getValue());
    }

    @Override
    public SqlContext fork() {
        ClusterDbSqlContext context = new ClusterDbSqlContext(cluster, shard, role,
                getClientVersion(), getClientZone(), getDbName());
        context.populateDbZone(getDbZone());
        context.populateDatabase(getDatabase());
        return context;
    }

    public void populateShard(Integer shard) {
        this.shard = shard;
    }

    public void populateRole(DatabaseRole role) {
        this.role = role;
    }

}

@@ -3302,32 +3302,178 @@
 context;%0A    %7D%0A%0A
+    public String getClusterName() %7B%0A        return cluster.getClusterName();%0A    %7D%0A%0A    public Integer getShard() %7B%0A        return shard;%0A    %7D%0A%0A
     public void 

add method to get shardindex