fix: 生成 default dashboard 时,先检查 index-pattern 是否存在,存在即删除

This commit is contained in:
zhangshuai
2024-10-25 17:09:38 +08:00
parent 8deb34d134
commit 890f5ccda9

View File

@@ -426,8 +426,28 @@ public class PcapServiceImpl extends ServiceImpl<PcapDao, PcapEntity> implements
if (log.isDebugEnabled()) {
log.debug("[generateKibanaDashboardUrl] [dashboard: {}] [exists: {}]", dashboardName, dashboardExists);
}
// delete index-patten
kibanaClient.deleteIndexPattern(token, workspaceId, true);
// check if index-pattern exists
JSONObject indexPattern = kibanaClient.findIndexPattern(token, "index-pattern", indexName);
JSONArray savedObjects = indexPattern.getJSONArray("saved_objects");
boolean indexExists = savedObjects.stream()
.filter(obj -> {
JSONObject attributes = ((JSONObject) obj).getJSONObject("attributes");
if (T.ObjectUtil.isEmpty(attributes)) return false;
String title = attributes.getString("title");
return T.StrUtil.equals(indexName, title);
})
.findFirst()
.isPresent();
if (indexExists){
// delete index-pattern
String indexId = savedObjects.stream().map(x -> {
return ((JSONObject) x).getString("id");
}).findFirst().get();
kibanaClient.deleteIndexPattern(token, indexId, true);
}
// import dashboard
Map<Object, Object> params = T.MapUtil.builder()