package com.ctrip.xpipe.redis.console.controller.consoleportal;

import com.ctrip.xpipe.endpoint.HostPort;
import com.ctrip.xpipe.redis.console.config.ConsoleConfig;
import com.ctrip.xpipe.redis.console.controller.AbstractConsoleController;
import com.ctrip.xpipe.redis.console.health.delay.DelayService;
import com.ctrip.xpipe.redis.console.health.ping.PingService;
import com.ctrip.xpipe.redis.console.service.ClusterService;
import com.google.common.collect.ImmutableMap;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author shyin
 *         <p>
 *         Jan 5, 2017
 */
@RestController
@RequestMapping(AbstractConsoleController.CONSOLE_PREFIX)
public class HealthCheckController extends AbstractConsoleController {

    @Autowired
    private PingService pingService;
    @Autowired
    private DelayService delayService;
    @Autowired
    private ConsoleConfig config;
    @Autowired
    private ClusterService clusterService;

    @RequestMapping(value = "/redis/health/{redisIp}/{redisPort}", method = RequestMethod.GET)
    public Map<String, Boolean> isRedisHealth(@PathVariable String redisIp, @PathVariable int redisPort) {
        return ImmutableMap.of("isHealth", pingService.isRedisAlive(new HostPort(redisIp, redisPort)));
    }

    @RequestMapping(value = "/redis/delay/{redisIp}/{redisPort}", method = RequestMethod.GET)
    public Map<String, Long> getReplDelayMillis(@PathVariable String redisIp, @PathVariable int redisPort) {
        return ImmutableMap.of("delay", delayService.getDelay(new HostPort(redisIp, redisPort)));
    }

    @RequestMapping(value = "/redis/health/hickwall/" + CLUSTER_NAME_PATH_VARIABLE + "/" + SHARD_NAME_PATH_VARIABLE + "/{redisIp}/{redisPort}", method = RequestMethod.GET)
    public Map<String, String> getHickwallAddress(@PathVariable String clusterName, @PathVariable String shardName, @PathVariable String redisIp, @PathVariable int redisPort) {
        String addr = config.getHickwallAddress();
        if (Strings.isEmpty(addr)) {
            return ImmutableMap.of("addr", "");
        }
        return ImmutableMap.of("addr",
                String.format("%s.aliasBy(fx.xpipe.delay;cluster=%s;shard=%s;address=%s:%d,srcaddr)",
                        addr, clusterName, shardName, redisIp, redisPort));
    }
}

@@ -2258,20 +2258,22 @@
  String 
-addr
+prefix
  = confi
@@ -2324,20 +2324,22 @@
 isEmpty(
-addr
+prefix
 )) %7B%0A   
@@ -2405,54 +2405,118 @@
     
-return ImmutableMap.of(%22addr%22,%0A               
+String suffix = %22&panel.datasource=incluster&panel.db=FX&panelId=1&fullscreen&edit%22;%0A        String template =
  Str
@@ -2531,11 +2531,8 @@
 at(%22
-%25s.
 alia
@@ -2615,22 +2615,8 @@
     
-     
-   addr, 
 clus
@@ -2654,16 +2654,83 @@
 disPort)
+;%0A        return ImmutableMap.of(%22addr%22, prefix + template + suffix
 );%0A    %7D

replace previous design