package cn.wise.sc.energy.power.plant.business.utils;

import cn.wise.sc.energy.power.plant.business.opentsdb.OpentsdbOkHttpClient;
import cn.wise.sc.energy.power.plant.business.opentsdb.bean.KafkaConsumeBean;
import cn.wise.sc.energy.power.plant.business.opentsdb.bean.KafkaDataBean;
import cn.wise.sc.energy.power.plant.business.opentsdb.bean.KafkaJsonBean;
import cn.wise.sc.energy.power.plant.business.opentsdb.bean.PutExt;
import cn.wise.sc.energy.power.plant.business.opentsdb.bean.PutRequestExt;
import cn.wise.sc.energy.power.plant.business.opentsdb.bean.QueryExt;
import cn.wise.sc.energy.power.plant.business.opentsdb.bean.QueryRequestExt;
import cn.wise.sc.energy.power.plant.common.core.exception.ResponseEnum;
import com.alibaba.fastjson.JSON;
import net.opentsdb.client.api.query.request.QueryLastRequest;
import net.opentsdb.client.api.query.response.QueryLastResponse;
import net.opentsdb.client.api.query.response.QueryResponse;
import net.opentsdb.client.bean.Aggregator;
import net.opentsdb.client.bean.LastDataPointQuery;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author neo.shu
 * @since 2020/5/19 19:21
 */

public class Utils {

    /**
     * kafka数据转换成opentsdb数据
     * @param bean
     * @return
     */
    public static PutRequestExt trans_k2t(KafkaConsumeBean bean) {
        List<KafkaDataBean> kafkaDataBeans = bean.getRecord().getData();
        PutRequestExt putRequestExt = PutRequestExt.builder()
                .details(true)
                .build();
        for (KafkaDataBean be:kafkaDataBeans) {
            putRequestExt.addDataPoints(PutExt.builder()
                            .metric(be.getKKSCode())
                            .value(be.getData())
                            .timestamp(bean.getStartTime())
                            .build()
                            .putTag("DataType", bean.getDataType()+"")
                            .putTag("SignalType", bean.getSignalType()+"")
                            .putTag("PartType",bean.getPartType()+""));
        }

        return putRequestExt;
    }

