package com.lanyuanxiaoyao.service.check.actions;

import com.lanyuanxiaoyao.service.check.configuration.HdfsConfigurationProperties;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Component;

/**
 * @author lanyuanxiaoyao
 * @date 2024-02-23
 */
@Component
@ConditionalOnBean(HdfsConfigurationProperties.class)
public class HdfsChecker extends Checker {
    private static final Logger logger = LoggerFactory.getLogger(HdfsChecker.class);

    private final HdfsConfigurationProperties hdfsConfigurationProperties;

    public HdfsChecker(HdfsConfigurationProperties hdfsConfigurationProperties) {this.hdfsConfigurationProperties = hdfsConfigurationProperties;}

    @Override
    public void check() throws Exception {
        logger.info("Check hdfs path");
        FileSystem fileSystem = FileSystem.get(new Configuration());
        for (String path : hdfsConfigurationProperties.getExistsPaths()) {
            if (!fileSystem.exists(new Path(path))) {
                logger.warn("Path not exists {}", path);
            }
            for (FileStatus fileStatus : fileSystem.listStatus(new Path(path))) {
                logger.info("{}", fileStatus.getPath().toString());
            }
        }
    }

    @Override
    public String description() {
        return "Check HDFS";
    }
}
