package com.lanyuanxiaoyao.service.yarn.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lanyuanxiaoyao.service.configuration.entity.yarn.YarnApplication;
import com.lanyuanxiaoyao.service.yarn.configuration.YarnConfiguration;
import com.lanyuanxiaoyao.service.yarn.response.ApplicationDetailResponse;
import com.lanyuanxiaoyao.service.yarn.response.ApplicationsListResponse;
import com.lanyuanxiaoyao.service.yarn.service.JobService;
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.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

/**
 * Job
 *
 * @author lanyuanxiaoyao
 * @date 2023-04-23
 */
@CacheConfig(cacheManager = "normal-cache")
@Service("JobService")
public class JobServiceImpl implements JobService {
    private static final Logger logger = LoggerFactory.getLogger(JobServiceImpl.class);

    private final ObjectMapper mapper;
    private final YarnConfiguration yarnConfiguration;

    public JobServiceImpl(ObjectMapper mapper, YarnConfiguration yarnConfiguration) {
        this.mapper = mapper;
        this.yarnConfiguration = yarnConfiguration;
    }

    @Cacheable(value = "job-list", sync = true)
    @Retryable(Throwable.class)
    @Override
    public ImmutableList<YarnApplication> list() throws JsonProcessingException {
        String queryUrl = URLUtil.completeUrl(yarnConfiguration.getWebUrl(), "/ws/v1/cluster/apps");
        String body = HttpUtil.createGet(queryUrl).setMaxRedirectCount(10).execute().body();
        return mapper.readValue(body, ApplicationsListResponse.class).getApps().getApp();
    }

    @Cacheable(value = "job-list", sync = true, key = "#{methodName+name}")
    @Retryable(Throwable.class)
    @Override
    public ImmutableList<YarnApplication> listEquals(String name) throws JsonProcessingException {
        return list().select(app -> StrUtil.equals(app.getName(), name));
    }

    @Cacheable(value = "job-list", sync = true, key = "#{methodName+name}")
    @Retryable(Throwable.class)
    @Override
    public ImmutableList<YarnApplication> listLike(String name) throws JsonProcessingException {
        return list().select(app -> StrUtil.contains(app.getName(), name));
    }

    @Override
    public void kill(String applicationId) {
    }

    @Cacheable(value = "job-detail", sync = true, key = "#applicationId")
    @Retryable(Throwable.class)
    @Override
    public YarnApplication detail(String applicationId) throws JsonProcessingException {
        String queryUrl = URLUtil.completeUrl(yarnConfiguration.getWebUrl(), "/ws/v1/cluster/apps/" + applicationId);
        String body = HttpUtil.createGet(queryUrl).setMaxRedirectCount(10).execute().body();
        return mapper.readValue(body, ApplicationDetailResponse.class).getApp();
    }
}
