package com.reyun.check.hdfs;

import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.reyun.check.util.ConfigLoader;
import com.reyun.check.util.DefaultConfigLoader;
import org.apache.log4j.Logger;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import static com.amazonaws.SDKGlobalConfiguration.ACCESS_KEY_SYSTEM_PROPERTY;
import static com.amazonaws.SDKGlobalConfiguration.SECRET_KEY_SYSTEM_PROPERTY;

public class HdfsPathSize {
    private static final Logger logger = Logger.getLogger(HdfsPathSize.class);

    private DefaultConfigLoader cfg;
    public HdfsPathSize() throws ClassNotFoundException
            ,NoSuchMethodException,InstantiationException,IllegalAccessException
            ,InvocationTargetException {
        this.cfg=new ConfigLoader().getConfigInfo();
    }

    private long getFolderSize(String folder,AmazonS3 s3,String BucketName) {
        long result = 0;
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
                .withBucketName(BucketName)
                .withPrefix(folder);
        ObjectListing objects = s3.listObjects(listObjectsRequest);
        do
        {
            for (S3ObjectSummary objectSummary : objects.getObjectSummaries())
            {
                result  += objectSummary.getSize();
            }
            objects = s3.listNextBatchOfObjects(objects);
        } while (objects.isTruncated());
        return result;
    }

    public Map calc(String path, String ds) {

        String BucketName=path.split("/")[0];
        String Prefix=path.replace(BucketName+"/","");
        logger.info(path);
        AmazonS3 s3 = new AmazonS3Client();
        s3.setRegion(Region.getRegion(Regions.CN_NORTH_1));
        System.setProperty(ACCESS_KEY_SYSTEM_PROPERTY, this.cfg.properties.getProperty("accessKeyId"));
        System.setProperty(SECRET_KEY_SYSTEM_PROPERTY, this.cfg.properties.getProperty("secretKey"));

        ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
                .withBucketName(BucketName)
                .withPrefix(Prefix+"/ds=" + ds+"/").withDelimiter("/");
        ObjectListing objects ;
        Map map=new HashMap<String,Long>();
        do
        {
            objects = s3.listObjects(listObjectsRequest);
            for(String objKey : objects.getCommonPrefixes())
            {
                String appid=objKey.split("/")[objKey.split("/").length-1].split("=")[1];
                Long len=getFolderSize(objKey,s3,BucketName);
                map.put(appid,len);
                logger.info(String.format("%s\t%s\t%s\t%s",objKey,appid,ds,len ));

            }
            listObjectsRequest.setMarker(objects.getNextMarker());
        } while (objects.isTruncated());
        return map;
    }

}
