Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ WORKDIR /build
ADD go.mod .
ADD go.sum .
RUN go mod download

# 重要:创建目录并复制字典文件
RUN mkdir -p /build/dict
# 复制gojieba的字典文件到构建目录
RUN cp -r /go/pkg/mod/github.com/yanyiwu/gojieba@v1.4.6/deps/cppjieba/dict/* /build/dict/ || true

COPY . .
RUN sh ./build.sh

Expand All @@ -25,5 +31,8 @@ ENV TZ Asia/Shanghai

WORKDIR /app
COPY --from=builder /build/output /app
COPY --from=builder /build/dict /app/dict

ENV JIEBA_DICT_PATH=/app/dict

CMD ["sh", "./bootstrap.sh"]
39 changes: 34 additions & 5 deletions biz/application/service/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type IDashboardService interface {

// 对话记录
DashboardUserConvRecords(ctx context.Context, req *core_api.DashboardUserConvRecordsReq) (*core_api.DashboardUserConvRecordsResp, error)
DashboardUnitConvRecords(ctx context.Context, req *core_api.DashboardUnitConvRecordsReq) (*core_api.DashboardUnitConvRecordsResp, error)
DashboardGetReport(ctx context.Context, req *core_api.DashboardGetReportReq) (*core_api.DashboardGetReportResp, error)
}

Expand Down Expand Up @@ -661,18 +662,19 @@ func (s *DashboardService) DashboardListClasses(ctx context.Context, req *core_a
}

// 查询结果
res, err := s.UserMapper.CountByClasses(ctx, unitOID, grades, classes)
clsStats, err := s.UserMapper.CountByClasses(ctx, unitOID, grades, classes)
clsTeachers, err := s.UserMapper.FindUnitClassTeachers(ctx, unitOID)
if err != nil {
return nil, errorx.New(errno.ErrCountUserByClasses)
}

// 整理结果,构建响应
return &core_api.DashboardListClassesResp{
Grades: aggregateAndSort(res),
Grades: aggregateAndSort(clsStats, clsTeachers),
}, nil
}

func aggregateAndSort(mapperRes []*user.ClassStatResult) []*core_api.GradeInfo {
func aggregateAndSort(mapperRes []*user.ClassStatResult, clsTeachers user.ClassTeachers) []*core_api.GradeInfo {
if len(mapperRes) == 0 {
return make([]*core_api.GradeInfo, 0)
}
Expand All @@ -696,8 +698,8 @@ func aggregateAndSort(mapperRes []*user.ClassStatResult) []*core_api.GradeInfo {
Class: item.Info.Class,
UserNum: uNum,
AlarmNum: aNum,
TeacherName: "",
TeacherPhone: "",
TeacherName: clsTeachers[item.Info.Grade][item.Info.Class].Name,
TeacherPhone: clsTeachers[item.Info.Grade][item.Info.Class].Code,
})
}

Expand Down Expand Up @@ -1035,3 +1037,30 @@ func (s *DashboardService) DashboardGetReport(ctx context.Context, req *core_api
Msg: "success",
}, nil
}

func (s *DashboardService) DashboardUnitConvRecords(ctx context.Context, req *core_api.DashboardUnitConvRecordsReq) (*core_api.DashboardUnitConvRecordsResp, error) {
if uid := req.GetUnitId(); uid != "" {
return s.getOneUnitConvs(ctx, req)
}

return s.getAllUnitsConvs(ctx, req)
}

// req包含unitId
func (s *DashboardService) getOneUnitConvs(ctx context.Context, req *core_api.DashboardUnitConvRecordsReq) (*core_api.DashboardUnitConvRecordsResp, error) {
unitOID, err := bson.ObjectIDFromHex(req.GetUnitId())
if err != nil {
return nil, errorx.New(errno.ErrInvalidParams, errorx.KV("field", "UnitId"), errorx.KV("value", "用户ID"))
}

_, err = s.ConversationMapper.CountByUnit(ctx, &unitOID)
if err != nil {
return nil, errorx.New(errno.ErrDashboardGetConversations)
}

return nil, errorx.New(errno.UnImplementErr)
}

func (s *DashboardService) getAllUnitsConvs(ctx context.Context, req *core_api.DashboardUnitConvRecordsReq) (*core_api.DashboardUnitConvRecordsResp, error) {
return nil, errorx.New(errno.UnImplementErr)
}
4 changes: 4 additions & 0 deletions biz/cst/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ const (
Status = "status"
DeletedStatus = -1

Student = "student"
Teacher = "teacher"
ClassTeacher = "class_teacher"

Emotion = "emotion"
Meta = "$meta"
TextScore = "textScore"
Expand Down
40 changes: 38 additions & 2 deletions biz/domain/wordcld/wordcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package wordcld
import (
"bufio"
"context"
"github.com/xh-polaris/psych-core-api/biz/application/dto/core_api"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"unicode/utf8"

"github.com/xh-polaris/psych-core-api/biz/application/dto/core_api"

"github.com/xh-polaris/psych-core-api/biz/cst"
"github.com/xh-polaris/psych-core-api/biz/infra/mapper/message"
"github.com/xh-polaris/psych-core-api/biz/infra/mapper/report"
Expand Down Expand Up @@ -107,10 +108,45 @@ func ensureStopWordsLoaded() {
stopWordsOnce.Do(loadStopWords)
}

// initJiebaInstance 初始化jieba实例
func initJiebaInstance() *gojieba.Jieba {
dictPath := os.Getenv("JIEBA_DICT_PATH")

// 在生产环境(Docker)中,必须使用自定义路径,因为默认的Go模块路径不存在
// 如果没有设置环境变量,设置默认值为Docker中的字典路径
if dictPath == "" {
dictPath = "/app/dict"
}

// 检查自定义字典目录是否存在并包含必要的字典文件
requiredFiles := []string{
"jieba.dict.utf8",
"hmm_model.utf8",
"user.dict.utf8",
"idf.utf8",
"stop_words.utf8",
}

// 检查所有字典文件是否存在
dictPaths := make([]string, 0, len(requiredFiles))
for _, filename := range requiredFiles {
fullPath := filepath.Join(dictPath, filename)
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
// 如果字典文件不存在,尝试使用gojieba的默认配置(仅限开发环境)
// 生产环境中这通常会失败,所以应该确保字典文件正确部署
return gojieba.NewJieba()
}
dictPaths = append(dictPaths, fullPath)
}

// 如果所有字典文件都存在,使用自定义路径
return gojieba.NewJieba(dictPaths...)
}

func NewWordCloudExtractor(rptMapper report.IMongoMapper) *WordCloudExtractor {
Extractor = WordCloudExtractor{
rptMapper: rptMapper,
jieba: gojieba.NewJieba(),
jieba: initJiebaInstance(),
}
return &Extractor
}
Expand Down
23 changes: 23 additions & 0 deletions biz/infra/mapper/user/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type IMongoMapper interface {
CountAlarmUsers(ctx context.Context, unitId *bson.ObjectID) (int32, error)
CountAlarmUsersByPeriod(ctx context.Context, unitId *bson.ObjectID, start, end time.Time) (int32, error)
RiskDistributionStats(ctx context.Context, unitId *bson.ObjectID) ([]*RiskStat, error)
FindUnitClassTeachers(ctx context.Context, unitId bson.ObjectID) (ClassTeachers, error)
}

type mongoMapper struct {
Expand Down Expand Up @@ -269,3 +270,25 @@ func (m *mongoMapper) RiskDistributionStats(ctx context.Context, unitId *bson.Ob
}
return results, nil
}

type ClassTeachers map[int32]map[int32]*User

func (m *mongoMapper) FindUnitClassTeachers(ctx context.Context, unitId bson.ObjectID) (ClassTeachers, error) {
filter := bson.M{
cst.UnitID: unitId,
cst.Role: RoleStoI[cst.ClassTeacher],
}

clsTeacherUsers, err := m.FindAllByFields(ctx, filter)
if err != nil {
logs.Error("[user mapper] FindUnitClassTeachers err:%s", errorx.ErrorWithoutStack(err))
return nil, err
}

clsTeachers := make(map[int32]map[int32]*User)
for _, u := range clsTeacherUsers {
clsTeachers[u.Grade][u.Class] = u
}

return clsTeachers, nil
}
4 changes: 4 additions & 0 deletions biz/infra/mapper/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
var (
RiskLevelStoI = map[string]int32{cst.High: 1, cst.Medium: 2, cst.Low: 3, cst.Normal: 4}
RiskLevelItoS = map[int32]string{1: cst.High, 2: cst.Medium, 3: cst.Low, 4: cst.Normal}

RoleStoI = map[string]int32{cst.Student: 0, cst.Teacher: 1, cst.ClassTeacher: 2}
RoleItoS = map[int32]string{0: cst.Student, 1: cst.Teacher, 2: cst.ClassTeacher}
)

type User struct {
Expand All @@ -24,6 +27,7 @@ type User struct {
RiskLevel int `json:"riskLevel,omitempty" bson:"risk_level,omitempty"`
Status int `json:"status,omitempty" bson:"status,omitempty"`
EnrollYear int32 `json:"enrollYear,omitempty" bson:"enroll_year,omitempty"`
Role int32 `json:"role,omitempty" bson:"role,omitempty"`
Grade int32 `json:"grade,omitempty" bson:"grade,omitempty"`
Class int32 `json:"class,omitempty" bson:"class,omitempty"`
Options map[string]any `json:"option,omitempty" bson:"option,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion types/errno/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
ErrDashboardEmotionRatio = 5009
ErrDashboardGetUnitKeywords = 5010
ErrDashboardGetUserInfo = 5011 // 获取用户信息失败
ErrDashboardGetConversations = 5012 // 获取用户对话记录失败
ErrDashboardGetConversations = 5012 // 获取对话记录失败
ErrDashboardGetConvReports = 5014 // 获取对话报表失败
ErrDashboardGenerateWordCloud = 5015 // 生成词云失败
ErrDashboardGetReport = 5016 // 获取报表失败
Expand Down
Loading