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 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 { + 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 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