feat(device): 按 device_id 记录客户端系统与机型

- 新增 app_device_profile 表与 upsert,匿名请求同样建档
- app-api 设备画像过滤器,同设备 60 秒节流写一次
- 收藏/点赞/观看列表优先用设备画像,缺失回退最近会话
This commit is contained in:
Vicky authored and trae-solo-agent committed 2026-10-27 13:53:47 +08:00
1 parent d9a1670a72
commit 2cf8f9a1c3
13 files changed
+362 -9

No files matched your search

@@ -3,7 +3,9 @@
package com.fruitshorts.admin.service;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fruitshorts.user.dal.entity.AppDeviceProfile;
import com.fruitshorts.user.dal.entity.AppUserSession;
import com.fruitshorts.user.dal.mapper.AppDeviceProfileMapper;
import com.fruitshorts.user.dal.mapper.AppUserSessionMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@@ -16,13 +18,15 @@ import java.util.List;
import java.util.Map;
/**
* 按用户最近会话解析系统。同一设备优先,否则用仍有效的最近一次登录。
* 按设备画像解析系统,缺画像时回退到用户最近会话。
* 设备画像不依赖登录态,游客行为也能显示系统。
*/
@Service
@RequiredArgsConstructor
public class AppSessionSystemService {
private final AppUserSessionMapper sessionMapper;
private final AppDeviceProfileMapper profileMapper;
/** 按用户分组,组内按最近活跃倒序。 */
public Map<Long, List<AppUserSession>> groupByUser(Collection<Long> userIds) {
@@ -40,6 +44,24 @@ public class AppSessionSystemService {
return grouped;
}
/** 按 device_id 批量取设备画像,未建档的设备不出现在结果里。 */
public Map<String, AppDeviceProfile> deviceProfiles(Collection<String> deviceIds) {
Map<String, AppDeviceProfile> result = new HashMap<>();
if (deviceIds == null || deviceIds.isEmpty()) {
return result;
}
List<String> keys = deviceIds.stream()
.filter(StringUtils::hasText).map(String::trim).distinct().toList();
if (keys.isEmpty()) {
return result;
}
for (AppDeviceProfile profile : profileMapper.selectList(
Wrappers.<AppDeviceProfile>lambdaQuery().in(AppDeviceProfile::getDeviceId, keys))) {
result.put(profile.getDeviceId(), profile);
}
return result;
}
/** 观看记录优先匹配同一设备,其次取未吊销会话。 */
public AppUserSession match(List<AppUserSession> sessions, String deviceId) {
if (sessions == null || sessions.isEmpty()) {
@@ -9,12 +9,17 @@ import com.fruitshorts.common.exception.ErrorCode;
import com.fruitshorts.common.exception.ServiceException;
import com.fruitshorts.content.dal.entity.Drama;
import com.fruitshorts.content.dal.mapper.DramaMapper;
import com.fruitshorts.user.dal.entity.AppDeviceProfile;
import com.fruitshorts.user.dal.entity.AppUser;
import com.fruitshorts.user.dal.entity.AppUserSession;
import com.fruitshorts.user.dal.mapper.AppDeviceProfileMapper;
import com.fruitshorts.user.dal.mapper.AppUserMapper;
import com.fruitshorts.user.dal.mapper.AppUserSessionMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@@ -35,6 +40,8 @@ public class EngagementAdminLookup {
private static final int LOOKUP_PROBE = MAX_LOOKUP_SIZE + 1;
private final AppUserMapper userMapper;
private final DramaMapper dramaMapper;
private final AppDeviceProfileMapper deviceProfileMapper;
private final AppUserSessionMapper sessionMapper;
/** 规范化页码和页大小。 */
public <T> Page<T> page(EngagementQueryReqVO req) {
@@ -96,6 +103,35 @@ public class EngagementAdminLookup {
.collect(Collectors.toMap(Drama::getId, Function.identity()));
}
/** 按 device_id 批量读取设备画像;游客也有记录,没建档的不返回。 */
public Map<String, AppDeviceProfile> deviceProfiles(Collection<String> deviceIds) {
List<String> keys = deviceIds.stream().filter(StringUtils::hasText)
.map(String::trim).distinct().toList();
return keys.isEmpty() ? Map.of() : deviceProfileMapper.selectList(
Wrappers.<AppDeviceProfile>lambdaQuery().in(AppDeviceProfile::getDeviceId, keys))
.stream().collect(Collectors.toMap(AppDeviceProfile::getDeviceId,
Function.identity(), (first, second) -> first));
}
/**
* 每个用户取最近一次登录会话。
* 已登录用户的互动行 device_id 为空(写入时会清空),只能退回会话取系统。
*/
public Map<Long, AppUserSession> latestSessions(Collection<Long> userIds) {
List<Long> keys = keys(userIds);
if (keys.isEmpty()) {
return Map.of();
}
Map<Long, AppUserSession> result = new HashMap<>();
for (AppUserSession session : sessionMapper.selectList(Wrappers.<AppUserSession>lambdaQuery()
.in(AppUserSession::getUserId, keys)
.orderByDesc(AppUserSession::getLastActiveAt)
.orderByDesc(AppUserSession::getId))) {
result.putIfAbsent(session.getUserId(), session);
}
return result;
}
private List<Long> keys(Collection<Long> ids) {
return ids.stream().filter(Objects::nonNull).distinct().toList();
}
@@ -10,7 +10,9 @@ import com.fruitshorts.content.dal.entity.Drama;
import com.fruitshorts.content.enums.ContentStatus;
import com.fruitshorts.playback.dal.entity.UserFavorite;
import com.fruitshorts.playback.dal.mapper.UserFavoriteMapper;
import com.fruitshorts.user.dal.entity.AppDeviceProfile;
import com.fruitshorts.user.dal.entity.AppUser;
import com.fruitshorts.user.dal.entity.AppUserSession;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
@@ -53,17 +55,35 @@ public class FavoriteAdminService {
}
Map<Long, AppUser> users = lookup.users(rows.stream().map(UserFavorite::getUserId).toList());
Map<Long, Drama> dramas = lookup.dramas(rows.stream().map(UserFavorite::getDramaId).toList());
Map<String, AppDeviceProfile> profiles =
lookup.deviceProfiles(rows.stream().map(UserFavorite::getDeviceId).toList());
Map<Long, AppUserSession> sessions =
lookup.latestSessions(rows.stream().map(UserFavorite::getUserId).toList());
return rows.stream().map(row -> toVo(row, row.getUserId() == null ? null : users.get(row.getUserId()),
row.getDramaId() == null ? null : dramas.get(row.getDramaId())))
row.getDramaId() == null ? null : dramas.get(row.getDramaId()),
row.getDeviceId() == null ? null : profiles.get(row.getDeviceId()),
row.getUserId() == null ? null : sessions.get(row.getUserId())))
.toList();
}
private AdminFavoriteVO toVo(UserFavorite row, AppUser user, Drama drama) {
private AdminFavoriteVO toVo(UserFavorite row, AppUser user, Drama drama,
AppDeviceProfile profile, AppUserSession session) {
AdminFavoriteVO vo = new AdminFavoriteVO();
vo.setId(row.getId());
vo.setUserId(row.getUserId());
vo.setDeviceId(row.getDeviceId());
vo.setAnonymous(row.getUserId() == null);
if (profile != null) {
vo.setPlatform(profile.getPlatform());
vo.setOsVersion(profile.getOsVersion());
vo.setDeviceModel(profile.getDeviceModel());
}
// 已登录用户的互动行不带 device_id,设备画像查不到时退回最近一次登录会话
if (!StringUtils.hasText(vo.getPlatform()) && session != null) {
vo.setPlatform(session.getPlatform());
vo.setOsVersion(session.getOsVersion());
vo.setDeviceModel(session.getDeviceModel());
}
vo.setDramaId(row.getDramaId());
vo.setCreatedAt(row.getCreatedAt());
if (user != null) {
@@ -14,7 +14,9 @@ import com.fruitshorts.interaction.dal.entity.ContentComment;
import com.fruitshorts.interaction.dal.entity.ContentLike;
import com.fruitshorts.interaction.dal.mapper.ContentCommentMapper;
import com.fruitshorts.interaction.dal.mapper.ContentLikeMapper;
import com.fruitshorts.user.dal.entity.AppDeviceProfile;
import com.fruitshorts.user.dal.entity.AppUser;
import com.fruitshorts.user.dal.entity.AppUserSession;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
@@ -110,7 +112,13 @@ public class LikeAdminService {
comments.values().stream().map(ContentComment::getDramaId).filter(Objects::nonNull).forEach(dramaIds::add);
episodes.values().stream().map(DramaEpisode::getDramaId).filter(Objects::nonNull).forEach(dramaIds::add);
Map<Long, Drama> dramas = lookup.dramas(dramaIds);
return rows.stream().map(row -> toVo(row, users, comments, episodes, dramas)).toList();
Map<String, AppDeviceProfile> profiles =
lookup.deviceProfiles(rows.stream().map(ContentLike::getDeviceId).toList());
Map<Long, AppUserSession> sessions =
lookup.latestSessions(rows.stream().map(ContentLike::getUserId).toList());
return rows.stream()
.map(row -> toVo(row, users, comments, episodes, dramas, profiles, sessions))
.toList();
}
private List<Long> targetIds(List<ContentLike> rows, String type) {
@@ -130,12 +138,29 @@ public class LikeAdminService {
}
private AdminLikeVO toVo(ContentLike row, Map<Long, AppUser> users, Map<Long, ContentComment> comments,
Map<Long, DramaEpisode> episodes, Map<Long, Drama> dramas) {
Map<Long, DramaEpisode> episodes, Map<Long, Drama> dramas,
Map<String, AppDeviceProfile> profiles,
Map<Long, AppUserSession> sessions) {
AdminLikeVO vo = new AdminLikeVO();
vo.setId(row.getId());
vo.setUserId(row.getUserId());
vo.setDeviceId(row.getDeviceId());
vo.setAnonymous(row.getUserId() == null);
AppDeviceProfile profile = row.getDeviceId() == null ? null : profiles.get(row.getDeviceId());
if (profile != null) {
vo.setPlatform(profile.getPlatform());
vo.setOsVersion(profile.getOsVersion());
vo.setDeviceModel(profile.getDeviceModel());
}
// 已登录用户的互动行不带 device_id,设备画像查不到时退回最近一次登录会话
if (!StringUtils.hasText(vo.getPlatform()) && row.getUserId() != null) {
AppUserSession session = sessions.get(row.getUserId());
if (session != null) {
vo.setPlatform(session.getPlatform());
vo.setOsVersion(session.getOsVersion());
vo.setDeviceModel(session.getDeviceModel());
}
}
vo.setTargetType(row.getTargetType());
vo.setTargetId(row.getTargetId());
vo.setCreatedAt(row.getCreatedAt());
@@ -16,6 +16,7 @@ import com.fruitshorts.content.dal.mapper.DramaMapper;
import com.fruitshorts.content.enums.ContentStatus;
import com.fruitshorts.playback.dal.entity.WatchHistory;
import com.fruitshorts.playback.dal.mapper.WatchHistoryMapper;
import com.fruitshorts.user.dal.entity.AppDeviceProfile;
import com.fruitshorts.user.dal.entity.AppUser;
import com.fruitshorts.user.dal.entity.AppUserSession;
import com.fruitshorts.user.dal.mapper.AppUserMapper;
@@ -129,11 +130,14 @@ public class WatchHistoryAdminService {
Map<Long, DramaEpisode> episodes = loadEpisodes(rows);
Map<Long, List<AppUserSession>> sessions = sessionSystem.groupByUser(
rows.stream().map(WatchHistory::getUserId).distinct().toList());
Map<String, AppDeviceProfile> profiles = sessionSystem.deviceProfiles(
rows.stream().map(WatchHistory::getDeviceId).toList());
List<AdminWatchHistoryVO> list = new ArrayList<>(rows.size());
for (WatchHistory row : rows) {
list.add(toVo(row, users.get(row.getUserId()), dramas.get(row.getDramaId()),
episodes.get(row.getEpisodeId()),
sessionSystem.match(sessions.get(row.getUserId()), row.getDeviceId())));
sessionSystem.match(sessions.get(row.getUserId()), row.getDeviceId()),
profiles.get(row.getDeviceId())));
}
return list;
}
@@ -166,7 +170,8 @@ public class WatchHistoryAdminService {
}
private AdminWatchHistoryVO toVo(WatchHistory row, AppUser user, Drama drama,
DramaEpisode episode, AppUserSession session) {
DramaEpisode episode, AppUserSession session,
AppDeviceProfile profile) {
AdminWatchHistoryVO vo = new AdminWatchHistoryVO();
vo.setId(row.getId());
vo.setUserId(row.getUserId());
@@ -177,7 +182,12 @@ public class WatchHistoryAdminService {
vo.setSource(row.getSource());
vo.setDeviceId(row.getDeviceId());
vo.setWatchedAt(row.getWatchedAt());
if (session != null) {
// 设备画像不依赖登录态,优先;没有画像(老数据)再回退到登录会话
if (profile != null && (StringUtils.hasText(profile.getPlatform())
|| StringUtils.hasText(profile.getOsVersion()))) {
vo.setPlatform(profile.getPlatform());
vo.setOsVersion(profile.getOsVersion());
} else if (session != null) {
vo.setPlatform(session.getPlatform());
vo.setOsVersion(session.getOsVersion());
}
@@ -13,6 +13,10 @@ public class AdminFavoriteVO {
private String uid;
private String nickname;
private String deviceId;
/** 客户端系统:ANDROID / IOS / WEB,来自设备画像,游客同样有值。 */
private String platform;
private String osVersion;
private String deviceModel;
private Boolean anonymous;
private Long dramaId;
private String dramaTitle;
@@ -13,6 +13,10 @@ public class AdminLikeVO {
private String uid;
private String nickname;
private String deviceId;
/** 客户端系统:ANDROID / IOS / WEB,来自设备画像,游客同样有值。 */
private String platform;
private String osVersion;
private String deviceModel;
private Boolean anonymous;
private Long dramaId;
private String dramaTitle;
@@ -13,7 +13,7 @@ import static org.assertj.core.api.Assertions.assertThat;
/** 观看记录匹配登录系统。 */
class AppSessionSystemServiceTest {
private final AppSessionSystemService service = new AppSessionSystemService(null);
private final AppSessionSystemService service = new AppSessionSystemService(null, null);
@Test
void should_use_same_device_session_when_watch_history_has_device() {