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

import net.opentsdb.client.OpenTSDBClient;
import net.opentsdb.client.OpenTSDBConfig;
import net.opentsdb.client.bean.Query;
import net.opentsdb.client.bean.QueryResult;
import org.apache.http.HttpException;
import org.apache.http.nio.reactor.IOReactorException;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.function.Consumer;

/**
 * opentsdb工具类
 * @author neo.shu
 * @since 2020/4/28 15:41
 *//*

public class OpentsdbHttpClient {

    public static OpenTSDBConfig config = OpenTSDBConfig.address("http://192.168.110.75", 4342)
            .httpConnectionPool(100)
            .httpConnectTimeout(100)
            .batchPutBufferSize(20000)
            .batchPutSize(50)
            .batchPutTimeLimit(300)
            .config();


    */
/**
     * 使用查询Query,同步查询
     *
     * @param query
     * @return
     *//*

    public static List<QueryResult> query(Query query) {
        OpenTSDBClient client = null;
        try {
            client = OpenTSDBClientFactory.connect(config);
            return client.query(query);
        } catch (IOReactorException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                client.gracefulClose();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    */
/**
     * 使用查询参数，异步调用
     *
     * @param query
     * @param consumer
     *//*

    public static void queryAsyn(Query query, Consumer<List<QueryResult>> consumer) {
        OpenTSDBClient client = null;
        try {
            client.query(query, new QueryHttpResponseCallback.QueryCallback() {
                @Override
                public void response(Query query, List<QueryResult> queryResults) {
                    // 在请求完成并且response code成功时回调
                    consumer.accept(queryResults);
                }

                @Override
                public void responseError(Query query, HttpException e) {
                    // 在response code失败时回调
                }

                @Override
                public void failed(Query query, Exception e) {
                    // 在发生错误是回调
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                client.gracefulClose();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    */
/**
     * 写入数据
     *
     * @param point
     * @return
     *//*

    public static boolean put(Point point) {
        OpenTSDBClient client = null;
        try {
            client = OpenTSDBClientFactory.connect(config);
            client.put(point);
            return true;
        } catch (IOReactorException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                client.gracefulClose();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    */
/**
     * 查询最新数据
     * @param query
     * @return
     *//*

    public static List<LastPointQueryResult> queryLast(LastPointQuery query) {
        OpenTSDBClient client = null;
        try {
            client = OpenTSDBClientFactory.connect(config);
            return client.queryLast(query);
        } catch (IOReactorException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } finally {
            try {
                client.gracefulClose();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    */
/**
     * 查询基本信息
     * @param query
     * @return
     *//*

    public static List<String> querySuggest(SuggestQuery query) {
        OpenTSDBClient client = null;
        try {
            client = OpenTSDBClientFactory.connect(config);
            return client.querySuggest(query);
        } catch (IOReactorException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } finally {
            try {
                client.gracefulClose();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
*/
