feat: ASW-109 pcap页面增加跳转到opensearch dashboard按钮
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user