Skip to content

Commit 36f1564

Browse files
xueyizhengCoda-bot
andcommitted
feat: [Coda] 强制读主库支撑洞察反馈
(LogID: 202511121049110100911082412387DAE) Co-Authored-By: Coda <[email protected]>
1 parent 11421da commit 36f1564

10 files changed

+335
-44
lines changed

backend/infra/platestwrite/latest_write_tracker.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,7 @@ const (
136136
ResourceTypeTarget ResourceType = "eval_target"
137137
ResourceTypeTargetVersion ResourceType = "eval_target_version"
138138
ResourceTypeEvaluator ResourceType = "evaluator"
139+
140+
ResourceTypeExptInsightAnalysisRecord ResourceType = "expt_insight_analysis_record"
141+
ResourceTypeExptInsightAnalysisFeedback ResourceType = "expt_insight_analysis_feedback"
139142
)

backend/modules/evaluation/application/wire_gen.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/modules/evaluation/infra/repo/experiment/expt_insight_analysis_record.go

Lines changed: 128 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ package experiment
55

66
import (
77
"context"
8+
"fmt"
89

910
"github.com/coze-dev/coze-loop/backend/infra/db"
1011
"github.com/coze-dev/coze-loop/backend/infra/idgen"
12+
"github.com/coze-dev/coze-loop/backend/infra/platestwrite"
1113
"github.com/coze-dev/coze-loop/backend/modules/evaluation/domain/entity"
1214
"github.com/coze-dev/coze-loop/backend/modules/evaluation/domain/repo"
1315
"github.com/coze-dev/coze-loop/backend/modules/evaluation/infra/repo/experiment/mysql"
@@ -19,19 +21,22 @@ type ExptInsightAnalysisRecordRepo struct {
1921
exptInsightAnalysisFeedbackCommentDAO mysql.IExptInsightAnalysisFeedbackCommentDAO
2022
exptInsightAnalysisFeedbackVoteDAO mysql.IExptInsightAnalysisFeedbackVoteDAO
2123
idgenerator idgen.IIDGenerator
24+
writeTracker platestwrite.ILatestWriteTracker
2225
}
2326

2427
func NewExptInsightAnalysisRecordRepo(
2528
exptInsightAnalysisRecordDAO mysql.IExptInsightAnalysisRecordDAO,
2629
exptInsightAnalysisFeedbackCommentDAO mysql.IExptInsightAnalysisFeedbackCommentDAO,
2730
exptInsightAnalysisFeedbackVoteDAO mysql.IExptInsightAnalysisFeedbackVoteDAO,
2831
idgenerator idgen.IIDGenerator,
32+
writeTracker platestwrite.ILatestWriteTracker,
2933
) repo.IExptInsightAnalysisRecordRepo {
3034
return &ExptInsightAnalysisRecordRepo{
3135
exptInsightAnalysisRecordDAO: exptInsightAnalysisRecordDAO,
3236
exptInsightAnalysisFeedbackCommentDAO: exptInsightAnalysisFeedbackCommentDAO,
3337
exptInsightAnalysisFeedbackVoteDAO: exptInsightAnalysisFeedbackVoteDAO,
3438
idgenerator: idgenerator,
39+
writeTracker: writeTracker,
3540
}
3641
}
3742

@@ -47,15 +52,34 @@ func (e ExptInsightAnalysisRecordRepo) CreateAnalysisRecord(ctx context.Context,
4752
return 0, err
4853
}
4954

55+
if e.writeTracker != nil {
56+
e.writeTracker.SetWriteFlag(ctx, platestwrite.ResourceTypeExptInsightAnalysisRecord, id,
57+
platestwrite.SetWithSearchParam(buildRecordSearchParam(record.SpaceID, record.ExptID)))
58+
}
59+
5060
return id, nil
5161
}
5262

5363
func (e ExptInsightAnalysisRecordRepo) UpdateAnalysisRecord(ctx context.Context, record *entity.ExptInsightAnalysisRecord, opts ...db.Option) error {
54-
return e.exptInsightAnalysisRecordDAO.Update(ctx, convert.ExptInsightAnalysisRecordDOToPO(record), opts...)
64+
if err := e.exptInsightAnalysisRecordDAO.Update(ctx, convert.ExptInsightAnalysisRecordDOToPO(record), opts...); err != nil {
65+
return err
66+
}
67+
68+
if e.writeTracker != nil {
69+
e.writeTracker.SetWriteFlag(ctx, platestwrite.ResourceTypeExptInsightAnalysisRecord, record.ID,
70+
platestwrite.SetWithSearchParam(buildRecordSearchParam(record.SpaceID, record.ExptID)))
71+
}
72+
73+
return nil
5574
}
5675

