TASK:AiDA模块化

This commit is contained in:
shahaibo
2025-03-16 13:09:50 +08:00
parent 1637db2fe3
commit 8fa76c6732
55 changed files with 3236 additions and 132 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,9 @@
package com.ai.da.common.utils;
import com.ai.da.python.vo.DesignPythonObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
@@ -8,6 +12,8 @@ import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@@ -279,6 +285,77 @@ public class RedisUtil {
redisTemplate.expire(redisKey, 5, TimeUnit.MINUTES);
}
public void addPathToCache(Long collectionId, Long userId, String path) {
// Redis 中的键,唯一标识由 collectionId 和 userId 组成
String redisKey = "path:cache:" + collectionId + ":" + userId;
// 增加路径的计数
redisTemplate.opsForHash().increment(redisKey, path, 1);
// 设置过期时间为 2 小时7200 秒)
redisTemplate.expire(redisKey, 8, TimeUnit.HOURS);
}
public int getPathUsageCount(Long collectionId, Long userId, String path) {
String redisKey = "path:cache:" + collectionId + ":" + userId;
// 获取路径的使用次数
Object count = redisTemplate.opsForHash().get(redisKey, path);
return count != null ? Integer.parseInt(count.toString()) : 0;
}
public void addAssembledObjects(Long collectionId, Set<DesignPythonObject> assembledObjects) {
// Redis 中的键,使用 collectionId 来唯一标识
String redisKey = "collection:assembledObjects:" + collectionId;
// 将 assembledObjects 转换为 JSON 格式存储,避免直接存储对象
String assembledObjectsJson = convertToJson(assembledObjects);
// 使用 Redis 的 set 操作更新集合
redisTemplate.opsForValue().set(redisKey, assembledObjectsJson);
// 设置过期时间为 5 分钟300 秒)
redisTemplate.expire(redisKey, 30, TimeUnit.MINUTES);
}
// 将 Set<DesignPythonObject> 转换为 JSON 格式
private String convertToJson(Set<DesignPythonObject> assembledObjects) {
try {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.writeValueAsString(assembledObjects);
} catch (JsonProcessingException e) {
e.printStackTrace();
return null;
}
}
public Set<DesignPythonObject> getAssembledObjects(Long collectionId) {
// Redis 中的键,使用 collectionId 来唯一标识
String redisKey = "collection:assembledObjects:" + collectionId;
// 从 Redis 获取存储的 JSON 字符串
String assembledObjectsJson = (String) redisTemplate.opsForValue().get(redisKey);
if (assembledObjectsJson == null) {
return new HashSet<>(); // 如果没有找到数据,返回一个空的 Set
}
// 将 JSON 字符串转换为 Set<DesignPythonObject>
return convertFromJson(assembledObjectsJson);
}
// 将 JSON 字符串转换为 Set<DesignPythonObject>
private Set<DesignPythonObject> convertFromJson(String json) {
try {
ObjectMapper objectMapper = new ObjectMapper();
// 使用 TypeReference 来指定目标类型是 Set<DesignPythonObject>
return objectMapper.readValue(json, new TypeReference<Set<DesignPythonObject>>() {});
} catch (JsonProcessingException e) {
e.printStackTrace();
return new HashSet<>(); // 如果转换失败,返回空的 Set
}
}
public final static String PAYMENT_INFO_LAST_SCAN_TIME = "PaymentInfoLastScanTime";
public final static String AFFILIATE_LINK_VIEW_KEY = "AffiliateLink:view:";
@@ -293,13 +370,126 @@ public class RedisUtil {
return redisTemplate.opsForValue().increment(key, 0);
}
public void batchDeleteKeysWithSamePrefix(String prefix){
Set<String> keys = redisTemplate.keys(prefix + "*");
assert keys != null;
if (!keys.isEmpty()){
redisTemplate.delete(keys);
/**
* 记录任务的耗时到Redis
* @param taskKey 任务标识,如 "taskA"
* @param elapsedTime 本次耗时,单位为毫秒
*/
public void recordTaskElapsedTime(String taskKey, long elapsedTime) {
String hashKey = "task:stats";
// 累加总耗时
redisTemplate.opsForHash().increment(hashKey, taskKey + ":totalTime", elapsedTime);
// 增加计数器
redisTemplate.opsForHash().increment(hashKey, taskKey + ":count", 1);
}
/**
* 获取任务的平均耗时
* @param taskKey 任务标识,如 "taskA"
* @return 平均耗时(毫秒)
*/
public double getTaskAverageTime(String taskKey) {
String hashKey = "task:stats";
// 获取总耗时和计数
Object totalTime = redisTemplate.opsForHash().get(hashKey, taskKey + ":totalTime");
Object count = redisTemplate.opsForHash().get(hashKey, taskKey + ":count");
// 计算平均值
if (totalTime == null || count == null) {
return 0;
}
return Double.parseDouble(totalTime.toString()) / Long.parseLong(count.toString());
}
/**
* 清除指定任务的统计数据
* @param taskKey 任务标识,如 "taskA"
*/
public void clearTaskStats(String taskKey) {
String hashKey = "task:stats";
// 删除总耗时和计数器
redisTemplate.opsForHash().delete(hashKey, taskKey + ":totalTime", taskKey + ":count");
}
public void recordTaskElapsedTime(String taskKey, double elapsedTimeInSeconds) {
// 将耗时转换为 BigDecimal并四舍五入保留四位小数
BigDecimal elapsedTime = new BigDecimal(elapsedTimeInSeconds).setScale(4, RoundingMode.HALF_UP);
// 累加总耗时(以毫秒为单位)
redisTemplate.opsForHash().increment("task:stats", taskKey + ":totalTime", elapsedTime.doubleValue());
// 增加计数器
redisTemplate.opsForHash().increment("task:stats", taskKey + ":count", 1);
}
// 获取第一部分Sketch耗时
public double getFirstSketchTime() {
// 获取 "firstSketchTime:totalTime" 对应的值,并返回(单位为秒)
Object time = redisTemplate.opsForHash().get("task:stats", "firstSketchTime:totalTime");
return time != null ? (double) time : 0.0;
}
// 获取第二部分(获取特征值)耗时
public double getGetAttributeRecognitionTime() {
// 获取 "getAttributeRecognitionTime:totalTime" 对应的值,并返回(单位为秒)
Object time = redisTemplate.opsForHash().get("task:stats", "getAttributeRecognitionTime:totalTime");
return time != null ? (double) time : 0.0;
}
// 获取第三部分(搭配 Sketch耗时
public double getOtherSketchTime() {
// 获取 "otherSketchTime:totalTime" 对应的值,并返回(单位为秒)
Object time = redisTemplate.opsForHash().get("task:stats", "otherSketchTime:totalTime");
return time != null ? (double) time : 0.0;
}
// 清理三部分的缓存
public void clearTaskElapsedTimeCache() {
// 删除第一部分的缓存
redisTemplate.opsForHash().delete("task:stats", "firstSketchTime:totalTime");
redisTemplate.opsForHash().delete("task:stats", "firstSketchTime:count");
// 删除第二部分的缓存
redisTemplate.opsForHash().delete("task:stats", "getAttributeRecognitionTime:totalTime");
redisTemplate.opsForHash().delete("task:stats", "getAttributeRecognitionTime:count");
// 删除第三部分的缓存
redisTemplate.opsForHash().delete("task:stats", "otherSketchTime:totalTime");
redisTemplate.opsForHash().delete("task:stats", "otherSketchTime:count");
}
public boolean incrementLikeCount(Long userId, String sketchPath) {
String redisKey = "user_like_count:" + userId;
try {
redisTemplate.opsForHash().increment(redisKey, sketchPath, 1);
return true;
} catch (Exception e) {
log.error("Error incrementing like count for userId {} and sketchPath {}: {}", userId, sketchPath, e.getMessage());
return false;
}
}
public int getLikeCount(Long userId, String sketchPath) {
String redisKey = "user_like_count:" + userId;
Object count = redisTemplate.opsForHash().get(redisKey, sketchPath);
return count != null ? Integer.parseInt(count.toString()) : 0;
}
public void storeMaxLikeCount(Long userId, int maxLikeCount) {
String redisKey = "user_max_like_count:" + userId;
redisTemplate.opsForValue().set(redisKey, String.valueOf(maxLikeCount));
}
public int getMaxLikeCount(Long userId) {
String redisKey = "user_max_like_count:" + userId;
String maxLikeCount = redisTemplate.opsForValue().get(redisKey);
return maxLikeCount != null ? Integer.parseInt(maxLikeCount) : 0;
}
public final static String STRIPE_EXCEPTION_LOG = "StripeException:";
}