[improve]: extract common constant str (#2698)

This commit is contained in:
shown 2024-09-11 07:32:06 +08:00 committed by GitHub
parent d5fdccf1d2
commit 4b81ae4abf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 69 additions and 48 deletions

View File

@ -544,9 +544,9 @@ public class HttpCollectImpl extends AbstractCollect {
} else if (DispatchConstants.BASIC_AUTH.equals(authorization.getType())) {
if (StringUtils.hasText(authorization.getBasicAuthUsername())
&& StringUtils.hasText(authorization.getBasicAuthPassword())) {
String authStr = authorization.getBasicAuthUsername() + ":" + authorization.getBasicAuthPassword();
String authStr = authorization.getBasicAuthUsername() + SignConstants.DOUBLE_MARK + authorization.getBasicAuthPassword();
String encodedAuth = new String(Base64.encodeBase64(authStr.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
requestBuilder.addHeader(HttpHeaders.AUTHORIZATION, DispatchConstants.BASIC + " " + encodedAuth);
requestBuilder.addHeader(HttpHeaders.AUTHORIZATION, DispatchConstants.BASIC + SignConstants.BLANK + encodedAuth);
}
}
}

View File

@ -23,6 +23,13 @@ package org.apache.hertzbeat.common.constants;
public interface ConfigConstants {
/**
* System Constant: hertzbeat
*/
interface SystemConstant {
String PROJECT_NAME = "hertzbeat";
}
/**
* Package name constant.
*/
@ -56,6 +63,8 @@ public interface ConfigConstants {
String DISPATCH = "dispatch";
String INFO = "info";
String GRAFANA = "grafana";
}
}

View File

@ -47,6 +47,10 @@ public interface NetworkConstants {
String LOCATION = "Location";
String BASIC = "Basic";
String AUTHORIZATION = "Authorization";
/**
* HttpClient Configuration Constants.
*/

View File

@ -17,15 +17,15 @@
package org.apache.hertzbeat.grafana.common;
import org.apache.hertzbeat.common.constants.ConfigConstants;
/**
* Grafana Common Constants
*/
public interface GrafanaConstants {
String HTTP = "http://";
String HTTPS = "https://";
String ADMIN = "admin";
String KIOSK = "?kiosk=tv";
String REFRESH = "&refresh=15s";
@ -50,7 +50,7 @@ public interface GrafanaConstants {
String GET_SERVICE_ACCOUNTS_API = "%s:%s@%s/api/serviceaccounts/search";
String ACCOUNT_NAME = "hertzbeat";
String ACCOUNT_NAME = ConfigConstants.SystemConstant.PROJECT_NAME;
String ACCOUNT_ROLE = "Admin";
@ -58,9 +58,5 @@ public interface GrafanaConstants {
String CREATE_SERVICE_TOKEN_API = "%s:%s@%s/api/serviceaccounts/%d/tokens";
String APPLICATION_JSON = "application/json";
String URL = "url";
String GRAFANA_CONFIG = "grafanaConfig";
}

View File

@ -17,13 +17,18 @@
package org.apache.hertzbeat.grafana.config;
import org.apache.hertzbeat.common.constants.ConfigConstants;
import org.apache.hertzbeat.common.constants.SignConstants;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
/**
* Grafana auto configuration.
*/
@ComponentScan(basePackages = "org.apache.hertzbeat.grafana")
@ComponentScan(basePackages = ConfigConstants.PkgConstant.PKG
+ SignConstants.DOT
+ ConfigConstants.FunctionModuleConstants.GRAFANA
)
@EnableConfigurationProperties(GrafanaProperties.class)
public class GrafanaAutoConfiguration {
}

View File

@ -17,9 +17,10 @@
package org.apache.hertzbeat.grafana.config;
import static org.apache.hertzbeat.grafana.common.GrafanaConstants.HTTP;
import static org.apache.hertzbeat.grafana.common.GrafanaConstants.HTTPS;
import lombok.extern.slf4j.Slf4j;
import org.apache.hertzbeat.common.constants.ConfigConstants;
import org.apache.hertzbeat.common.constants.NetworkConstants;
import org.apache.hertzbeat.grafana.common.GrafanaConstants;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.bind.DefaultValue;
@ -27,31 +28,31 @@ import org.springframework.boot.context.properties.bind.DefaultValue;
* grafana configuration
*/
@Slf4j
@ConfigurationProperties(prefix = "grafana")
@ConfigurationProperties(prefix = ConfigConstants.FunctionModuleConstants.GRAFANA)
public record GrafanaProperties(@DefaultValue("false") boolean enabled,
@DefaultValue("http://127.0.0.1:3000") String url,
@DefaultValue("admin") String username,
@DefaultValue("admin") String password) {
@DefaultValue(GrafanaConstants.ADMIN) String username,
@DefaultValue(GrafanaConstants.ADMIN) String password) {
/**
* get the prefix of the grafana url, such as http or https
*/
public String getPrefix() {
if (url.startsWith(HTTP)) {
return HTTP;
} else if (url.startsWith(HTTPS)) {
return HTTPS;
if (url.startsWith(NetworkConstants.HTTP_HEADER)) {
return NetworkConstants.HTTP_HEADER;
} else if (url.startsWith(NetworkConstants.HTTPS_HEADER)) {
return NetworkConstants.HTTPS_HEADER;
}
return HTTP;
return NetworkConstants.HTTP_HEADER;
}
/**
* get the grafana url without the prefix, such as localhost:3000
*/
public String getUrl() {
if (getPrefix().equals(HTTP)) {
return url.replace(HTTP, "");
} else if (getPrefix().equals(HTTPS)) {
return url.replace(HTTPS, "");
if (getPrefix().equals(NetworkConstants.HTTP_HEADER)) {
return url.replace(NetworkConstants.HTTP_HEADER, "");
} else if (getPrefix().equals(NetworkConstants.HTTPS_HEADER)) {
return url.replace(NetworkConstants.HTTPS_HEADER, "");
}
return url;
}

View File

@ -19,7 +19,6 @@ package org.apache.hertzbeat.grafana.controller;
import static org.apache.hertzbeat.common.constants.CommonConstants.FAIL_CODE;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
@ -41,7 +40,8 @@ import org.springframework.web.bind.annotation.RestController;
@Slf4j
@Tag(name = "Dashboard API")
@RestController
@RequestMapping(path = "/api/grafana/dashboard", produces = {APPLICATION_JSON_VALUE})
@RequestMapping(path = "/api/grafana/dashboard",
produces = {APPLICATION_JSON_VALUE})
public class DashboardController {
@Autowired

View File

@ -41,7 +41,6 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
import lombok.extern.slf4j.Slf4j;
/**
* Service for managing Grafana dashboards.
*/

View File

@ -17,7 +17,6 @@
package org.apache.hertzbeat.grafana.service;
import static org.apache.hertzbeat.grafana.common.GrafanaConstants.CREATE_DATASOURCE_API;
import static org.apache.hertzbeat.grafana.common.GrafanaConstants.DATASOURCE_ACCESS;
import static org.apache.hertzbeat.grafana.common.GrafanaConstants.DATASOURCE_NAME;

View File

@ -28,6 +28,8 @@ import jakarta.annotation.PostConstruct;
import java.util.Base64;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.apache.hertzbeat.common.constants.NetworkConstants;
import org.apache.hertzbeat.common.constants.SignConstants;
import org.apache.hertzbeat.common.entity.manager.GeneralConfig;
import org.apache.hertzbeat.common.util.CommonUtil;
import org.apache.hertzbeat.common.util.JsonUtil;
@ -43,7 +45,6 @@ import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
/**
* Service for managing Grafana service accounts and tokens.
*/
@ -189,13 +190,14 @@ public class ServiceAccountService {
}
private HttpHeaders createHeaders() {
String auth = username + ":" + password;
String auth = username + SignConstants.DOUBLE_MARK + password;
byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes());
String authHeader = "Basic " + new String(encodedAuth);
String authHeader = NetworkConstants.BASIC
+ SignConstants.BLANK + new String(encodedAuth);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", authHeader);
headers.set(NetworkConstants.AUTHORIZATION, authHeader);
return headers;
}
}

View File

@ -40,6 +40,8 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.hertzbeat.common.constants.CommonConstants;
import org.apache.hertzbeat.common.constants.NetworkConstants;
import org.apache.hertzbeat.common.constants.SignConstants;
import org.apache.hertzbeat.common.entity.dto.Value;
import org.apache.hertzbeat.common.entity.message.CollectRep;
import org.apache.hertzbeat.common.util.CommonUtil;
@ -58,6 +60,8 @@ import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import static org.apache.hertzbeat.common.constants.ConfigConstants.FunctionModuleConstants.STATUS;
/**
* tdengine data storage
*/
@ -70,14 +74,12 @@ public class VictoriaMetricsClusterDataStorage extends AbstractHistoryDataStorag
private static final String IMPORT_PATH = "/api/v1/import";
private static final String EXPORT_PATH = "/api/v1/export";
private static final String STATUS_PATH = "/api/v1/status/tsdb";
private static final String STATUS = "status";
private static final String STATUS_SUCCESS = "success";
private static final String QUERY_RANGE_PATH = "/api/v1/query_range";
private static final String LABEL_KEY_NAME = "__name__";
private static final String LABEL_KEY_JOB = "job";
private static final String LABEL_KEY_INSTANCE = "instance";
private static final String SPILT = "_";
private static final String BASIC = "Basic";
private static final String MONITOR_METRICS_KEY = "__metrics__";
private static final String MONITOR_METRIC_KEY = "__metric__";
@ -177,7 +179,8 @@ public class VictoriaMetricsClusterDataStorage extends AbstractHistoryDataStorag
String encodedAuth = new String(
Base64.encodeBase64(authStr.getBytes(StandardCharsets.UTF_8)),
StandardCharsets.UTF_8);
headers.add(HttpHeaders.AUTHORIZATION, BASIC + " " + encodedAuth);
headers.add(HttpHeaders.AUTHORIZATION, NetworkConstants.BASIC
+ SignConstants.BLANK + encodedAuth);
}
HttpEntity<VictoriaMetricsContent> httpEntity = new HttpEntity<>(content, headers);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(
@ -219,7 +222,8 @@ public class VictoriaMetricsClusterDataStorage extends AbstractHistoryDataStorag
String authStr = vmSelectProps.username() + ":" + vmSelectProps.password();
String encodedAuth = new String(Base64.encodeBase64(authStr.getBytes(StandardCharsets.UTF_8)),
StandardCharsets.UTF_8);
headers.add(HttpHeaders.AUTHORIZATION, BASIC + " " + encodedAuth);
headers.add(HttpHeaders.AUTHORIZATION, NetworkConstants.BASIC
+ SignConstants.BLANK + encodedAuth);
}
HttpEntity<Void> httpEntity = new HttpEntity<>(headers);
URI uri = UriComponentsBuilder.fromHttpUrl(vmSelectProps.url() + EXPORT_PATH)
@ -314,7 +318,8 @@ public class VictoriaMetricsClusterDataStorage extends AbstractHistoryDataStorag
String authStr = vmSelectProps.username() + ":" + vmSelectProps.password();
String encodedAuth = new String(Base64.encodeBase64(authStr.getBytes(StandardCharsets.UTF_8)),
StandardCharsets.UTF_8);
headers.add(HttpHeaders.AUTHORIZATION, BASIC + " " + encodedAuth);
headers.add(HttpHeaders.AUTHORIZATION, NetworkConstants.BASIC
+ SignConstants.BLANK + encodedAuth);
}
HttpEntity<Void> httpEntity = new HttpEntity<>(headers);
URI uri = UriComponentsBuilder.fromHttpUrl(vmSelectProps.url() + QUERY_RANGE_PATH)

View File

@ -39,6 +39,8 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.hertzbeat.common.constants.CommonConstants;
import org.apache.hertzbeat.common.constants.NetworkConstants;
import org.apache.hertzbeat.common.constants.SignConstants;
import org.apache.hertzbeat.common.entity.dto.Value;
import org.apache.hertzbeat.common.entity.message.CollectRep;
import org.apache.hertzbeat.common.util.CommonUtil;
@ -75,13 +77,10 @@ public class VictoriaMetricsDataStorage extends AbstractHistoryDataStorage {
private static final String EXPORT_PATH = "/api/v1/export";
private static final String QUERY_RANGE_PATH = "/api/v1/query_range";
private static final String STATUS_PATH = "/api/v1/status/tsdb";
private static final String STATUS = "status";
private static final String STATUS_SUCCESS = "success";
private static final String LABEL_KEY_NAME = "__name__";
private static final String LABEL_KEY_JOB = "job";
private static final String LABEL_KEY_INSTANCE = "instance";
private static final String SPILT = "_";
private static final String BASIC = "Basic";
private static final String MONITOR_METRICS_KEY = "__metrics__";
private static final String MONITOR_METRIC_KEY = "__metric__";
@ -105,9 +104,9 @@ public class VictoriaMetricsDataStorage extends AbstractHistoryDataStorage {
HttpHeaders headers = new HttpHeaders();
if (StringUtils.hasText(victoriaMetricsProp.username())
&& StringUtils.hasText(victoriaMetricsProp.password())) {
String authStr = victoriaMetricsProp.username() + ":" + victoriaMetricsProp.password();
String authStr = victoriaMetricsProp.username() + SignConstants.DOUBLE_MARK + victoriaMetricsProp.password();
String encodedAuth = new String(Base64.encodeBase64(authStr.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
headers.add(HttpHeaders.AUTHORIZATION, BASIC + " " + encodedAuth);
headers.add(HttpHeaders.AUTHORIZATION, NetworkConstants.BASIC + " " + encodedAuth);
}
HttpEntity<Void> httpEntity = new HttpEntity<>(headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(victoriaMetricsProp.url() + STATUS_PATH,
@ -185,9 +184,10 @@ public class VictoriaMetricsDataStorage extends AbstractHistoryDataStorage {
headers.setContentType(MediaType.APPLICATION_JSON);
if (StringUtils.hasText(victoriaMetricsProp.username())
&& StringUtils.hasText(victoriaMetricsProp.password())) {
String authStr = victoriaMetricsProp.username() + ":" + victoriaMetricsProp.password();
String authStr = victoriaMetricsProp.username() + SignConstants.DOUBLE_MARK + victoriaMetricsProp.password();
String encodedAuth = new String(Base64.encodeBase64(authStr.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
headers.add(HttpHeaders.AUTHORIZATION, BASIC + " " + encodedAuth);
headers.add(HttpHeaders.AUTHORIZATION, NetworkConstants.BASIC
+ SignConstants.BLANK + encodedAuth);
}
HttpEntity<VictoriaMetricsContent> httpEntity = new HttpEntity<>(content, headers);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(victoriaMetricsProp.url() + IMPORT_PATH,
@ -227,7 +227,7 @@ public class VictoriaMetricsDataStorage extends AbstractHistoryDataStorage {
&& StringUtils.hasText(victoriaMetricsProp.password())) {
String authStr = victoriaMetricsProp.username() + ":" + victoriaMetricsProp.password();
String encodedAuth = new String(Base64.encodeBase64(authStr.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
headers.add(HttpHeaders.AUTHORIZATION, BASIC + " " + encodedAuth);
headers.add(HttpHeaders.AUTHORIZATION, NetworkConstants.BASIC + SignConstants.BLANK + encodedAuth);
}
HttpEntity<Void> httpEntity = new HttpEntity<>(headers);
URI uri = UriComponentsBuilder.fromHttpUrl(victoriaMetricsProp.url() + EXPORT_PATH)
@ -321,7 +321,8 @@ public class VictoriaMetricsDataStorage extends AbstractHistoryDataStorage {
&& StringUtils.hasText(victoriaMetricsProp.password())) {
String authStr = victoriaMetricsProp.username() + ":" + victoriaMetricsProp.password();
String encodedAuth = new String(Base64.encodeBase64(authStr.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
headers.add(HttpHeaders.AUTHORIZATION, BASIC + " " + encodedAuth);
headers.add(HttpHeaders.AUTHORIZATION, NetworkConstants.BASIC
+ SignConstants.BLANK + encodedAuth);
}
HttpEntity<Void> httpEntity = new HttpEntity<>(headers);
URI uri = UriComponentsBuilder.fromHttpUrl(victoriaMetricsProp.url() + QUERY_RANGE_PATH)