Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support config timezone locale language region on web ui #1154

Merged
merged 7 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.googlecode.aviator.exception.ExpressionRuntimeException;
import com.googlecode.aviator.exception.ExpressionSyntaxErrorException;
import org.dromara.hertzbeat.alert.AlerterWorkerPool;
import org.dromara.hertzbeat.alert.dao.AlertDao;
import org.dromara.hertzbeat.alert.reduce.AlarmCommonReduce;
import org.dromara.hertzbeat.alert.service.AlertService;
import org.dromara.hertzbeat.common.queue.CommonDataQueue;
Expand All @@ -35,11 +34,13 @@
import org.dromara.hertzbeat.common.entity.manager.Monitor;
import org.dromara.hertzbeat.common.entity.message.CollectRep;
import org.dromara.hertzbeat.common.constants.CommonConstants;
import org.dromara.hertzbeat.common.support.event.SystemConfigChangeEvent;
import org.dromara.hertzbeat.common.util.CommonUtil;
import org.dromara.hertzbeat.common.util.ResourceBundleUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

import javax.persistence.criteria.Predicate;
import java.util.*;
Expand All @@ -55,7 +56,7 @@
*
* @author tom
*/
@Configuration
@Component
@Slf4j
public class CalculateAlarm {

Expand All @@ -71,7 +72,7 @@ public class CalculateAlarm {
private final CommonDataQueue dataQueue;
private final AlertDefineService alertDefineService;
private final AlarmCommonReduce alarmCommonReduce;
private final ResourceBundle bundle;
private ResourceBundle bundle;
private final AlertService alertService;

public CalculateAlarm(AlerterWorkerPool workerPool, CommonDataQueue dataQueue,
Expand Down Expand Up @@ -366,4 +367,10 @@ private List<Alert> queryAvailabilityAlerts(long monitorId, Alert restoreAlert)
//query results
return alertService.getAlerts(specification);
}

@EventListener(SystemConfigChangeEvent.class)
public void onEvent(SystemConfigChangeEvent event) {
log.info("calculate alarm receive system config change event: {}.", event.getSource());
this.bundle = ResourceBundleUtil.getBundle("alerter");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -291,4 +291,9 @@ public interface CommonConstants {
* default main collector name
*/
String MAIN_COLLECTOR_NODE = "main-default-collector";

/**
* locale spilt
*/
String LOCALE_SEPARATOR = "_";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.dromara.hertzbeat.common.support.event;

import org.springframework.context.ApplicationEvent;

/**
* the event for system config change
* @author tom
*/
public class SystemConfigChangeEvent extends ApplicationEvent {

public SystemConfigChangeEvent(Object source) {
super(source);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@

package org.dromara.hertzbeat.manager.component.alerter.impl;

import lombok.extern.slf4j.Slf4j;
import org.dromara.hertzbeat.alert.AlerterProperties;
import org.dromara.hertzbeat.common.entity.alerter.Alert;
import org.dromara.hertzbeat.common.support.event.SystemConfigChangeEvent;
import org.dromara.hertzbeat.common.util.CommonUtil;
import org.dromara.hertzbeat.common.util.ResourceBundleUtil;
import org.dromara.hertzbeat.manager.component.alerter.AlertNotifyHandler;
import org.springframework.context.event.EventListener;
import org.springframework.web.client.RestTemplate;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
Expand All @@ -37,9 +40,10 @@
* @version 2.1
* Created by Musk.Chen on 2023/1/16
*/
abstract class AbstractAlertNotifyHandlerImpl implements AlertNotifyHandler {
@Slf4j
public abstract class AbstractAlertNotifyHandlerImpl implements AlertNotifyHandler {

protected final ResourceBundle bundle = ResourceBundleUtil.getBundle("alerter");
protected ResourceBundle bundle = ResourceBundleUtil.getBundle("alerter");
protected static final DateTimeFormatter DTF = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

@Resource
Expand Down Expand Up @@ -81,5 +85,10 @@ protected String renderContent(Alert alert) {
* @return Thymeleaf模板名称
*/
protected abstract String templateName();


@EventListener(SystemConfigChangeEvent.class)
public void onEvent(SystemConfigChangeEvent event) {
log.info("{} receive system config change event: {}.", this.getClass().getName(), event.getSource());
this.bundle = ResourceBundleUtil.getBundle("alerter");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
@Component
@RequiredArgsConstructor
@Slf4j
final class DingTalkRobotAlertNotifyHandlerImpl extends AbstractAlertNotifyHandlerImpl {
public class DingTalkRobotAlertNotifyHandlerImpl extends AbstractAlertNotifyHandlerImpl {

@Override
public void send(NoticeReceiver receiver, Alert alert) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
@Component
@RequiredArgsConstructor
@Slf4j
final class DiscordBotAlertNotifyHandlerImpl extends AbstractAlertNotifyHandlerImpl {
public class DiscordBotAlertNotifyHandlerImpl extends AbstractAlertNotifyHandlerImpl {

@Override
public void send(NoticeReceiver receiver, Alert alert) throws AlertNoticeException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
import org.dromara.hertzbeat.common.entity.alerter.Alert;
import org.dromara.hertzbeat.common.entity.manager.GeneralConfig;
import org.dromara.hertzbeat.common.entity.manager.NoticeReceiver;
import org.dromara.hertzbeat.common.support.event.SystemConfigChangeEvent;
import org.dromara.hertzbeat.common.util.ResourceBundleUtil;
import org.dromara.hertzbeat.manager.component.alerter.AlertNotifyHandler;
import org.dromara.hertzbeat.manager.dao.GeneralConfigDao;
import org.dromara.hertzbeat.manager.pojo.dto.EmailNoticeSender;
import org.dromara.hertzbeat.manager.service.MailService;
import org.dromara.hertzbeat.manager.support.exception.AlertNoticeException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.event.EventListener;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
Expand All @@ -47,7 +49,7 @@
@Component
@RequiredArgsConstructor
@Slf4j
final class EmailAlertNotifyHandlerImpl implements AlertNotifyHandler {
public class EmailAlertNotifyHandlerImpl implements AlertNotifyHandler {

private final JavaMailSender javaMailSender;

Expand All @@ -74,7 +76,7 @@ final class EmailAlertNotifyHandlerImpl implements AlertNotifyHandler {

private static final String TYPE = "email";

private final ResourceBundle bundle = ResourceBundleUtil.getBundle("alerter");
private ResourceBundle bundle = ResourceBundleUtil.getBundle("alerter");

@Override
public void send(NoticeReceiver receiver, Alert alert) throws AlertNoticeException {
Expand Down Expand Up @@ -134,4 +136,10 @@ public void send(NoticeReceiver receiver, Alert alert) throws AlertNoticeExcepti
public byte type() {
return 1;
}

@EventListener(SystemConfigChangeEvent.class)
public void onEvent(SystemConfigChangeEvent event) {
log.info("{} receive system config change event: {}.", this.getClass().getName(), event.getSource());
this.bundle = ResourceBundleUtil.getBundle("alerter");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
@Component
@RequiredArgsConstructor
@Slf4j
final class FlyBookAlertNotifyHandlerImpl extends AbstractAlertNotifyHandlerImpl {
public class FlyBookAlertNotifyHandlerImpl extends AbstractAlertNotifyHandlerImpl {

@Override
public void send(NoticeReceiver receiver, Alert alert) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@
import lombok.extern.slf4j.Slf4j;
import org.dromara.hertzbeat.common.entity.alerter.Alert;
import org.dromara.hertzbeat.common.entity.manager.NoticeReceiver;
import org.dromara.hertzbeat.common.util.ResourceBundleUtil;
import org.dromara.hertzbeat.manager.support.exception.AlertNoticeException;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.ResourceBundle;
import java.util.concurrent.ConcurrentHashMap;

/**
Expand All @@ -41,9 +39,7 @@
@Component
@RequiredArgsConstructor
@Slf4j
final class HuaweiCloudSmnAlertNotifyHandlerImpl extends AbstractAlertNotifyHandlerImpl {
private final ResourceBundle bundle = ResourceBundleUtil.getBundle("alerter");

public class HuaweiCloudSmnAlertNotifyHandlerImpl extends AbstractAlertNotifyHandlerImpl {
private final Map<String, SmnClient> smnClientMap = new ConcurrentHashMap<>();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import org.springframework.http.*;
import org.springframework.stereotype.Component;
/**
* Server酱发送
* @author zqr10159
* @description Server酱发送
*/
@Component
@RequiredArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
@Component
@RequiredArgsConstructor
@Slf4j
final class SlackAlertNotifyHandlerImpl extends AbstractAlertNotifyHandlerImpl {
public class SlackAlertNotifyHandlerImpl extends AbstractAlertNotifyHandlerImpl {
private static final String SUCCESS = "ok";
private final RestTemplate restTemplate;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
@RequiredArgsConstructor
@Slf4j
@ConditionalOnProperty("common.sms.tencent.app-id")
final class SmsAlertNotifyHandlerImpl implements AlertNotifyHandler {
public class SmsAlertNotifyHandlerImpl implements AlertNotifyHandler {

private final TencentSmsClient tencentSmsClient;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
* Send alarm information by Telegram Bot
Expand All @@ -42,8 +41,7 @@
@Component
@RequiredArgsConstructor
@Slf4j
final class TelegramBotAlertNotifyHandlerImpl extends AbstractAlertNotifyHandlerImpl {
private final RestTemplate restTemplate;
public class TelegramBotAlertNotifyHandlerImpl extends AbstractAlertNotifyHandlerImpl {

@Override
public void send(NoticeReceiver receiver, Alert alert) throws AlertNoticeException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @author <a href="mailto:Musk.Chen@fanruan.com">Musk.Chen</a>
*
*/
final class WeChatAlertNotifyHandlerImpl implements AlertNotifyHandler {
public class WeChatAlertNotifyHandlerImpl implements AlertNotifyHandler {
@Override
public void send(NoticeReceiver receiver, Alert alert) {
// todo
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.dromara.hertzbeat.manager.component.alerter.impl;

import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hertzbeat.common.entity.alerter.Alert;
import org.dromara.hertzbeat.common.entity.manager.NoticeReceiver;
import org.dromara.hertzbeat.common.util.JsonUtil;
import org.dromara.hertzbeat.manager.component.alerter.AlertNotifyHandler;
import org.dromara.hertzbeat.manager.pojo.dto.WeChatAppDTO;
import org.dromara.hertzbeat.manager.pojo.dto.WeChatAppReq;
Expand All @@ -21,7 +21,6 @@
/**
* WeChat app alert notify impl
* @author hdd
* @create 2023/04/04
*/
@Component
@RequiredArgsConstructor
Expand Down Expand Up @@ -61,7 +60,7 @@ public void send(NoticeReceiver receiver, Alert alert) throws AlertNoticeExcepti
if (Objects.nonNull(entityResponse.getBody())) {
String accessToken = entityResponse.getBody().getAccessToken();
WeChatAppDTO.TextDTO textDTO = new WeChatAppDTO.TextDTO();
textDTO.setContent(JSON.toJSONString(alert));
textDTO.setContent(JsonUtil.toJson(alert));
WeChatAppDTO weChatAppDTO = WeChatAppDTO.builder()
.toUser(DEFAULT_ALL)
.msgType(DEFAULT_TYPE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
@Component
@RequiredArgsConstructor
@Slf4j
final class WebHookAlertNotifyHandlerImpl implements AlertNotifyHandler {
public class WebHookAlertNotifyHandlerImpl implements AlertNotifyHandler {
private final RestTemplate restTemplate;

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.dromara.hertzbeat.manager.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.dromara.hertzbeat.common.constants.CommonConstants;
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.service.impl.SystemGeneralConfigServiceImpl;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

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

/**
* @author ceilzcx
* @since 4/7/2023
*/
@Component
public class SystemCommandLineRunner implements CommandLineRunner {

private static final Integer LANG_REGION_LENGTH = 2;

@Resource
private SystemGeneralConfigServiceImpl systemGeneralConfigService;

@Resource
protected GeneralConfigDao generalConfigDao;

@Resource
protected ObjectMapper objectMapper;

@Override
public void run(String... args) throws Exception {
SystemConfig systemConfig = systemGeneralConfigService.getConfig();
if (systemConfig != null) {
if (systemConfig.getTimeZoneId() != null) {
TimeZone.setDefault(TimeZone.getTimeZone(systemConfig.getTimeZoneId()));
}
if (systemConfig.getLocale() != null) {
String[] arr = systemConfig.getLocale().split(CommonConstants.LOCALE_SEPARATOR);
if (arr.length == LANG_REGION_LENGTH) {
String language = arr[0];
String country = arr[1];
Locale.setDefault(new Locale(language, country));
}
}
} else {
// init system config data
systemConfig = SystemConfig.builder().timeZoneId(TimeZone.getDefault().getID())
.locale(Locale.getDefault().getLanguage() + CommonConstants.LOCALE_SEPARATOR
+ Locale.getDefault().getCountry())
.build();
String contentJson = objectMapper.writeValueAsString(systemConfig);
GeneralConfig generalConfig2Save = GeneralConfig.builder()
.type(systemGeneralConfigService.type())
.content(contentJson)
.build();
generalConfigDao.save(generalConfig2Save);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public ResponseEntity<Message<Map<String, String>>> queryI18n(
if (lang == null || "".equals(lang)) {
lang = "zh-CN";
}
lang = "zh-cn".equalsIgnoreCase(lang) ? "zh-CN" : lang;
lang = "en-us".equalsIgnoreCase(lang) ? "en-US" : lang;
lang = "zh-cn".equalsIgnoreCase(lang) || "zh_cn".equalsIgnoreCase(lang) ? "zh-CN" : lang;
lang = "en-us".equalsIgnoreCase(lang) || "en_us".equalsIgnoreCase(lang) ? "en-US" : lang;
Map<String, String> i18nResource = appService.getI18nResources(lang);
return ResponseEntity.ok(new Message<>(i18nResource));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.dromara.hertzbeat.manager.pojo.dto;

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

import javax.validation.constraints.*;

Expand All @@ -10,6 +12,8 @@
* @author zqr
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class EmailNoticeSender {

@NotNull(message = "类型不能为空|Type cannot be empty")
Expand Down
Loading