Skip to content

Commit

Permalink
[improve]: extract common constant str (apache#2698)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuluo-yx authored Sep 10, 2024
1 parent d5fdccf commit 4b81ae4
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -544,9 +544,9 @@ public HttpUriRequest createHttpRequest(HttpProtocol httpProtocol) {
} 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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@

public interface ConfigConstants {

/**
* System Constant: hertzbeat
*/
interface SystemConstant {
String PROJECT_NAME = "hertzbeat";
}

/**
* Package name constant.
*/
Expand Down Expand Up @@ -56,6 +63,8 @@ interface FunctionModuleConstants {
String DISPATCH = "dispatch";

String INFO = "info";

String GRAFANA = "grafana";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public interface NetworkConstants {

String LOCATION = "Location";

String BASIC = "Basic";

String AUTHORIZATION = "Authorization";

/**
* HttpClient Configuration Constants.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -50,17 +50,13 @@ 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";

String CREATE_SERVICE_ACCOUNT_API = "%s:%s@%s/api/serviceaccounts";

String CREATE_SERVICE_TOKEN_API = "%s:%s@%s/api/serviceaccounts/%d/tokens";

String APPLICATION_JSON = "application/json";

String URL = "url";

String GRAFANA_CONFIG = "grafanaConfig";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,42 @@

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;

/**
* 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

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;
Expand All @@ -41,7 +40,8 @@
@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import org.springframework.web.client.RestTemplate;
import lombok.extern.slf4j.Slf4j;


/**
* Service for managing Grafana dashboards.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
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;
Expand All @@ -43,7 +45,6 @@
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;


/**
* Service for managing Grafana service accounts and tokens.
*/
Expand Down Expand Up @@ -189,13 +190,14 @@ public ResponseEntity<String> getAccounts() {
}

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
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;
Expand All @@ -58,6 +60,8 @@
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
*/
Expand All @@ -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__";

Expand Down Expand Up @@ -177,7 +179,8 @@ public void saveData(CollectRep.MetricsData metricsData) {
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(
Expand Down Expand Up @@ -219,7 +222,8 @@ public Map<String, List<Value>> getHistoryMetricData(Long monitorId, String app,
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)
Expand Down Expand Up @@ -314,7 +318,8 @@ public Map<String, List<Value>> getHistoryIntervalMetricData(Long monitorId, Str
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
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;
Expand Down Expand Up @@ -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__";

Expand All @@ -105,9 +104,9 @@ private boolean checkVictoriaMetricsDatasourceAvailable() {
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,
Expand Down Expand Up @@ -185,9 +184,10 @@ public void saveData(CollectRep.MetricsData metricsData) {
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,
Expand Down Expand Up @@ -227,7 +227,7 @@ public Map<String, List<Value>> getHistoryMetricData(Long monitorId, String app,
&& 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)
Expand Down Expand Up @@ -321,7 +321,8 @@ public Map<String, List<Value>> getHistoryIntervalMetricData(Long monitorId, Str
&& 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)
Expand Down

0 comments on commit 4b81ae4

Please sign in to comment.