refactor(device): 设备平台只记 iOS/Android
去掉系统版本、机型与版本号上报,表结构收敛为 device_id + platform。
This commit is contained in:
1 parent
2dadca6288
commit
229f1d0198
10 files changed
+39
-80
No files matched your search
+1
-4
@@ -11,7 +11,7 @@ import lombok.Data;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 设备画像:按 device_id 记录客户端系统与机型。
|
||||
* 设备平台:按 device_id 记录客户端是 iOS 还是 Android。
|
||||
* 与会话不同,它不依赖用户身份,游客请求也会建档。
|
||||
*/
|
||||
@Data
|
||||
@@ -22,9 +22,6 @@ public class AppDeviceProfile {
|
||||
private Long id;
|
||||
private String deviceId;
|
||||
private String platform;
|
||||
private String osVersion;
|
||||
private String deviceModel;
|
||||
private String appVersion;
|
||||
private LocalDateTime firstSeenAt;
|
||||
private LocalDateTime lastSeenAt;
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
+4
-10
@@ -10,23 +10,17 @@ import org.apache.ibatis.annotations.Param;
|
||||
public interface AppDeviceProfileMapper extends BaseMapper<AppDeviceProfile> {
|
||||
|
||||
/**
|
||||
* 按 device_id 建档或刷新。
|
||||
* 空字段不覆盖已有值:老版本客户端只带 deviceId,不能把已记录的系统抹掉。
|
||||
* 按 device_id 建档或刷新平台。
|
||||
* 空平台不覆盖已有值:老版本客户端不带平台头,不能把已记录的值抹掉。
|
||||
*/
|
||||
@Insert("""
|
||||
INSERT INTO app_device_profile (id, device_id, platform, os_version, device_model, app_version,
|
||||
INSERT INTO app_device_profile (id, device_id, platform,
|
||||
first_seen_at, last_seen_at, created_at, updated_at, deleted)
|
||||
VALUES (#{p.id}, #{p.deviceId}, #{p.platform}, #{p.osVersion}, #{p.deviceModel}, #{p.appVersion},
|
||||
VALUES (#{p.id}, #{p.deviceId}, #{p.platform},
|
||||
CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 0)
|
||||
ON CONFLICT (device_id) DO UPDATE SET
|
||||
platform = CASE WHEN EXCLUDED.platform <> '' THEN EXCLUDED.platform
|
||||
ELSE app_device_profile.platform END,
|
||||
os_version = CASE WHEN EXCLUDED.os_version <> '' THEN EXCLUDED.os_version
|
||||
ELSE app_device_profile.os_version END,
|
||||
device_model = CASE WHEN EXCLUDED.device_model <> '' THEN EXCLUDED.device_model
|
||||
ELSE app_device_profile.device_model END,
|
||||
app_version = CASE WHEN EXCLUDED.app_version <> '' THEN EXCLUDED.app_version
|
||||
ELSE app_device_profile.app_version END,
|
||||
last_seen_at = CURRENT_TIMESTAMP,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
""")
|
||||
|
||||
+9
-22
@@ -10,7 +10,9 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/** 设备画像写入:按 device_id 建档,匿名请求也记录。 */
|
||||
import java.util.Locale;
|
||||
|
||||
/** 设备平台写入:按 device_id 建档,匿名请求也记录。 */
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@@ -18,39 +20,24 @@ public class DeviceProfileService {
|
||||
|
||||
private static final int DEVICE_ID_MAX = 128;
|
||||
private static final int PLATFORM_MAX = 16;
|
||||
private static final int OS_VERSION_MAX = 64;
|
||||
private static final int DEVICE_MODEL_MAX = 128;
|
||||
private static final int APP_VERSION_MAX = 32;
|
||||
|
||||
private final AppDeviceProfileMapper mapper;
|
||||
|
||||
/**
|
||||
* 记录或刷新设备画像。header 全部为空时直接跳过,
|
||||
* 避免老版本客户端只带 deviceId 时产生无意义的空记录。
|
||||
*/
|
||||
public void touch(String deviceId, String platform, String osVersion, String deviceModel, String appVersion) {
|
||||
if (!StringUtils.hasText(deviceId)) {
|
||||
return;
|
||||
}
|
||||
String cleanPlatform = sanitize(platform, PLATFORM_MAX).toUpperCase();
|
||||
String cleanOs = sanitize(osVersion, OS_VERSION_MAX);
|
||||
String cleanModel = sanitize(deviceModel, DEVICE_MODEL_MAX);
|
||||
String cleanApp = sanitize(appVersion, APP_VERSION_MAX);
|
||||
if (cleanPlatform.isEmpty() && cleanOs.isEmpty() && cleanModel.isEmpty() && cleanApp.isEmpty()) {
|
||||
/** 记录或刷新设备平台;平台为空时直接跳过,避免产生无意义记录。 */
|
||||
public void touch(String deviceId, String platform) {
|
||||
String cleanPlatform = sanitize(platform, PLATFORM_MAX).toUpperCase(Locale.ROOT);
|
||||
if (!StringUtils.hasText(deviceId) || cleanPlatform.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
AppDeviceProfile profile = new AppDeviceProfile();
|
||||
profile.setId(IdWorker.getId());
|
||||
profile.setDeviceId(sanitize(deviceId, DEVICE_ID_MAX));
|
||||
profile.setPlatform(cleanPlatform);
|
||||
profile.setOsVersion(cleanOs);
|
||||
profile.setDeviceModel(cleanModel);
|
||||
profile.setAppVersion(cleanApp);
|
||||
try {
|
||||
mapper.upsert(profile);
|
||||
} catch (RuntimeException ex) {
|
||||
// 画像只用于后台展示,写失败不能影响正常业务请求
|
||||
log.warn("device-profile upsert failed deviceId={} reason={}", profile.getDeviceId(), ex.getMessage());
|
||||
// 平台只用于后台展示,写失败不能影响正常业务请求
|
||||
log.warn("device-platform upsert failed deviceId={} reason={}", profile.getDeviceId(), ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user