| | |
| | | import com.influxdb.query.FluxTable; |
| | | import com.influxdb.query.InfluxQLQueryResult; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import javax.annotation.Resource; |
| | | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.math.BigDecimal; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import static com.iailab.framework.common.pojo.CommonResult.success; |
| | | |
| | | /** |
| | | * InfluxDB操作类 |
| | |
| | | |
| | | private InfluxQLQueryApi influxQLQueryApi; |
| | | |
| | | private String VALUE = "value"; |
| | | public static final String VALUE = "value"; |
| | | |
| | | private String TIME = "time"; |
| | | public static final String TIME = "time"; |
| | | |
| | | private int rawOffset = TimeZone.getDefault().getRawOffset(); |
| | | |
| | | private int pas_ms = 1000; |
| | | |
| | | private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | |
| | | @Resource |
| | | private InfluxDBService influxDBService; |
| | | |
| | | @Override |
| | | public void syncWriteFloatValue(String pointNo, String dataValue, long time) { |
| | |
| | | } |
| | | if (!CollectionUtils.isEmpty(tagValues)) { |
| | | tagValues.forEach(item -> { |
| | | log.info("bucket==="+ influxDBInstance.getBucket() + ",org===" + influxDBInstance.org); |
| | | log.info("bucket===" + influxDBInstance.getBucket() + ",org===" + influxDBInstance.org); |
| | | writeApi.writeMeasurement(influxDBInstance.getBucket(), influxDBInstance.org, WritePrecision.MS, item); |
| | | }); |
| | | } |
| | |
| | | calendar.set(Calendar.MILLISECOND, 0); |
| | | // String start = startTime.getTime() - calendar.getTime().getTime() + "ms"; |
| | | String start = startTime.getTime() - calendar.getTime().getTime() - pas_ms + "ms"; |
| | | if (endTime==null){ |
| | | endTime= new Date(); |
| | | if (endTime == null) { |
| | | endTime = new Date(); |
| | | } |
| | | String stop = endTime.getTime() - calendar.getTime().getTime() + "ms"; |
| | | |
| | |
| | | List<Map<String, Object>> list = data.get(queryDto.getTagIds().get(0)); |
| | | |
| | | //导出 |
| | | try{ |
| | | try { |
| | | String sheetTitle = "采集数据"; |
| | | String[] title = new String[]{"值", "时间"}; |
| | | ExcelUtil.exportPointValue(sheetTitle, title, new ArrayList<>(list), response); |
| | |
| | | |
| | | //提取list |
| | | List<Map<String, Object>> list = data.get(queryDto.getPointNos().get(0)); |
| | | for(Map<String, Object> map : list){ |
| | | for (Map<String, Object> map : list) { |
| | | PointValueExportVO dto = new PointValueExportVO(); |
| | | dto.setDatatime(map.get("time").toString()); |
| | | dto.setDatavalue(map.get("value").toString()); |
| | |
| | | sql.append("'"); |
| | | InfluxQLQueryResult data = influxQLQueryApi.query(new InfluxQLQuery(sql.toString(), influxDBInstance.getBucket())); |
| | | Object value = data.getResults().get(0).getSeries().get(0).getValues().get(0).getValueByKey("last"); |
| | | result.put(point.getPoint(), value); |
| | | if (value != null) { |
| | | result.put(point.getPoint(), value); |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | |
| | | if (influxQLQueryApi == null) { |
| | | influxQLQueryApi = influxDBInstance.getClient().getInfluxQLQueryApi(); |
| | | } |
| | | long utcMillis = startTime.getTime() -rawOffset; |
| | | long utcMillis = startTime.getTime() - rawOffset; |
| | | String utsStart = DateUtils.format(new Date(utcMillis), DateUtils.FORMAT_YEAR_MONTH_DAY); |
| | | log.info("utsStart=" + utsStart); |
| | | String measurement = PointValueUtils.getMeasurement(point.getType()); |
| | |
| | | sql.append(measurement); |
| | | sql.append(" WHERE point = '"); |
| | | sql.append(point.getPoint()); |
| | | sql.append("' AND time >= '" + utsStart +"'"); |
| | | sql.append("' AND time >= '" + utsStart + "'"); |
| | | InfluxQLQueryResult data = influxQLQueryApi.query(new InfluxQLQuery(sql.toString(), influxDBInstance.getBucket())); |
| | | if (data == null) { |
| | | return null; |
| | |
| | | Map<String, List<Map<String, Object>>> result = new HashMap<>(); |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.set(Calendar.MILLISECOND, 0); |
| | | String start = startTime.getTime() - calendar.getTime().getTime() + "ms"; |
| | | if (endTime==null){ |
| | | endTime= new Date(); |
| | | String start = (startTime.getTime() - calendar.getTime().getTime() - pas_ms) + "ms"; |
| | | if (endTime == null) { |
| | | endTime = new Date(); |
| | | } |
| | | String stop = endTime.getTime() - calendar.getTime().getTime() + "ms"; |
| | | |
| | |
| | | InfluxPointValuePOJO point = influxParams.get(i); |
| | | String measurement = PointValueUtils.getMeasurement(point.getType()); |
| | | StringBuilder sb = new StringBuilder(); |
| | | sb.append("from(bucket:\"" + influxDBInstance.bucket + "\") "); |
| | | sb.append("from(bucket:\"" + influxDBInstance.getBucket() + "\") "); |
| | | sb.append("|> range(start: ").append(start).append(", stop: ").append(stop).append(") "); |
| | | sb.append("|> filter(fn: (r) => r[\"_measurement\"] == \"" + measurement + "\")"); |
| | | sb.append("|> filter(fn: (r) => r[\"_field\"] == \"value\")"); |
| | |
| | | for (FluxRecord record : records) { |
| | | Map<String, Object> dataIem = new HashMap<>(2); |
| | | dataIem.put(VALUE, record.getValueByKey("_value")); |
| | | dataIem.put(TIME, sdf.format(Date.from(record.getTime()))); |
| | | dataIem.put(TIME, DateUtils.format(Date.from(record.getTime()), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)); |
| | | dataList.add(dataIem); |
| | | } |
| | | } |