package com.cnd3b.utility.obs;

import com.huaweicloud.sdk.ces.v1.CesClient;
import com.huaweicloud.sdk.ces.v1.model.Datapoint;
import com.huaweicloud.sdk.ces.v1.model.ShowMetricDataRequest;
import com.huaweicloud.sdk.ces.v1.model.ShowMetricDataResponse;
import com.huaweicloud.sdk.ces.v1.region.CesRegion;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class BucketCESManageTest {
    protected String bucketname;
    CesClient cesClient;
    ArrayList<String> sqllist;
    long fromtime;
    long totime;

    public BucketCESManageTest(String siteid) {
        this.bucketname = siteid.toLowerCase();
        ICredential auth = new BasicCredentials().withAk("EKJMDXUVJLBGDRCVVVLP").withSk("9EUAXeFmxKjJ5x12lYVTzCCUuIQIjrO6VAyzGr9u");
        cesClient = CesClient.newBuilder() .withCredential(auth) .withRegion(CesRegion.valueOf("cn-east-2")) .build();
        sqllist = new ArrayList<>();
    }

    public static void main(String[] args) {
        BucketCESManageTest bucketCESManage = new BucketCESManageTest("bcdq");
        bucketCESManage.getData();
    }

    public void getData() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);

        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        fromtime = calendar.getTimeInMillis();
        calendar.add(Calendar.DATE, 1);
        totime = calendar.getTimeInMillis();


        getDownload_traffic();
        getUpload_traffic();
    }

    /**
     * 总下载流量
     */
    private void getDownload_traffic() {
        ShowMetricDataRequest request = new ShowMetricDataRequest();
        request.withNamespace("SYS.OBS");
        request.withMetricName("download_traffic");
        request.withDim0("bucket_name," + bucketname);
        request.withFilter(ShowMetricDataRequest.FilterEnum.fromValue("sum"));
        request.withPeriod(14400);
        request.withFrom(fromtime);
        request.withTo(totime);
        try {
            ShowMetricDataResponse response = cesClient.showMetricData(request);
            List<Datapoint> list = response.getDatapoints();
            if (!list.isEmpty()) {
                for (Datapoint datapoint : list) {
                    System.err.println(datapoint.toString());
                }
            }
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }

    /**
     * 总上传流量
     */
    private void getUpload_traffic() {
        ShowMetricDataRequest request = new ShowMetricDataRequest();
        request.withNamespace("SYS.OBS");
        request.withMetricName("upload_traffic");
        request.withDim0("bucket_name," + bucketname);
        request.withFilter(ShowMetricDataRequest.FilterEnum.fromValue("sum"));
        request.withPeriod(14400);
        request.withFrom(fromtime);
        request.withTo(totime);
        try {
            ShowMetricDataResponse response = cesClient.showMetricData(request);
            List<Datapoint> list = response.getDatapoints();
            if (!list.isEmpty()) {
                for (Datapoint datapoint : list) {
                    System.err.println(datapoint.toString());
                }
            }
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}
