[improve] add some code style improve (#2481)

Co-authored-by: Calvin <naruse_shinji@163.com>
Co-authored-by: tomsun28 <tomsun28@outlook.com>
This commit is contained in:
aias00
2024-08-07 09:09:16 +08:00
committed by GitHub
co-authored by Calvin tomsun28
parent 6ac092dcd5
commit c4fa738d65
40 changed files with 600 additions and 573 deletions
@@ -60,7 +60,7 @@ public class AlarmConvergeReduce {
// restored alert
boolean isHasIgnore = false;
Map<String, String> tags = currentAlert.getTags();
if (tags.containsKey(CommonConstants.IGNORE)) {
if (Objects.requireNonNull(tags).containsKey(CommonConstants.IGNORE)) {
isHasIgnore = true;
tags.remove(CommonConstants.IGNORE);
}
@@ -58,18 +58,20 @@ class AlertDefineYamlImExportServiceTest {
private AlertDefineYamlImExportServiceImpl service;
private static final String YAML_DATA =
"- alertDefine:\n" +
" app: App1\n" +
" metric: Metric1\n" +
" field: Field1\n" +
" preset: true\n" +
" expr: Expr1\n" +
" priority: 1\n" +
" times: 1\n" +
" tags: []\n" +
" enable: true\n" +
" recoverNotice: true\n" +
" template: Template1\n";
"""
- alertDefine:
app: App1
metric: Metric1
field: Field1
preset: true
expr: Expr1
priority: 1
times: 1
tags: []
enable: true
recoverNotice: true
template: Template1
""";
private InputStream inputStream;
private List<ExportAlertDefineDTO> alertDefineList;
@@ -99,7 +101,7 @@ class AlertDefineYamlImExportServiceTest {
}
@Test
void testParseImport() throws IllegalAccessException {
void testParseImport() {
List<ExportAlertDefineDTO> result = service.parseImport(inputStream);
@@ -108,7 +108,7 @@ public class JmxCollectImpl extends AbstractCollect {
attributes = Arrays.stream(attrInfos)
.filter(item -> item.isReadable() && attributeNameSet.contains(item.getName()))
.map(MBeanFeatureInfo::getName)
.collect(Collectors.toList()).toArray(attributes);
.toList().toArray(attributes);
AttributeList attributeList = serverConnection.getAttributes(currentObjectName, attributes);
Map<String, String> attributeValueMap = extractAttributeValue(attributeList);
@@ -105,7 +105,7 @@ public class PrometheusAutoCollectImpl {
String resp = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
long collectTime = System.currentTimeMillis();
builder.setTime(collectTime);
if (resp == null || "".equals(resp)) {
if (resp == null || !StringUtils.hasText(resp)) {
log.error("http response content is empty, status: {}.", statusCode);
builder.setCode(CollectRep.Code.FAIL);
builder.setMsg("http response content is empty");
@@ -161,7 +161,7 @@ public class PrometheusAutoCollectImpl {
}
PrometheusProtocol protocol = metrics.getPrometheus();
if (protocol.getPath() == null
|| "".equals(protocol.getPath())
|| !StringUtils.hasText(protocol.getPath())
|| !protocol.getPath().startsWith(RIGHT_DASH)) {
protocol.setPath(protocol.getPath() == null ? RIGHT_DASH : RIGHT_DASH + protocol.getPath().trim());
}
@@ -158,8 +158,7 @@ public class RedfishCollectImpl extends AbstractCollect {
}
String resourceIdPath = "$.Members[*].['@odata.id']";
List<Object> resourceIds = JsonPathParser.parseContentWithJsonPath(resp, resourceIdPath);
List<String> res = resourceIds.stream().filter(Objects::nonNull).map(String::valueOf).toList();
return res;
return resourceIds.stream().filter(Objects::nonNull).map(String::valueOf).toList();
}
private List<String> getCollectionResource(String uri, ConnectSession connectSession) {
@@ -88,8 +88,7 @@ public class RedfishConnectSession implements ConnectSession {
if (statusCode != HttpStatus.SC_OK) {
throw new Exception("Http State code: " + statusCode);
}
String resp = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
return resp;
return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
} catch (Exception e) {
throw new Exception("Redfish session get resource error:" + e.getMessage());
} finally {
@@ -27,6 +27,7 @@ import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.telnet.TelnetClient;
import org.apache.hertzbeat.collector.collect.AbstractCollect;
import org.apache.hertzbeat.collector.dispatch.DispatchConstants;
@@ -116,7 +117,7 @@ public class TelnetCollectImpl extends AbstractCollect {
}
private static Map<String, String> execCmdAndParseResult(TelnetClient telnetClient, String cmd, String app) throws IOException {
if (cmd == null || cmd.trim().length() == 0) {
if (cmd == null || StringUtils.isEmpty(cmd.trim())) {
return new HashMap<>(16);
}
OutputStream outputStream = telnetClient.getOutputStream();
@@ -644,7 +644,7 @@ public class HashedWheelTimer implements Timer {
task.run(this);
} catch (Throwable t) {
if (logger.isWarnEnabled()) {
logger.warn("An exception was thrown by " + TimerTask.class.getSimpleName() + '.', t);
logger.warn("An exception was thrown by {}.", TimerTask.class.getSimpleName(), t);
}
}
}
@@ -71,7 +71,7 @@ public enum DataUnit {
private final String unit;
private final long scale;
private DataUnit(String unit, long scale) {
DataUnit(String unit, long scale) {
this.unit = unit;
this.scale = scale;
}
@@ -53,7 +53,7 @@ public enum TimeLengthUnit {
private final String unit;
private final long scale;
private TimeLengthUnit(String unit, long scale) {
TimeLengthUnit(String unit, long scale) {
this.unit = unit;
this.scale = scale;
}
@@ -28,45 +28,46 @@ class ExporterParserTest {
@Test
void textToMetric() {
String resp = "# HELP disk_total_bytes Total space for path\n"
+ "# TYPE disk_total_bytes gauge\n"
+ "disk_total_bytes{path=\"C:\\\\hertzbeat\\\\repo\\\\testpath\",} 4.29496725504E11\n"
+ "# HELP go_gc_cycles_automatic_gc_cycles_total Count of completed GC cycles generated by the Go runtime.\n"
+ "# TYPE go_gc_cycles_automatic_gc_cycles_total counter\n"
+ "go_gc_cycles_automatic_gc_cycles_total 0\n"
+ "# HELP go_gc_cycles_forced_gc_cycles_total Count of completed GC cycles forced by the application.\n"
+ "# TYPE go_gc_cycles_forced_gc_cycles_total counter\n"
+ "go_gc_cycles_forced_gc_cycles_total 0\n"
+ "# HELP go_gc_cycles_total_gc_cycles_total Count of all completed GC cycles.\n"
+ "# TYPE go_gc_cycles_total_gc_cycles_total counter\n"
+ "go_gc_cycles_total_gc_cycles_total 0\n"
+ "# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.\n"
+ "# TYPE go_gc_duration_seconds summary\n"
+ "go_gc_duration_seconds{quantile=\"0\"} 0\n"
+ "go_gc_duration_seconds{quantile=\"0.25\"} 0\n"
+ "go_gc_duration_seconds{quantile=\"0.5\"} 0\n"
+ "go_gc_duration_seconds{quantile=\"0.75\"} 0\n"
+ "go_gc_duration_seconds{quantile=\"1\"} 0\n"
+ "# TYPE jvm info\n"
+ "# HELP jvm VM version info\n"
+ "jvm_info{runtime=\"OpenJDK Runtime Environment\",vendor=\"Azul Systems, Inc.\",version=\"11.0.13+8-LTS\"} 1.0\n"
+ "# TYPE jvm_gc_collection_seconds summary\n"
+ "# HELP jvm_gc_collection_seconds Time spent in a given JVM garbage collector in seconds.\n"
+ "jvm_gc_collection_seconds_count{gc=\"G1 Young Generation\"} 10.0\n"
+ "jvm_gc_collection_seconds_sum{gc=\"G1 Young Generation\"} 0.051\n"
+ "jvm_gc_collection_seconds_count{gc=\"G1 Old Generation\"} 0.0\n"
+ "jvm_gc_collection_seconds_sum{gc=\"G1 Old Generation\"} 0.0\n"
+ "# TYPE resource_group_aggregate_usage_secs summary\n"
+ "resource_group_aggregate_usage_secs{cluster=\"standalone\",quantile=\"0.5\"} 2.69245E-4\n"
+ "resource_group_aggregate_usage_secs{cluster=\"standalone\",quantile=\"0.9\"} 3.49601E-4\n"
+ "resource_group_aggregate_usage_secs_count{cluster=\"standalone\"} 13.0\n"
+ "resource_group_aggregate_usage_secs_sum{cluster=\"standalone\"} 0.004832498\n"
+ "resource_group_aggregate_usage_secs_created{cluster=\"standalone\"} 1.715842140749E9\n"
+ "# TYPE metadata_store_ops_latency_ms histogram\n"
+ "metadata_store_ops_latency_ms_bucket{cluster=\"standalone\",name=\"metadata-store\",type=\"get\",status=\"success\",le=\"1.0\"} 59.0\n"
+ "metadata_store_ops_latency_ms_bucket{cluster=\"standalone\",name=\"metadata-store\",type=\"get\",status=\"success\",le=\"3.0\"} 61.0\n"
+ "metadata_store_ops_latency_ms_bucket{cluster=\"standalone\",name=\"metadata-store\",type=\"get\",status=\"success\",le=\"5.0\"} 61.0\n"
+ "# EOF";
String resp = """
# HELP disk_total_bytes Total space for path
# TYPE disk_total_bytes gauge
disk_total_bytes{path="C:\\\\hertzbeat\\\\repo\\\\testpath",} 4.29496725504E11
# HELP go_gc_cycles_automatic_gc_cycles_total Count of completed GC cycles generated by the Go runtime.
# TYPE go_gc_cycles_automatic_gc_cycles_total counter
go_gc_cycles_automatic_gc_cycles_total 0
# HELP go_gc_cycles_forced_gc_cycles_total Count of completed GC cycles forced by the application.
# TYPE go_gc_cycles_forced_gc_cycles_total counter
go_gc_cycles_forced_gc_cycles_total 0
# HELP go_gc_cycles_total_gc_cycles_total Count of all completed GC cycles.
# TYPE go_gc_cycles_total_gc_cycles_total counter
go_gc_cycles_total_gc_cycles_total 0
# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 0
go_gc_duration_seconds{quantile="0.25"} 0
go_gc_duration_seconds{quantile="0.5"} 0
go_gc_duration_seconds{quantile="0.75"} 0
go_gc_duration_seconds{quantile="1"} 0
# TYPE jvm info
# HELP jvm VM version info
jvm_info{runtime="OpenJDK Runtime Environment",vendor="Azul Systems, Inc.",version="11.0.13+8-LTS"} 1.0
# TYPE jvm_gc_collection_seconds summary
# HELP jvm_gc_collection_seconds Time spent in a given JVM garbage collector in seconds.
jvm_gc_collection_seconds_count{gc="G1 Young Generation"} 10.0
jvm_gc_collection_seconds_sum{gc="G1 Young Generation"} 0.051
jvm_gc_collection_seconds_count{gc="G1 Old Generation"} 0.0
jvm_gc_collection_seconds_sum{gc="G1 Old Generation"} 0.0
# TYPE resource_group_aggregate_usage_secs summary
resource_group_aggregate_usage_secs{cluster="standalone",quantile="0.5"} 2.69245E-4
resource_group_aggregate_usage_secs{cluster="standalone",quantile="0.9"} 3.49601E-4
resource_group_aggregate_usage_secs_count{cluster="standalone"} 13.0
resource_group_aggregate_usage_secs_sum{cluster="standalone"} 0.004832498
resource_group_aggregate_usage_secs_created{cluster="standalone"} 1.715842140749E9
# TYPE metadata_store_ops_latency_ms histogram
metadata_store_ops_latency_ms_bucket{cluster="standalone",name="metadata-store",type="get",status="success",le="1.0"} 59.0
metadata_store_ops_latency_ms_bucket{cluster="standalone",name="metadata-store",type="get",status="success",le="3.0"} 61.0
metadata_store_ops_latency_ms_bucket{cluster="standalone",name="metadata-store",type="get",status="success",le="5.0"} 61.0
# EOF""";
ExporterParser parser = new ExporterParser();
Map<String, MetricFamily> metricFamilyMap = parser.textToMetric(resp);
@@ -346,13 +346,14 @@ public class NginxCollectImplTest {
@Test
public void testReqStatusMatch() {
String urlContent = "zone_name\tkey\tmax_active\tmax_bw\ttraffic\trequests\tactive\tbandwidth\n" +
"server_addr\t172.17.0.3\t2\t 440\t68K\t23\t1\t 0\n" +
"server_name\tlocalhost\t2\t 440\t68K\t23\t1\t 0\n" +
"server_url\tlocalhost/\t1\t 0\t 0\t4\t0\t 0\n" +
"server_url\tlocalhost/index.html\t1\t 104\t27K\t4\t0\t 0\n" +
"server_url\tlocalhost/nginx-status\t1\t 32\t 9896\t5\t0\t 0\n" +
"server_url\tlocalhost/req-status\t1\t 0\t31K\t10\t1\t 0";
String urlContent = """
zone_name\tkey\tmax_active\tmax_bw\ttraffic\trequests\tactive\tbandwidth
server_addr\t172.17.0.3\t2\t 440\t68K\t23\t1\t 0
server_name\tlocalhost\t2\t 440\t68K\t23\t1\t 0
server_url\tlocalhost/\t1\t 0\t 0\t4\t0\t 0
server_url\tlocalhost/index.html\t1\t 104\t27K\t4\t0\t 0
server_url\tlocalhost/nginx-status\t1\t 32\t 9896\t5\t0\t 0
server_url\tlocalhost/req-status\t1\t 0\t31K\t10\t1\t 0""";
String[] lines = urlContent.split("\\r?\\n");
List<String> zoneNames = new ArrayList<>();
@@ -27,33 +27,34 @@ class PrivateKeyUtilsTest {
@DisplayName("write key to ~/.ssh")
@Test
void writePrivateKey() throws IOException {
var key = "-----BEGIN RSA PRIVATE KEY-----\n"
+ "MIIEogIBAAKCAQEA4ctFYk/xy89L6/6YFeeMrwCW9lCP/ThXMn+9G63s5bGn4oIN\n"
+ "8cEf/JYkmGw8vMP41IAP9dyH8ji2wIZSLeTPWucEK6P6jA01iIBQ95ng6RTsnQgL\n"
+ "h4pYHxlEaNHcXkjy5GlMdzaWadjdRevpThGR1VOtWFtK3yoC0c/te2Junu04f+11\n"
+ "cpk8QvmVfzrBUooVnG0/7oekwUy1c5sSl0qVoLzXOv4XG9w34cyvacFC30zv1Nl8\n"
+ "ASi2pmOBVx9njPvqQ7qZrDk0nwn+RZUmGh/PbmHxrBV7ZA5NjZcEnf2VGIfjGUVu\n"
+ "qE4VnkbvS4j03afV2rsp1yo74K+k/ZC6GCHB5QIBIwKCAQBG9r4I9I3SVxfcdJYy\n"
+ "xR2WFiDREgFeNkdKYqkl9NVsws5dIY9am8g5cQQv54DNnK1KGZ6dulaclXtD0nGZ\n"
+ "ZSs505OYr+EHcd2f7dBN0Uavp32QcD4jSLycD0FixZ0HsIbaEnceJxlUd1t8YBYf\n"
+ "2aLcpUUbxOulORbUOgjPAa286uDeQYN5IbdruDfvbuFFm7hBoGZoKLJ7FPcJ0U3A\n"
+ "14KRK+Z1oCYJIS0ubaHbhaPIVPPQEmTNHpsvxIJXfZtVy9+XIuBGmD3+Aq6SSFPC\n"
+ "A8mU1iKzzdRCXZwvPeUiivIIZc6DRXjhtJ2Lya/XndKidOT/QUj8Z+f9pWAonlzM\n"
+ "3PMXAoGBAPvzctkkDjUJjLyEuYQq8soYokS4n4ykFTP5oFgnodK/cYocbxTT6Tn9\n"
+ "vH7b6lK6ZAf+tZk8rcEeIO650pOvmaa1/OuZSxfcFUGBvOvYXiHF7zmkePh/pQgB\n"
+ "7Cl0RYrI52Cjbd9aCUIYK3A82qsUq30INGeOhMNrfaHn2pgx8xlDAoGBAOVsNctw\n"
+ "CHnLaIQX8eS+eUcQEm+NZppnDBJavdpP48ZZM/t5v/2fQ5ytbYqk0KEzIGu0dP8g\n"
+ "jfB76JbMvStvTfB+TrXsfhGyA3oJrEcG+3IUshsRU2sohT1ScY27z2VMLgilnWvF\n"
+ "7t49sQm9uB/yn669n8LIciHxDItOpvqgKdG3AoGBAO2NxA6PtZ+4jAIz/19bsbc7\n"
+ "zDIqaovrKe8tMMglXg/ZE0e0aLvdvqRkRAKU1Z51Ob5lLuDwEYoyWZCgk1gL90Vp\n"
+ "wpT+P3zlcyCBo39IWMDB8C8IydRbF/GbaaNtoKds92m+qWwwUd87XCf+3M0wvvI6\n"
+ "75TW1PLEbyOgFz8Khh8hAoGBAJbDc87Ul9sCAtp2Ip2hvWk2coPR8vfADz9C8cn5\n"
+ "/BShBOcVfipSt2b1n8GCP/TnFU4XgBVeiSkA9+4Rg6AzMzejdY1+JvWvfqCnRVM/\n"
+ "GkOnMzZb17tyZi+ck8OKC/IcHkAyUYFWL0GWQSOojvBsPQxt+0V8aEIwsHjNSSha\n"
+ "nyNpAoGAd0XqdByRxbWgg5ZsvM0tvrpMITpEZsGMG9VeQPGl0wsQvC2zw5QGLvz/\n"
+ "57YhofOOr0M3yElcFA9Imvek5CYZsyL8eIWGZyadfRiYvGOUyvDDO3BYRG4DmhyF\n"
+ "KVk3URjEuOCC29ORvZ/7HaCO9iuEbvAA/mrAtd7KdCA+3PzfEOw=\n"
+ "-----END RSA PRIVATE KEY-----";
var key = """
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEA4ctFYk/xy89L6/6YFeeMrwCW9lCP/ThXMn+9G63s5bGn4oIN
8cEf/JYkmGw8vMP41IAP9dyH8ji2wIZSLeTPWucEK6P6jA01iIBQ95ng6RTsnQgL
h4pYHxlEaNHcXkjy5GlMdzaWadjdRevpThGR1VOtWFtK3yoC0c/te2Junu04f+11
cpk8QvmVfzrBUooVnG0/7oekwUy1c5sSl0qVoLzXOv4XG9w34cyvacFC30zv1Nl8
ASi2pmOBVx9njPvqQ7qZrDk0nwn+RZUmGh/PbmHxrBV7ZA5NjZcEnf2VGIfjGUVu
qE4VnkbvS4j03afV2rsp1yo74K+k/ZC6GCHB5QIBIwKCAQBG9r4I9I3SVxfcdJYy
xR2WFiDREgFeNkdKYqkl9NVsws5dIY9am8g5cQQv54DNnK1KGZ6dulaclXtD0nGZ
ZSs505OYr+EHcd2f7dBN0Uavp32QcD4jSLycD0FixZ0HsIbaEnceJxlUd1t8YBYf
2aLcpUUbxOulORbUOgjPAa286uDeQYN5IbdruDfvbuFFm7hBoGZoKLJ7FPcJ0U3A
14KRK+Z1oCYJIS0ubaHbhaPIVPPQEmTNHpsvxIJXfZtVy9+XIuBGmD3+Aq6SSFPC
A8mU1iKzzdRCXZwvPeUiivIIZc6DRXjhtJ2Lya/XndKidOT/QUj8Z+f9pWAonlzM
3PMXAoGBAPvzctkkDjUJjLyEuYQq8soYokS4n4ykFTP5oFgnodK/cYocbxTT6Tn9
vH7b6lK6ZAf+tZk8rcEeIO650pOvmaa1/OuZSxfcFUGBvOvYXiHF7zmkePh/pQgB
7Cl0RYrI52Cjbd9aCUIYK3A82qsUq30INGeOhMNrfaHn2pgx8xlDAoGBAOVsNctw
CHnLaIQX8eS+eUcQEm+NZppnDBJavdpP48ZZM/t5v/2fQ5ytbYqk0KEzIGu0dP8g
jfB76JbMvStvTfB+TrXsfhGyA3oJrEcG+3IUshsRU2sohT1ScY27z2VMLgilnWvF
7t49sQm9uB/yn669n8LIciHxDItOpvqgKdG3AoGBAO2NxA6PtZ+4jAIz/19bsbc7
zDIqaovrKe8tMMglXg/ZE0e0aLvdvqRkRAKU1Z51Ob5lLuDwEYoyWZCgk1gL90Vp
wpT+P3zlcyCBo39IWMDB8C8IydRbF/GbaaNtoKds92m+qWwwUd87XCf+3M0wvvI6
75TW1PLEbyOgFz8Khh8hAoGBAJbDc87Ul9sCAtp2Ip2hvWk2coPR8vfADz9C8cn5
/BShBOcVfipSt2b1n8GCP/TnFU4XgBVeiSkA9+4Rg6AzMzejdY1+JvWvfqCnRVM/
GkOnMzZb17tyZi+ck8OKC/IcHkAyUYFWL0GWQSOojvBsPQxt+0V8aEIwsHjNSSha
nyNpAoGAd0XqdByRxbWgg5ZsvM0tvrpMITpEZsGMG9VeQPGl0wsQvC2zw5QGLvz/
57YhofOOr0M3yElcFA9Imvek5CYZsyL8eIWGZyadfRiYvGOUyvDDO3BYRG4DmhyF
KVk3URjEuOCC29ORvZ/7HaCO9iuEbvAA/mrAtd7KdCA+3PzfEOw=
-----END RSA PRIVATE KEY-----""";
PrivateKeyUtils.writePrivateKey("127.0.0.1", key);
}
}
@@ -89,14 +89,15 @@ public class NoticeTemplate {
@Schema(title = "Template content",
description = "Template content",
example = "[${title}]\n"
+ "${targetLabel} : ${target}\n"
+ "<#if (monitorId??)>${monitorIdLabel} : ${monitorId} </#if>\n"
+ "<#if (monitorName??)>${monitorNameLabel} : ${monitorName} </#if>\n"
+ "<#if (monitorHost??)>${monitorHostLabel} : ${monitorHost} </#if>\n"
+ "${priorityLabel} : ${priority}\n"
+ "${triggerTimeLabel} : ${triggerTime}\n"
+ "${contentLabel} : ${content}", accessMode = READ_WRITE)
example = """
[${title}]
${targetLabel} : ${target}
<#if (monitorId??)>${monitorIdLabel} : ${monitorId} </#if>
<#if (monitorName??)>${monitorNameLabel} : ${monitorName} </#if>
<#if (monitorHost??)>${monitorHostLabel} : ${monitorHost} </#if>
${priorityLabel} : ${priority}
${triggerTimeLabel} : ${triggerTime}
${contentLabel} : ${content}""", accessMode = READ_WRITE)
@Size(max = 60000)
@Lob
@NotBlank
@@ -106,15 +106,7 @@ public final class AesUtil {
*/
public static String aesDecode(String content, String decryptKey) {
try {
SecretKeySpec keySpec = new SecretKeySpec(decryptKey.getBytes(StandardCharsets.UTF_8), AES);
// cipher based on the algorithm AES
Cipher cipher = Cipher.getInstance(ALGORITHM_STR);
// init cipher Encrypt_mode or Decrypt_mode operation, the second parameter is the KEY used
cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(decryptKey.getBytes(StandardCharsets.UTF_8)));
// base64 decode content
byte[] bytesContent = Base64.getDecoder().decode(content);
// decode content to byte array
byte[] byteDecode = cipher.doFinal(bytesContent);
byte[] byteDecode = getBytes(content, decryptKey);
return new String(byteDecode, StandardCharsets.UTF_8);
} catch (BadPaddingException e) {
if (!ENCODE_RULES.equals(decryptKey)) {
@@ -134,7 +126,19 @@ public final class AesUtil {
}
return content;
}
private static byte[] getBytes(final String content, final String decryptKey) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(decryptKey.getBytes(StandardCharsets.UTF_8), AES);
// cipher based on the algorithm AES
Cipher cipher = Cipher.getInstance(ALGORITHM_STR);
// init cipher Encrypt_mode or Decrypt_mode operation, the second parameter is the KEY used
cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(decryptKey.getBytes(StandardCharsets.UTF_8)));
// base64 decode content
byte[] bytesContent = Base64.getDecoder().decode(content);
// decode content to byte array
return cipher.doFinal(bytesContent);
}
/**
* Determine whether it is encrypted
* @param text text
@@ -145,11 +149,7 @@ public final class AesUtil {
if (Base64Util.isBase64(text)) {
// if it is base64, decrypt directly to determine
try {
SecretKeySpec keySpec = new SecretKeySpec(decryptKey.getBytes(StandardCharsets.UTF_8), AES);
Cipher cipher = Cipher.getInstance(ALGORITHM_STR);
cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(decryptKey.getBytes(StandardCharsets.UTF_8)));
byte[] bytesContent = Base64.getDecoder().decode(text);
byte[] byteDecode = cipher.doFinal(bytesContent);
byte[] byteDecode = getBytes(text, decryptKey);
return byteDecode != null;
} catch (Exception e) {
log.warn("isCiphertext method error: {}", e.getMessage());
@@ -178,16 +178,16 @@ public final class CommonUtil {
if (cause != null) {
message = cause.getMessage();
}
if (message == null || "".equals(message)) {
if (message == null || StringUtils.isBlank(message)) {
message = throwable.getMessage();
}
if (message == null || "".equals(message)) {
if (message == null || StringUtils.isBlank(message)) {
message = throwable.getLocalizedMessage();
}
if (message == null || "".equals(message)) {
if (message == null || StringUtils.isBlank(message)) {
message = throwable.toString();
}
if (message == null || "".equals(message)) {
if (message == null || StringUtils.isBlank(message)) {
message = "unknown error.";
}
return message;
@@ -53,7 +53,7 @@ public final class IpDomainUtil {
* @return true-yes false-no
*/
public static boolean validateIpDomain(String ipDomain) {
if (ipDomain == null || "".equals(ipDomain)) {
if (ipDomain == null || !StringUtils.hasText(ipDomain)) {
return false;
}
ipDomain = ipDomain.trim();
@@ -75,7 +75,7 @@ public final class IpDomainUtil {
* @return true or false
*/
public static boolean isHasSchema(String domainIp) {
if (domainIp == null || "".equals(domainIp)) {
if (domainIp == null || !StringUtils.hasText(domainIp)) {
return false;
}
return DOMAIN_SCHEMA.matcher(domainIp).matches();
@@ -21,7 +21,6 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@@ -85,7 +84,7 @@ final class FlyBookAlertNotifyHandlerImpl extends AbstractAlertNotifyHandlerImpl
atContent.setUserId(userID);
return atContent;
})
.collect(Collectors.toList());
.toList();
contentList.addAll(atContents);
}
@@ -30,6 +30,7 @@ import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.hertzbeat.common.constants.CommonConstants;
/**
@@ -215,7 +216,7 @@ public class ConsistentHash {
* @return collector node
*/
public Node dispatchJob(String dispatchKey, Long jobId) {
if (dispatchKey == null || "".equals(dispatchKey)) {
if (dispatchKey == null || StringUtils.isBlank(dispatchKey)) {
log.error("The dispatch key can not null.");
return null;
}
@@ -230,7 +231,7 @@ public class ConsistentHash {
* @return collector node
*/
public Node preDispatchJob(String dispatchKey) {
if (dispatchKey == null || "".equals(dispatchKey)) {
if (dispatchKey == null || StringUtils.isBlank(dispatchKey)) {
log.error("The dispatch key can not null.");
return null;
}
@@ -62,7 +62,7 @@ public abstract class AbstractImExportServiceImpl implements ImExportService {
var formList = parseImport(is)
.stream()
.map(this::convert)
.collect(Collectors.toUnmodifiableList());
.toList();
if (!CollectionUtils.isEmpty(formList)) {
formList.forEach(monitorDto -> {
monitorService.validate(monitorDto, false);
@@ -80,7 +80,7 @@ public abstract class AbstractImExportServiceImpl implements ImExportService {
.map(it -> monitorService.getMonitorDto(it))
.filter(Objects::nonNull)
.map(this::convert)
.collect(Collectors.toUnmodifiableList());
.toList();
writeOs(monitorList, os);
}
@@ -130,7 +130,7 @@ public class AppServiceImpl implements AppService, CommandLineRunner {
List<Param> params = paramDao.findParamsByMonitorId(monitorId);
List<Configmap> configmaps = params.stream()
.map(param -> new Configmap(param.getField(), param.getParamValue(),
param.getType())).collect(Collectors.toList());
param.getType())).toList();
Map<String, Configmap> configmap = configmaps.stream().collect(Collectors.toMap(Configmap::getKey, item -> item, (key1, key2) -> key1));
CollectUtil.replaceFieldsForPushStyleMonitor(metric, configmap);
metricsTmp.add(metric);
@@ -197,10 +197,10 @@ public class AppServiceImpl implements AppService, CommandLineRunner {
if (appDefine == null) {
throw new IllegalArgumentException("The app " + app + " not support.");
}
metricNames.addAll(appDefine.getMetrics().stream().map(Metrics::getName).collect(Collectors.toList()));
metricNames.addAll(appDefine.getMetrics().stream().map(Metrics::getName).toList());
} else {
appDefines.forEach((k, v) ->
metricNames.addAll(v.getMetrics().stream().map(Metrics::getName).collect(Collectors.toList())));
metricNames.addAll(v.getMetrics().stream().map(Metrics::getName).toList()));
}
return metricNames;
}
@@ -29,6 +29,7 @@ import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
@@ -302,7 +303,7 @@ public class ExcelImExportServiceImpl extends AbstractImExportServiceImpl{
valueCell.setCellStyle(cellStyle);
}
}
if (paramList.size() > 0) {
if (CollectionUtils.isNotEmpty(paramList)) {
RegionUtil.setBorderTop(BorderStyle.THICK, new CellRangeAddress(rowIndex - paramList.size(), rowIndex - 1, 0, 10), sheet);
RegionUtil.setBorderBottom(BorderStyle.THICK, new CellRangeAddress(rowIndex - paramList.size(), rowIndex - 1, 0, 10), sheet);
RegionUtil.setBorderLeft(BorderStyle.THICK, new CellRangeAddress(rowIndex - paramList.size(), rowIndex - 1, 0, 10), sheet);
@@ -377,7 +377,7 @@ public class MonitorServiceImpl implements MonitorService {
if (paramDefine.isRequired() && (param == null || param.getParamValue() == null)) {
throw new IllegalArgumentException("Params field " + field + " is required.");
}
if (param != null && param.getParamValue() != null && !"".equals(param.getParamValue())) {
if (param != null && param.getParamValue() != null && StringUtils.hasText(param.getParamValue())) {
switch (paramDefine.getType()) {
case "number":
double doubleValue;
@@ -748,7 +748,7 @@ public class MonitorServiceImpl implements MonitorService {
new Configmap(param.getField(), param.getParamValue(), param.getType())).collect(Collectors.toList());
List<ParamDefine> paramDefaultValue = appDefine.getParams().stream()
.filter(item -> StringUtils.hasText(item.getDefaultValue()))
.collect(Collectors.toList());
.toList();
paramDefaultValue.forEach(defaultVar -> {
if (configmaps.stream().noneMatch(item -> item.getKey().equals(defaultVar.getField()))) {
Configmap configmap = new Configmap(defaultVar.getField(), defaultVar.getDefaultValue(), (byte) 1);
@@ -31,6 +31,7 @@ import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.hertzbeat.common.cache.CacheFactory;
import org.apache.hertzbeat.common.cache.CommonCacheService;
import org.apache.hertzbeat.common.constants.CommonConstants;
@@ -100,7 +101,7 @@ public class NoticeConfigServiceImpl implements NoticeConfigService, CommandLine
public List<NoticeTemplate> getNoticeTemplates(String name) {
Specification<NoticeTemplate> specification = (root, query, criteriaBuilder) -> {
Predicate predicate = criteriaBuilder.conjunction();
if (name != null && !"".equals(name)) {
if (name != null && StringUtils.isNoneBlank(name)) {
Predicate predicateName = criteriaBuilder.like(root.get("name"), "%" + name + "%");
predicate = criteriaBuilder.and(predicateName);
}
@@ -22,7 +22,6 @@ import com.obs.services.model.ListObjectsRequest;
import java.io.InputStream;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.apache.hertzbeat.common.constants.SignConstants;
import org.apache.hertzbeat.manager.pojo.dto.FileDTO;
@@ -86,7 +85,7 @@ public class ObsObjectStoreServiceImpl implements ObjectStoreService {
return obsClient.listObjects(request).getObjects()
.stream()
.map(it -> new FileDTO(it.getObjectKey(), it.getObjectContent()))
.collect(Collectors.toUnmodifiableList());
.toList();
}
@Override
@@ -52,14 +52,15 @@ class DingTalkRobotAlertNotifyHandlerImplTest extends AbstractSpringIntegrationT
NoticeTemplate noticeTemplate = new NoticeTemplate();
noticeTemplate.setId(1L);
noticeTemplate.setName("dingding");
noticeTemplate.setContent("#### [${title}]\n"
+ "##### **${targetLabel}** : ${target}\n"
+ "<#if (monitorId??)>##### **${monitorIdLabel}** : ${monitorId} </#if>\n"
+ "<#if (monitorName??)>##### **${monitorNameLabel}** : ${monitorName} </#if>\n"
+ "<#if (monitorHost??)>##### **${monitorHostLabel}** : ${monitorHost} </#if>\n"
+ "##### **${priorityLabel}** : ${priority}\n"
+ "##### **${triggerTimeLabel}** : ${triggerTime}\n"
+ "##### **${contentLabel}** : ${content}");
noticeTemplate.setContent("""
#### [${title}]
##### **${targetLabel}** : ${target}
<#if (monitorId??)>##### **${monitorIdLabel}** : ${monitorId} </#if>
<#if (monitorName??)>##### **${monitorNameLabel}** : ${monitorName} </#if>
<#if (monitorHost??)>##### **${monitorHostLabel}** : ${monitorHost} </#if>
##### **${priorityLabel}** : ${priority}
##### **${triggerTimeLabel}** : ${triggerTime}
##### **${contentLabel}** : ${content}""");
Alert alert = new Alert();
alert.setId(1L);
alert.setTarget("Mock Target");
@@ -53,13 +53,14 @@ class DiscordBotAlertNotifyHandlerImplTest extends AbstractSpringIntegrationTest
var noticeTemplate = new NoticeTemplate();
noticeTemplate.setId(1L);
noticeTemplate.setName("DiscordBot");
noticeTemplate.setContent("${targetLabel} : ${target}\n"
+ "<#if (monitorId??)>${monitorIdLabel} : ${monitorId} </#if>\n"
+ "<#if (monitorName??)>${monitorNameLabel} : ${monitorName} </#if>\n"
+ "<#if (monitorHost??)>${monitorHostLabel} : ${monitorHost} </#if>\n"
+ "${priorityLabel} : ${priority}\n"
+ "${triggerTimeLabel} : ${triggerTime}\n"
+ "${contentLabel} : ${content}");
noticeTemplate.setContent("""
${targetLabel} : ${target}
<#if (monitorId??)>${monitorIdLabel} : ${monitorId} </#if>
<#if (monitorName??)>${monitorNameLabel} : ${monitorName} </#if>
<#if (monitorHost??)>${monitorHostLabel} : ${monitorHost} </#if>
${priorityLabel} : ${priority}
${triggerTimeLabel} : ${triggerTime}
${contentLabel} : ${content}""");
var alert = new Alert();
alert.setId(1L);
alert.setTarget("Mock Target");
@@ -52,13 +52,14 @@ class FlyBookAlertNotifyHandlerImplTest extends AbstractSpringIntegrationTest {
NoticeTemplate noticeTemplate = new NoticeTemplate();
noticeTemplate.setId(1L);
noticeTemplate.setName("FlyBook");
noticeTemplate.setContent("{targetLabel} : ${target}\n"
+ "<#if (monitorId??)>${monitorIdLabel} : ${monitorId} </#if>\n"
+ "<#if (monitorName??)>${monitorNameLabel} : ${monitorName} </#if>\n"
+ "<#if (monitorHost??)>${monitorHostLabel} : ${monitorHost} </#if>\n"
+ "${priorityLabel} : ${priority}\n"
+ "${triggerTimeLabel} : ${triggerTime}\n"
+ "${contentLabel} : ${content}");
noticeTemplate.setContent("""
{targetLabel} : ${target}
<#if (monitorId??)>${monitorIdLabel} : ${monitorId} </#if>
<#if (monitorName??)>${monitorNameLabel} : ${monitorName} </#if>
<#if (monitorHost??)>${monitorHostLabel} : ${monitorHost} </#if>
${priorityLabel} : ${priority}
${triggerTimeLabel} : ${triggerTime}
${contentLabel} : ${content}""");
Alert alert = new Alert();
alert.setId(1L);
alert.setTarget("Mock Target");
@@ -73,14 +73,15 @@ class HuaweiCloudSmnAlertNotifyHandlerImplTest extends AbstractSpringIntegration
var noticeTemplate = new NoticeTemplate();
noticeTemplate.setId(1L);
noticeTemplate.setName("HuaWeiCloud");
noticeTemplate.setContent("[${title}]\n"
+ "${targetLabel} : ${target}\n"
+ "<#if (monitorId??)>${monitorIdLabel} : ${monitorId} </#if>\n"
+ "<#if (monitorName??)>${monitorNameLabel} : ${monitorName} </#if>\n"
+ "<#if (monitorHost??)>${monitorHostLabel} : ${monitorHost} </#if>\n"
+ "${priorityLabel} : ${priority}\n"
+ "${triggerTimeLabel} : ${triggerTime}\n"
+ "${contentLabel} : ${content}");
noticeTemplate.setContent("""
[${title}]
${targetLabel} : ${target}
<#if (monitorId??)>${monitorIdLabel} : ${monitorId} </#if>
<#if (monitorName??)>${monitorNameLabel} : ${monitorName} </#if>
<#if (monitorHost??)>${monitorHostLabel} : ${monitorHost} </#if>
${priorityLabel} : ${priority}
${triggerTimeLabel} : ${triggerTime}
${contentLabel} : ${content}""");
var alert = new Alert();
alert.setId(1L);
alert.setTarget("Mock Target");
@@ -55,14 +55,15 @@ class SlackAlertNotifyHandlerImplTest extends AbstractSpringIntegrationTest {
var noticeTemplate = new NoticeTemplate();
noticeTemplate.setId(1L);
noticeTemplate.setName("Slack");
noticeTemplate.setContent("*[${title}]*\n"
+ "${targetLabel} : ${target}\n"
+ "<#if (monitorId??)>${monitorIdLabel} : ${monitorId} </#if>\n"
+ "<#if (monitorName??)>${monitorNameLabel} : ${monitorName} </#if>\n"
+ "<#if (monitorHost??)>${monitorHostLabel} : ${monitorHost} </#if>\n"
+ "${priorityLabel} : ${priority}\n"
+ "${triggerTimeLabel} : ${triggerTime}\n"
+ "${contentLabel} : ${content}");
noticeTemplate.setContent("""
*[${title}]*
${targetLabel} : ${target}
<#if (monitorId??)>${monitorIdLabel} : ${monitorId} </#if>
<#if (monitorName??)>${monitorNameLabel} : ${monitorName} </#if>
<#if (monitorHost??)>${monitorHostLabel} : ${monitorHost} </#if>
${priorityLabel} : ${priority}
${triggerTimeLabel} : ${triggerTime}
${contentLabel} : ${content}""");
var map = Map.of(
CommonConstants.TAG_MONITOR_ID, "Mock monitor id",
CommonConstants.TAG_MONITOR_NAME, "Mock monitor name",
@@ -58,14 +58,15 @@ class TelegramBotAlertNotifyHandlerImplTest extends AbstractSpringIntegrationTes
NoticeTemplate noticeTemplate = new NoticeTemplate();
noticeTemplate.setId(1L);
noticeTemplate.setName("Telegram");
noticeTemplate.setContent("[${title}]\n"
+ "${targetLabel} : ${target}\n"
+ "<#if (monitorId??)>${monitorIdLabel} : ${monitorId} </#if>\n"
+ "<#if (monitorName??)>${monitorNameLabel} : ${monitorName} </#if>\n"
+ "<#if (monitorHost??)>${monitorHostLabel} : ${monitorHost} </#if>\n"
+ "${priorityLabel} : ${priority}\n"
+ "${triggerTimeLabel} : ${triggerTime}\n"
+ "${contentLabel} : ${content}");
noticeTemplate.setContent("""
[${title}]
${targetLabel} : ${target}
<#if (monitorId??)>${monitorIdLabel} : ${monitorId} </#if>
<#if (monitorName??)>${monitorNameLabel} : ${monitorName} </#if>
<#if (monitorHost??)>${monitorHostLabel} : ${monitorHost} </#if>
${priorityLabel} : ${priority}
${triggerTimeLabel} : ${triggerTime}
${contentLabel} : ${content}""");
Map<String, String> map = new HashMap<>();
map.put(CommonConstants.TAG_MONITOR_ID, "Mock monitor id");
map.put(CommonConstants.TAG_MONITOR_NAME, "Mock monitor name");
@@ -55,14 +55,15 @@ class WeComRobotAlertNotifyHandlerImplTest extends AbstractSpringIntegrationTest
NoticeTemplate noticeTemplate = new NoticeTemplate();
noticeTemplate.setId(1L);
noticeTemplate.setName("WeWork");
noticeTemplate.setContent("[${title}]\n"
+ "${targetLabel} : ${target}\n"
+ "<#if (monitorId??)>${monitorIdLabel} : ${monitorId} </#if>\n"
+ "<#if (monitorName??)>${monitorNameLabel} : ${monitorName} </#if>\n"
+ "<#if (monitorHost??)>${monitorHostLabel} : ${monitorHost} </#if>\n"
+ "${priorityLabel} : ${priority}\n"
+ "${triggerTimeLabel} : ${triggerTime}\n"
+ "${contentLabel} : ${content}");
noticeTemplate.setContent("""
[${title}]
${targetLabel} : ${target}
<#if (monitorId??)>${monitorIdLabel} : ${monitorId} </#if>
<#if (monitorName??)>${monitorNameLabel} : ${monitorName} </#if>
<#if (monitorHost??)>${monitorHostLabel} : ${monitorHost} </#if>
${priorityLabel} : ${priority}
${triggerTimeLabel} : ${triggerTime}
${contentLabel} : ${content}""");
Map<String, String> map = new HashMap<>();
map.put(CommonConstants.TAG_MONITOR_ID, "Mock monitor id");
map.put(CommonConstants.TAG_MONITOR_NAME, "Mock monitor name");
@@ -17,14 +17,6 @@
package org.apache.hertzbeat.manager.controller;
import static org.mockito.Mockito.doNothing;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.hertzbeat.common.constants.CommonConstants;
import org.apache.hertzbeat.common.util.JsonUtil;
import org.apache.hertzbeat.manager.service.impl.MonitorServiceImpl;
@@ -39,7 +31,15 @@ import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.multipart.MultipartFile;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static org.mockito.Mockito.doNothing;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* Test case for {@link MonitorsController}
@@ -128,7 +128,7 @@ class MonitorsControllerTest {
String type = "JSON";
this.mockMvc.perform(MockMvcRequestBuilders.get("/api/monitors/export")
.param("ids", String.join(",", ids.stream().map(String::valueOf).collect(Collectors.toList())))
.param("ids", ids.stream().map(String::valueOf).collect(Collectors.joining(",")))
.param("type", type))
.andExpect(status().isOk())
.andReturn();
@@ -137,7 +137,7 @@ class MonitorsControllerTest {
@Test
void export2() throws Exception {
// Mock the behavior of monitorService.importConfig
doNothing().when(monitorService).importConfig((MultipartFile) Mockito.any());
doNothing().when(monitorService).importConfig(Mockito.any());
// Perform the request and verify the response
this.mockMvc.perform(MockMvcRequestBuilders.post("/api/monitors/import")
@@ -99,13 +99,14 @@ class NoticeConfigControllerTest {
NoticeTemplate template = new NoticeTemplate();
template.setId(5L);
template.setName("Dingding");
template.setContent("[${title}]\n"
+ "${targetLabel} : ${target}\n"
+ "<#if (monitorId??)>${monitorIdLabel} : ${monitorId} </#if>\n"
+ "<#if (monitorName??)>${monitorNameLabel} : ${monitorName} </#if>\n"
+ "${priorityLabel} : ${priority}\n"
+ "${triggerTimeLabel} : ${triggerTime}\n"
+ "${contentLabel} : ${content}");
template.setContent("""
[${title}]
${targetLabel} : ${target}
<#if (monitorId??)>${monitorIdLabel} : ${monitorId} </#if>
<#if (monitorName??)>${monitorNameLabel} : ${monitorName} </#if>
${priorityLabel} : ${priority}
${triggerTimeLabel} : ${triggerTime}
${contentLabel} : ${content}""");
template.setType((byte) 5);
return template;
@@ -17,25 +17,6 @@
package org.apache.hertzbeat.manager.service;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.apache.hertzbeat.alert.dao.AlertDefineBindDao;
import org.apache.hertzbeat.common.constants.CommonConstants;
import org.apache.hertzbeat.common.entity.alerter.Alert;
@@ -72,6 +53,26 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
/**
* newBranch feature-clickhouse#179
* <a href="https://www.cnblogs.com/it1042290135/p/16202478.html">...</a>
@@ -742,7 +743,7 @@ class MonitorServiceTest {
when(appService.getAppDefine(monitor.getApp())).thenReturn(job);
List<Param> params = Collections.singletonList(new Param());
List<String> metrics = Arrays.asList();
List<String> metrics = List.of();
try {
monitorService.addNewMonitorOptionalMetrics(metrics, monitor, params);
} catch (MonitorMetricsException e) {
@@ -750,7 +751,7 @@ class MonitorServiceTest {
}
reset();
when(monitorDao.save(monitor)).thenThrow(RuntimeException.class);
metrics = Arrays.asList("metric-001");
metrics = List.of("metric-001");
List<Metrics> metricsDefine = new ArrayList<>();
Metrics e = new Metrics();
e.setName("metric-001");
@@ -47,6 +47,7 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.hertzbeat.common.constants.CommonConstants;
import org.apache.hertzbeat.common.entity.dto.Value;
import org.apache.hertzbeat.common.entity.message.CollectRep;
@@ -57,374 +58,373 @@ import org.springframework.stereotype.Component;
/**
* GreptimeDB data storage, only supports GreptimeDB version >= v0.5
*
*/
@Component
@ConditionalOnProperty(prefix = "warehouse.store.greptime", name = "enabled", havingValue = "true")
@Slf4j
public class GreptimeDbDataStorage extends AbstractHistoryDataStorage {
private static final String CONSTANT_DB_TTL = "30d";
private static final String QUERY_HISTORY_SQL = "SELECT CAST (ts AS Int64) ts, instance, `%s` FROM `%s` WHERE ts >= now() - interval '%s' and monitor_id = %s order by ts desc;";
@SuppressWarnings("checkstyle:LineLength")
private static final String QUERY_HISTORY_WITH_INSTANCE_SQL = "SELECT CAST (ts AS Int64) ts, instance, `%s` FROM `%s` WHERE ts >= now() - interval '%s' and monitor_id = %s and instance = '%s' order by ts desc;";
private static final String QUERY_INSTANCE_SQL = "SELECT DISTINCT instance FROM `%s` WHERE ts >= now() - interval '1 WEEK'";
@SuppressWarnings("checkstyle:LineLength")
private static final String QUERY_HISTORY_INTERVAL_WITH_INSTANCE_SQL = "SELECT CAST (ts AS Int64) ts, first_value(`%s`) range '4h' first, avg(`%s`) range '4h' avg, min(`%s`) range '4h' min, max(`%s`) range '4h' max FROM `%s` WHERE instance = '%s' AND ts >= now() - interval '%s' ALIGN '4h'";
private static final String TABLE_NOT_EXIST = "not found";
private static final String CONSTANTS_CREATE_DATABASE = "CREATE DATABASE IF NOT EXISTS `%s` WITH(ttl='%s')";
private static final Runnable INSTANCE_EXCEPTION_PRINT = () -> {
if (log.isErrorEnabled()) {
log.error("""
\t---------------GreptimeDB Init Failed---------------
\t--------------Please Config GreptimeDB--------------
t-----------Can Not Use Metric History Now-----------
""");
}
if (log.isErrorEnabled()) {
log.error("""
\t---------------GreptimeDB Init Failed---------------
\t--------------Please Config GreptimeDB--------------
t-----------Can Not Use Metric History Now-----------
""");
}
};
private HikariDataSource hikariDataSource;
private GreptimeDB greptimeDb;
public GreptimeDbDataStorage(GreptimeProperties greptimeProperties) {
if (greptimeProperties == null) {
log.error("init error, please config Warehouse GreptimeDB props in application.yml");
throw new IllegalArgumentException("please config Warehouse GreptimeDB props");
}
serverAvailable = initGreptimeDbClient(greptimeProperties) && initGreptimeDbDataSource(greptimeProperties);
if (greptimeProperties == null) {
log.error("init error, please config Warehouse GreptimeDB props in application.yml");
throw new IllegalArgumentException("please config Warehouse GreptimeDB props");
}
serverAvailable = initGreptimeDbClient(greptimeProperties) && initGreptimeDbDataSource(greptimeProperties);
}
private void initGreptimeDb(final GreptimeProperties greptimeProperties) throws SQLException {
final DriverPropertyInfo[] properties = new Driver().getPropertyInfo(greptimeProperties.url(), null);
final String host = ObjectUtils.requireNonEmpty(properties[0].value);
final String port = ObjectUtils.requireNonEmpty(properties[1].value);
final String dbName = ObjectUtils.requireNonEmpty(properties[2].value);
String ttl = greptimeProperties.expireTime();
if (ttl == null || "".equals(ttl.trim())) {
ttl = CONSTANT_DB_TTL;
}
try (final Connection tempConnection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port,
greptimeProperties.username(), greptimeProperties.password());
final PreparedStatement pstmt = tempConnection
.prepareStatement(String.format(CONSTANTS_CREATE_DATABASE, dbName, ttl))) {
log.info("[warehouse greptime] try to create database `{}` if not exists", dbName);
pstmt.execute();
}
final DriverPropertyInfo[] properties = new Driver().getPropertyInfo(greptimeProperties.url(), null);
final String host = ObjectUtils.requireNonEmpty(properties[0].value);
final String port = ObjectUtils.requireNonEmpty(properties[1].value);
final String dbName = ObjectUtils.requireNonEmpty(properties[2].value);
String ttl = greptimeProperties.expireTime();
if (ttl == null || StringUtils.isBlank(ttl.trim())) {
ttl = CONSTANT_DB_TTL;
}
try (final Connection tempConnection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port,
greptimeProperties.username(), greptimeProperties.password());
final PreparedStatement pstmt = tempConnection
.prepareStatement(String.format(CONSTANTS_CREATE_DATABASE, dbName, ttl))) {
log.info("[warehouse greptime] try to create database `{}` if not exists", dbName);
pstmt.execute();
}
}
private boolean initGreptimeDbClient(GreptimeProperties greptimeProperties) {
String endpoints = greptimeProperties.grpcEndpoints();
try {
final DriverPropertyInfo[] properties = new Driver().getPropertyInfo(greptimeProperties.url(), null);
final String dbName = ObjectUtils.requireNonEmpty(properties[2].value);
GreptimeOptions opts = GreptimeOptions.newBuilder(endpoints.split(","), dbName) //
.writeMaxRetries(3) //
.authInfo(new AuthInfo(greptimeProperties.username(), greptimeProperties.password()))
.routeTableRefreshPeriodSeconds(30) //
.build();
this.greptimeDb = GreptimeDB.create(opts);
} catch (Exception e) {
log.error("[warehouse greptime] Fail to start GreptimeDB client");
return false;
}
return true;
String endpoints = greptimeProperties.grpcEndpoints();
try {
final DriverPropertyInfo[] properties = new Driver().getPropertyInfo(greptimeProperties.url(), null);
final String dbName = ObjectUtils.requireNonEmpty(properties[2].value);
GreptimeOptions opts = GreptimeOptions.newBuilder(endpoints.split(","), dbName) //
.writeMaxRetries(3) //
.authInfo(new AuthInfo(greptimeProperties.username(), greptimeProperties.password()))
.routeTableRefreshPeriodSeconds(30) //
.build();
this.greptimeDb = GreptimeDB.create(opts);
} catch (Exception e) {
log.error("[warehouse greptime] Fail to start GreptimeDB client");
return false;
}
return true;
}
private boolean initGreptimeDbDataSource(final GreptimeProperties greptimeProperties) {
try {
initGreptimeDb(greptimeProperties);
} catch (Exception e) {
if (log.isErrorEnabled()) {
log.error(e.getMessage(), e);
}
INSTANCE_EXCEPTION_PRINT.run();
return false;
}
final HikariConfig config = new HikariConfig();
// jdbc properties
config.setJdbcUrl(greptimeProperties.url());
config.setUsername(greptimeProperties.username());
config.setPassword(greptimeProperties.password());
config.setDriverClassName(greptimeProperties.driverClassName());
// minimum number of idle connection
config.setMinimumIdle(10);
// maximum number of connection in the pool
config.setMaximumPoolSize(10);
// maximum wait milliseconds for get connection from pool
config.setConnectionTimeout(30000);
// maximum lifetime for each connection
config.setMaxLifetime(0);
// max idle time for recycle idle connection
config.setIdleTimeout(0);
// validation query
config.setConnectionTestQuery("select 1");
try {
this.hikariDataSource = new HikariDataSource(config);
} catch (Exception e) {
INSTANCE_EXCEPTION_PRINT.run();
return false;
}
return true;
try {
initGreptimeDb(greptimeProperties);
} catch (Exception e) {
if (log.isErrorEnabled()) {
log.error(e.getMessage(), e);
}
INSTANCE_EXCEPTION_PRINT.run();
return false;
}
final HikariConfig config = new HikariConfig();
// jdbc properties
config.setJdbcUrl(greptimeProperties.url());
config.setUsername(greptimeProperties.username());
config.setPassword(greptimeProperties.password());
config.setDriverClassName(greptimeProperties.driverClassName());
// minimum number of idle connection
config.setMinimumIdle(10);
// maximum number of connection in the pool
config.setMaximumPoolSize(10);
// maximum wait milliseconds for get connection from pool
config.setConnectionTimeout(30000);
// maximum lifetime for each connection
config.setMaxLifetime(0);
// max idle time for recycle idle connection
config.setIdleTimeout(0);
// validation query
config.setConnectionTestQuery("select 1");
try {
this.hikariDataSource = new HikariDataSource(config);
} catch (Exception e) {
INSTANCE_EXCEPTION_PRINT.run();
return false;
}
return true;
}
@Override
public void saveData(CollectRep.MetricsData metricsData) {
if (!isServerAvailable() || metricsData.getCode() != CollectRep.Code.SUCCESS) {
return;
}
if (metricsData.getValuesList().isEmpty()) {
log.info("[warehouse greptime] flush metrics data {} is null, ignore.", metricsData.getId());
return;
}
String monitorId = String.valueOf(metricsData.getId());
String tableName = getTableName(metricsData.getApp(), metricsData.getMetrics());
TableSchema.Builder tableSchemaBuilder = TableSchema.newBuilder(tableName);
tableSchemaBuilder.addTag("monitor_id", DataType.String) //
.addTag("instance", DataType.String) //
.addTimestamp("ts", DataType.TimestampMillisecond);
List<CollectRep.Field> fieldsList = metricsData.getFieldsList();
for (CollectRep.Field field : fieldsList) {
// handle field type
if (field.getType() == CommonConstants.TYPE_NUMBER) {
tableSchemaBuilder.addField(field.getName(), DataType.Float64);
} else if (field.getType() == CommonConstants.TYPE_STRING) {
tableSchemaBuilder.addField(field.getName(), DataType.String);
}
}
Table table = Table.from(tableSchemaBuilder.build());
try {
long now = System.currentTimeMillis();
Object[] values = new Object[3 + fieldsList.size()];
values[0] = monitorId;
values[2] = now;
for (CollectRep.ValueRow valueRow : metricsData.getValuesList()) {
Map<String, String> labels = new HashMap<>(8);
for (int i = 0; i < fieldsList.size(); i++) {
if (!CommonConstants.NULL_VALUE.equals(valueRow.getColumns(i))) {
CollectRep.Field field = fieldsList.get(i);
if (field.getType() == CommonConstants.TYPE_NUMBER) {
values[3 + i] = Double.parseDouble(valueRow.getColumns(i));
} else if (field.getType() == CommonConstants.TYPE_STRING) {
values[3 + i] = valueRow.getColumns(i);
}
if (field.getLabel()) {
labels.put(field.getName(), String.valueOf(values[3 + i]));
}
} else {
values[3 + i] = null;
}
}
values[1] = JsonUtil.toJson(labels);
table.addRow(values);
}
CompletableFuture<Result<WriteOk, Err>> writeFuture = greptimeDb.write(table);
try {
Result<WriteOk, Err> result = writeFuture.get(10, TimeUnit.SECONDS);
if (result.isOk()) {
log.debug("[warehouse greptime]-Write successful");
} else {
log.warn("[warehouse greptime]--Write failed: {}", result.getErr());
}
} catch (Throwable throwable) {
log.error("[warehouse greptime]--Error occurred: {}", throwable.getMessage());
}
} catch (Exception e) {
log.error("[warehouse greptime]--Error: {}", e.getMessage(), e);
}
if (!isServerAvailable() || metricsData.getCode() != CollectRep.Code.SUCCESS) {
return;
}
if (metricsData.getValuesList().isEmpty()) {
log.info("[warehouse greptime] flush metrics data {} is null, ignore.", metricsData.getId());
return;
}
String monitorId = String.valueOf(metricsData.getId());
String tableName = getTableName(metricsData.getApp(), metricsData.getMetrics());
TableSchema.Builder tableSchemaBuilder = TableSchema.newBuilder(tableName);
tableSchemaBuilder.addTag("monitor_id", DataType.String) //
.addTag("instance", DataType.String) //
.addTimestamp("ts", DataType.TimestampMillisecond);
List<CollectRep.Field> fieldsList = metricsData.getFieldsList();
for (CollectRep.Field field : fieldsList) {
// handle field type
if (field.getType() == CommonConstants.TYPE_NUMBER) {
tableSchemaBuilder.addField(field.getName(), DataType.Float64);
} else if (field.getType() == CommonConstants.TYPE_STRING) {
tableSchemaBuilder.addField(field.getName(), DataType.String);
}
}
Table table = Table.from(tableSchemaBuilder.build());
try {
long now = System.currentTimeMillis();
Object[] values = new Object[3 + fieldsList.size()];
values[0] = monitorId;
values[2] = now;
for (CollectRep.ValueRow valueRow : metricsData.getValuesList()) {
Map<String, String> labels = new HashMap<>(8);
for (int i = 0; i < fieldsList.size(); i++) {
if (!CommonConstants.NULL_VALUE.equals(valueRow.getColumns(i))) {
CollectRep.Field field = fieldsList.get(i);
if (field.getType() == CommonConstants.TYPE_NUMBER) {
values[3 + i] = Double.parseDouble(valueRow.getColumns(i));
} else if (field.getType() == CommonConstants.TYPE_STRING) {
values[3 + i] = valueRow.getColumns(i);
}
if (field.getLabel()) {
labels.put(field.getName(), String.valueOf(values[3 + i]));
}
} else {
values[3 + i] = null;
}
}
values[1] = JsonUtil.toJson(labels);
table.addRow(values);
}
CompletableFuture<Result<WriteOk, Err>> writeFuture = greptimeDb.write(table);
try {
Result<WriteOk, Err> result = writeFuture.get(10, TimeUnit.SECONDS);
if (result.isOk()) {
log.debug("[warehouse greptime]-Write successful");
} else {
log.warn("[warehouse greptime]--Write failed: {}", result.getErr());
}
} catch (Throwable throwable) {
log.error("[warehouse greptime]--Error occurred: {}", throwable.getMessage());
}
} catch (Exception e) {
log.error("[warehouse greptime]--Error: {}", e.getMessage(), e);
}
}
@Override
public Map<String, List<Value>> getHistoryMetricData(Long monitorId, String app, String metrics, String metric,
String label, String history) {
Map<String, List<Value>> instanceValuesMap = new HashMap<>(8);
if (!isServerAvailable()) {
INSTANCE_EXCEPTION_PRINT.run();
return instanceValuesMap;
}
String table = getTableName(app, metrics);
String interval = history2interval(history);
String selectSql = label == null ? String.format(QUERY_HISTORY_SQL, metric, table, interval, monitorId)
: String.format(QUERY_HISTORY_WITH_INSTANCE_SQL, metric, table, interval, monitorId, label);
if (log.isDebugEnabled()) {
log.debug("[warehouse greptime] getHistoryMetricData SQL: {}", selectSql);
}
try (Connection connection = hikariDataSource.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(selectSql)) {
while (resultSet.next()) {
long ts = resultSet.getLong(1);
if (ts == 0) {
if (log.isErrorEnabled()) {
log.error("[warehouse greptime] getHistoryMetricData query result timestamp is 0, ignore. {}.",
selectSql);
}
continue;
}
String instanceValue = resultSet.getString(2);
if (instanceValue == null || "".equals(instanceValue)) {
instanceValue = "";
}
double value = resultSet.getDouble(3);
String strValue = double2decimalString(value);
List<Value> valueList = instanceValuesMap.computeIfAbsent(instanceValue, k -> new LinkedList<>());
valueList.add(new Value(strValue, ts));
}
return instanceValuesMap;
} catch (SQLException sqlException) {
String msg = sqlException.getMessage();
if (msg != null && !msg.contains(TABLE_NOT_EXIST)) {
if (log.isWarnEnabled()) {
log.warn("[warehouse greptime] failed to getHistoryMetricData: " + sqlException.getMessage());
}
}
} catch (Exception e) {
if (log.isErrorEnabled()) {
log.error("[warehouse greptime] failed to getHistoryMetricData:" + e.getMessage(), e);
}
}
return instanceValuesMap;
String label, String history) {
Map<String, List<Value>> instanceValuesMap = new HashMap<>(8);
if (!isServerAvailable()) {
INSTANCE_EXCEPTION_PRINT.run();
return instanceValuesMap;
}
String table = getTableName(app, metrics);
String interval = history2interval(history);
String selectSql = label == null ? String.format(QUERY_HISTORY_SQL, metric, table, interval, monitorId)
: String.format(QUERY_HISTORY_WITH_INSTANCE_SQL, metric, table, interval, monitorId, label);
if (log.isDebugEnabled()) {
log.debug("[warehouse greptime] getHistoryMetricData SQL: {}", selectSql);
}
try (Connection connection = hikariDataSource.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(selectSql)) {
while (resultSet.next()) {
long ts = resultSet.getLong(1);
if (ts == 0) {
if (log.isErrorEnabled()) {
log.error("[warehouse greptime] getHistoryMetricData query result timestamp is 0, ignore. {}.",
selectSql);
}
continue;
}
String instanceValue = resultSet.getString(2);
if (instanceValue == null || StringUtils.isBlank(instanceValue)) {
instanceValue = "";
}
double value = resultSet.getDouble(3);
String strValue = double2decimalString(value);
List<Value> valueList = instanceValuesMap.computeIfAbsent(instanceValue, k -> new LinkedList<>());
valueList.add(new Value(strValue, ts));
}
return instanceValuesMap;
} catch (SQLException sqlException) {
String msg = sqlException.getMessage();
if (msg != null && !msg.contains(TABLE_NOT_EXIST)) {
if (log.isWarnEnabled()) {
log.warn("[warehouse greptime] failed to getHistoryMetricData: {}", sqlException.getMessage());
}
}
} catch (Exception e) {
if (log.isErrorEnabled()) {
log.error("[warehouse greptime] failed to getHistoryMetricData:{}", e.getMessage(), e);
}
}
return instanceValuesMap;
}
private String getTableName(String app, String metrics) {
return app + "_" + metrics;
return app + "_" + metrics;
}
@Override
public Map<String, List<Value>> getHistoryIntervalMetricData(Long monitorId, String app, String metrics,
String metric, String label, String history) {
if (!isServerAvailable()) {
INSTANCE_EXCEPTION_PRINT.run();
return Collections.emptyMap();
}
String table = getTableName(app, metrics);
List<String> instances = new LinkedList<>();
if (label != null && !"".equals(label)) {
instances.add(label);
}
if (instances.isEmpty()) {
String selectSql = String.format(QUERY_INSTANCE_SQL, table);
if (log.isDebugEnabled()) {
log.debug("[warehouse greptime] getHistoryIntervalMetricData sql: {}", selectSql);
}
try (Connection connection = hikariDataSource.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(selectSql)) {
while (resultSet.next()) {
String instanceValue = resultSet.getString(1);
if (instanceValue == null || "".equals(instanceValue)) {
instances.add("''");
} else {
instances.add(instanceValue);
}
}
} catch (Exception e) {
if (log.isErrorEnabled()) {
log.error("[warehouse greptime] failed to query instances" + e.getMessage(), e);
}
}
}
Map<String, List<Value>> instanceValuesMap = new HashMap<>(instances.size());
for (String instanceValue : instances) {
String selectSql = String.format(QUERY_HISTORY_INTERVAL_WITH_INSTANCE_SQL, metric, metric, metric, metric,
table, instanceValue, history2interval(history));
if (log.isDebugEnabled()) {
log.debug("[warehouse greptime] getHistoryIntervalMetricData sql: {}", selectSql);
}
List<Value> values = instanceValuesMap.computeIfAbsent(instanceValue, k -> new LinkedList<>());
try (Connection connection = hikariDataSource.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(selectSql)) {
while (resultSet.next()) {
long ts = resultSet.getLong(1);
if (ts == 0) {
if (log.isErrorEnabled()) {
log.error(
"[warehouse greptime] getHistoryIntervalMetricData query result timestamp is 0, ignore. {}.",
selectSql);
}
continue;
}
double origin = resultSet.getDouble(2);
String originStr = double2decimalString(origin);
double avg = resultSet.getDouble(3);
String avgStr = double2decimalString(avg);
double min = resultSet.getDouble(4);
String minStr = double2decimalString(min);
double max = resultSet.getDouble(5);
String maxStr = double2decimalString(max);
Value value = Value.builder().origin(originStr).mean(avgStr).min(minStr).max(maxStr).time(ts)
.build();
values.add(value);
}
resultSet.close();
} catch (Exception e) {
if (log.isErrorEnabled()) {
log.error("[warehouse greptime] failed to getHistoryIntervalMetricData: " + e.getMessage(), e);
}
}
}
return instanceValuesMap;
String metric, String label, String history) {
if (!isServerAvailable()) {
INSTANCE_EXCEPTION_PRINT.run();
return Collections.emptyMap();
}
String table = getTableName(app, metrics);
List<String> instances = new LinkedList<>();
if (label != null && !StringUtils.isBlank(label)) {
instances.add(label);
}
if (instances.isEmpty()) {
String selectSql = String.format(QUERY_INSTANCE_SQL, table);
if (log.isDebugEnabled()) {
log.debug("[warehouse greptime] getHistoryIntervalMetricData sql: {}", selectSql);
}
try (Connection connection = hikariDataSource.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(selectSql)) {
while (resultSet.next()) {
String instanceValue = resultSet.getString(1);
if (instanceValue == null || StringUtils.isBlank(instanceValue)) {
instances.add("''");
} else {
instances.add(instanceValue);
}
}
} catch (Exception e) {
if (log.isErrorEnabled()) {
log.error("[warehouse greptime] failed to query instances{}", e.getMessage(), e);
}
}
}
Map<String, List<Value>> instanceValuesMap = new HashMap<>(instances.size());
for (String instanceValue : instances) {
String selectSql = String.format(QUERY_HISTORY_INTERVAL_WITH_INSTANCE_SQL, metric, metric, metric, metric,
table, instanceValue, history2interval(history));
if (log.isDebugEnabled()) {
log.debug("[warehouse greptime] getHistoryIntervalMetricData sql: {}", selectSql);
}
List<Value> values = instanceValuesMap.computeIfAbsent(instanceValue, k -> new LinkedList<>());
try (Connection connection = hikariDataSource.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(selectSql)) {
while (resultSet.next()) {
long ts = resultSet.getLong(1);
if (ts == 0) {
if (log.isErrorEnabled()) {
log.error(
"[warehouse greptime] getHistoryIntervalMetricData query result timestamp is 0, ignore. {}.",
selectSql);
}
continue;
}
double origin = resultSet.getDouble(2);
String originStr = double2decimalString(origin);
double avg = resultSet.getDouble(3);
String avgStr = double2decimalString(avg);
double min = resultSet.getDouble(4);
String minStr = double2decimalString(min);
double max = resultSet.getDouble(5);
String maxStr = double2decimalString(max);
Value value = Value.builder().origin(originStr).mean(avgStr).min(minStr).max(maxStr).time(ts)
.build();
values.add(value);
}
resultSet.close();
} catch (Exception e) {
if (log.isErrorEnabled()) {
log.error("[warehouse greptime] failed to getHistoryIntervalMetricData: {}", e.getMessage(), e);
}
}
}
return instanceValuesMap;
}
// TODO(dennis): we can remove it when
// https://github.com/GreptimeTeam/greptimedb/issues/4168 is fixed.
// default 6h-6 hours: s-seconds, M-minutes, h-hours, d-days, w-weeks
private String history2interval(String history) {
if (history == null) {
return null;
}
history = history.trim().toLowerCase();
// Be careful, the order matters.
return history.replaceAll("d", " day") //
.replaceAll("s", " second") //
.replaceAll("w", " week") //
.replaceAll("h", " hour")//
.replaceAll("m", " minute");
if (history == null) {
return null;
}
history = history.trim().toLowerCase();
// Be careful, the order matters.
return history.replaceAll("d", " day") //
.replaceAll("s", " second") //
.replaceAll("w", " week") //
.replaceAll("h", " hour")//
.replaceAll("m", " minute");
}
private String double2decimalString(double d) {
return BigDecimal.valueOf(d).setScale(4, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString();
return BigDecimal.valueOf(d).setScale(4, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString();
}
@Override
public void destroy() {
if (this.greptimeDb != null) {
this.greptimeDb.shutdownGracefully();
this.greptimeDb = null;
}
if (this.hikariDataSource != null) {
this.hikariDataSource.close();
hikariDataSource = null;
}
if (this.greptimeDb != null) {
this.greptimeDb.shutdownGracefully();
this.greptimeDb = null;
}
if (this.hikariDataSource != null) {
this.hikariDataSource.close();
hikariDataSource = null;
}
}
}
@@ -248,9 +248,12 @@ public class IotDbDataStorage extends AbstractHistoryDataStorage {
String label, String history) {
Map<String, List<Value>> instanceValuesMap = new HashMap<>(8);
if (!isServerAvailable()) {
log.error("\n\t---------------IotDb Init Failed---------------\n"
+ "\t--------------Please Config IotDb--------------\n"
+ "\t----------Can Not Use Metric History Now----------\n");
log.error("""
\t---------------IotDb Init Failed---------------
\t--------------Please Config IotDb--------------
\t----------Can Not Use Metric History Now----------
""");
return instanceValuesMap;
}
String deviceId = getDeviceId(app, metrics, monitorId, label, true);
@@ -309,9 +312,12 @@ public class IotDbDataStorage extends AbstractHistoryDataStorage {
String metric, String label, String history) {
Map<String, List<Value>> instanceValuesMap = new HashMap<>(8);
if (!isServerAvailable()) {
log.error("\n\t---------------IotDb Init Failed---------------\n"
+ "\t--------------Please Config IotDb--------------\n"
+ "\t----------Can Not Use Metric History Now----------\n");
log.error("""
\t---------------IotDb Init Failed---------------
\t--------------Please Config IotDb--------------
\t----------Can Not Use Metric History Now----------
""");
return instanceValuesMap;
}
String deviceId = getDeviceId(app, metrics, monitorId, label, true);
@@ -408,7 +414,6 @@ public class IotDbDataStorage extends AbstractHistoryDataStorage {
/**
* use ${group}.${app}.${metrics}.${monitor}.${labels} to get device id if there is a way to get instanceId
* otherwise use ${group}.${app}.${metrics}.${monitor}
*
* Use ${group}.${app}.${metrics}.${monitor}.* to get all instance data when you tend to query
*/
private String getDeviceId(String app, String metrics, Long monitorId, String labels, boolean useQuote) {
@@ -37,6 +37,7 @@ import java.util.Properties;
import java.util.regex.Pattern;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.hertzbeat.common.constants.CommonConstants;
import org.apache.hertzbeat.common.entity.dto.Value;
import org.apache.hertzbeat.common.entity.message.CollectRep;
@@ -337,7 +338,7 @@ public class TdEngineDataStorage extends AbstractHistoryDataStorage {
continue;
}
String instanceValue = resultSet.getString(2);
if (instanceValue == null || "".equals(instanceValue)) {
if (instanceValue == null || StringUtils.isBlank(instanceValue)) {
instanceValue = "";
}
double value = resultSet.getDouble(3);
@@ -385,7 +386,7 @@ public class TdEngineDataStorage extends AbstractHistoryDataStorage {
ResultSet resultSet = statement.executeQuery(queryInstanceSql);
while (resultSet.next()) {
String instanceValue = resultSet.getString(1);
if (instanceValue == null || "".equals(instanceValue)) {
if (instanceValue == null || StringUtils.isBlank(instanceValue)) {
instances.add("''");
} else {
instances.add(instanceValue);
@@ -274,9 +274,12 @@ public class VictoriaMetricsClusterDataStorage extends AbstractHistoryDataStorag
public Map<String, List<Value>> getHistoryIntervalMetricData(Long monitorId, String app, String metrics,
String metric, String label, String history) {
if (!serverAvailable) {
log.error("\n\t---------------VictoriaMetrics Init Failed---------------\n"
+ "\t--------------Please Config VictoriaMetrics--------------\n"
+ "\t----------Can Not Use Metric History Now----------\n");
log.error("""
\t---------------VictoriaMetrics Init Failed---------------
\t--------------Please Config VictoriaMetrics--------------
\t----------Can Not Use Metric History Now----------
""");
return Collections.emptyMap();
}
long endTime = ZonedDateTime.now().toEpochSecond();
@@ -281,9 +281,12 @@ public class VictoriaMetricsDataStorage extends AbstractHistoryDataStorage {
public Map<String, List<Value>> getHistoryIntervalMetricData(Long monitorId, String app, String metrics,
String metric, String label, String history) {
if (!serverAvailable) {
log.error("\n\t---------------VictoriaMetrics Init Failed---------------\n"
+ "\t--------------Please Config VictoriaMetrics--------------\n"
+ "\t----------Can Not Use Metric History Now----------\n");
log.error("""
\t---------------VictoriaMetrics Init Failed---------------
\t--------------Please Config VictoriaMetrics--------------
\t----------Can Not Use Metric History Now----------
""");
return Collections.emptyMap();
}
long endTime = ZonedDateTime.now().toEpochSecond();