summaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorzhangshuai <[email protected]>2024-10-25 10:54:31 +0800
committerzhangshuai <[email protected]>2024-10-25 10:54:31 +0800
commitc3ec839d1841088852b4f22f1f32ffa7d7de89b9 (patch)
tree43c5f215e1d49033072adfe0e9f00c8066a0dc3c /src/main
parenta6a9e5c2e7044dc55cbde4583b521b04c668a9c5 (diff)
feat: ASW-109 pcap页面增加跳转到opensearch dashboard按钮dev-1.0
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/net/geedge/asw/module/workspace/service/impl/WorkspaceServiceImpl.java93
1 files changed, 91 insertions, 2 deletions
diff --git a/src/main/java/net/geedge/asw/module/workspace/service/impl/WorkspaceServiceImpl.java b/src/main/java/net/geedge/asw/module/workspace/service/impl/WorkspaceServiceImpl.java
index 00be010..a5d1193 100644
--- a/src/main/java/net/geedge/asw/module/workspace/service/impl/WorkspaceServiceImpl.java
+++ b/src/main/java/net/geedge/asw/module/workspace/service/impl/WorkspaceServiceImpl.java
@@ -66,11 +66,14 @@ public class WorkspaceServiceImpl extends ServiceImpl<WorkspaceDao, WorkspaceEnt
workspace.setUpdateUserId(StpUtil.getLoginIdAsString());
workspace.setCreateTimestamp(System.currentTimeMillis());
workspace.setUpdateTimestamp(System.currentTimeMillis());
+
+ workspace = this.createWorkspaceDashboard(workspace);
workspaceService.save(workspace);
+ String id = workspace.getId();
List<WorkspaceMemberEntity> members = workspace.getMembers();
members.stream().forEach(x -> {
- x.setWorkspaceId(workspace.getId());
+ x.setWorkspaceId(id);
x.setCreateTimestamp(System.currentTimeMillis());
x.setCreateUserId(StpUtil.getLoginIdAsString());
});
@@ -79,6 +82,92 @@ public class WorkspaceServiceImpl extends ServiceImpl<WorkspaceDao, WorkspaceEnt
return workspace;
}
+ private WorkspaceEntity createWorkspaceDashboard(WorkspaceEntity workspace) {
+ log.info("[createWorkspaceDashboard] [workspaceId: {}]", workspace.getId());
+ File dashboardFile = null;
+ try {
+ // index name
+ String indexName = String.format("workspace-%s-*", workspace.getName());
+ String indexId = T.StrUtil.uuid();
+
+ SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
+ String token = tokenInfo.getTokenValue();
+ JSONObject index = kibanaClient.findIndexPattern(token, "index-pattern", indexName);
+ JSONArray savedObjects = index.getJSONArray("saved_objects");
+
+ // check if index exists
+ 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();
+
+ log.info("[createWorkspaceDashboard] [workspace index exists:{}] [workspace indexName: {}]", indexExists, indexName);
+
+ // delete index-patten
+ if (indexExists) {
+ indexId = savedObjects.stream().map(x -> {
+ return ((JSONObject) x).getString("id");
+ }).findFirst().get();
+ kibanaClient.deleteIndexPattern(token, indexId, true);
+
+ if (log.isDebugEnabled()) {
+ log.debug("[createWorkspaceDashboard] [workspace index exists] [delete workspace index] [workspace indexName: {}] [workspace indexId: {}]", indexName, indexId);
+ }
+ }
+
+ // dashboard Name
+ String dashboardName = String.format("workspace-%s", workspace.getName());
+ Map<Object, Object> params = T.MapUtil.builder()
+ .put("indexId", indexId)
+ .put("indexName", indexName)
+ .put("dashboardName", dashboardName)
+ .build();
+
+ // render dashboard template
+ String opensearchDashboardTemplate = sysConfigService.getValue("opensearch_dashboard_template");
+ Template template = T.TemplateUtil.createEngine().getTemplate(opensearchDashboardTemplate);
+ opensearchDashboardTemplate = template.render(params);
+ dashboardFile = T.FileUtil.file(Constants.TEMP_PATH, "dashboard_template.ndjson");
+ T.FileUtil.writeString(opensearchDashboardTemplate, dashboardFile, "utf-8");
+
+ // create dashboard
+ dashboardClient.importDashboard(token, dashboardFile, true);
+
+ // get dashboardId
+ JSONObject dashboardObj = kibanaClient.findIndexPattern(token, "dashboard", dashboardName);
+ savedObjects = dashboardObj.getJSONArray("saved_objects");
+ String dashboardId = savedObjects.stream().map(x -> {
+ return ((JSONObject) x).getString("id");
+ }).findFirst().get();
+
+ // get indexId
+ JSONObject indexObj = kibanaClient.findIndexPattern(token, "index-pattern", indexName);
+ savedObjects = indexObj.getJSONArray("saved_objects");
+ indexId = savedObjects.stream().map(x -> {
+ return ((JSONObject) x).getString("id");
+ }).findFirst().get();
+
+
+ log.info("[createWorkspaceDashboard] [create dashboard] [dashboard name: {}] [dashboard id: {}]", dashboardName, dashboardId);
+ String properties = workspace.getProperties();
+ if (!T.StrUtil.isBlank(properties)){
+ T.JSONUtil.parseObj(properties).set("dashboardId", dashboardId);
+ }else {
+ workspace.setProperties(T.JSONUtil.toJsonStr(Map.of("dashboardId", dashboardId)));
+ }
+
+ workspace.setId(indexId);
+ return workspace;
+ }finally {
+ T.FileUtil.del(dashboardFile);
+ }
+ }
+
@Override
@Transactional(rollbackFor = Exception.class)
public WorkspaceEntity updateWorkspace(WorkspaceEntity workspace) {
@@ -112,7 +201,7 @@ public class WorkspaceServiceImpl extends ServiceImpl<WorkspaceDao, WorkspaceEnt
private void validateWorkspaceInfo(WorkspaceEntity workspace, boolean isUpdate) {
- if (!T.StrUtil.equalsIgnoreCase(workspace.getVisibility(), "private")) {
+ if (!T.StrUtil.equalsAnyIgnoreCase(workspace.getVisibility(), "private", "public")) {
throw new ASWException(RCode.WORKSPACE_VISIBILITY_ERROR);
}