  //    public static void main(String[] args) throws IOException, URISyntaxException {
  ////         ResponseEnum.LICENCE_NOT_FOUND.assertNotNull(null);
  //        Object object = "11111";
  //
  //        String a =
  // "{\"record\":[{\"DataType\":0,\"SignalType\":0,\"PartType\":1,\"StartTime\":1589529222621,\"TimeSpan\":0,\"Record\":{\"Data\":[{\"KKSCode\":\"rm000000000000000000001292\",\"Data\":61.000},{\"KKSCode\":\"rm000000000000000000001293\",\"Data\":61.000},{\"KKSCode\":\"rm000000000000000000001294\",\"Data\":61.000},{\"KKSCode\":\"rm000000000000000000001295\",\"Data\":61.000},{\"KKSCode\":\"rm000000000000000000001296\",\"Data\":61.000},{\"KKSCode\":\"rm000000000000000000001297\",\"Data\":61.000}]}},{\"DataType\":0,\"SignalType\":0,\"PartType\":1,\"StartTime\":1589529223621,\"TimeSpan\":0,\"Record\":{\"Data\":[{\"KKSCode\":\"rm000000000000000000001292\",\"Data\":61.424},{\"KKSCode\":\"rm000000000000000000001293\",\"Data\":61.424},{\"KKSCode\":\"rm000000000000000000001294\",\"Data\":61.424},{\"KKSCode\":\"rm000000000000000000001295\",\"Data\":61.424},{\"KKSCode\":\"rm000000000000000000001296\",\"Data\":61.424},{\"KKSCode\":\"rm000000000000000000001297\",\"Data\":61.424}]}},{\"DataType\":0,\"SignalType\":0,\"PartType\":1,\"StartTime\":1589529224621,\"TimeSpan\":0,\"Record\":{\"Data\":[{\"KKSCode\":\"rm000000000000000000001292\",\"Data\":61.849},{\"KKSCode\":\"rm000000000000000000001293\",\"Data\":61.849},{\"KKSCode\":\"rm000000000000000000001294\",\"Data\":61.849},{\"KKSCode\":\"rm000000000000000000001295\",\"Data\":61.849},{\"KKSCode\":\"rm000000000000000000001296\",\"Data\":61.849},{\"KKSCode\":\"rm000000000000000000001297\",\"Data\":61.849}]}},{\"DataType\":0,\"SignalType\":0,\"PartType\":1,\"StartTime\":1589529225621,\"TimeSpan\":0,\"Record\":{\"Data\":[{\"KKSCode\":\"rm000000000000000000001292\",\"Data\":62.273},{\"KKSCode\":\"rm000000000000000000001293\",\"Data\":62.273},{\"KKSCode\":\"rm000000000000000000001294\",\"Data\":62.273},{\"KKSCode\":\"rm000000000000000000001295\",\"Data\":62.273},{\"KKSCode\":\"rm000000000000000000001296\",\"Data\":62.273},{\"KKSCode\":\"rm000000000000000000001297\",\"Data\":62.273}]}},{\"DataType\":0,\"SignalType\":0,\"PartType\":1,\"StartTime\":1589529226621,\"TimeSpan\":0,\"Record\":{\"Data\":[{\"KKSCode\":\"rm000000000000000000001292\",\"Data\":62.697},{\"KKSCode\":\"rm000000000000000000001293\",\"Data\":62.697},{\"KKSCode\":\"rm000000000000000000001294\",\"Data\":62.697},{\"KKSCode\":\"rm000000000000000000001295\",\"Data\":62.697},{\"KKSCode\":\"rm000000000000000000001296\",\"Data\":62.697},{\"KKSCode\":\"rm000000000000000000001297\",\"Data\":62.697}]}},{\"DataType\":0,\"SignalType\":0,\"PartType\":1,\"StartTime\":1589529227621,\"TimeSpan\":0,\"Record\":{\"Data\":[{\"KKSCode\":\"rm000000000000000000001292\",\"Data\":63.121},{\"KKSCode\":\"rm000000000000000000001293\",\"Data\":125.337},{\"KKSCode\":\"rm000000000000000000001294\",\"Data\":113.312},{\"KKSCode\":\"rm000000000000000000001295\",\"Data\":120.229},{\"KKSCode\":\"rm000000000000000000001296\",\"Data\":107.624},{\"KKSCode\":\"rm000000000000000000001297\",\"Data\":27.497}]}},{\"DataType\":0,\"SignalType\":0,\"PartType\":1,\"StartTime\":1589529228621,\"TimeSpan\":0,\"Record\":{\"Data\":[{\"KKSCode\":\"rm000000000000000000001292\",\"Data\":63.546},{\"KKSCode\":\"rm000000000000000000001293\",\"Data\":125.005},{\"KKSCode\":\"rm000000000000000000001294\",\"Data\":113.174},{\"KKSCode\":\"rm000000000000000000001295\",\"Data\":120.275},{\"KKSCode\":\"rm000000000000000000001296\",\"Data\":107.349},{\"KKSCode\":\"rm000000000000000000001297\",\"Data\":28.197}]}},{\"DataType\":0,\"SignalType\":0,\"PartType\":1,\"StartTime\":1589529229621,\"TimeSpan\":0,\"Record\":{\"Data\":[{\"KKSCode\":\"rm000000000000000000001292\",\"Data\":63.970},{\"KKSCode\":\"rm000000000000000000001293\",\"Data\":124.672},{\"KKSCode\":\"rm000000000000000000001294\",\"Data\":113.037},{\"KKSCode\":\"rm000000000000000000001295\",\"Data\":120.321},{\"KKSCode\":\"rm000000000000000000001296\",\"Data\":107.074},{\"KKSCode\":\"rm000000000000000000001297\",\"Data\":28.896}]}}]}";
  //        KafkaJsonBean kafkaJsonBean = JSON.parseObject(a, KafkaJsonBean.class);
  //        /*for (KafkaConsumeBean consumeBean : kafkaJsonBean.getRecord()) {
  //            try {
  //                OpentsdbOkHttpClient.put(Utils.trans_k2t(consumeBean));
  //            } catch (IOException e) {
  //                e.printStackTrace();
  //            } catch (URISyntaxException e) {
  //                e.printStackTrace();
  //            }
  //        }*/

  //    }

    public static void main(String[] args) throws IOException, URISyntaxException {

        QueryResponse queryResponse = OpentsdbOkHttpClient.query(QueryRequestExt.builder()
                .start("480h-ago")
                .build()
                .addQuery(QueryExt.builder()
                        .aggregator(Aggregator.AVG)
                        .downsample("2d-avg-zero")
                        .metric("C0A00101CFA01CBA00R002")
                        .build()
                ));

        QueryLastRequest queryLastRequest = new QueryLastRequest();
        LastDataPointQuery lastDataPointQuery = new LastDataPointQuery();
        lastDataPointQuery.setMetric("C0A00101CFA01CBA00R002");
        List<LastDataPointQuery> list = new ArrayList<>();
        list.add(lastDataPointQuery);
        queryLastRequest.setQueries(list);
        QueryLastResponse queryLastResponse = OpentsdbOkHttpClient.queryLast(queryLastRequest);
        System.out.println(JSON.toJSONString(queryLastRequest));
        System.out.println("");

    }

}
