统一全系统资源加载、轻量趋势与缓存恢复

This commit is contained in:
2026-11-02 09:54:01 +08:00
parent 8b8f9b7728
commit b711327c8e
58 changed files with 3150 additions and 661 deletions
@@ -5,6 +5,7 @@ import cn.yunsheng.quality.identity.AppUserPrincipal;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.HexFormat;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.springframework.jdbc.core.JdbcTemplate;
@@ -26,14 +27,25 @@ public class WorkspaceStateController {
var policy = jdbc.queryForList("SELECT detail_allowed,evidence_allowed,original_download_allowed,CAST(allowed_fields AS CHAR) AS allowed_fields,valid_until,inherit_department_children,row_version FROM user_access_policy WHERE user_id=?", actor.getId().toString());
String auth = digest(List.of(actor.getId(), actor.getRoleCodes().stream().sorted().toList(),
actor.getDepartments().stream().sorted().toList(), actor.getGroups().stream().sorted().toList(), policy));
var currentRelease = jdbc.queryForList("SELECT * FROM current_release ORDER BY dataset_id");
String currentReleaseId = currentRelease.stream()
.filter(row -> "quality".equals(String.valueOf(row.get("dataset_id"))))
.map(row -> row.get("release_id"))
.filter(java.util.Objects::nonNull)
.map(String::valueOf)
.findFirst().orElse(null);
String data = digest(List.of(
jdbc.queryForList("SELECT * FROM current_release ORDER BY dataset_id"),
currentRelease,
jdbc.queryForList("SELECT * FROM platform_metadata ORDER BY metadata_key"),
jdbc.queryForList("SELECT id,row_version FROM target_version WHERE status='ACTIVE' ORDER BY id"),
jdbc.queryForList("SELECT * FROM query_filter_field ORDER BY field_key"),
jdbc.queryForList("SELECT id,row_version FROM service_identity ORDER BY id"),
jdbc.queryForList("SELECT * FROM service_binding ORDER BY service_type")));
return Map.of("authRevision", auth, "dataRevision", data);
Map<String, String> result = new LinkedHashMap<>();
result.put("authRevision", auth);
result.put("dataRevision", data);
result.put("currentReleaseId", currentReleaseId);
return result;
}
private static String digest(Object value) {
try { return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(value.toString().getBytes(StandardCharsets.UTF_8))); }
@@ -76,6 +76,37 @@ public class MetricsController {
extraFilters, exclusions, columnFilters, stage, actor);
}
/** 轻量趋势接口:查询参数与正式指标接口一致,但只返回版本、上下文、趋势和覆盖信息。 */
@GetMapping("/trend")
@PreAuthorize("hasAnyRole('SYSTEM_ADMIN','QUALITY_VIEWER','BUSINESS_ANALYST')")
public MetricsService.MetricTrendSnapshot trendHttp(@RequestParam(required = false) Integer year,
@RequestParam(required = false) Integer month,
@RequestParam(required = false) Integer monthStart,
@RequestParam(required = false) Integer monthEnd,
@RequestParam(required = false) String quarter,
@RequestParam(required = false) String department,
@RequestParam(required = false) String group,
@RequestParam(required = false) String product,
@RequestParam(required = false) String productSearch,
@RequestParam(required = false) String projectGroup,
@RequestParam(defaultValue = "overall") String view,
@RequestParam(defaultValue = "true") boolean includeUnassigned,
@RequestParam(required = false) String releaseId,
@RequestParam(defaultValue = "month") String trendGranularity,
@RequestParam(required = false) String extraFilters,
@RequestParam(required = false) String exclusions,
@RequestParam(required = false) String columnFilters,
@RequestParam(required = false) String stage,
@AuthenticationPrincipal AppUserPrincipal actor) {
Integer quarterNumber = parseQuarter(quarter);
MetricsService.MetricsQuery query = new MetricsService.MetricsQuery(year, month, quarterNumber, department,
group, product, projectGroup, view, includeUnassigned, releaseId, trendGranularity,
parseExclusions(exclusions));
query = query.withMonthRange(monthStart, monthEnd).withExtraFilters(extraFilters)
.withProductSearch(productSearch).withColumnFilters(columnFilters).withStage(stage);
return responseAccess.visibleTrend(metrics.calculateTrend(query, actor), actor);
}
/** Existing direct callers retain the pre-range controller signature. */
public MetricsService.MetricSnapshot calculate(Integer year, Integer month, String quarter,
String department, String group, String product, String productSearch,
@@ -5,6 +5,7 @@ import cn.yunsheng.quality.identity.AppUserPrincipal;
import cn.yunsheng.quality.metrics.MetricsService.DefectRow;
import cn.yunsheng.quality.metrics.MetricsService.MetricRow;
import cn.yunsheng.quality.metrics.MetricsService.MetricSnapshot;
import cn.yunsheng.quality.metrics.MetricsService.MetricTrendSnapshot;
import cn.yunsheng.quality.metrics.MetricsService.MetricStage;
import cn.yunsheng.quality.metrics.MetricsService.TrendMetricRow;
import java.util.List;
@@ -30,6 +31,25 @@ public class MetricsResponseAccess {
return new MetricSnapshot(filtered.release(), filtered.context(), hideDefects(filtered.summary()), List.of(), List.of(), defects, filtered.coverage());
}
/** 趋势查询沿用明细授权和指标字段可见性边界。 */
public MetricTrendSnapshot visibleTrend(MetricTrendSnapshot snapshot, AppUserPrincipal actor) {
governance.requireUserActivePolicy(actor);
if (!Boolean.TRUE.equals(governance.policy(actor.getId().toString()).get("detailAllowed")))
return new MetricTrendSnapshot(snapshot.release(), snapshot.context(), List.of(), snapshot.coverage());
governance.requireBoundAction("QUERY", "query.detail");
Set<String> allowed = governance.allowedFields(actor);
List<TrendMetricRow> trend = allowed.contains("*") ? snapshot.trend()
: snapshot.trend().stream().map(row -> restrictTrend(row, allowed)).toList();
return new MetricTrendSnapshot(snapshot.release(), snapshot.context(), trend, snapshot.coverage());
}
private TrendMetricRow restrictTrend(TrendMetricRow row, Set<String> allowed) {
return new TrendMetricRow(row.month(), row.key(), row.label(), row.department(), row.group(), row.product(),
row.projectGroup(), row.compositeRate(), restrictDefects(row.black(), "black", allowed),
restrictDefects(row.bright(), "bright", allowed), row.coverage(),
row.compositeRateWithoutHeadquartersScrap(), row.period(), row.periodLabel(), row.granularity());
}
private MetricRow hideDefects(MetricRow row) {
return new MetricRow(row.key(), row.label(), row.department(), row.group(), row.product(), row.projectGroup(), row.compositeRate(),
hideDefects(row.black()), hideDefects(row.bright()), row.coverage(), row.compositeRateWithoutHeadquartersScrap());
@@ -71,6 +71,113 @@ public class MetricsService {
return calculateForRelease(query, actor, release);
}
/** 只按时间桶生成趋势,不构造总体摘要或产品/分组维度行。 */
public MetricTrendSnapshot calculateTrend(MetricsQuery query, AppUserPrincipal actor) {
if (actor == null) throw new ResponseStatusException(HttpStatus.FORBIDDEN, "指标查询必须绑定当前有效用户");
governance.requireUserActivePolicy(actor);
governance.requireBoundAction("QUERY", "query.aggregate");
validate(query);
String releaseId = query.releaseId() == null || query.releaseId().isBlank()
? currentReleaseId() : query.releaseId();
if (releaseId == null) return emptyTrendSnapshot(query);
ReleaseInfo release = release(releaseId, actor);
return withConsistentSnapshot(() -> calculateTrendForReleaseSnapshot(query, actor, release));
}
private MetricTrendSnapshot calculateTrendForReleaseSnapshot(MetricsQuery query, AppUserPrincipal actor,
ReleaseInfo release) {
boolean hasUnrolledBuilds = hasUnrolledBuilds(release.id());
Map<String, AggregateStats> periods = queryTrendAggregates(release.id(), query, actor, hasUnrolledBuilds);
String ruleVersion = jdbc.queryForObject("SELECT rule_version FROM data_release WHERE id=?", String.class, release.id());
ConfigurableRuleEngine rules = rulesForVersion(ruleVersion);
List<TrendMetricRow> trend = trendRows(periods, query.trendGranularity()).stream().map(row -> {
MetricRow adjusted = rules.applyMetrics(new MetricRow(row.key(), row.label(), row.department(), row.group(),
row.product(), row.projectGroup(), row.compositeRate(), row.black(), row.bright(), row.coverage(),
row.compositeRateWithoutHeadquartersScrap()));
return new TrendMetricRow(row.month(), adjusted.key(), adjusted.label(), adjusted.department(), adjusted.group(),
adjusted.product(), adjusted.projectGroup(), adjusted.compositeRate(), adjusted.black(), adjusted.bright(),
adjusted.coverage(), adjusted.compositeRateWithoutHeadquartersScrap(), row.period(), row.periodLabel(),
row.granularity());
}).toList();
Map<String, Object> coverage = queryCoverage(release.id(), query, actor, hasUnrolledBuilds);
Map<String, Object> context = context(query, query.includeUnassigned());
context.put("ruleVersionId", ruleVersion == null ? BusinessRowMapper.RULE_VERSION : ruleVersion);
context.put("calculationRules", rules.calculationRules());
context.put("metricFormulaDescriptions", rules.formulaDescriptions());
return new MetricTrendSnapshot(release.asMap(), context, trend, coverage);
}
private Map<String, AggregateStats> queryTrendAggregates(String releaseId, MetricsQuery query,
AppUserPrincipal actor, boolean hasUnrolledBuilds) {
Map<String, AggregateStats> periods = new TreeMap<>();
boolean hasSourceFilters = !query.extraFilters().isEmpty() || !query.columnFilters().isEmpty();
if (!hasSourceFilters) {
aggregateTrendStage(periods, "black", releaseId, query, actor, false);
aggregateTrendStage(periods, "bright", releaseId, query, actor, false);
}
if (hasUnrolledBuilds || hasSourceFilters) {
aggregateTrendStage(periods, "black", releaseId, query, actor, true);
aggregateTrendStage(periods, "bright", releaseId, query, actor, true);
}
return periods;
}
private void aggregateTrendStage(Map<String, AggregateStats> periods, String stage, String releaseId,
MetricsQuery query, AppUserPrincipal actor, boolean factFallback) {
if (query.stage() != null && !query.stage().equals(stage)) return;
String table = factFallback ? (stage.equals("black") ? "fact_black" : "fact_bright") : "metric_rollup";
StringBuilder where = new StringBuilder(" WHERE rm.release_id=?");
List<Object> args = new ArrayList<>();
args.add(releaseId);
boolean hasSourceFilters = !query.extraFilters().isEmpty() || !query.columnFilters().isEmpty();
if (factFallback && !hasSourceFilters)
where.append(" AND NOT EXISTS (SELECT 1 FROM metric_rollup_build rb WHERE rb.build_id=f.build_id)");
else if (!factFallback) { where.append(" AND f.stage=?"); args.add(stage); }
appendTime(where, args, query);
appendDimensions(where, args, query);
appendExclusions(where, args, query);
if (factFallback) {
new QueryFilterFields(jdbc, governance).append(where, args, stage, query.extraFilters(), actor);
if (!query.columnFilters().isEmpty())
new SourceColumnQueries(jdbc, governance, JsonMapper.builder().build())
.append(where, args, stage, query.columnFilters(), releaseId, actor);
}
DataScope.append(where, args, actor, "f.department COLLATE utf8mb4_0900_as_cs",
"f.group_name COLLATE utf8mb4_0900_as_cs");
if (!effectiveIncludeUnassigned(query)) where.append(" AND (f.group_name IS NOT NULL AND f.group_name<>'')");
String countExpression = factFallback ? "COUNT(*)" : "SUM(f.source_row_count)";
String sql = """
SELECT %s AS trend_period,
SUM(f.qualified_weight) AS qualified_weight,
SUM(f.scrap_weight) AS scrap_weight,
SUM(f.total_weight) AS total_weight,
%s AS black_responsibility_scrap,
%s AS black_material_scrap,
%s AS black_external_scrap,
%s AS bright_headquarters_scrap,
%s AS bright_plating_scrap,
%s AS bright_material_scrap,
%s AS bright_processing_scrap,
%s AS bright_diffusion_scrap,
%s AS row_count
FROM release_member rm JOIN %s f ON f.build_id=rm.transform_build_id
%s
GROUP BY trend_period
ORDER BY trend_period
""".formatted(trendBucketExpression(query.trendGranularity()),
componentSum("black_responsibility_scrap"), componentSum("black_material_scrap"),
componentSum("black_external_scrap"), componentSum("bright_headquarters_scrap"),
componentSum("bright_plating_scrap"), componentSum("bright_material_scrap"),
componentSum("bright_processing_scrap"), componentSum("bright_diffusion_scrap"),
countExpression, table, where);
jdbc.query(sql, (RowCallbackHandler) rs -> {
String period = rs.getString("trend_period");
AggregateStats stats = new AggregateStats().read(rs, stage);
periods.computeIfAbsent(period, ignored -> new AggregateStats()).merge(stage, stats);
}, args.toArray());
}
/** Candidate and historical release path reserved for the explicit governance reconciliation flow. */
public MetricSnapshot calculateCandidateForGovernance(MetricsQuery query, AppUserPrincipal actor) {
return withGovernanceSnapshot(() -> calculateForGovernance(query, actor, true));
@@ -393,8 +500,12 @@ public class MetricsService {
}
private List<TrendMetricRow> trendRows(AggregateSet aggregates, String granularity) {
return trendRows(aggregates.periods(), granularity);
}
private List<TrendMetricRow> trendRows(Map<String, AggregateStats> periods, String granularity) {
List<TrendMetricRow> trend = new ArrayList<>();
aggregates.periods().forEach((period, stats) -> {
periods.forEach((period, stats) -> {
String label = switch (granularity) {
case "quarter" -> period.replace("-Q", "年第") + "季度";
case "year" -> period + "年";
@@ -688,6 +799,12 @@ public class MetricsService {
Map.of("black", empty.black().defects(), "bright", empty.bright().defects()), coverage);
}
private MetricTrendSnapshot emptyTrendSnapshot(MetricsQuery query) {
Map<String, Object> coverage = Map.of("rawRows", 0, "includedRows", 0, "excludedBlankRows", 0,
"unmappedRows", 0, "projectGroupUnconfiguredRows", 0, "missingStages", List.of(), "sourceMonths", List.of());
return new MetricTrendSnapshot(null, context(query, query.includeUnassigned()), List.of(), coverage);
}
void validate(MetricsQuery query) {
if (query.year() != null && (query.year() < 2000 || query.year() > 2100)) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "年份无效");
if (query.month() != null && (query.month() < 1 || query.month() > 12)) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "月份无效");
@@ -831,6 +948,8 @@ public class MetricsService {
public record MetricSnapshot(Map<String, Object> release, Map<String, Object> context, MetricRow summary,
List<MetricRow> rows, List<TrendMetricRow> trend, Map<String, Object> defects,
Map<String, Object> coverage) {}
public record MetricTrendSnapshot(Map<String, Object> release, Map<String, Object> context,
List<TrendMetricRow> trend, Map<String, Object> coverage) {}
public record ComparisonSnapshots(MetricSnapshot current, MetricSnapshot comparison) {}
private record Fact(String stage, String month, String department, String group, String projectGroup, String product,
@@ -2,6 +2,13 @@ server:
address: 127.0.0.1
port: ${PORT:5281}
shutdown: graceful
# 只压缩较大的 JSON 响应;SSE、表格和压缩包不在类型白名单中。
compression:
enabled: true
min-response-size: 1KB
mime-types:
- application/json
- application/problem+json
servlet:
session:
cookie:
@@ -20,7 +20,9 @@ class WorkspaceStateControllerTest {
when(jdbc.queryForList(contains("user_access_policy"), any(Object[].class))).thenReturn(List.of(Map.of("allowed_fields","private-field")));
var first = controller.state(actor(List.of("A","B")));
assertEquals(first, controller.state(actor(List.of("B","A"))));
assertTrue(first.values().stream().allMatch(value -> value.matches("[a-f0-9]{64}")));
assertTrue(first.get("authRevision").matches("[a-f0-9]{64}"));
assertTrue(first.get("dataRevision").matches("[a-f0-9]{64}"));
assertNull(first.get("currentReleaseId"));
assertFalse(first.toString().contains("private-field"));
verify(access,times(2)).requireUserActivePolicy(any());
}
@@ -41,4 +43,17 @@ class WorkspaceStateControllerTest {
assertThrows(IllegalStateException.class,()->controller.state(actor));
verifyNoInteractions(jdbc);
}
@Test void currentReleaseIdMatchesTheReleaseSnapshotIncludedInDataRevision() {
String query = "SELECT * FROM current_release ORDER BY dataset_id";
when(jdbc.queryForList(query)).thenReturn(List.of(Map.of("dataset_id", "quality", "release_id", "release-a")))
.thenReturn(List.of(Map.of("dataset_id", "quality", "release_id", "release-b")));
var first = controller.state(actor(List.of("A")));
var second = controller.state(actor(List.of("A")));
assertEquals("release-a", first.get("currentReleaseId"));
assertEquals("release-b", second.get("currentReleaseId"));
assertNotEquals(first.get("dataRevision"), second.get("dataRevision"));
verify(jdbc, times(2)).queryForList(query);
verify(access, times(2)).requireUserActivePolicy(any());
}
}
@@ -66,4 +66,22 @@ class MetricsResponseAccessTest {
when(governance.allowedFields(actor)).thenReturn(Set.of("*"));
assertSame(source, new MetricsResponseAccess(governance).visible(source, actor));
}
@Test void lightweightTrendStillHonorsDetailAndMetricFieldPermissions() {
var trend = new MetricTrendSnapshot(Map.of("id", "release-a"), Map.of("view", "product"),
source.trend(), Map.of("rawRows", 10));
when(governance.policy(actor.getId().toString())).thenReturn(Map.of("detailAllowed", true));
when(governance.allowedFields(actor)).thenReturn(Set.of("black_total_weight"));
MetricTrendSnapshot visible = new MetricsResponseAccess(governance).visibleTrend(trend, actor);
assertEquals(1, visible.trend().size());
assertNull(visible.trend().getFirst().black().qualifiedWeight());
assertNull(visible.trend().getFirst().black().scrapWeight());
assertEquals(stage.totalWeight(), visible.trend().getFirst().black().totalWeight());
clearInvocations(governance);
when(governance.policy(actor.getId().toString())).thenReturn(Map.of("detailAllowed", false));
MetricTrendSnapshot aggregateOnly = new MetricsResponseAccess(governance).visibleTrend(trend, actor);
assertTrue(aggregateOnly.trend().isEmpty());
verify(governance, never()).requireBoundAction("QUERY", "query.detail");
}
}
@@ -0,0 +1,54 @@
package cn.yunsheng.quality.metrics;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import cn.yunsheng.quality.identity.AppUserPrincipal;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
class MetricsTrendControllerTest {
@Test void trendRouteCarriesEveryPeriodFilterStageExclusionAndSourceFilterIntoMetrics() {
MetricsService metrics = mock(MetricsService.class);
MetricsResponseAccess access = mock(MetricsResponseAccess.class);
AppUserPrincipal actor = new AppUserPrincipal(UUID.randomUUID(), "trend", "unused", "趋势", true, false,
List.of("QUALITY_VIEWER"), List.of("D"), List.of("G"), List.of());
MetricsService.MetricTrendSnapshot response = new MetricsService.MetricTrendSnapshot(
Map.of(), Map.of(), List.of(), Map.of());
when(metrics.calculateTrend(any(), eq(actor))).thenReturn(response);
when(access.visibleTrend(response, actor)).thenReturn(response);
MetricsService.MetricTrendSnapshot actual = new MetricsController(metrics, access).trendHttp(
2026, null, 3, 9, null, "D", "G", "P", "18X15", "PG", "product", false,
"release-a", "quarter", "{\"customerCode\":[\"000123\"]}",
"{\"product\":[\"P2\"]}",
"[{\"field\":\"src_any\",\"stages\":[\"black\"],\"operator\":\"in\",\"values\":[\"000123\"]}]",
"black", actor);
assertEquals(response, actual);
ArgumentCaptor<MetricsService.MetricsQuery> query = ArgumentCaptor.forClass(MetricsService.MetricsQuery.class);
verify(metrics).calculateTrend(query.capture(), eq(actor));
MetricsService.MetricsQuery value = query.getValue();
assertEquals(2026, value.year());
assertEquals(3, value.monthStart());
assertEquals(9, value.monthEnd());
assertEquals("D", value.department());
assertEquals("G", value.group());
assertEquals("P", value.product());
assertEquals("18X15", value.productSearch());
assertEquals("PG", value.projectGroup());
assertEquals("product", value.view());
assertEquals("quarter", value.trendGranularity());
assertEquals("black", value.stage());
assertEquals(Map.of("product", List.of("P2")), value.exclusions());
assertEquals(Map.of("customerCode", List.of("000123")), value.extraFilters());
assertEquals("src_any", value.columnFilters().getFirst().get("field"));
}
}
@@ -0,0 +1,189 @@
package cn.yunsheng.quality.metrics;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import cn.yunsheng.quality.governance.GovernanceAccessService;
import cn.yunsheng.quality.identity.AppUserPrincipal;
import cn.yunsheng.quality.ingestion.BusinessRowMapper;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.junit.jupiter.api.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.RowMapper;
class MetricsTrendServiceTest {
private static final String RELEASE = "release-trend";
private final AppUserPrincipal actor = new AppUserPrincipal(UUID.randomUUID(), "trend-reader", "unused", "趋势读者",
true, false, List.of("SYSTEM_ADMIN"), List.of("*"), List.of("*"), List.of());
@Test void directTrendMatchesFullMetricsTrendWithoutBuildingProductGroups() {
JdbcTemplate jdbc = jdbc(true, true);
MetricsService service = new MetricsService(jdbc, mock(GovernanceAccessService.class));
var query = new MetricsService.MetricsQuery(2026, null, null, null, null, null, null,
"product", true, RELEASE, "month", Map.of());
MetricsService.MetricSnapshot full = service.calculate(query, actor);
MetricsService.MetricTrendSnapshot trend = service.calculateTrend(query, actor);
assertEquals(full.trend(), trend.trend());
assertEquals(full.coverage(), trend.coverage());
assertEquals("release-trend", trend.release().get("id"));
assertNull(trend.trend().getFirst().product());
assertNull(trend.trend().getFirst().group());
assertEquals(List.of("release", "context", "trend", "coverage"),
Arrays.stream(MetricsService.MetricTrendSnapshot.class.getRecordComponents())
.map(java.lang.reflect.RecordComponent::getName).toList());
List<String> trendSql = capturedSql(jdbc).stream()
.filter(sql -> sql.contains("GROUP BY trend_period") && !sql.contains("GROUP BY trend_period,"))
.toList();
assertFalse(trendSql.isEmpty());
assertTrue(trendSql.stream().allMatch(sql -> !sql.contains("bucket_key") && !sql.contains("f.product")));
}
@Test void rangeStageAndFiltersReachOnlyTheRequestedTrendAndCoverageQueries() {
JdbcTemplate jdbc = jdbc(true, true);
MetricsService service = new MetricsService(jdbc, mock(GovernanceAccessService.class));
var query = new MetricsService.MetricsQuery(2026, null, null, null, null, null, null,
"product", true, RELEASE, "month", Map.of("product", List.of("excluded-product")))
.withMonthRange(3, 5).withProductSearch("18X15").withStage("black");
MetricsService.MetricTrendSnapshot result = service.calculateTrend(query, actor);
assertEquals("black", result.context().get("stage"));
assertEquals(3, result.context().get("monthStart"));
assertEquals(5, result.context().get("monthEnd"));
List<String> sql = capturedSql(jdbc);
List<String> trendSql = sql.stream().filter(value -> value.contains("GROUP BY trend_period")).toList();
assertEquals(1, trendSql.size());
assertTrue(trendSql.getFirst().contains("f.month_key BETWEEN ? AND ?"));
assertTrue(trendSql.getFirst().contains("LOCATE(LOWER(?), LOWER(COALESCE(f.product,''))) > 0"));
assertTrue(trendSql.getFirst().contains("f.product COLLATE utf8mb4_0900_as_cs"));
assertTrue(trendSql.getFirst().contains("f.stage=?"));
assertFalse(trendSql.getFirst().contains("JOIN fact_bright"));
}
@Test void emptyPeriodReturnsAnEmptyTrendAndZeroCoverage() {
JdbcTemplate jdbc = jdbc(false, false);
MetricsService service = new MetricsService(jdbc, mock(GovernanceAccessService.class));
var query = new MetricsService.MetricsQuery(2026, 3, null, null, null, null, null,
"overall", true, RELEASE, "month", Map.of());
MetricsService.MetricTrendSnapshot result = service.calculateTrend(query, actor);
assertTrue(result.trend().isEmpty());
assertEquals(0L, result.coverage().get("rawRows"));
assertEquals(0L, result.coverage().get("includedRows"));
}
private static JdbcTemplate jdbc(boolean emitAggregates, boolean emitCoverage) {
JdbcTemplate jdbc = mock(JdbcTemplate.class);
when(jdbc.queryForObject(contains("metric_rollup_build"), eq(Integer.class), any(Object[].class))).thenReturn(0);
when(jdbc.queryForObject("SELECT rule_version FROM data_release WHERE id=?", String.class, RELEASE))
.thenReturn(BusinessRowMapper.RULE_VERSION);
when(jdbc.query(eq("SELECT id,name,status FROM data_release WHERE id=?"), any(RowMapper.class), eq(RELEASE)))
.thenAnswer(invocation -> {
RowMapper<?> mapper = invocation.getArgument(1);
ResultSet row = mock(ResultSet.class);
when(row.getString(1)).thenReturn(RELEASE);
when(row.getString(2)).thenReturn("Trend fixture");
when(row.getString(3)).thenReturn("PUBLISHED");
return List.of(mapper.mapRow(row, 0));
});
doAnswer(invocation -> {
String sql = invocation.getArgument(0);
RowCallbackHandler callback = invocation.getArgument(1);
Object[] invocationArgs = invocation.getArguments();
Object[] args = Arrays.copyOfRange(invocationArgs, 2, invocationArgs.length);
if (sql.contains("SELECT f.month_key,")) {
if (emitCoverage) emitCoverageRow(callback, sql, args);
} else if (emitAggregates) {
emitAggregateRows(callback, sql, args);
}
return null;
}).when(jdbc).query(anyString(), any(RowCallbackHandler.class), any(Object[].class));
return jdbc;
}
private static void emitCoverageRow(RowCallbackHandler callback, String sql, Object[] args) throws Exception {
String stage = stage(sql, args);
ResultSet row = mock(ResultSet.class);
when(row.getString("month_key")).thenReturn("2026-03");
when(row.getLong("row_count")).thenReturn(2L);
when(row.getLong("excluded_blank_rows")).thenReturn(0L);
when(row.getLong("unmapped_rows")).thenReturn(0L);
when(row.getLong("project_unconfigured_rows")).thenReturn(0L);
callback.processRow(row);
}
private static void emitAggregateRows(RowCallbackHandler callback, String sql, Object[] args) throws Exception {
String stage = stage(sql, args);
List<Contribution> contributions = stage.equals("black")
? List.of(new Contribution("P1", "90", "10", "100", "2"),
new Contribution("P2", "40", "10", "50", "1"))
: List.of(new Contribution("P1", "8", "2", "10", "1"),
new Contribution("P2", "9", "1", "10", "0"));
if (!sql.contains("AS bucket_key")) {
BigDecimal good = contributions.stream().map(Contribution::good).reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal scrap = contributions.stream().map(Contribution::scrap).reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal total = contributions.stream().map(Contribution::total).reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal component = contributions.stream().map(Contribution::component).reduce(BigDecimal.ZERO, BigDecimal::add);
emitAggregateRow(callback, stage, null, good, scrap, total, component, 2L);
} else {
for (Contribution contribution : contributions)
emitAggregateRow(callback, stage, contribution.key(), contribution.good(), contribution.scrap(),
contribution.total(), contribution.component(), 1L);
}
}
private static void emitAggregateRow(RowCallbackHandler callback, String stage, String key, BigDecimal good,
BigDecimal scrap, BigDecimal total, BigDecimal component, long count)
throws Exception {
ResultSet row = mock(ResultSet.class);
when(row.getString("trend_period")).thenReturn("2026-03");
if (key != null) when(row.getString("bucket_key")).thenReturn(key);
when(row.getLong("row_count")).thenReturn(count);
when(row.getBigDecimal("qualified_weight")).thenReturn(good);
when(row.getBigDecimal("scrap_weight")).thenReturn(scrap);
when(row.getBigDecimal("total_weight")).thenReturn(total);
for (String column : List.of("black_responsibility_scrap", "black_material_scrap", "black_external_scrap",
"bright_headquarters_scrap", "bright_plating_scrap", "bright_material_scrap",
"bright_processing_scrap", "bright_diffusion_scrap"))
when(row.getBigDecimal(column)).thenReturn(BigDecimal.ZERO);
when(row.getBigDecimal(stage.equals("black") ? "black_responsibility_scrap" : "bright_plating_scrap"))
.thenReturn(component);
callback.processRow(row);
}
private static String stage(String sql, Object[] args) {
if (sql.contains("JOIN fact_black f")) return "black";
if (sql.contains("JOIN fact_bright f")) return "bright";
for (Object value : args) if ("black".equals(value) || "bright".equals(value)) return String.valueOf(value);
return "black";
}
private static List<String> capturedSql(JdbcTemplate jdbc) {
var details = mockingDetails(jdbc).getInvocations();
List<String> result = new ArrayList<>();
for (var invocation : details) {
if (invocation.getMethod().getName().equals("query") && invocation.getArguments().length > 0
&& invocation.getArguments()[0] instanceof String sql) result.add(sql);
}
return result;
}
private record Contribution(String key, String goodText, String scrapText, String totalText, String componentText) {
BigDecimal good() { return new BigDecimal(goodText); }
BigDecimal scrap() { return new BigDecimal(scrapText); }
BigDecimal total() { return new BigDecimal(totalText); }
BigDecimal component() { return new BigDecimal(componentText); }
}
}