| | |
| | | |
| | | } |
| | | |
| | | //zxl |
| | | @Override |
| | | public BigDecimal videoPointOnlineRateCount(DataCenterQuery params){ |
| | | //卡片统计 |
| | | int totalCount = 0; |
| | | int onlineCount = 0; |
| | | int offlineCount = 0; |
| | | int unknownCount = 0; |
| | | //构建条件 |
| | | List<Criteria> criteriaList = new ArrayList<>(); |
| | | // 添加固定条件 |
| | | criteriaList.add(Criteria.where("monitorType").regex(".*" + CheckConstants.Rule_Category_Video + ".*")); |
| | | criteriaList.add(Criteria.where("mongoCreateTime").gte(params.getStartTime()).lte(params.getEndTime())); |
| | | // 根据dataType动态添加条件 |
| | | if (params.getDataType() == 1) { |
| | | criteriaList.add(Criteria.where("provinceTag").is(Boolean.TRUE)); |
| | | } else if (params.getDataType() == 2) { |
| | | criteriaList.add(Criteria.where("deptTag").is(Boolean.TRUE)); |
| | | } |
| | | // 构建match操作 |
| | | MatchOperation match = Aggregation.match( |
| | | new Criteria().andOperator(criteriaList.toArray(new Criteria[0])) |
| | | ); |
| | | GroupOperation group = Aggregation.group() |
| | | .sum(ConditionalOperators.when(Criteria.where("online").is(ApiConstants.UY_OnlineSite_Online)).then(1).otherwise(0)).as("onlineCount") |
| | | .sum(ConditionalOperators.when(Criteria.where("online").is(ApiConstants.UY_OnlineSite_Offline)).then(1).otherwise(0)).as("offlineCount") |
| | | .sum(ConditionalOperators.when(Criteria.where("online").is(ApiConstants.UY_OnlineSite_Unknown)).then(1).otherwise(0)).as("unknownCount"); |
| | | // 将匹配阶段和分组阶段组合起来 |
| | | Aggregation aggregation = Aggregation.newAggregation(match, group); |
| | | // 执行聚合查询 |
| | | AggregationResults<Map> results = mongoTemplate.aggregate(aggregation, "t_monitor_online", Map.class); // 替换为你的集合名称 |
| | | for (Map<String, Object> result : results.getMappedResults()) { |
| | | offlineCount = (Integer) result.getOrDefault("offlineCount", 0L); |
| | | unknownCount = (Integer) result.getOrDefault("unknownCount", 0L); |
| | | onlineCount = (Integer) result.getOrDefault("onlineCount", 0L); |
| | | totalCount = offlineCount + unknownCount + onlineCount; |
| | | } |
| | | BigDecimal onlineRate = BigDecimal.ZERO; |
| | | if (totalCount!=0) { |
| | | onlineRate = new BigDecimal(onlineCount).divide(new BigDecimal(totalCount), 3,RoundingMode.DOWN).multiply(new BigDecimal("100")); |
| | | } |
| | | return onlineRate; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 视频:点位在线率 |
| | |
| | | return recordingMinTime; |
| | | } |
| | | private final TMonitorMapper tMonitorMapper; |
| | | |
| | | //zxl |
| | | @Override |
| | | public BigDecimal videoAvailabilityRateCount(DataCenterQuery params){ |
| | | System.out.println("------------------------------------------------------------------------------------"); |
| | | System.out.println(params); |
| | | // 统计数量 |
| | | List<String> noString = tMonitorMapper.getIdListVideo(); |
| | | MongoDatabase database = mongoTemplate.getDb(); |
| | | MongoCollection<Document> collection = database.getCollection("uy_record_meta_d_sum"); |
| | | List<Integer> status = Arrays.asList(1, 0, -1); |
| | | List<String> resultCount = status.stream().map(item -> { |
| | | List<Document> dList = new ArrayList<>(2); |
| | | |
| | | dList.add(new Document("recordStatus", new Document("$eq", item))); |
| | | dList.add(new Document("no", new Document("$in", noString))); |
| | | setTag(params, dList); |
| | | Document filter = new Document("$and", dList); |
| | | // 构建聚合管道 |
| | | List<Document> pipeline = Arrays.asList( |
| | | new Document("$match", filter), |
| | | // $group 去重 |
| | | new Document("$group", new Document("_id", "$deviceId")), |
| | | new Document("$count", "uniqueDeviceIds") |
| | | ); |
| | | // 执行聚合查询并获取结果 |
| | | AggregateIterable<Document> result = collection.aggregate(pipeline); |
| | | Integer uniqueDeviceIdCount = 0; |
| | | for (Document doc : result) { |
| | | uniqueDeviceIdCount = doc.getInteger("uniqueDeviceIds"); |
| | | break; // 不需要继续遍历,因为 $count 只会产生一个结果 |
| | | } |
| | | return uniqueDeviceIdCount + ""; |
| | | }).collect(Collectors.toList()); |
| | | |
| | | //计算录像可用率 |
| | | MongoDatabase databaes2 = mongoTemplate.getDb(); |
| | | MongoCollection<Document> collection2 = databaes2.getCollection("uy_record_meta_d_sum"); |
| | | |
| | | double finalRecordingMinTime = getSySMinTime(); |
| | | |
| | | List<Document> documentList = new ArrayList<>(2); |
| | | documentList.add(new Document("no", new Document("$in", noString))); |
| | | setTag(params, documentList); |
| | | Document recording = new Document("missDuration",new Document("$lte", finalRecordingMinTime)); |
| | | documentList.add(recording); |
| | | |
| | | Document filter = new Document("$and", documentList); |
| | | // 构建聚合管道 |
| | | List<Document> pipeline = Arrays.asList( |
| | | new Document("$match", filter), |
| | | // $group 去重 |
| | | new Document("$group", new Document("_id", "$deviceId")), |
| | | new Document("$count", "uniqueDeviceIds") |
| | | ); |
| | | AggregateIterable<Document> result = collection2.aggregate(pipeline); |
| | | |
| | | |
| | | Integer uniqueDeviceIdCount = 0; |
| | | for (Document doc : result) { |
| | | uniqueDeviceIdCount = doc.getInteger("uniqueDeviceIds"); |
| | | break; // 不需要继续遍历,因为 $count 只会产生一个结果 |
| | | } |
| | | int totalCount = 0; |
| | | for (String s : resultCount) { |
| | | totalCount += Integer.parseInt(s); |
| | | } |
| | | resultCount.add(0, totalCount + ""); |
| | | |
| | | BigDecimal onlineRate = BigDecimal.ZERO; |
| | | // 1:完整 0:间歇 -1:异常 | |
| | | if (!StringUtils.isEmpty(resultCount.get(0)) && !"0".equals(resultCount.get(0))) { |
| | | //resultCount.get(0)是总数 uniqueDeviceIdCount是更具系统参数查询到mongodb中大于等于 recordDuration字段的总数 |
| | | onlineRate = new BigDecimal(uniqueDeviceIdCount).divide(new BigDecimal(resultCount.get(0)), 3,RoundingMode.DOWN).multiply(new BigDecimal("100")); |
| | | } |
| | | System.out.println(resultCount); |
| | | System.out.println(onlineRate); |
| | | return onlineRate; |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 视频:录像可用率 |