From c27d5084e3873caabea4898e1954b8f90f54fc65 Mon Sep 17 00:00:00 2001 From: "1437892690@qq.com" <1437892690@qq.com> Date: Tue, 28 Oct 2025 17:59:49 +0800 Subject: [PATCH 1/2] =?UTF-8?q?[=E5=8A=9F=E8=83=BD]=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E5=90=8C=E6=AD=A5balantflow=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E8=A7=86=E5=9B=BE=E6=95=B0=E6=8D=AE=E5=88=B0?= =?UTF-8?q?mongodb=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 关联 #[1421065807429632]增加一个同步balantflow自定义视图数据到mongodb接口 http://192.168.0.96:8090/demo/rdm.html#/story-detail/939050947543040/939050947543042/1421065807429632 --- ...CiEntityDataToMongoDBForBalantFlowApi.java | 43 ++++++++++++++++++- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/src/main/java/neatlogic/module/cmdb/api/synccientity/BatchSyncCiEntityDataToMongoDBForBalantFlowApi.java b/src/main/java/neatlogic/module/cmdb/api/synccientity/BatchSyncCiEntityDataToMongoDBForBalantFlowApi.java index 6ba29732..ea91b0c8 100644 --- a/src/main/java/neatlogic/module/cmdb/api/synccientity/BatchSyncCiEntityDataToMongoDBForBalantFlowApi.java +++ b/src/main/java/neatlogic/module/cmdb/api/synccientity/BatchSyncCiEntityDataToMongoDBForBalantFlowApi.java @@ -48,7 +48,9 @@ import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.data.domain.Sort; import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.index.Index; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.stereotype.Service; @@ -57,6 +59,7 @@ import javax.annotation.Resource; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.*; +import java.util.concurrent.TimeUnit; @Service @AuthAction(action = ADMIN.class) @@ -123,6 +126,9 @@ public class BatchSyncCiEntityDataToMongoDBForBalantFlowApi extends PrivateApiCo defaultJson.put("integrationRequestMaxCount", 10000); defaultJson.put("pageSize", 1000); defaultJson.put("needSyncData", true); + defaultJson.put("collectionName", "COLLECT_BALANTFLOWCIENTITY"); + defaultJson.put("needDeleteCollection", false); + defaultJson.put("expireAfterHours", 24); return defaultJson; } @@ -146,7 +152,15 @@ public class BatchSyncCiEntityDataToMongoDBForBalantFlowApi extends PrivateApiCo JSONArray batchList = paramObj.getJSONArray("batchList"); Boolean needSyncData = paramObj.getBoolean("needSyncData"); needSyncData = needSyncData != null && needSyncData; + String collectionName = paramObj.getString("collectionName"); + Boolean needDeleteCollection = paramObj.getBoolean("needDeleteCollection"); + needDeleteCollection = needDeleteCollection != null && needDeleteCollection; + Integer expireAfterHours = paramObj.getInteger("expireAfterHours"); + expireAfterHours = expireAfterHours != null ? expireAfterHours : 24; if (CollectionUtils.isNotEmpty(batchList)) { + if (StringUtils.isNotBlank(collectionName)) { + createCollection(collectionName, needDeleteCollection, expireAfterHours); + } JSONArray tbodyList = new JSONArray(); for (int i = 0; i < batchList.size(); i++) { JSONObject jsonObj = batchList.getJSONObject(i); @@ -321,7 +335,8 @@ public class BatchSyncCiEntityDataToMongoDBForBalantFlowApi extends PrivateApiCo balantflowObj.put("customViewName", customViewName); dictionaryObj.put("balantflow", balantflowObj); JSONObject filterObj = new JSONObject(); - filterObj.put("dictionaryName", dictionaryName); + filterObj.put("_OBJ_CATEGORY", _OBJ_CATEGORY); + filterObj.put("_OBJ_TYPE", _OBJ_TYPE); dictionaryObj.put("filter", filterObj); dictionaryList.add(dictionaryObj); } @@ -436,6 +451,29 @@ public class BatchSyncCiEntityDataToMongoDBForBalantFlowApi extends PrivateApiCo } } + private void createCollection(String collectionName, boolean needDeleteCollection, int expireAfterHours) { + boolean collectionExists = mongoTemplate.collectionExists(collectionName); + if (collectionExists && needDeleteCollection) { + mongoTemplate.dropCollection(collectionName); + collectionExists = false; + } + // 检查集合是否已存在 + if (!collectionExists) { + // 创建集合 + mongoTemplate.createCollection(collectionName); + Index index = new Index() + .named("idx_obj_category_type") + .on("_OBJ_CATEGORY", Sort.Direction.ASC) + .on("_OBJ_TYPE", Sort.Direction.ASC); + mongoTemplate.indexOps(collectionName).ensureIndex(index); + // 创建过期索引(TTL) + mongoTemplate.indexOps(collectionName) + .ensureIndex(new Index().named("idx_ttl") + .on("insertTime", Sort.Direction.ASC) + .expire(TimeUnit.HOURS.toSeconds(expireAfterHours)) // 几小时后自动删除(秒) + ); + } + } private void savePageData( JSONArray resultList, @@ -603,7 +641,8 @@ public class BatchSyncCiEntityDataToMongoDBForBalantFlowApi extends PrivateApiCo String _OBJ_TYPE = dictionaryObj.getString("_OBJ_TYPE"); dataObj.put("_OBJ_TYPE", _OBJ_TYPE); dataObj.put("dictionaryName", dictionaryObj.getString("name")); - dataObj.put("insertTime", localDateTime.format(dateTimeFormatter)); + dataObj.put("insertDate", localDateTime.format(dateTimeFormatter)); + dataObj.put("insertTime", localDateTime); dataObj.put("balantflowCiEntityId", balantflowCiEntityId); return dataObj; } -- Gitee From 20c6113024ab167edb25128bb0c374fdae297d86 Mon Sep 17 00:00:00 2001 From: "1437892690@qq.com" <1437892690@qq.com> Date: Tue, 28 Oct 2025 18:04:56 +0800 Subject: [PATCH 2/2] =?UTF-8?q?[=E5=8A=9F=E8=83=BD]=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E5=90=8C=E6=AD=A5balantflow=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E8=A7=86=E5=9B=BE=E6=95=B0=E6=8D=AE=E5=88=B0?= =?UTF-8?q?mongodb=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 关联 #[1421065807429632]增加一个同步balantflow自定义视图数据到mongodb接口 http://192.168.0.96:8090/demo/rdm.html#/story-detail/939050947543040/939050947543042/1421065807429632 --- .../BatchSyncCiEntityDataToMongoDBForBalantFlowApi.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/neatlogic/module/cmdb/api/synccientity/BatchSyncCiEntityDataToMongoDBForBalantFlowApi.java b/src/main/java/neatlogic/module/cmdb/api/synccientity/BatchSyncCiEntityDataToMongoDBForBalantFlowApi.java index ea91b0c8..b69574d0 100644 --- a/src/main/java/neatlogic/module/cmdb/api/synccientity/BatchSyncCiEntityDataToMongoDBForBalantFlowApi.java +++ b/src/main/java/neatlogic/module/cmdb/api/synccientity/BatchSyncCiEntityDataToMongoDBForBalantFlowApi.java @@ -220,7 +220,7 @@ public class BatchSyncCiEntityDataToMongoDBForBalantFlowApi extends PrivateApiCo if (MapUtils.isNotEmpty(columnObj)) { Long ciId = columnObj.getLong("ciId"); Long attrId = columnObj.getLong("attrId"); - String label = columnObj.getString("label"); +// String label = columnObj.getString("label"); attrId2CiIdMap.put(attrId, ciId); } } @@ -256,7 +256,7 @@ public class BatchSyncCiEntityDataToMongoDBForBalantFlowApi extends PrivateApiCo ) { JSONArray dictionaryList = new JSONArray(); Set balantflowCiIdSet = new HashSet<>(); - Map attrId2AttrLabelMap = new HashMap<>(); +// Map attrId2AttrLabelMap = new HashMap<>(); Map ciId2AttrListMap = new LinkedHashMap<>(); if (CollectionUtils.isNotEmpty(columnList)) { for (int i = 0; i < columnList.size(); i++) { @@ -273,7 +273,7 @@ public class BatchSyncCiEntityDataToMongoDBForBalantFlowApi extends PrivateApiCo attrObj.put("type", "String"); attrObj.put("attrId", attrId); ciId2AttrListMap.computeIfAbsent(ciId, key -> new JSONArray()).add(attrObj); - attrId2AttrLabelMap.put(attrId, label); +// attrId2AttrLabelMap.put(attrId, label); } } } -- Gitee