package com.gic.haoban.manage.service.util;

import com.gic.haoban.manage.service.pojo.HBQwMonitorBO;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.Point;
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class InfluxDbMonitorUtils {

    private static Logger logger = LogManager.getLogger(InfluxDbMonitorUtils.class);

    private static String url = "http://10.0.2.9:8086";

    private static InfluxDB influxDB;

    private static final String DB_NAME = "haoban_info";

    private static final String RP_NAME = "default";

    private static volatile InfluxDbMonitorUtils instance = null;

    public static InfluxDbMonitorUtils getInstance() {
        if (instance == null) {
            synchronized (InfluxDbMonitorUtils.class) {
                if (instance == null) {
                    instance = new InfluxDbMonitorUtils();
                }
            }
        }
        return instance;
    }

    private InfluxDbMonitorUtils() {
        influxDB = InfluxDBFactory.connect(url, "inflxudb", "inflxudb");
        if (!influxDB.describeDatabases().contains(DB_NAME)) {
            influxDB.createDatabase(DB_NAME);
            influxDB.createRetentionPolicy(RP_NAME, DB_NAME, "7d", 1, true);
        }
        influxDB.setDatabase(DB_NAME);
        influxDB.setRetentionPolicy(RP_NAME);
    }

    public InfluxDB getInfluxDB() {
        return influxDB;
    }

    public void friendAdd(HBQwMonitorBO bo) {
        influxDB.write(Point.measurement("hb_qw_friend_monitor")
                .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
                .addField("num", bo.getNum())
                 .addField("failNum",bo.getFailNum())
                .tag("application","haoban-manage3")
                .tag("host", bo.getHost())
                .build());
    }

    /**
     * 查询
     *
     * @param command 查询语句
     */
    public List<Map<String, Object>> query(String command) {
        QueryResult query = influxDB.query(new Query(command, DB_NAME));
        return queryResultProcess(query);
    }

    /**
     * @param queryResult
     * @desc 查询结果处理
     */
    public List<Map<String, Object>> queryResultProcess(QueryResult queryResult) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        List<QueryResult.Result> resultList = queryResult.getResults();
        //把查询出的结果集转换成对应的实体对象，聚合成list
        for (QueryResult.Result query : resultList) {
            List<QueryResult.Series> seriesList = query.getSeries();
            if (seriesList != null && seriesList.size() != 0) {
                for (QueryResult.Series series : seriesList) {
                    List<String> columns = series.getColumns();
                    String[] keys = columns.toArray(new String[columns.size()]);
                    List<List<Object>> values = series.getValues();
                    if (values != null && values.size() != 0) {
                        for (List<Object> value : values) {
                            Map<String, Object> map = new HashMap(keys.length);
                            for (int i = 0; i < keys.length; i++) {
                                map.put(keys[i], value.get(i));
                            }
                            mapList.add(map);
                        }
                    }
                }
            }
        }
        return mapList;
    }
}
