增加:管理员->知识库模块接口
This commit is contained in:
parent
d4f41a0166
commit
6362d10c6b
|
@ -0,0 +1,14 @@
|
|||
package com.moyz.adi.admin.controller;
|
||||
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 聊天会话管理
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/admin/conv")
|
||||
@Validated
|
||||
public class AdminConvController {
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.moyz.adi.admin.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.moyz.adi.common.dto.KbInfoResp;
|
||||
import com.moyz.adi.common.dto.KbSearchReq;
|
||||
import com.moyz.adi.common.service.KnowledgeBaseService;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* Knowledge Base
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/admin/kb")
|
||||
@Validated
|
||||
public class AdminKbController {
|
||||
|
||||
@Resource
|
||||
private KnowledgeBaseService knowledgeBaseService;
|
||||
|
||||
@PostMapping("/search")
|
||||
public Page<KbInfoResp> search(@RequestBody KbSearchReq kbSearchReq, @NotNull @Min(1) Integer currentPage, @NotNull @Min(10) Integer pageSize) {
|
||||
return knowledgeBaseService.search(kbSearchReq, currentPage, pageSize);
|
||||
}
|
||||
}
|
|
@ -1,29 +1,20 @@
|
|||
package com.moyz.adi.admin.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.moyz.adi.common.base.ThreadContext;
|
||||
import com.moyz.adi.common.dto.UserAddReq;
|
||||
import com.moyz.adi.common.dto.UserEditReq;
|
||||
import com.moyz.adi.common.dto.UserInfoDto;
|
||||
import com.moyz.adi.common.dto.UsersReq;
|
||||
import com.moyz.adi.common.dto.UserSearchReq;
|
||||
import com.moyz.adi.common.entity.User;
|
||||
import com.moyz.adi.common.service.UserService;
|
||||
import com.talanlabs.avatargenerator.Avatar;
|
||||
import com.talanlabs.avatargenerator.cat.CatAvatar;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/admin/user")
|
||||
@Validated
|
||||
|
@ -32,9 +23,9 @@ public class AdminUserController {
|
|||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
@PostMapping("/list")
|
||||
public Page<UserInfoDto> userList(@RequestBody UsersReq usersReq, @NotNull @Min(1) Integer currentPage, @NotNull @Min(10) Integer pageSize) {
|
||||
return userService.listUsers(usersReq, currentPage, pageSize);
|
||||
@PostMapping("/search")
|
||||
public Page<UserInfoDto> search(@RequestBody UserSearchReq userSearchReq, @NotNull @Min(1) Integer currentPage, @NotNull @Min(10) Integer pageSize) {
|
||||
return userService.search(userSearchReq, currentPage, pageSize);
|
||||
}
|
||||
|
||||
@Operation(summary = "用户信息")
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
package com.moyz.adi.admin.controller;
|
||||
|
||||
import com.moyz.adi.common.dto.StatisticDto;
|
||||
import com.moyz.adi.common.service.StatisticService;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/admin/statistic")
|
||||
@Validated
|
||||
public class StatisticController {
|
||||
|
||||
@Resource
|
||||
private StatisticService statisticService;
|
||||
|
||||
@GetMapping("/info")
|
||||
public StatisticDto statistic() {
|
||||
StatisticDto result = new StatisticDto();
|
||||
result.setKbStatistic(statisticService.calKbStat());
|
||||
result.setUserStatistic(statisticService.calUserStat());
|
||||
result.setTokenCostStatistic(statisticService.calTokenCostStat());
|
||||
result.setConvStatistic(statisticService.calConvStatistic());
|
||||
return result;
|
||||
}
|
||||
}
|
|
@ -2,12 +2,14 @@ package com.moyz.adi;
|
|||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableAsync
|
||||
@EnableScheduling
|
||||
@EnableCaching
|
||||
public class BootstrapApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
|
|
@ -24,6 +24,8 @@ spring:
|
|||
logging:
|
||||
file:
|
||||
path: D:/data/logs
|
||||
level:
|
||||
com.baomidou.mybatisplus: DEBUG
|
||||
|
||||
adi:
|
||||
frontend-url: http://localhost:1002
|
||||
|
|
|
@ -21,8 +21,8 @@ spring:
|
|||
cache:
|
||||
type: redis
|
||||
redis:
|
||||
key-prefix: CACHE
|
||||
time-to-live: 1d
|
||||
key-prefix: 'AUTO_CACHE\:'
|
||||
time-to-live: 1h
|
||||
mail:
|
||||
default-encoding: UTF-8
|
||||
protocol: smtps
|
||||
|
|
|
@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|||
import com.moyz.adi.common.base.ThreadContext;
|
||||
import com.moyz.adi.common.dto.KbEditReq;
|
||||
import com.moyz.adi.common.dto.KbInfoResp;
|
||||
import com.moyz.adi.common.dto.KbSearchReq;
|
||||
import com.moyz.adi.common.entity.AdiFile;
|
||||
import com.moyz.adi.common.entity.KnowledgeBase;
|
||||
import com.moyz.adi.common.service.KnowledgeBaseService;
|
||||
|
@ -47,7 +48,7 @@ public class KnowledgeBaseController {
|
|||
|
||||
@GetMapping("/searchPublic")
|
||||
public Page<KbInfoResp> searchPublic(@RequestParam(defaultValue = "") String keyword, @NotNull @Min(1) Integer currentPage, @NotNull @Min(10) Integer pageSize) {
|
||||
return knowledgeBaseService.searchPublic(keyword, currentPage, pageSize);
|
||||
return knowledgeBaseService.search(KbSearchReq.builder().isPublic(true).title(keyword).build(), currentPage, pageSize);
|
||||
}
|
||||
|
||||
@GetMapping("/info/{uuid}")
|
||||
|
|
|
@ -91,6 +91,10 @@ public class UserController {
|
|||
}
|
||||
|
||||
private synchronized void writeToResponse(Long userId, HttpServletResponse response) throws IOException{
|
||||
response.setHeader("Cache-Control", "max-age=" + 3600 * 24 * 7);
|
||||
// 设置Expires头,指示响应过期的具体日期时间
|
||||
long expiresTime = System.currentTimeMillis() + 3600 * 24 * 7 * 1000;
|
||||
response.setDateHeader("Expires", expiresTime);
|
||||
Avatar avatar = CatAvatar.newAvatarBuilder().size(64, 64).build();
|
||||
BufferedImage bufferedImage = avatar.create(userId);
|
||||
ImageIO.write(bufferedImage, "png", response.getOutputStream());
|
||||
|
|
|
@ -97,4 +97,10 @@ public class RedisKeyConstant {
|
|||
* 值:知识库uuid
|
||||
*/
|
||||
public static final String KB_STATISTIC_RECALCULATE_SIGNAL = "kb:statistic:recalculate:signal";
|
||||
|
||||
public static final String STATISTIC = "statistic";
|
||||
public static final String STATISTIC_USER = "user";
|
||||
public static final String STATISTIC_KNOWLEDGE_BASE = "kb";
|
||||
public static final String STATISTIC_TOKEN_COST = "token_cost";
|
||||
public static final String STATISTIC_CONVERSATION = "conversation";
|
||||
}
|
||||
|
|
|
@ -2,8 +2,11 @@ package com.moyz.adi.common.dto;
|
|||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class KbInfoResp {
|
||||
private Long id;
|
||||
private String uuid;
|
||||
private String title;
|
||||
private String remark;
|
||||
|
@ -13,4 +16,6 @@ public class KbInfoResp {
|
|||
private String ownerName;
|
||||
private Integer itemCount;
|
||||
private Integer embeddingCount;
|
||||
private LocalDateTime createTime;
|
||||
private LocalDateTime updateTime;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
package com.moyz.adi.common.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class KbSearchReq {
|
||||
private String title;
|
||||
private Boolean isPublic;
|
||||
private Integer minItemCount;
|
||||
private Integer minEmbeddingCount;
|
||||
private Long[] createTime;
|
||||
private Long[] updateTime;
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.moyz.adi.common.dto;
|
||||
|
||||
import com.moyz.adi.common.vo.ConvStatistic;
|
||||
import com.moyz.adi.common.vo.KbStatistic;
|
||||
import com.moyz.adi.common.vo.TokenCostStatistic;
|
||||
import com.moyz.adi.common.vo.UserStatistic;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class StatisticDto {
|
||||
private UserStatistic userStatistic;
|
||||
private KbStatistic kbStatistic;
|
||||
private TokenCostStatistic tokenCostStatistic;
|
||||
private ConvStatistic convStatistic;
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.moyz.adi.common.dto;
|
||||
|
||||
import com.moyz.adi.common.enums.UserStatusEnum;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class UserSearchReq {
|
||||
private String name;
|
||||
private String email;
|
||||
private String uuid;
|
||||
private Integer userStatus;
|
||||
private Boolean isAdmin;
|
||||
private Long[] createTime;
|
||||
private Long[] updateTime;
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package com.moyz.adi.common.dto;
|
||||
|
||||
import com.moyz.adi.common.enums.UserStatusEnum;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class UsersReq {
|
||||
String name;
|
||||
String email;
|
||||
String uuid;
|
||||
|
||||
UserStatusEnum userStatus;
|
||||
|
||||
Boolean isAdmin;
|
||||
}
|
|
@ -3,6 +3,8 @@ package com.moyz.adi.common.enums;
|
|||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum UserStatusEnum implements BaseEnum {
|
||||
|
@ -14,4 +16,8 @@ public enum UserStatusEnum implements BaseEnum {
|
|||
private final Integer value;
|
||||
private final String desc;
|
||||
|
||||
public static UserStatusEnum getByValue(Integer val) {
|
||||
return Arrays.stream(UserStatusEnum.values()).filter(item -> item.value.equals(val)).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -79,7 +79,6 @@ public class TokenFilter extends OncePerRequestFilter {
|
|||
}
|
||||
ThreadContext.setCurrentUser(user);
|
||||
ThreadContext.setToken(token);
|
||||
log.info("response::" + response);
|
||||
filterChain.doFilter(request, response);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
package com.moyz.adi.common.mapper;
|
||||
|
||||
import com.moyz.adi.common.entity.Conversation;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.moyz.adi.common.entity.Conversation;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
@ -14,5 +17,7 @@ import org.apache.ibatis.annotations.Mapper;
|
|||
*/
|
||||
@Mapper
|
||||
public interface ConversationMapper extends BaseMapper<Conversation> {
|
||||
Integer countCreatedByTimePeriod(@Param("beginTime") LocalDateTime beginTime, @Param("endTime") LocalDateTime endTime);
|
||||
|
||||
Integer countAllCreated();
|
||||
}
|
||||
|
|
|
@ -8,6 +8,8 @@ import com.moyz.adi.common.entity.KnowledgeBaseItem;
|
|||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Mapper
|
||||
public interface KnowledgeBaseItemMapper extends BaseMapper<KnowledgeBaseItem> {
|
||||
|
||||
|
@ -15,4 +17,8 @@ public interface KnowledgeBaseItemMapper extends BaseMapper<KnowledgeBaseItem> {
|
|||
Page<KbItemDto> searchByKb(Page<KbItemDto> page, @Param("kbUuid") String kbUuid, @Param("keyword") String keyword);
|
||||
|
||||
KnowledgeBaseItem getByUuid(String uuid);
|
||||
|
||||
Integer countCreatedByTimePeriod(@Param("beginTime") LocalDateTime beginTime, @Param("endTime") LocalDateTime endTime);
|
||||
|
||||
Integer countAllCreated();
|
||||
}
|
||||
|
|
|
@ -6,6 +6,8 @@ import com.moyz.adi.common.entity.KnowledgeBase;
|
|||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Mapper
|
||||
public interface KnowledgeBaseMapper extends BaseMapper<KnowledgeBase> {
|
||||
|
||||
|
@ -32,4 +34,8 @@ public interface KnowledgeBaseMapper extends BaseMapper<KnowledgeBase> {
|
|||
* @param uuid
|
||||
*/
|
||||
void updateStatByUuid(@Param("uuid") String uuid);
|
||||
|
||||
Integer countCreatedByTimePeriod(@Param("beginTime") LocalDateTime beginTime, @Param("endTime") LocalDateTime endTime);
|
||||
|
||||
Integer countAllCreated();
|
||||
}
|
||||
|
|
|
@ -3,7 +3,11 @@ package com.moyz.adi.common.mapper;
|
|||
import com.moyz.adi.common.entity.UserDayCost;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
@Mapper
|
||||
public interface UserDayCostMapper extends BaseMapper<UserDayCost> {
|
||||
Long sumCostByDay(@Param("day") Integer day);
|
||||
|
||||
Long sumCostByDayPeriod(@Param("beginDate") Integer beginDate, @Param("endDate") Integer endDate);
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@ import org.apache.commons.lang3.StringUtils;
|
|||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static com.moyz.adi.common.util.LocalCache.MODEL_ID_TO_OBJ;
|
||||
|
@ -92,6 +93,14 @@ public class AiModelService extends ServiceImpl<AiModelMapper, AiModel> {
|
|||
baseMapper.updateById(model);
|
||||
}
|
||||
|
||||
public List<AiModelDto> listEnable() {
|
||||
List<AiModel> aiModels = ChainWrappers.lambdaQueryChain(baseMapper)
|
||||
.eq(AiModel::getIsEnable, true)
|
||||
.eq(AiModel::getIsDeleted, false)
|
||||
.list();
|
||||
return MPPageUtil.convertToList(aiModels, AiModelDto.class);
|
||||
}
|
||||
|
||||
public void softDelete(Long id) {
|
||||
AiModel existModel = getByIdOrThrow(id);
|
||||
|
||||
|
|
|
@ -18,6 +18,7 @@ import org.apache.commons.lang3.StringUtils;
|
|||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
@ -182,4 +183,14 @@ public class ConversationService extends ServiceImpl<ConversationMapper, Convers
|
|||
.update();
|
||||
}
|
||||
|
||||
public int countTodayCreated() {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
LocalDateTime beginTime = LocalDateTime.of(now.getYear(), now.getMonth(), now.getDayOfMonth(), 0, 0, 0);
|
||||
LocalDateTime endTime = beginTime.plusDays(1);
|
||||
return baseMapper.countCreatedByTimePeriod(beginTime, endTime);
|
||||
}
|
||||
|
||||
public int countAllCreated() {
|
||||
return baseMapper.countAllCreated();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@ import org.springframework.context.annotation.Lazy;
|
|||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
|
@ -142,7 +143,6 @@ public class KnowledgeBaseItemService extends ServiceImpl<KnowledgeBaseItemMappe
|
|||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public int countByKbUuid(String kbUuid) {
|
||||
return ChainWrappers.lambdaQueryChain(baseMapper)
|
||||
.eq(KnowledgeBaseItem::getKbUuid, kbUuid)
|
||||
|
@ -150,7 +150,15 @@ public class KnowledgeBaseItemService extends ServiceImpl<KnowledgeBaseItemMappe
|
|||
.count()
|
||||
.intValue();
|
||||
}
|
||||
|
||||
public int countTodayCreated() {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
LocalDateTime beginTime = LocalDateTime.of(now.getYear(), now.getMonth(), now.getDayOfMonth(), 0, 0, 0);
|
||||
LocalDateTime endTime = beginTime.plusDays(1);
|
||||
return baseMapper.countCreatedByTimePeriod(beginTime, endTime);
|
||||
}
|
||||
public int countAllCreated() {
|
||||
return baseMapper.countAllCreated();
|
||||
}
|
||||
private boolean checkPrivilege(String uuid) {
|
||||
if (StringUtils.isBlank(uuid)) {
|
||||
throw new BaseException(A_PARAMS_ERROR);
|
||||
|
|
|
@ -10,6 +10,7 @@ import com.moyz.adi.common.cosntant.AdiConstant;
|
|||
import com.moyz.adi.common.cosntant.RedisKeyConstant;
|
||||
import com.moyz.adi.common.dto.KbEditReq;
|
||||
import com.moyz.adi.common.dto.KbInfoResp;
|
||||
import com.moyz.adi.common.dto.KbSearchReq;
|
||||
import com.moyz.adi.common.dto.QAReq;
|
||||
import com.moyz.adi.common.entity.*;
|
||||
import com.moyz.adi.common.exception.BaseException;
|
||||
|
@ -35,7 +36,6 @@ import org.springframework.context.annotation.Lazy;
|
|||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
@ -84,9 +84,6 @@ public class KnowledgeBaseService extends ServiceImpl<KnowledgeBaseMapper, Knowl
|
|||
@Resource
|
||||
private UserDayCostService userDayCostService;
|
||||
|
||||
@Resource
|
||||
private ThreadPoolTaskExecutor mainExecutor;
|
||||
|
||||
public KnowledgeBase saveOrUpdate(KbEditReq kbEditReq) {
|
||||
String uuid = kbEditReq.getUuid();
|
||||
KnowledgeBase knowledgeBase = new KnowledgeBase();
|
||||
|
@ -222,14 +219,28 @@ public class KnowledgeBaseService extends ServiceImpl<KnowledgeBaseMapper, Knowl
|
|||
return MPPageUtil.convertToPage(knowledgeBasePage, result, KbInfoResp.class, null);
|
||||
}
|
||||
|
||||
public Page<KbInfoResp> searchPublic(String keyword, Integer currentPage, Integer pageSize) {
|
||||
public Page<KbInfoResp> search(KbSearchReq req, Integer currentPage, Integer pageSize) {
|
||||
Page<KbInfoResp> result = new Page<>();
|
||||
LambdaQueryWrapper<KnowledgeBase> wrapper = new LambdaQueryWrapper();
|
||||
wrapper.eq(KnowledgeBase::getIsPublic, true);
|
||||
wrapper.eq(KnowledgeBase::getIsDeleted, false);
|
||||
if (StringUtils.isNotBlank(keyword)) {
|
||||
wrapper.like(KnowledgeBase::getTitle, keyword);
|
||||
if (StringUtils.isNotBlank(req.getTitle())) {
|
||||
wrapper.like(KnowledgeBase::getTitle, req.getTitle());
|
||||
}
|
||||
if (null != req.getIsPublic()) {
|
||||
wrapper.eq(KnowledgeBase::getIsPublic, req.getIsPublic());
|
||||
}
|
||||
if (null != req.getMinItemCount()) {
|
||||
wrapper.ge(KnowledgeBase::getItemCount, req.getMinItemCount());
|
||||
}
|
||||
if (null != req.getMinEmbeddingCount()) {
|
||||
wrapper.ge(KnowledgeBase::getEmbeddingCount, req.getMinEmbeddingCount());
|
||||
}
|
||||
if (null != req.getCreateTime() && req.getCreateTime().length == 2) {
|
||||
wrapper.between(KnowledgeBase::getCreateTime, LocalDateTimeUtil.parse(req.getCreateTime()[0]), LocalDateTimeUtil.parse(req.getCreateTime()[1]));
|
||||
}
|
||||
if (null != req.getUpdateTime() && req.getUpdateTime().length == 2) {
|
||||
wrapper.between(KnowledgeBase::getUpdateTime, LocalDateTimeUtil.parse(req.getUpdateTime()[0]), LocalDateTimeUtil.parse(req.getUpdateTime()[1]));
|
||||
}
|
||||
wrapper.eq(KnowledgeBase::getIsDeleted, false);
|
||||
wrapper.orderByDesc(KnowledgeBase::getStarCount, KnowledgeBase::getUpdateTime);
|
||||
Page<KnowledgeBase> knowledgeBasePage = baseMapper.selectPage(new Page<>(currentPage, pageSize), wrapper);
|
||||
return MPPageUtil.convertToPage(knowledgeBasePage, result, KbInfoResp.class, null);
|
||||
|
@ -359,6 +370,17 @@ public class KnowledgeBaseService extends ServiceImpl<KnowledgeBaseMapper, Knowl
|
|||
stringRedisTemplate.opsForSet().add(KB_STATISTIC_RECALCULATE_SIGNAL, kbUuid);
|
||||
}
|
||||
|
||||
public int countTodayCreated() {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
LocalDateTime beginTime = LocalDateTime.of(now.getYear(), now.getMonth(), now.getDayOfMonth(), 0, 0, 0);
|
||||
LocalDateTime endTime = beginTime.plusDays(1);
|
||||
return baseMapper.countCreatedByTimePeriod(beginTime, endTime);
|
||||
}
|
||||
|
||||
public int countAllCreated() {
|
||||
return baseMapper.countAllCreated();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update knowledge base stat
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,100 @@
|
|||
package com.moyz.adi.common.service;
|
||||
|
||||
import com.moyz.adi.common.entity.User;
|
||||
import com.moyz.adi.common.enums.UserStatusEnum;
|
||||
import com.moyz.adi.common.vo.ConvStatistic;
|
||||
import com.moyz.adi.common.vo.KbStatistic;
|
||||
import com.moyz.adi.common.vo.TokenCostStatistic;
|
||||
import com.moyz.adi.common.vo.UserStatistic;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static com.moyz.adi.common.cosntant.RedisKeyConstant.*;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class StatisticService {
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
@Resource
|
||||
private UserDayCostService userDayCostService;
|
||||
|
||||
@Resource
|
||||
private KnowledgeBaseService knowledgeBaseService;
|
||||
|
||||
@Resource
|
||||
private KnowledgeBaseItemService knowledgeBaseItemService;
|
||||
|
||||
@Resource
|
||||
private ConversationService conversationService;
|
||||
|
||||
@Cacheable(value = STATISTIC + ":" + STATISTIC_USER)
|
||||
public UserStatistic calUserStat() {
|
||||
UserStatistic result = new UserStatistic();
|
||||
LocalDate today = LocalDate.now();
|
||||
int todayCreated = userService.lambdaQuery()
|
||||
.gt(User::getCreateTime, today)
|
||||
.count()
|
||||
.intValue();
|
||||
result.setTodayCreated(todayCreated);
|
||||
|
||||
int todayActivated = userService.lambdaQuery()
|
||||
.gt(User::getCreateTime, today)
|
||||
.eq(User::getUserStatus, UserStatusEnum.NORMAL)
|
||||
.count()
|
||||
.intValue();
|
||||
int totalNormal = userService.lambdaQuery()
|
||||
.eq(User::getUserStatus, UserStatusEnum.NORMAL)
|
||||
.count()
|
||||
.intValue();
|
||||
|
||||
result.setTodayCreated(todayCreated);
|
||||
result.setTotalNormal(totalNormal);
|
||||
result.setTodayActivated(todayActivated);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Cacheable(value = STATISTIC + ":" + STATISTIC_TOKEN_COST)
|
||||
public TokenCostStatistic calTokenCostStat() {
|
||||
Long todayCost = userDayCostService.sumTodayCost();
|
||||
Long currentMonthCost = userDayCostService.sumCurrentMonthCost();
|
||||
TokenCostStatistic aiModelStat = new TokenCostStatistic();
|
||||
aiModelStat.setTodayTokenCost(todayCost);
|
||||
aiModelStat.setMonthTokenCost(currentMonthCost);
|
||||
return aiModelStat;
|
||||
}
|
||||
|
||||
@Cacheable(value = STATISTIC + ":" + STATISTIC_KNOWLEDGE_BASE)
|
||||
public KbStatistic calKbStat() {
|
||||
int kbTodayCreated = knowledgeBaseService.countTodayCreated();
|
||||
int kbTotal = knowledgeBaseService.countAllCreated();
|
||||
int itemTodayCreated = knowledgeBaseItemService.countTodayCreated();
|
||||
int itemTotal = knowledgeBaseItemService.countAllCreated();
|
||||
KbStatistic stat = new KbStatistic();
|
||||
stat.setKbTodayCreated(kbTodayCreated);
|
||||
stat.setKbTotal(kbTotal);
|
||||
stat.setItemTotal(itemTotal);
|
||||
stat.setItemTodayCreated(itemTodayCreated);
|
||||
return stat;
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计会话信息
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Cacheable(value = STATISTIC + ":" + STATISTIC_CONVERSATION)
|
||||
public ConvStatistic calConvStatistic() {
|
||||
return ConvStatistic.builder()
|
||||
.todayCreated(conversationService.countTodayCreated())
|
||||
.total(conversationService.countAllCreated())
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
|
@ -73,4 +73,15 @@ public class UserDayCostService extends ServiceImpl<UserDayCostMapper, UserDayCo
|
|||
.eq(UserDayCost::getSecretKeyType, UserUtil.getSecretType(user))
|
||||
.one();
|
||||
}
|
||||
|
||||
public Long sumTodayCost() {
|
||||
int today = LocalDateTimeUtil.getToday();
|
||||
return baseMapper.sumCostByDay(today);
|
||||
}
|
||||
|
||||
public Long sumCurrentMonthCost() {
|
||||
int start = LocalDateTimeUtil.getIntDay(LocalDateTime.now().withDayOfMonth(1));
|
||||
int end = LocalDateTimeUtil.getIntDay(LocalDateTime.now().plusMonths(1).withDayOfMonth(1).minusDays(1));
|
||||
return baseMapper.sumCostByDayPeriod(start, end);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ import com.moyz.adi.common.helper.AdiMailSender;
|
|||
import com.moyz.adi.common.mapper.UserMapper;
|
||||
import com.moyz.adi.common.util.JsonUtil;
|
||||
import com.moyz.adi.common.util.LocalCache;
|
||||
import com.moyz.adi.common.util.LocalDateTimeUtil;
|
||||
import com.moyz.adi.common.util.MPPageUtil;
|
||||
import com.moyz.adi.common.vo.CostStat;
|
||||
import jakarta.annotation.Resource;
|
||||
|
@ -354,20 +355,27 @@ public class UserService extends ServiceImpl<UserMapper, User> {
|
|||
return user;
|
||||
}
|
||||
|
||||
public Page<UserInfoDto> listUsers(UsersReq usersReq, Integer currentPage, Integer pageSize) {
|
||||
public Page<UserInfoDto> search(UserSearchReq req, Integer currentPage, Integer pageSize) {
|
||||
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
|
||||
if (StringUtils.isNotBlank(usersReq.getName())) {
|
||||
wrapper.eq(User::getName, usersReq.getName());
|
||||
if (StringUtils.isNotBlank(req.getName())) {
|
||||
wrapper.like(User::getName, req.getName());
|
||||
}
|
||||
if (StringUtils.isNotBlank(usersReq.getUuid())) {
|
||||
wrapper.eq(User::getUuid, usersReq.getUuid());
|
||||
if (StringUtils.isNotBlank(req.getUuid())) {
|
||||
wrapper.eq(User::getUuid, req.getUuid());
|
||||
}
|
||||
if (StringUtils.isNotBlank(usersReq.getEmail())) {
|
||||
wrapper.eq(User::getEmail, usersReq.getEmail());
|
||||
if (StringUtils.isNotBlank(req.getEmail())) {
|
||||
wrapper.eq(User::getEmail, req.getEmail());
|
||||
}
|
||||
if (null != usersReq.getUserStatus()) {
|
||||
wrapper.eq(User::getUserStatus, usersReq.getUserStatus());
|
||||
if (null != req.getUserStatus()) {
|
||||
wrapper.eq(User::getUserStatus, UserStatusEnum.getByValue(req.getUserStatus()));
|
||||
}
|
||||
if (null != req.getCreateTime() && req.getCreateTime().length == 2) {
|
||||
wrapper.between(User::getCreateTime, LocalDateTimeUtil.parse(req.getCreateTime()[0]), LocalDateTimeUtil.parse(req.getCreateTime()[1]));
|
||||
}
|
||||
if (null != req.getUpdateTime() && req.getUpdateTime().length == 2) {
|
||||
wrapper.between(User::getUpdateTime, LocalDateTimeUtil.parse(req.getUpdateTime()[0]), LocalDateTimeUtil.parse(req.getUpdateTime()[1]));
|
||||
}
|
||||
wrapper.eq(User::getIsDeleted, false);
|
||||
wrapper.orderByDesc(User::getUpdateTime);
|
||||
Page<User> page = baseMapper.selectPage(new Page<>(currentPage, pageSize), wrapper);
|
||||
Page<UserInfoDto> result = new Page<>();
|
||||
|
@ -395,6 +403,7 @@ public class UserService extends ServiceImpl<UserMapper, User> {
|
|||
newOne.setEmail(addUserReq.getEmail());
|
||||
newOne.setPassword(hashed);
|
||||
newOne.setUserStatus(UserStatusEnum.NORMAL);
|
||||
newOne.setActiveTime(LocalDateTime.now());
|
||||
baseMapper.insert(newOne);
|
||||
|
||||
UserInfoDto result = new UserInfoDto();
|
||||
|
|
|
@ -4,7 +4,9 @@ import com.fasterxml.jackson.databind.module.SimpleModule;
|
|||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
public class LocalDateTimeUtil {
|
||||
|
@ -23,6 +25,10 @@ public class LocalDateTimeUtil {
|
|||
return LocalDateTime.parse(localDateTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
||||
}
|
||||
|
||||
public static LocalDateTime parse(Long epochMilli) {
|
||||
return LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneId.systemDefault());
|
||||
}
|
||||
|
||||
public static String format(LocalDateTime localDateTime) {
|
||||
if (null == localDateTime) {
|
||||
return StringUtils.EMPTY;
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
package com.moyz.adi.common.vo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 会话统计信息
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class ConvStatistic implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private Integer todayCreated;
|
||||
private Integer total;
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.moyz.adi.common.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 知识库的统计信息
|
||||
*/
|
||||
@Data
|
||||
public class KbStatistic implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private Integer kbTodayCreated;
|
||||
private Integer itemTodayCreated;
|
||||
private Integer kbTotal;
|
||||
private Integer itemTotal;
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.moyz.adi.common.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* LLM相关的统计
|
||||
*/
|
||||
@Data
|
||||
public class TokenCostStatistic implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private Long todayTokenCost;
|
||||
private Long monthTokenCost;
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.moyz.adi.common.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 用户的统计信息
|
||||
*/
|
||||
@Data
|
||||
public class UserStatistic implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private Integer todayCreated;
|
||||
private Integer todayActivated;
|
||||
private Integer totalNormal;
|
||||
}
|
|
@ -2,5 +2,17 @@
|
|||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.moyz.adi.common.mapper.ConversationMapper">
|
||||
|
||||
<select id="countAllCreated" resultType="Integer">
|
||||
select count(1)
|
||||
from adi_conversation
|
||||
where is_deleted = false
|
||||
</select>
|
||||
|
||||
<select id="countCreatedByTimePeriod" resultType="Integer">
|
||||
select count(1)
|
||||
from adi_conversation
|
||||
where is_deleted = false
|
||||
and create_time between #{beginTime} and #{endTime}
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
|
|
|
@ -19,4 +19,17 @@
|
|||
from adi_knowledge_base_item a
|
||||
where uuid = #{uuid}
|
||||
</select>
|
||||
|
||||
<select id="countCreatedByTimePeriod" resultType="Integer">
|
||||
select count(1)
|
||||
from adi_knowledge_base_item
|
||||
where is_deleted = false
|
||||
and create_time between #{beginTime} and #{endTime}
|
||||
</select>
|
||||
|
||||
<select id="countAllCreated" resultType="Integer">
|
||||
select count(1)
|
||||
from adi_knowledge_base_item
|
||||
where is_deleted = false
|
||||
</select>
|
||||
</mapper>
|
||||
|
|
|
@ -29,6 +29,19 @@
|
|||
order by star_count,update_time desc
|
||||
</select>
|
||||
|
||||
<select id="countCreatedByTimePeriod" resultType="Integer">
|
||||
select count(1)
|
||||
from adi_knowledge_base
|
||||
where is_deleted = false
|
||||
and create_time between #{beginTime} and #{endTime}
|
||||
</select>
|
||||
|
||||
<select id="countAllCreated" resultType="Integer">
|
||||
select count(1)
|
||||
from adi_knowledge_base
|
||||
where is_deleted = false
|
||||
</select>
|
||||
|
||||
<update id="updateStatByUuid">
|
||||
update adi_knowledge_base
|
||||
set item_count = (select count(1)
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.moyz.adi.common.mapper.UserDayCostMapper">
|
||||
<select id="sumCostByDay" resultType="java.lang.Long">
|
||||
select coalesce(sum(tokens),0)
|
||||
from adi_user_day_cost
|
||||
where day = #{day}
|
||||
</select>
|
||||
<select id="sumCostByDayPeriod" resultType="java.lang.Long">
|
||||
select coalesce(sum(tokens), 0)
|
||||
from adi_user_day_cost
|
||||
where day between #{beginDate} and #{endDate}
|
||||
</select>
|
||||
</mapper>
|
Loading…
Reference in New Issue