fix: 内置 workspace 生成 dashboard

This commit is contained in:
zhangshuai
2024-10-25 16:51:43 +08:00
parent 3adafde3fe
commit 8deb34d134
2 changed files with 90 additions and 3 deletions

View File

@@ -4,6 +4,7 @@ import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.net.URLEncodeUtil;
import cn.hutool.core.net.url.UrlBuilder;
import cn.hutool.extra.template.Template;
import cn.hutool.log.Log;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
@@ -14,6 +15,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.geedge.asw.common.config.Query;
import net.geedge.asw.common.config.SpringContextUtils;
import net.geedge.asw.common.util.ASWException;
import net.geedge.asw.common.util.Constants;
import net.geedge.asw.common.util.RCode;
import net.geedge.asw.common.util.T;
import net.geedge.asw.module.app.entity.PackageEntity;
@@ -21,6 +23,7 @@ import net.geedge.asw.module.app.service.IApplicationService;
import net.geedge.asw.module.app.service.IPackageService;
import net.geedge.asw.module.environment.entity.EnvironmentEntity;
import net.geedge.asw.module.environment.service.IEnvironmentService;
import net.geedge.asw.module.feign.client.DashboardClient;
import net.geedge.asw.module.feign.client.KibanaClient;
import net.geedge.asw.module.runner.dao.PcapDao;
import net.geedge.asw.module.runner.entity.JobEntity;
@@ -31,6 +34,7 @@ import net.geedge.asw.module.runner.service.IPcapService;
import net.geedge.asw.module.runner.service.IPlaybookService;
import net.geedge.asw.module.runner.util.PcapParserThread;
import net.geedge.asw.module.runner.util.RunnerConstant;
import net.geedge.asw.module.sys.service.ISysConfigService;
import net.geedge.asw.module.workbook.service.IWorkbookResourceService;
import net.geedge.asw.module.workbook.util.WorkbookConstant;
import net.geedge.asw.module.workspace.entity.WorkspaceEntity;
@@ -84,12 +88,18 @@ public class PcapServiceImpl extends ServiceImpl<PcapDao, PcapEntity> implements
@Autowired
private IWorkspaceService workspaceService;
@Autowired
private ISysConfigService configService;
@Value("${kibana.url:127.0.0.1:5601}")
private String kibanaUrl;
@jakarta.annotation.Resource
private KibanaClient kibanaClient;
@jakarta.annotation.Resource
private DashboardClient dashboardClient;
@Value("${controller.url:http://127.0.0.1}")
private String aswControllerUrl;
@@ -335,6 +345,14 @@ public class PcapServiceImpl extends ServiceImpl<PcapDao, PcapEntity> implements
kibanaClient.saveIndexPattern(token, workspaceId, body);
}
// get index-patten id
String indexId = savedObjects.stream()
.map(obj -> {
return ((JSONObject) obj).getString("id");
})
.findFirst()
.get();
// build url
String baseUrl = UrlBuilder.ofHttp(kibanaUrl)
.addPath("/app/data-explorer/discover")
@@ -342,7 +360,7 @@ public class PcapServiceImpl extends ServiceImpl<PcapDao, PcapEntity> implements
.toString();
// build query param
String param1 = String.format("_a=(discover:(columns:!(_source),isDirty:!f,sort:!()),metadata:(indexPattern:'%s',view:discover))", workspaceId);
String param1 = String.format("_a=(discover:(columns:!(_source),isDirty:!f,sort:!()),metadata:(indexPattern:'%s',view:discover))", indexId);
String param2 = "_g=(filters:!(),refreshInterval:(pause:!t,value:0),time:(from:now-15m,to:now))";
String filter = pcapList.stream()
@@ -385,8 +403,70 @@ public class PcapServiceImpl extends ServiceImpl<PcapDao, PcapEntity> implements
SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
String token = tokenInfo.getTokenValue();
String dashboardName = String.format("dashboard-%s", workspace.getName());
String indexName = String.format("workspace-%s-*", workspace.getName());
// check if dashboard exists
JSONObject dashboard = kibanaClient.findIndexPattern(token, "dashboard" ,dashboardName);
JSONArray dashboardObjects = dashboard.getJSONArray("saved_objects");
boolean dashboardExists = dashboardObjects.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(dashboardName, title);
})
.findFirst()
.isPresent();
File dashboardFile = null;
try {
if (!dashboardExists) {
if (log.isDebugEnabled()) {
log.debug("[generateKibanaDashboardUrl] [dashboard: {}] [exists: {}]", dashboardName, dashboardExists);
}
// delete index-patten
kibanaClient.deleteIndexPattern(token, workspaceId, true);
// import dashboard
Map<Object, Object> params = T.MapUtil.builder()
.put("indexName", indexName)
.put("dashboardName", dashboardName)
.build();
// render dashboard template
String opensearchDashboardTemplate = configService.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);
JSONArray dashboards = dashboardObj.getJSONArray("saved_objects");
String dashboardId = dashboards.stream()
.filter(x -> {
JSONObject attributes = ((JSONObject) x).getJSONObject("attributes");
if (T.ObjectUtil.isEmpty(attributes)) return false;
String title = attributes.getString("title");
return T.StrUtil.equals(dashboardName, title);
})
.map(x -> {
return ((JSONObject) x).getString("id");
}).findFirst().get();
workspace.setProperties(T.JSONUtil.toJsonStr(Map.of("dashboardId", dashboardId)));
workspaceService.updateById(workspace);
}
} finally {
T.FileUtil.del(dashboardFile);
}
String dashboardId = T.JSONUtil.parseObj(workspace.getProperties()).getStr("dashboardId");
String dashboardName = String.format("workspace-%s", workspace.getName());
// build url
String baseUrl = UrlBuilder.ofHttp(kibanaUrl)

View File

@@ -158,7 +158,14 @@ public class WorkspaceServiceImpl extends ServiceImpl<WorkspaceDao, WorkspaceEnt
// get dashboardId
JSONObject dashboardObj = kibanaClient.findIndexPattern(token, "dashboard", dashboardName);
savedObjects = dashboardObj.getJSONArray("saved_objects");
String dashboardId = savedObjects.stream().map(x -> {
String dashboardId = savedObjects.stream()
.filter(x -> {
JSONObject attributes = ((JSONObject) x).getJSONObject("attributes");
if (T.ObjectUtil.isEmpty(attributes)) return false;
String title = attributes.getString("title");
return T.StrUtil.equals(dashboardName, title);
})
.map(x -> {
return ((JSONObject) x).getString("id");
}).findFirst().get();