This is an automated email from the ASF dual-hosted git repository.
doleyzi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/inlong.git
The following commit(s) were added to refs/heads/master by this push:
new c8a61e40b4 [INLONG-12000][Audit]Implement API for obtaining audit data
in Audit Tool (#12001)
c8a61e40b4 is described below
commit c8a61e40b4e38408757ae10013e3830c322cf2d7
Author: lsq888lsq <[email protected]>
AuthorDate: Sun Sep 14 22:18:47 2025 +0800
[INLONG-12000][Audit]Implement API for obtaining audit data in Audit Tool
(#12001)
* [Feature][Audit]Implement API for obtaining audit data in Audit Tool
* [Feature][Audit]Implement API for obtaining audit data in Audit Tool
* [INLONG-12000][Audit]Implement API for obtaining audit data in Audit Tool
* [INLONG-12000][Audit]Implement API for obtaining audit data in Audit Tool
* [INLONG-12000][Audit]Implement API for obtaining audit data in Audit Tool
* [INLONG-12000][Audit] Remove DTO directory
* [INLONG-12000][Audit]Implement API for obtaining audit data in Audit Tool
---------
Co-authored-by: lsq <[email protected]>
---
.../tool/{DTO => dto}/AuditAlertCondition.java | 0
.../audit/tool/{DTO => dto}/AuditAlertRule.java | 0
.../AuditMetric.java} | 26 ++----
.../inlong/audit/tool/mapper/AuditMapper.java | 36 ++++++++
.../inlong/audit/tool/metric/AuditMetric.java | 37 ++++++++
.../inlong/audit/tool/reporter/MetricReporter.java | 26 ++++++
.../audit/tool/reporter/PrometheusReporter.java | 102 +++++++++++++++++++++
.../audit/tool/service/AuditMetricService.java | 45 +++++++++
.../inlong/audit/tool/util/AuditSQLUtil.java | 81 ++++++++++++++++
.../src/main/resources/application.properties | 6 +-
.../AuditAlertRuleManagerTest.java | 83 -----------------
.../tool/service/AuditMetricServiceTest.java | 72 +++++++++++++++
12 files changed, 408 insertions(+), 106 deletions(-)
diff --git
a/inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/DTO/AuditAlertCondition.java
b/inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/dto/AuditAlertCondition.java
similarity index 100%
copy from
inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/DTO/AuditAlertCondition.java
copy to
inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/dto/AuditAlertCondition.java
diff --git
a/inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/DTO/AuditAlertRule.java
b/inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/dto/AuditAlertRule.java
similarity index 100%
rename from
inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/DTO/AuditAlertRule.java
rename to
inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/dto/AuditAlertRule.java
diff --git
a/inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/DTO/AuditAlertCondition.java
b/inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/entity/AuditMetric.java
similarity index 50%
rename from
inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/DTO/AuditAlertCondition.java
rename to
inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/entity/AuditMetric.java
index 2e5013d3cb..bc435e5ddd 100644
---
a/inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/DTO/AuditAlertCondition.java
+++
b/inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/entity/AuditMetric.java
@@ -15,29 +15,15 @@
* limitations under the License.
*/
-package org.apache.inlong.audit.tool.dto;
+package org.apache.inlong.audit.tool.entity;
-import io.swagger.annotations.ApiModel;
-import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
-import javax.validation.constraints.NotBlank;
-import javax.validation.constraints.NotNull;
-
@Data
-@ApiModel("Audit Alert Condition")
-public class AuditAlertCondition {
-
- @ApiModelProperty(value = "Condition type (e.g., data_loss, delay,
count)", required = true)
- @NotBlank(message = "Condition type cannot be blank")
- private String type;
-
- @ApiModelProperty(value = "Operator for comparison (e.g., >, <, >=, <=,
==, !=)", required = true)
- @NotBlank(message = "Operator cannot be blank")
- private String operator;
+public class AuditMetric {
- @ApiModelProperty(value = "Value for comparison", required = true)
- @NotNull(message = "Value cannot be null")
- private Double value;
+ private String inlongGroupId;
+ private String inlongStreamId;
+ private long count;
-}
\ No newline at end of file
+}
diff --git
a/inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/mapper/AuditMapper.java
b/inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/mapper/AuditMapper.java
new file mode 100644
index 0000000000..7e2fbf350d
--- /dev/null
+++
b/inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/mapper/AuditMapper.java
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.inlong.audit.tool.mapper;
+
+import org.apache.inlong.audit.tool.entity.AuditMetric;
+
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Select;
+
+import java.util.List;
+
+@Mapper
+public interface AuditMapper {
+
+ @Select("select inlong_group_id as inlongGroupId,inlong_stream_id as
inlongStreamId, sum(count) count" +
+ " from audit_data where audit_id = #{audit_id}" +
+ " and log_ts between #{startLogTs} and #{endLogTs} group by
inlong_group_id,inlong_stream_id")
+ List<AuditMetric> getAuditMetrics(@Param("startLogTs") String startLogTs,
@Param("endLogTs") String endLogTs,
+ @Param("audit_id") String auditId);
+}
diff --git
a/inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/metric/AuditMetric.java
b/inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/metric/AuditMetric.java
new file mode 100644
index 0000000000..1538e24037
--- /dev/null
+++
b/inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/metric/AuditMetric.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.inlong.audit.tool.metric;
+
+import io.prometheus.client.CollectorRegistry;
+import io.prometheus.client.Gauge;
+
+public class AuditMetric {
+
+ private final Gauge sourceAndSinkAuditDiffMetric;
+
+ public AuditMetric(CollectorRegistry registry) {
+ this.sourceAndSinkAuditDiffMetric = Gauge.build()
+ .name("inlong_source_sink_diff")
+ .help("The difference in count between inflow and outflow")
+ .register(registry);
+ }
+
+ public void updateSourceAndSinkAuditDiffMetric(double diff) {
+ sourceAndSinkAuditDiffMetric.set(diff);
+ }
+}
diff --git
a/inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/reporter/MetricReporter.java
b/inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/reporter/MetricReporter.java
new file mode 100644
index 0000000000..73d2761e1d
--- /dev/null
+++
b/inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/reporter/MetricReporter.java
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.inlong.audit.tool.reporter;
+
+import java.util.Map;
+
+public interface MetricReporter {
+
+ void init(Map<String, Object> config);
+
+}
\ No newline at end of file
diff --git
a/inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/reporter/PrometheusReporter.java
b/inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/reporter/PrometheusReporter.java
new file mode 100644
index 0000000000..2260810b3a
--- /dev/null
+++
b/inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/reporter/PrometheusReporter.java
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.inlong.audit.tool.reporter;
+
+import org.apache.inlong.audit.tool.metric.AuditMetric;
+
+import io.prometheus.client.CollectorRegistry;
+import io.prometheus.client.Gauge;
+import io.prometheus.client.exporter.HTTPServer;
+import lombok.Getter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.Map;
+
+import static
org.apache.inlong.audit.tool.config.ConfigConstants.AUDIT_TOOL_ALERTS_TOTAL;
+import static
org.apache.inlong.audit.tool.config.ConfigConstants.AUDIT_TOOL_DATA_LOSS_RATE;
+import static
org.apache.inlong.audit.tool.config.ConfigConstants.DEFAULT_PROMETHEUS_PORT;
+import static
org.apache.inlong.audit.tool.config.ConfigConstants.DESC_AUDIT_TOOL_ALERTS_TOTAL;
+import static
org.apache.inlong.audit.tool.config.ConfigConstants.DESC_AUDIT_TOOL_DATA_LOSS_RATE;
+import static
org.apache.inlong.audit.tool.config.ConfigConstants.KEY_ALERT_TYPE;
+import static org.apache.inlong.audit.tool.config.ConfigConstants.KEY_GROUP_ID;
+import static
org.apache.inlong.audit.tool.config.ConfigConstants.KEY_PROMETHEUS_PORT;
+import static
org.apache.inlong.audit.tool.config.ConfigConstants.KEY_STREAM_ID;
+
+/**
+ * PrometheusReporter implements the MetricReporter interface to expose audit
tool metrics
+ * to a Prometheus monitoring system.
+ */
+public class PrometheusReporter implements MetricReporter {
+
+ private static final Logger LOGGER =
LoggerFactory.getLogger(PrometheusReporter.class);
+
+ private final CollectorRegistry registry;
+ @Getter
+ private final AuditMetric auditMetric;
+
+ /**
+ * Constructor for PrometheusReporter.
+ * Initializes a new CollectorRegistry for DTO isolation.
+ */
+ public PrometheusReporter() {
+ this.registry = new CollectorRegistry();
+ auditMetric = new AuditMetric(registry);
+ }
+
+ /**
+ * Initializes the Prometheus reporter.
+ * Starts an HTTP server to expose metrics and registers the gauges.
+ *
+ * @param config A map containing configuration for the reporter, e.g.,
port.
+ */
+ @Override
+ public void init(Map<String, Object> config) {
+ // It's better to get configuration from the passed map than a global
singleton.
+ int port = (int) config.getOrDefault(KEY_PROMETHEUS_PORT,
DEFAULT_PROMETHEUS_PORT);
+ try {
+ // Start the Prometheus HTTP server on the configured port with
our registry.
+ HTTPServer server = new HTTPServer.Builder()
+ .withPort(port)
+ .withRegistry(registry)
+ .build();
+ LOGGER.info("Prometheus server started on port {}", port);
+
+ // Define and register the 'alerts total' gauge.
+ Gauge alertGauge = Gauge.build()
+ .name(AUDIT_TOOL_ALERTS_TOTAL)
+ .help(DESC_AUDIT_TOOL_ALERTS_TOTAL)
+ .labelNames(KEY_GROUP_ID, KEY_STREAM_ID, KEY_ALERT_TYPE)
+ .register(registry); // Register with our specific
registry instance
+
+ // Define and register the 'data loss rate' gauge.
+ Gauge dataLossRateGauge = Gauge.build()
+ .name(AUDIT_TOOL_DATA_LOSS_RATE)
+ .help(DESC_AUDIT_TOOL_DATA_LOSS_RATE)
+ .labelNames(KEY_GROUP_ID, KEY_STREAM_ID)
+ .register(registry); // Register with our specific
registry instance
+
+ } catch (IOException e) {
+ LOGGER.error("Failed to start Prometheus server on port {}", port,
e);
+ // Throwing a runtime exception is appropriate if the reporter
cannot start.
+ throw new RuntimeException("Failed to start Prometheus server", e);
+ }
+ }
+
+}
diff --git
a/inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/service/AuditMetricService.java
b/inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/service/AuditMetricService.java
new file mode 100644
index 0000000000..0be4e2fd1a
--- /dev/null
+++
b/inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/service/AuditMetricService.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.inlong.audit.tool.service;
+
+import org.apache.inlong.audit.tool.entity.AuditMetric;
+import org.apache.inlong.audit.tool.mapper.AuditMapper;
+import org.apache.inlong.audit.tool.util.AuditSQLUtil;
+
+import org.apache.ibatis.session.SqlSession;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Collections;
+import java.util.List;
+
+public class AuditMetricService {
+
+ private static final Logger LOGGER =
LoggerFactory.getLogger(AuditMetricService.class);
+
+ public List<AuditMetric> getStorageAuditMetrics(String auditId, String
startLogTs, String endLogTs) {
+ try (SqlSession sqlSession = AuditSQLUtil.getSqlSession()) {
+ AuditMapper auditMapper = sqlSession.getMapper(AuditMapper.class);
+ return auditMapper.getAuditMetrics(startLogTs, endLogTs, auditId);
+ } catch (Exception e) {
+ LOGGER.error("Exception occurred during database query: ", e);
+ return Collections.emptyList();
+ }
+ }
+
+}
\ No newline at end of file
diff --git
a/inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/util/AuditSQLUtil.java
b/inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/util/AuditSQLUtil.java
new file mode 100644
index 0000000000..c48190336b
--- /dev/null
+++
b/inlong-audit/audit-tool/src/main/java/org/apache/inlong/audit/tool/util/AuditSQLUtil.java
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.inlong.audit.tool.util;
+
+import org.apache.inlong.audit.tool.mapper.AuditMapper;
+
+import org.apache.ibatis.datasource.pooled.PooledDataSource;
+import org.apache.ibatis.session.SqlSession;
+import org.apache.ibatis.session.SqlSessionFactory;
+import org.apache.ibatis.session.SqlSessionFactoryBuilder;
+
+import javax.sql.DataSource;
+
+import java.util.Properties;
+
+public class AuditSQLUtil {
+
+ private static SqlSessionFactory sqlSessionFactory;
+ private static Properties appProperties;
+
+ public static void initialize(Properties properties) {
+ appProperties = properties;
+ try {
+ // Create a data source
+ DataSource dataSource = createDataSourceFromProperties();
+
+ // Create a SqlSessionFactory
+ org.apache.ibatis.session.Configuration configuration = new
org.apache.ibatis.session.Configuration();
+ configuration.setEnvironment(new
org.apache.ibatis.mapping.Environment(
+ "development",
+ new
org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory(),
+ dataSource));
+
+ configuration.addMapper(AuditMapper.class);
+
+ sqlSessionFactory = new
SqlSessionFactoryBuilder().build(configuration);
+
+ } catch (Exception e) {
+ throw new RuntimeException("Error initializing MyBatis with
application properties", e);
+ }
+ }
+
+ private static DataSource createDataSourceFromProperties() {
+ String url = appProperties.getProperty("audit.data.source.url");
+ String username =
appProperties.getProperty("audit.data.source.username");
+ String password =
appProperties.getProperty("audit.data.source.password");
+
+ PooledDataSource dataSource = new PooledDataSource();
+ dataSource.setDriver("com.mysql.cj.jdbc.Driver");
+ dataSource.setUrl(url);
+ dataSource.setUsername(username);
+ dataSource.setPassword(password);
+
+ dataSource.setPoolMaximumActiveConnections(10);
+ dataSource.setPoolMaximumIdleConnections(5);
+
+ return dataSource;
+ }
+
+ public static SqlSession getSqlSession() {
+ if (sqlSessionFactory == null) {
+ throw new IllegalStateException("MyBatisUtil not initialized. Call
initialize() first.");
+ }
+ return sqlSessionFactory.openSession();
+ }
+}
\ No newline at end of file
diff --git a/inlong-audit/audit-tool/src/main/resources/application.properties
b/inlong-audit/audit-tool/src/main/resources/application.properties
index 998e9e18d1..e357739ceb 100644
--- a/inlong-audit/audit-tool/src/main/resources/application.properties
+++ b/inlong-audit/audit-tool/src/main/resources/application.properties
@@ -41,7 +41,7 @@ logging.file.path=/var/log/inlong/audit-tool.log
# Audit Database settings
audit.data.source=apache_inlong_audit
-audit.data.source.url=jdbc:mysql://192.168.10.3:3306/apache_inlong_audit
+audit.data.source.url=jdbc:mysql://127.0.0.1:3306/apache_inlong_audit
audit.data.source.username=root
audit.data.source.password=inlong
audit.data.time.interval.minute=1
@@ -49,5 +49,5 @@ audit.data.time.delay.minute=1
audit.id.source=5
# Audit SecretId SecretKey
-audit.secretId = admin
-audit.secretKey = inlong
\ No newline at end of file
+audit.secretId=xxx
+audit.secretKey=xxx
\ No newline at end of file
diff --git
a/inlong-audit/audit-tool/src/test/java/org/apache/inlong/tool/AuditAlertRuleManager/AuditAlertRuleManagerTest.java
b/inlong-audit/audit-tool/src/test/java/org/apache/inlong/tool/AuditAlertRuleManager/AuditAlertRuleManagerTest.java
deleted file mode 100644
index 19c0cff240..0000000000
---
a/inlong-audit/audit-tool/src/test/java/org/apache/inlong/tool/AuditAlertRuleManager/AuditAlertRuleManagerTest.java
+++ /dev/null
@@ -1,83 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.inlong.tool.AuditAlertRuleManager;
-
-import org.apache.inlong.audit.tool.config.AppConfig;
-import org.apache.inlong.audit.tool.dto.AuditAlertRule;
-import org.apache.inlong.audit.tool.manager.AuditAlertRuleManager;
-import org.apache.inlong.audit.tool.response.Response;
-
-import org.junit.jupiter.api.Test;
-
-import java.util.Collections;
-import java.util.List;
-
-import static org.junit.jupiter.api.Assertions.*;
-
-class AuditAlertRuleManagerTest {
-
- private final AuditAlertRuleManager auditAlertRuleManager =
AuditAlertRuleManager.getInstance();
-
- @Test
- void testFetchAlertRulesFromManager() {
- // Mock data
- AuditAlertRule rule = new AuditAlertRule();
- rule.setId(1);
- rule.setInlongGroupId("group1");
-
- Response<List<AuditAlertRule>> mockResponse = new Response<>();
- mockResponse.setSuccess(true);
- mockResponse.setData(Collections.singletonList(rule));
-
- try {
- // Execute
- auditAlertRuleManager.init(new AppConfig());
- List<AuditAlertRule> result =
auditAlertRuleManager.fetchAlertRulesFromManager();
-
- // Verify
- assertNotNull(result);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- @Test
- void testFetchAuditIds() {
- // Mock alert rules
- AuditAlertRule rule = new AuditAlertRule();
- rule.setInlongGroupId("group1");
- rule.setInlongStreamId("stream1");
- rule.setAuditId("1,2,3");
-
- Response<List<AuditAlertRule>> alertRulesResponse = new Response<>();
- alertRulesResponse.setSuccess(true);
- alertRulesResponse.setData(Collections.singletonList(rule));
-
- try {
- // Execute
- auditAlertRuleManager.init(new AppConfig());
- List<String> result = auditAlertRuleManager.getAuditIds();
-
- // Verify
- assertNotNull(result);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-}
\ No newline at end of file
diff --git
a/inlong-audit/audit-tool/src/test/java/org/apache/inlong/tool/service/AuditMetricServiceTest.java
b/inlong-audit/audit-tool/src/test/java/org/apache/inlong/tool/service/AuditMetricServiceTest.java
new file mode 100644
index 0000000000..97e7349dd5
--- /dev/null
+++
b/inlong-audit/audit-tool/src/test/java/org/apache/inlong/tool/service/AuditMetricServiceTest.java
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.inlong.tool.service;
+
+import org.apache.inlong.audit.tool.config.AppConfig;
+import org.apache.inlong.audit.tool.entity.AuditMetric;
+import org.apache.inlong.audit.tool.mapper.AuditMapper;
+import org.apache.inlong.audit.tool.service.AuditMetricService;
+import org.apache.inlong.audit.tool.util.AuditSQLUtil;
+
+import org.apache.ibatis.session.SqlSession;
+import org.junit.jupiter.api.Test;
+import org.mockito.MockedStatic;
+import org.mockito.Mockito;
+
+import java.util.Collections;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+public class AuditMetricServiceTest {
+
+ @Test
+ public void getStorageAuditMetricsReturnsEmptyListWhenNoDataFound() {
+ AuditMetricService auditMetricService = new AuditMetricService();
+
+ try (MockedStatic<AuditSQLUtil> sqlUtilMockedStatic =
Mockito.mockStatic(AuditSQLUtil.class)) {
+ SqlSession sqlSessionMock = Mockito.mock(SqlSession.class);
+ AuditMapper auditMapperMock = Mockito.mock(AuditMapper.class);
+
+
sqlUtilMockedStatic.when(AuditSQLUtil::getSqlSession).thenReturn(sqlSessionMock);
+
Mockito.when(sqlSessionMock.getMapper(AuditMapper.class)).thenReturn(auditMapperMock);
+ Mockito.when(auditMapperMock.getAuditMetrics(Mockito.anyString(),
Mockito.anyString(), Mockito.anyString()))
+ .thenReturn(Collections.emptyList());
+
+ List<AuditMetric> result =
auditMetricService.getStorageAuditMetrics("nonexistentId", "2023-01-01
00:00:00",
+ "2023-01-01 01:00:00");
+ assertTrue(result.isEmpty());
+ }
+ }
+
+ @Test
+ public void getStorageAuditMetricsHandlesInvalidTimestampsGracefully() {
+ // Query service initialization
+ AppConfig appConfig = new AppConfig();
+ AuditSQLUtil.initialize(appConfig.getProperties());
+ AuditMetricService auditMetricService = new AuditMetricService();
+
+ String invalidStartLogTs = "invalid-timestamp";
+ String invalidEndLogTs = "invalid-timestamp";
+
+ List<AuditMetric> result =
auditMetricService.getStorageAuditMetrics("5", invalidStartLogTs,
invalidEndLogTs);
+
+ assertTrue(result.isEmpty());
+ }
+
+}