优化:缓存查询与智能分析页面,修复组织下钻和草稿保留

This commit is contained in:
2026-10-23 16:06:59 +08:00
parent 6394e057bf
commit f14400bd60
9 changed files with 261 additions and 50 deletions
@@ -0,0 +1,42 @@
package cn.yunsheng.quality.common;
import cn.yunsheng.quality.governance.GovernanceAccessService;
import cn.yunsheng.quality.identity.AppUserPrincipal;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.HexFormat;
import java.util.List;
import java.util.Map;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/** 只校验缓存依赖,不读取业务事实表,也不向前端返回权限策略正文。 */
@RestController
public class WorkspaceStateController {
private final JdbcTemplate jdbc;
private final GovernanceAccessService access;
public WorkspaceStateController(JdbcTemplate jdbc, GovernanceAccessService access) {
this.jdbc = jdbc; this.access = access;
}
@GetMapping("/api/workspace/state")
public Map<String, String> state(@AuthenticationPrincipal AppUserPrincipal actor) {
access.requireUserActivePolicy(actor);
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));
String data = digest(List.of(
jdbc.queryForList("SELECT * FROM current_release ORDER BY dataset_id"),
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);
}
private static String digest(Object value) {
try { return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(value.toString().getBytes(StandardCharsets.UTF_8))); }
catch (java.security.NoSuchAlgorithmException exception) { throw new IllegalStateException(exception); }
}
}
@@ -0,0 +1,44 @@
package cn.yunsheng.quality.common;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import cn.yunsheng.quality.governance.GovernanceAccessService;
import cn.yunsheng.quality.identity.AppUserPrincipal;
import java.util.*;
import org.junit.jupiter.api.Test;
import org.springframework.jdbc.core.JdbcTemplate;
class WorkspaceStateControllerTest {
final JdbcTemplate jdbc = mock(JdbcTemplate.class);
final GovernanceAccessService access = mock(GovernanceAccessService.class);
final WorkspaceStateController controller = new WorkspaceStateController(jdbc, access);
final UUID id = UUID.randomUUID();
AppUserPrincipal actor(List<String> departments) {
return new AppUserPrincipal(id,"test","","test",true,false,List.of("QUALITY_VIEWER"),departments,List.of("*"),List.of());
}
@Test void stableDigestIgnoresScopeOrderAndDoesNotExposePolicy() {
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}")));
assertFalse(first.toString().contains("private-field"));
verify(access,times(2)).requireUserActivePolicy(any());
}
@Test void permissionAndTargetChangesInvalidateSeparateDependencies() {
var before = controller.state(actor(List.of("A")));
var scoped = controller.state(actor(List.of("B")));
assertNotEquals(before.get("authRevision"),scoped.get("authRevision"));
assertEquals(before.get("dataRevision"),scoped.get("dataRevision"));
when(jdbc.queryForList(contains("target_version"))).thenReturn(List.of(Map.of("id","target","row_version",2)));
var target = controller.state(actor(List.of("A")));
assertEquals(before.get("authRevision"),target.get("authRevision"));
assertNotEquals(before.get("dataRevision"),target.get("dataRevision"));
verify(jdbc,never()).queryForList(contains("fact_"));
}
@Test void expiredAuthorizationStopsBeforeLoadingCacheMetadata() {
var actor=actor(List.of("A"));
doThrow(new IllegalStateException("expired")).when(access).requireUserActivePolicy(actor);
assertThrows(IllegalStateException.class,()->controller.state(actor));
verifyNoInteractions(jdbc);
}
}