Skip to content

Commit

Permalink
support custom main menus in monitor template (#1703)
Browse files Browse the repository at this point in the history
Signed-off-by: tomsun28 <[email protected]>
Co-authored-by: Logic <[email protected]>
  • Loading branch information
tomsun28 and zqr10159 authored Apr 2, 2024
1 parent f33226f commit c7a82e1
Show file tree
Hide file tree
Showing 22 changed files with 350 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public class Job {
* Monitoring Task ID
*/
private long monitorId;
/**
* Is hide this app in main menus layout, only for app type, default true.
*/
private boolean hide = true;
/**
* Large categories of monitoring
* service-application service monitoring db-database monitoring custom-custom monitoring os-operating system monitoring...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,37 @@
import org.dromara.hertzbeat.common.entity.manager.GeneralConfig;
import org.dromara.hertzbeat.manager.dao.GeneralConfigDao;
import org.dromara.hertzbeat.manager.pojo.dto.SystemConfig;
import org.dromara.hertzbeat.manager.pojo.dto.TemplateConfig;
import org.dromara.hertzbeat.manager.service.AppService;
import org.dromara.hertzbeat.manager.service.impl.SystemGeneralConfigServiceImpl;
import org.dromara.hertzbeat.manager.service.impl.TemplateConfigServiceImpl;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import jakarta.annotation.Resource;
import java.util.Locale;
import java.util.TimeZone;

/**
* SystemCommandLineRunner class
* @since 4/7/2023
* Common CommandLineRunner class
*/
@Component
public class SystemCommandLineRunner implements CommandLineRunner {
@Order(value = Ordered.HIGHEST_PRECEDENCE + 2)
public class CommonCommandLineRunner implements CommandLineRunner {

private static final Integer LANG_REGION_LENGTH = 2;

@Resource
private SystemGeneralConfigServiceImpl systemGeneralConfigService;

@Resource
private TemplateConfigServiceImpl templateConfigService;

@Resource
private AppService appService;

@Resource
protected GeneralConfigDao generalConfigDao;

Expand Down Expand Up @@ -76,5 +87,8 @@ public void run(String... args) throws Exception {
.build();
generalConfigDao.save(generalConfig2Save);
}
// flush the template config in db to memory
TemplateConfig templateConfig = templateConfigService.getConfig();
appService.updateCustomTemplateConfig(templateConfig);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hertzbeat.common.entity.dto.Message;
import org.dromara.hertzbeat.manager.pojo.dto.TemplateConfig;
import org.dromara.hertzbeat.manager.service.GeneralConfigService;
import org.dromara.hertzbeat.manager.service.impl.TemplateConfigServiceImpl;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand Down Expand Up @@ -55,7 +57,7 @@ public GeneralConfigController(List<GeneralConfigService> generalConfigServices)
}

@PostMapping(path = "/{type}")
@Operation(summary = "Save the sender config", description = "保存公共配置")
@Operation(summary = "Save or update common config", description = "保存公共配置")
public ResponseEntity<Message<String>> saveOrUpdateConfig(
@Parameter(description = "Config Type", example = "email")
@PathVariable("type") @NotNull final String type,
Expand All @@ -79,4 +81,25 @@ public ResponseEntity<Message<Object>> getConfig(
}
return ResponseEntity.ok(Message.success(configService.getConfig()));
}

@PutMapping(path = "/template/{app}")
@Operation(summary = "Update the app template config")
public ResponseEntity<Message<Void>> updateTemplateAppConfig(
@PathVariable("app") @NotNull final String app,
@RequestBody TemplateConfig.AppTemplate template) {
GeneralConfigService configService = configServiceMap.get("template");
if (configService == null || !(configService instanceof TemplateConfigServiceImpl)) {
throw new IllegalArgumentException("Not supported this config type: template");
}
TemplateConfig config = ((TemplateConfigServiceImpl) configService).getConfig();
if (config == null) {
config = new TemplateConfig();
}
if (config.getApps() == null) {
config.setApps(new HashMap<>(8));
}
config.getApps().put(app, template);
configService.saveConfig(config);
return ResponseEntity.ok(Message.success());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public class Hierarchy {
*/
@Schema(description = "Is it a leaf node", example = "true", accessMode = READ_WRITE)
Boolean isLeaf = false;

/**
* Is hide this app type in main menus layout
*/
@Schema(description = "Is hide this app in main menus layout, only for app type, default true.", example = "true")
Boolean hide = true;

/**
* For leaf metric
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
import lombok.NoArgsConstructor;

/**
* 系统配置
* System Configuration
*
* @since 4/7/2023
*/
@Data
@Builder
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.dromara.hertzbeat.manager.pojo.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Map;

/**
* App Template Config
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TemplateConfig {

/**
* app template config map
* key: app name
* value: app template config
*/
private Map<String, AppTemplate> apps;


/**
* app template
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class AppTemplate {

/**
* Is hide this app in main menus layout, only for app type, default true
*/
private boolean hide = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@
import org.dromara.hertzbeat.common.entity.job.Job;
import org.dromara.hertzbeat.common.entity.manager.ParamDefine;
import org.dromara.hertzbeat.manager.pojo.dto.Hierarchy;
import org.dromara.hertzbeat.manager.pojo.dto.TemplateConfig;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
* Monitoring Type Management Interface
* 监控类型管理接口
*
*
*/
public interface AppService {

Expand Down Expand Up @@ -127,4 +125,10 @@ public interface AppService {
* @param app app
*/
void deleteMonitorDefine(String app);

/**
* update custom template config in memory
* @param config template config
*/
void updateCustomTemplateConfig(TemplateConfig config);
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.dromara.hertzbeat.manager.pojo.dto.Hierarchy;
import org.dromara.hertzbeat.manager.pojo.dto.ObjectStoreConfigChangeEvent;
import org.dromara.hertzbeat.manager.pojo.dto.ObjectStoreDTO;
import org.dromara.hertzbeat.manager.pojo.dto.TemplateConfig;
import org.dromara.hertzbeat.manager.service.AppService;
import org.dromara.hertzbeat.manager.service.MonitorService;
import org.dromara.hertzbeat.manager.service.ObjectStoreService;
Expand Down Expand Up @@ -256,6 +257,7 @@ public List<Hierarchy> getAllAppHierarchy(String lang) {
var hierarchyApp = new Hierarchy();
hierarchyApp.setCategory(job.getCategory());
hierarchyApp.setValue(job.getApp());
hierarchyApp.setHide(job.isHide());
var nameMap = job.getName();
if (nameMap != null && !nameMap.isEmpty()) {
var i18nName = CommonUtil.getLangMappingValueFromI18nMap(lang, nameMap);
Expand Down Expand Up @@ -419,6 +421,29 @@ public void deleteMonitorDefine(String app) {
appDefines.remove(app.toLowerCase());
}

@Override
public void updateCustomTemplateConfig(TemplateConfig config) {
if (config == null) {
return;
}
Map<String, TemplateConfig.AppTemplate> templateMap = config.getApps();
if (templateMap == null || templateMap.isEmpty()) {
return;
}
for (var entry : templateMap.entrySet()) {
var app = entry.getKey().toLowerCase();
var appTemplate = entry.getValue();
if (appTemplate == null) {
continue;
}
var appDefine = appDefines.get(app);
if (appDefine == null) {
continue;
}
appDefine.setHide(appTemplate.isHide());
}
}

@Override
public void run(String... args) throws Exception {
var objectStoreConfig = objectStoreConfigService.getConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
import java.util.TimeZone;

/**
*
* @since 4/7/2023
* system config service impl
*/
@Service
public class SystemGeneralConfigServiceImpl extends AbstractGeneralConfigServiceImpl<SystemConfig> {
Expand Down Expand Up @@ -77,14 +76,7 @@ public void handler(SystemConfig systemConfig) {
public String type() {
return "system";
}

/**
* 该方法用于获取NoticeSender类型的TypeReference,以供后续处理。
* This method is used to get the TypeReference of NoticeSender type for subsequent processing.
*
* @return NoticeSender类型的TypeReference
* a TypeReference of NoticeSender type
*/

@Override
protected TypeReference<SystemConfig> getTypeReference() {
return new TypeReference<>() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.dromara.hertzbeat.manager.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.dromara.hertzbeat.manager.dao.GeneralConfigDao;
import org.dromara.hertzbeat.manager.pojo.dto.TemplateConfig;
import org.dromara.hertzbeat.manager.service.AppService;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.lang.reflect.Type;

/**
* template config service impl
*/
@Service
public class TemplateConfigServiceImpl extends AbstractGeneralConfigServiceImpl<TemplateConfig> {

@Resource
private AppService appService;


/**
* 构造方法,传入GeneralConfigDao、ObjectMapper和type。
*
* <p>Constructor, passing in GeneralConfigDao, ObjectMapper and type.</p>
*
* @param generalConfigDao 配置Dao对象
* @param objectMapper JSON工具类对象
*/
protected TemplateConfigServiceImpl(GeneralConfigDao generalConfigDao, ObjectMapper objectMapper) {
super(generalConfigDao, objectMapper);
}

@Override
public void handler(TemplateConfig templateConfig) {
if (templateConfig != null) {
appService.updateCustomTemplateConfig(templateConfig);
}
}

@Override
public String type() {
return "template";
}


@Override
protected TypeReference<TemplateConfig> getTypeReference() {
return new TypeReference<>() {
@Override
public Type getType() {
return TemplateConfig.class;
}
};
}
}
19 changes: 18 additions & 1 deletion web-app/src/app/core/startup/startup.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ export class StartupService {
this.aclService.setFull(true);
// Menu data, https://ng-alain.com/theme/menu
this.menuService.add(appData.menu);
menuData.data.forEach((item: { category: string; value: string }) => {
menuData.data.forEach((item: { category: string; value: string; hide: boolean }) => {
if (item.hide) {
return;
}
let category = item.category;
let app = item.value;
let menu: Menu | null = this.menuService.getItem(category);
Expand All @@ -75,6 +78,20 @@ export class StartupService {
link: `/monitors?app=${app}`,
i18n: `monitor.app.${app}`
});
} else {
if (app != 'prometheus' && app != 'push') {
this.menuService.getItem('monitoring')?.children?.push({
text: app,
link: `/monitors?app=${app}`,
i18n: `monitor.app.${app}`,
icon: 'anticon-project'
});
}
}
});
this.menuService.getItem('monitoring')?.children?.forEach(item => {
if (item.key != null && (item.children == null || item.children.length == 0)) {
item.hide = true;
}
});
this.storageService.putData('hierarchy', menuData.data);
Expand Down
Loading

0 comments on commit c7a82e1

Please sign in to comment.