package com.lanyuanxiaoyao.service.exporter.controller;

import com.lanyuanxiaoyao.service.common.utils.NameHelper;
import com.lanyuanxiaoyao.service.configuration.entity.zookeeper.ZookeeperNode;
import com.lanyuanxiaoyao.service.exporter.entity.FlinkJobIdAndName;
import com.lanyuanxiaoyao.service.forest.service.InfoService;
import com.lanyuanxiaoyao.service.forest.service.ZookeeperService;
import org.eclipse.collections.api.list.ImmutableList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 出口
 *
 * @author lanyuanxiaoyao
 * @date 2024-02-02
 */
@RestController
@RequestMapping("exporter")
@CacheConfig(cacheManager = "normal-cache")
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
public class ExporterController {
    private static final Logger logger = LoggerFactory.getLogger(ExporterController.class);

    private final InfoService infoService;
    private final ZookeeperService zookeeperService;

    public ExporterController(InfoService infoService, ZookeeperService zookeeperService) {
        this.infoService = infoService;
        this.zookeeperService = zookeeperService;
    }

    @GetMapping("un_running_flink_job")
    @Cacheable(value = "un_running_flink_job", sync = true)
    public ImmutableList<FlinkJobIdAndName> unRunningFlinkJob() {
        ImmutableList<String> locks = zookeeperService.getChildren(NameHelper.ZK_SYNC_RUNNING_LOCK_PATH).collect(ZookeeperNode::getPath);
        return infoService.simpleTableMetas()
                .collect(meta -> new FlinkJobIdAndName(meta.getFlinkJobId(), meta.getFlinkJobName()))
                .distinct()
                .reject(job -> locks.contains(NameHelper.syncRunningLockPath(job.getId())));
    }
}
