package com.njcn;

import com.njcn.influx.utils.InfluxDbUtils;
import lombok.AllArgsConstructor;
import org.influxdb.InfluxDB;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Point;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * 类的介绍：
 *
 * @author xuyang
 * @version 1.0.0
 * @createTime 2023/6/5 14:44
 */
@AllArgsConstructor
public class InfluxDbTest {

    public static void main(String[] args) {
        InfluxDbUtils influxDbUtils = new InfluxDbUtils("root", "123456", "http://127.0.0.1:8086", "pqsadmin_jb", "");
        List<String> records = new ArrayList<>();
        List<String> phasic = Arrays.asList("A","B","C","M");
        List<String> dataType = Arrays.asList("max","min","avg","cp95");
        long time = System.currentTimeMillis();
        for (String item1 : phasic) {
            for (String item2 : dataType) {
                Map<String, String> tags = new HashMap<>();
                Map<String, Object> fields = new HashMap<>();
                tags.put("line_id","00B78D0171091");
                tags.put("phasic_type",item1);
                tags.put("value_type",item2);
                tags.put("cl_did","1");
                tags.put("process","4");
                tags.put("quality_flag","0");
                fields.put("Pq_P",1);
                fields.put("Pq_PF",null);
                fields.put("Pq_DF",1);
                Point point = influxDbUtils.pointBuilder("data_harmpower_p", time, TimeUnit.MILLISECONDS, tags, fields);
                BatchPoints batchPoints = BatchPoints.database(influxDbUtils.getDbName ()).tag("line_id", "00B78D0171091")
                        .tag("phasic_type",item1)
                        .tag("value_type",item2)
                        .tag("cl_did","1")
                        .tag("process","4")
                        .tag("quality_flag","0")
                        .retentionPolicy("")
                        .consistency(InfluxDB.ConsistencyLevel.ALL)
                        .build();
                batchPoints.point(point);
                records.add(batchPoints.lineProtocol());
            }
        }
        influxDbUtils.batchInsert(influxDbUtils.getDbName (),"", InfluxDB.ConsistencyLevel.ALL, records);
    }

}
