|
@@ -9,8 +9,12 @@ import com.influxdb.client.QueryApi;
|
|
|
import com.influxdb.query.FluxTable;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.Assert;
|
|
|
+import vip.xiaonuo.coldchain.core.alarm.service.SensorAlarmService;
|
|
|
import vip.xiaonuo.coldchain.core.bean.influxdb.SensorData;
|
|
|
+import vip.xiaonuo.coldchain.core.config.JfcloudColdChainConstants;
|
|
|
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
@@ -24,8 +28,11 @@ import java.util.stream.Collectors;
|
|
|
*/
|
|
|
@Service
|
|
|
public class JfcloudSensorDataService extends JfcloudFluxDataService<SensorData> {
|
|
|
- public JfcloudSensorDataService(JfcloudInfluxDBService jfcloudInfluxDBService, JfcloudInfluxDB2Properties jfcloudInfluxDB2Properties) {
|
|
|
+ private SensorAlarmService sensorAlarmService;
|
|
|
+
|
|
|
+ public JfcloudSensorDataService(JfcloudInfluxDBService jfcloudInfluxDBService, JfcloudInfluxDB2Properties jfcloudInfluxDB2Properties, SensorAlarmService sensorAlarmService) {
|
|
|
super(jfcloudInfluxDBService, jfcloudInfluxDB2Properties);
|
|
|
+ this.sensorAlarmService = sensorAlarmService;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -75,7 +82,7 @@ public class JfcloudSensorDataService extends JfcloudFluxDataService<SensorData>
|
|
|
// 获取聚合窗口的代码
|
|
|
String aggregationWindowCode = aggregationWindow != null ? aggregationWindow.getCode() : "default";
|
|
|
// 构建查询语句
|
|
|
- String measurement = "sensor_data"; // 数据表名称
|
|
|
+ String measurement = JfcloudColdChainConstants.INFLUXDB_DEFAULT_MEASUREMENT_NAME; // 数据表名称
|
|
|
Map<String, String> filters = Map.of(
|
|
|
"device_id", deviceId,
|
|
|
"roads", roads.toString()
|
|
@@ -130,4 +137,122 @@ public class JfcloudSensorDataService extends JfcloudFluxDataService<SensorData>
|
|
|
// return outputFormatter.format(instant.atZone(ZoneOffset.UTC));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取当前月的数据总数
|
|
|
+ *
|
|
|
+ * @param deviceId 设备ID
|
|
|
+ * @param roads 道路字段
|
|
|
+ * @return 当前月数据总数
|
|
|
+ */
|
|
|
+ public long getCurrentMonthDataCount(String deviceId, String roads) {
|
|
|
+ // 获取当前月份的起始时间和结束时间
|
|
|
+ String startDate = getFirstDayOfCurrentMonth();
|
|
|
+ String endDate = getLastDayOfCurrentMonth();
|
|
|
+ return getSensorDataCount(startDate, endDate, deviceId, roads);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取上月的数据总数
|
|
|
+ *
|
|
|
+ * @param deviceId 设备ID
|
|
|
+ * @param roads 道路字段
|
|
|
+ * @return 上月数据总数
|
|
|
+ */
|
|
|
+ public long getLastMonthDataCount(String deviceId, String roads) {
|
|
|
+ // 获取上个月的起始时间和结束时间
|
|
|
+ String startDate = getFirstDayOfLastMonth();
|
|
|
+ String endDate = getLastDayOfLastMonth();
|
|
|
+ return getSensorDataCount(startDate, endDate, deviceId, roads);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取数据总数,传入查询时间范围和参数
|
|
|
+ *
|
|
|
+ * @param startDate 查询开始日期
|
|
|
+ * @param endDate 查询结束日期
|
|
|
+ * @param deviceId 设备ID
|
|
|
+ * @param roads 道路字段
|
|
|
+ * @return 数据总数
|
|
|
+ */
|
|
|
+ private long getSensorDataCount(String startDate, String endDate, String deviceId, String roads) {
|
|
|
+ long count = 0;
|
|
|
+ try {
|
|
|
+ // 构建Flux查询,简化了不必要的group操作
|
|
|
+ String query = String.format("""
|
|
|
+ from(bucket: "%s")
|
|
|
+ |> range(start: %s, stop: %s)
|
|
|
+ |> filter(fn: (r) => r["_measurement"] == "%s")
|
|
|
+ |> filter(fn: (r) => r["device_id"] == "%s" and r["roads"] == "%s")
|
|
|
+ |> count()
|
|
|
+ """, getBucketName(), startDate, endDate, JfcloudColdChainConstants.INFLUXDB_DEFAULT_MEASUREMENT_NAME, deviceId, roads);
|
|
|
+
|
|
|
+ // 获取QueryApi并执行查询
|
|
|
+ QueryApi queryApi = jfcloudInfluxDBService.getInfluxDBClient().getQueryApi();
|
|
|
+ List<FluxTable> result = queryApi.query(query);
|
|
|
+
|
|
|
+ // 处理查询结果
|
|
|
+ if (result != null && !result.isEmpty()) {
|
|
|
+ FluxTable fluxTable = result.get(0);
|
|
|
+ if (fluxTable.getRecords() != null && !fluxTable.getRecords().isEmpty()) {
|
|
|
+ // 获取结果值
|
|
|
+ Object countValue = fluxTable.getRecords().get(0).getValues().get("_value");
|
|
|
+ if (countValue != null) {
|
|
|
+ // 确保转换为正确的数字类型
|
|
|
+ if (countValue instanceof Number) {
|
|
|
+ count = ((Number) countValue).longValue(); // 类型安全转换
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace(); // 打印异常信息
|
|
|
+ }
|
|
|
+ return count; // 返回统计结果
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前月的起始日期
|
|
|
+ */
|
|
|
+ private String getFirstDayOfCurrentMonth() {
|
|
|
+ LocalDate firstDayOfCurrentMonth = LocalDate.now().withDayOfMonth(1);
|
|
|
+ return firstDayOfCurrentMonth.format(DateTimeFormatter.ISO_DATE) + "T00:00:00Z";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前月的结束日期
|
|
|
+ */
|
|
|
+ private String getLastDayOfCurrentMonth() {
|
|
|
+ LocalDate lastDayOfCurrentMonth = LocalDate.now().withDayOfMonth(1).plusMonths(1).minusDays(1);
|
|
|
+ return lastDayOfCurrentMonth.format(DateTimeFormatter.ISO_DATE) + "T23:59:59Z";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取上月的起始日期
|
|
|
+ */
|
|
|
+ private String getFirstDayOfLastMonth() {
|
|
|
+ LocalDate firstDayOfLastMonth = LocalDate.now().minusMonths(1).withDayOfMonth(1);
|
|
|
+ return firstDayOfLastMonth.format(DateTimeFormatter.ISO_DATE) + "T00:00:00Z";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取上月的结束日期
|
|
|
+ */
|
|
|
+ private String getLastDayOfLastMonth() {
|
|
|
+ LocalDate lastDayOfLastMonth = LocalDate.now().minusMonths(1).withDayOfMonth(1).plusMonths(1).minusDays(1);
|
|
|
+ return lastDayOfLastMonth.format(DateTimeFormatter.ISO_DATE) + "T23:59:59Z";
|
|
|
+ }
|
|
|
+
|
|
|
+ public long getLastMonthAlarmCount(String sensorCode, String sensorRoute) {
|
|
|
+ // 获取上个月的起始时间和结束时间
|
|
|
+ String startDate = getFirstDayOfLastMonth();
|
|
|
+ String endDate = getLastDayOfLastMonth();
|
|
|
+ return sensorAlarmService.countAlarms(startDate, endDate, sensorCode, sensorRoute);
|
|
|
+ }
|
|
|
+
|
|
|
+ public long getCurrentMonthAlarmCount(String sensorCode, String sensorRoute) {
|
|
|
+ String startDate = getFirstDayOfCurrentMonth();
|
|
|
+ String endDate = getLastDayOfCurrentMonth();
|
|
|
+ return sensorAlarmService.countAlarms(startDate, endDate, sensorCode, sensorRoute);
|
|
|
+ }
|
|
|
}
|