5776
func (e ExptInsightAnalysisRecordRepo) GetAnalysisRecordByID(ctx context.Context, spaceID, exptID, recordID int64) (*entity.ExptInsightAnalysisRecord, error) {
58-
po, err := e.exptInsightAnalysisRecordDAO.GetByID(ctx, spaceID, exptID, recordID)
77+
opts := make([]db.Option, 0)
78+
if e.needForceMasterByRecord(ctx, platestwrite.ResourceTypeExptInsightAnalysisRecord, recordID, buildRecordSearchParam(spaceID, exptID)) {
79+
opts = append(opts, db.WithMaster())
80+
}
81+
82+
po, err := e.exptInsightAnalysisRecordDAO.GetByID(ctx, spaceID, exptID, recordID, opts...)
5983
if err != nil {
6084
return nil, err
6185
}
@@ -64,7 +88,11 @@ func (e ExptInsightAnalysisRecordRepo) GetAnalysisRecordByID(ctx context.Context
6488
}
6589

6690
func (e ExptInsightAnalysisRecordRepo) ListAnalysisRecord(ctx context.Context, spaceID, exptID int64, page entity.Page) ([]*entity.ExptInsightAnalysisRecord, int64, error) {
67-
pos, total, err := e.exptInsightAnalysisRecordDAO.List(ctx, spaceID, exptID, page)
91+
opts := make([]db.Option, 0)
92+
if e.needForceMasterByRecord(ctx, platestwrite.ResourceTypeExptInsightAnalysisRecord, 0, buildRecordSearchParam(spaceID, exptID)) {
93+
opts = append(opts, db.WithMaster())
94+
}
95+
pos, total, err := e.exptInsightAnalysisRecordDAO.List(ctx, spaceID, exptID, page, opts...)
6896
if err != nil {
6997
return nil, 0, err
7098
}
@@ -77,7 +105,14 @@ func (e ExptInsightAnalysisRecordRepo) ListAnalysisRecord(ctx context.Context, s
77105
}
78106

79107
func (e ExptInsightAnalysisRecordRepo) DeleteAnalysisRecord(ctx context.Context, spaceID, exptID, recordID int64) error {
80-
return e.exptInsightAnalysisRecordDAO.Delete(ctx, spaceID, exptID, recordID)
108+
if err := e.exptInsightAnalysisRecordDAO.Delete(ctx, spaceID, exptID, recordID); err != nil {
109+
return err
110+
}
111+
if e.writeTracker != nil {
112+
e.writeTracker.SetWriteFlag(ctx, platestwrite.ResourceTypeExptInsightAnalysisRecord, recordID,
113+
platestwrite.SetWithSearchParam(buildRecordSearchParam(spaceID, exptID)))
114+
}
115+
return nil
81116
}
82117

83118
func (e ExptInsightAnalysisRecordRepo) CreateFeedbackComment(ctx context.Context, feedbackComment *entity.ExptInsightAnalysisFeedbackComment, opts ...db.Option) error {
@@ -86,23 +121,56 @@ func (e ExptInsightAnalysisRecordRepo) CreateFeedbackComment(ctx context.Context
86121
return err
87122
}
88123
feedbackComment.ID = id
89-
return e.exptInsightAnalysisFeedbackCommentDAO.Create(ctx, convert.ExptInsightAnalysisFeedbackCommentDOToPO(feedbackComment), opts...)
124+
if err := e.exptInsightAnalysisFeedbackCommentDAO.Create(ctx, convert.ExptInsightAnalysisFeedbackCommentDOToPO(feedbackComment), opts...); err != nil {
125+
return err
126+
}
127+
if e.writeTracker != nil {
128+
e.writeTracker.SetWriteFlag(ctx, platestwrite.ResourceTypeExptInsightAnalysisFeedback, feedbackComment.AnalysisRecordID,
129+
platestwrite.SetWithSearchParam(buildFeedbackSearchParam(feedbackComment.SpaceID, feedbackComment.ExptID, feedbackComment.AnalysisRecordID)))
130+
}
131+
return nil
90132
}
91133

92134
func (e ExptInsightAnalysisRecordRepo) UpdateFeedbackComment(ctx context.Context, feedbackComment *entity.ExptInsightAnalysisFeedbackComment, opts ...db.Option) error {
93-
return e.exptInsightAnalysisFeedbackCommentDAO.Update(ctx, convert.ExptInsightAnalysisFeedbackCommentDOToPO(feedbackComment), opts...)
135+
if err := e.exptInsightAnalysisFeedbackCommentDAO.Update(ctx, convert.ExptInsightAnalysisFeedbackCommentDOToPO(feedbackComment), opts...); err != nil {
136+
return err
137+
}
138+
if e.writeTracker != nil {
139+
e.writeTracker.SetWriteFlag(ctx, platestwrite.ResourceTypeExptInsightAnalysisFeedback, feedbackComment.AnalysisRecordID,
140+
platestwrite.SetWithSearchParam(buildFeedbackSearchParam(feedbackComment.SpaceID, feedbackComment.ExptID, feedbackComment.AnalysisRecordID)))
141+
}
142+
return nil
94143
}
95144

96145
func (e ExptInsightAnalysisRecordRepo) GetFeedbackCommentByRecordID(ctx context.Context, spaceID, exptID, recordID int64, opts ...db.Option) (*entity.ExptInsightAnalysisFeedbackComment, error) {
97-
po, err := e.exptInsightAnalysisFeedbackCommentDAO.GetByRecordID(ctx, spaceID, exptID, recordID, opts...)
146+
innerOpts := append([]db.Option{}, opts...)
147+
if e.needForceMasterByRecord(ctx, platestwrite.ResourceTypeExptInsightAnalysisFeedback, recordID, buildFeedbackSearchParam(spaceID, exptID, recordID)) && !db.ContainWithMasterOpt(innerOpts) {
148+
innerOpts = append(innerOpts, db.WithMaster())
149+
}
150+
po, err := e.exptInsightAnalysisFeedbackCommentDAO.GetByRecordID(ctx, spaceID, exptID, recordID, innerOpts...)
98151
if err != nil {
99152
return nil, err
100153
}
101154
return convert.ExptInsightAnalysisFeedbackCommentPOToDO(po), nil
102155
}
103156

104157
func (e ExptInsightAnalysisRecordRepo) DeleteFeedbackComment(ctx context.Context, spaceID, exptID, commentID int64) error {
105-
return e.exptInsightAnalysisFeedbackCommentDAO.Delete(ctx, spaceID, exptID, commentID)
158+
po, err := e.exptInsightAnalysisFeedbackCommentDAO.GetByID(ctx, spaceID, exptID, commentID, db.WithMaster())
159+
if err != nil {
160+
return err
161+
}
162+
if err := e.exptInsightAnalysisFeedbackCommentDAO.Delete(ctx, spaceID, exptID, commentID); err != nil {
163+
return err
164+
}
165+
recordID := int64(0)
166+
if po.AnalysisRecordID != nil {
167+
recordID = *po.AnalysisRecordID
168+
}
169+
if e.writeTracker != nil && recordID > 0 {
170+
e.writeTracker.SetWriteFlag(ctx, platestwrite.ResourceTypeExptInsightAnalysisFeedback, recordID,
171+
platestwrite.SetWithSearchParam(buildFeedbackSearchParam(po.SpaceID, po.ExptID, recordID)))
172+
}
173+
return nil
106174
}
107175

108176
func (e ExptInsightAnalysisRecordRepo) CreateFeedbackVote(ctx context.Context, feedbackVote *entity.ExptInsightAnalysisFeedbackVote, opts ...db.Option) error {
@@ -111,27 +179,53 @@ func (e ExptInsightAnalysisRecordRepo) CreateFeedbackVote(ctx context.Context, f
111179
return err
112180
}
113181
feedbackVote.ID = id
114-
return e.exptInsightAnalysisFeedbackVoteDAO.Create(ctx, convert.ExptInsightAnalysisFeedbackVoteDOToPO(feedbackVote), opts...)
182+
if err := e.exptInsightAnalysisFeedbackVoteDAO.Create(ctx, convert.ExptInsightAnalysisFeedbackVoteDOToPO(feedbackVote), opts...); err != nil {
183+
return err
184+
}
185+
if e.writeTracker != nil {
186+
e.writeTracker.SetWriteFlag(ctx, platestwrite.ResourceTypeExptInsightAnalysisFeedback, feedbackVote.AnalysisRecordID,
187+
platestwrite.SetWithSearchParam(buildFeedbackSearchParam(feedbackVote.SpaceID, feedbackVote.ExptID, feedbackVote.AnalysisRecordID)))
188+
}
189+
return nil
115190
}
116191

117192
func (e ExptInsightAnalysisRecordRepo) UpdateFeedbackVote(ctx context.Context, feedbackVote *entity.ExptInsightAnalysisFeedbackVote, opts ...db.Option) error {
118-
return e.exptInsightAnalysisFeedbackVoteDAO.Update(ctx, convert.ExptInsightAnalysisFeedbackVoteDOToPO(feedbackVote), opts...)
193+
if err := e.exptInsightAnalysisFeedbackVoteDAO.Update(ctx, convert.ExptInsightAnalysisFeedbackVoteDOToPO(feedbackVote), opts...); err != nil {
194+
return err
195+
}
196+
if e.writeTracker != nil {
197+
e.writeTracker.SetWriteFlag(ctx, platestwrite.ResourceTypeExptInsightAnalysisFeedback, feedbackVote.AnalysisRecordID,
198+
platestwrite.SetWithSearchParam(buildFeedbackSearchParam(feedbackVote.SpaceID, feedbackVote.ExptID, feedbackVote.AnalysisRecordID)))
199+
}
200+
return nil
119201
}
120202

121203
func (e ExptInsightAnalysisRecordRepo) GetFeedbackVoteByUser(ctx context.Context, spaceID, exptID, recordID int64, userID string, opts ...db.Option) (*entity.ExptInsightAnalysisFeedbackVote, error) {
122-
po, err := e.exptInsightAnalysisFeedbackVoteDAO.GetByUser(ctx, spaceID, exptID, recordID, userID, opts...)
204+
innerOpts := append([]db.Option{}, opts...)
205+
if e.needForceMasterByRecord(ctx, platestwrite.ResourceTypeExptInsightAnalysisFeedback, recordID, buildFeedbackSearchParam(spaceID, exptID, recordID)) && !db.ContainWithMasterOpt(innerOpts) {
206+
innerOpts = append(innerOpts, db.WithMaster())
207+
}
208+
po, err := e.exptInsightAnalysisFeedbackVoteDAO.GetByUser(ctx, spaceID, exptID, recordID, userID, innerOpts...)
123209
if err != nil {
124210
return nil, err
125211
}
126212
return convert.ExptInsightAnalysisFeedbackVotePOToDO(po), nil
127213
}
128214

129215
func (e ExptInsightAnalysisRecordRepo) CountFeedbackVote(ctx context.Context, spaceID, exptID, recordID int64) (int64, int64, error) {
130-
return e.exptInsightAnalysisFeedbackVoteDAO.Count(ctx, spaceID, exptID, recordID)
216+
opts := make([]db.Option, 0)
217+
if e.needForceMasterByRecord(ctx, platestwrite.ResourceTypeExptInsightAnalysisFeedback, recordID, buildFeedbackSearchParam(spaceID, exptID, recordID)) {
218+
opts = append(opts, db.WithMaster())
219+
}
220+
return e.exptInsightAnalysisFeedbackVoteDAO.Count(ctx, spaceID, exptID, recordID, opts...)
131221
}
132222

133223
func (e ExptInsightAnalysisRecordRepo) List(ctx context.Context, spaceID, exptID, recordID int64, page entity.Page) ([]*entity.ExptInsightAnalysisFeedbackComment, int64, error) {
134-
pos, total, err := e.exptInsightAnalysisFeedbackCommentDAO.List(ctx, spaceID, exptID, recordID, page)
224+
opts := make([]db.Option, 0)
225+
if e.needForceMasterByRecord(ctx, platestwrite.ResourceTypeExptInsightAnalysisFeedback, recordID, buildFeedbackSearchParam(spaceID, exptID, recordID)) {
226+
opts = append(opts, db.WithMaster())
227+
}
228+
pos, total, err := e.exptInsightAnalysisFeedbackCommentDAO.List(ctx, spaceID, exptID, recordID, page, opts...)
135229
if err != nil {
136230
return nil, 0, err
137231
}
@@ -141,3 +235,24 @@ func (e ExptInsightAnalysisRecordRepo) List(ctx context.Context, spaceID, exptID
141235
}
142236
return dos, total, nil
143237
}
238+
239+
func (e ExptInsightAnalysisRecordRepo) needForceMasterByRecord(ctx context.Context, resourceType platestwrite.ResourceType, resourceID int64, searchParam string) bool {
240+
if e.writeTracker == nil {
241+
return false
242+
}
243+
if resourceID > 0 && e.writeTracker.CheckWriteFlagByID(ctx, resourceType, resourceID) {
244+
return true
245+
}
246+
if searchParam != "" && e.writeTracker.CheckWriteFlagBySearchParam(ctx, resourceType, searchParam) {
247+
return true
248+
}
249+
return false
250+
}
251+
252+
func buildRecordSearchParam(spaceID, exptID int64) string {
253+
return fmt.Sprintf("%d:%d", spaceID, exptID)
254+
}
255+
256+
func buildFeedbackSearchParam(spaceID, exptID, recordID int64) string {
257+
return fmt.Sprintf("%d:%d:%d", spaceID, exptID, recordID)
258+
}

0 commit comments

Comments
 (0)