| | |
| | | import com.influxdb.client.InfluxDBClientFactory; |
| | | import com.influxdb.client.WriteApiBlocking; |
| | | import com.influxdb.client.write.Point; |
| | | import com.influxdb.query.FluxRecord; |
| | | import com.influxdb.query.FluxTable; |
| | | import com.zhitan.config.influxdb.InfluxdbConfig; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | WriteApiBlocking writeApi = client.getWriteApiBlocking(); |
| | | writeApi.writePoints(points); |
| | | } |
| | | |
| | | public double getLastPoint(String measurement, String tag, String s) { |
| | | if (client == null || !config.isEnable()) { |
| | | log.warn("InfluxDB client is not initialized or disabled."); |
| | | return 0; |
| | | } |
| | | |
| | | String query = String.format("from(bucket: \"%s\") " + |
| | | "|> range(start: -1h) " + |
| | | "|> filter(fn: (r) => r._measurement == \"%s\" and r._field == \"value\") " + |
| | | "|> filter(fn: (r) => r.tag == \"%s\") " + |
| | | "|> last()", config.getBucket(), measurement, s); |
| | | |
| | | List<FluxTable> tables = client.getQueryApi().query(query, config.getOrg()); |
| | | |
| | | if (tables != null && !tables.isEmpty()) { |
| | | List<FluxRecord> records = tables.get(0).getRecords(); |
| | | if (records != null && !records.isEmpty()) { |
| | | FluxRecord record = records.get(0); |
| | | double value = (double) record.getValue(); |
| | | |
| | | return value; |
| | | } |
| | | } |
| | | |
| | | log.warn("No data found for measurement: {}, tag: {}, field: {}", measurement, tag, s); |
| | | return 0; |
| | | } |
| | | } |