Skip to content

Commit

Permalink
remove excessive sample reloads
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Jan 8, 2025
1 parent 8213019 commit 8f15d8f
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 82 deletions.
12 changes: 0 additions & 12 deletions app/bot/spam.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,6 @@ func (s *SpamFilter) UpdateSpam(msg string) error {
if err := s.Detector.UpdateSpam(cleanMsg); err != nil {
return fmt.Errorf("can't update spam samples: %w", err)
}
if err := s.ReloadSamples(); err != nil {
return fmt.Errorf("can't reload samples: %w", err)
}
return nil
}

Expand All @@ -129,9 +126,6 @@ func (s *SpamFilter) UpdateHam(msg string) error {
if err := s.Detector.UpdateHam(cleanMsg); err != nil {
return fmt.Errorf("can't update ham samples: %w", err)
}
if err := s.ReloadSamples(); err != nil {
return fmt.Errorf("can't reload samples: %w", err)
}
return nil
}

Expand All @@ -146,9 +140,6 @@ func (s *SpamFilter) AddApprovedUser(id int64, name string) error {
if err := s.Detector.AddApprovedUser(approved.UserInfo{UserID: fmt.Sprintf("%d", id), UserName: name}); err != nil {
return fmt.Errorf("failed to write approved user to storage: %w", err)
}
if err := s.ReloadSamples(); err != nil {
return fmt.Errorf("failed to reload samples: %w", err)
}
return nil
}

Expand All @@ -158,9 +149,6 @@ func (s *SpamFilter) RemoveApprovedUser(id int64) error {
if err := s.Detector.RemoveApprovedUser(fmt.Sprintf("%d", id)); err != nil {
return fmt.Errorf("failed to delete approved user from storage: %w", err)
}
if err := s.ReloadSamples(); err != nil {
return fmt.Errorf("failed to reload samples: %w", err)
}
return nil
}

Expand Down
73 changes: 4 additions & 69 deletions app/bot/spam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ func TestSpamFilter_UpdateSpam(t *testing.T) {
name string
message string
updateErr error
reloadErr error
expectError bool
}{
{
Expand All @@ -232,37 +231,15 @@ func TestSpamFilter_UpdateSpam(t *testing.T) {
updateErr: errors.New("update error"),
expectError: true,
},
{
name: "reload error",
message: "spam",
reloadErr: errors.New("reload error"),
expectError: true,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
det := &mocks.DetectorMock{
UpdateSpamFunc: func(msg string) error {
return tc.updateErr
},
LoadSamplesFunc: func(exclReader io.Reader, spamReaders []io.Reader, hamReaders []io.Reader) (tgspam.LoadResult, error) {
return tgspam.LoadResult{}, tc.reloadErr
},
LoadStopWordsFunc: func(readers ...io.Reader) (tgspam.LoadResult, error) {
return tgspam.LoadResult{}, nil
},
}

samplesStore := &mocks.SamplesStoreMock{
StatsFunc: func(ctx context.Context) (*storage.SamplesStats, error) {
return &storage.SamplesStats{PresetSpam: 1, PresetHam: 1}, nil
},
ReaderFunc: func(ctx context.Context, t storage.SampleType, o storage.SampleOrigin) (io.ReadCloser, error) {
return io.NopCloser(strings.NewReader("")), nil
},
UpdateSpamFunc: func(msg string) error { return tc.updateErr },
}

samplesStore := &mocks.SamplesStoreMock{}
dictStore := &mocks.DictStoreMock{
ReaderFunc: func(ctx context.Context, t storage.DictionaryType) (io.ReadCloser, error) {
return io.NopCloser(strings.NewReader("")), nil
Expand All @@ -283,8 +260,6 @@ func TestSpamFilter_UpdateSpam(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 1, len(det.UpdateSpamCalls()))
assert.Equal(t, strings.ReplaceAll(tc.message, "\n", " "), det.UpdateSpamCalls()[0].Msg)

assert.Equal(t, 1, len(samplesStore.StatsCalls()))
})
}
}
Expand All @@ -294,7 +269,6 @@ func TestSpamFilter_UpdateHam(t *testing.T) {
name string
message string
updateErr error
reloadErr error
expectError bool
}{
{
Expand All @@ -308,37 +282,15 @@ func TestSpamFilter_UpdateHam(t *testing.T) {
updateErr: errors.New("update error"),
expectError: true,
},
{
name: "reload error",
message: "ham",
reloadErr: errors.New("reload error"),
expectError: true,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
det := &mocks.DetectorMock{
UpdateHamFunc: func(msg string) error {
return tc.updateErr
},
LoadSamplesFunc: func(exclReader io.Reader, spamReaders []io.Reader, hamReaders []io.Reader) (tgspam.LoadResult, error) {
return tgspam.LoadResult{}, tc.reloadErr
},
LoadStopWordsFunc: func(readers ...io.Reader) (tgspam.LoadResult, error) {
return tgspam.LoadResult{}, nil
},
}

samplesStore := &mocks.SamplesStoreMock{
StatsFunc: func(ctx context.Context) (*storage.SamplesStats, error) {
return &storage.SamplesStats{PresetSpam: 1, PresetHam: 1}, nil
},
ReaderFunc: func(ctx context.Context, t storage.SampleType, o storage.SampleOrigin) (io.ReadCloser, error) {
return io.NopCloser(strings.NewReader("")), nil
},
UpdateHamFunc: func(msg string) error { return tc.updateErr },
}

samplesStore := &mocks.SamplesStoreMock{}
dictStore := &mocks.DictStoreMock{
ReaderFunc: func(ctx context.Context, t storage.DictionaryType) (io.ReadCloser, error) {
return io.NopCloser(strings.NewReader("")), nil
Expand All @@ -359,8 +311,6 @@ func TestSpamFilter_UpdateHam(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 1, len(det.UpdateHamCalls()))
assert.Equal(t, strings.ReplaceAll(tc.message, "\n", " "), det.UpdateHamCalls()[0].Msg)

assert.Equal(t, 1, len(samplesStore.StatsCalls()))
})
}
}
Expand All @@ -372,7 +322,6 @@ func TestSpamFilter_ApprovedUsers(t *testing.T) {
userName string
operation string // "add" or "remove"
operationErr error
reloadErr error
expectError bool
}{
{
Expand All @@ -390,14 +339,6 @@ func TestSpamFilter_ApprovedUsers(t *testing.T) {
operationErr: errors.New("operation failed"),
expectError: true,
},
{
name: "add user reload error",
userID: 123,
userName: "test_user",
operation: "add",
reloadErr: errors.New("reload failed"),
expectError: true,
},
{
name: "remove user success",
userID: 123,
Expand Down Expand Up @@ -431,12 +372,6 @@ func TestSpamFilter_ApprovedUsers(t *testing.T) {
assert.Equal(t, strconv.FormatInt(tc.userID, 10), id)
return nil
},
LoadSamplesFunc: func(exclReader io.Reader, spamReaders []io.Reader, hamReaders []io.Reader) (tgspam.LoadResult, error) {
return tgspam.LoadResult{}, tc.reloadErr
},
LoadStopWordsFunc: func(readers ...io.Reader) (tgspam.LoadResult, error) {
return tgspam.LoadResult{}, nil
},
IsApprovedUserFunc: func(userID string) bool {
return userID == strconv.FormatInt(tc.userID, 10)
},
Expand Down
2 changes: 1 addition & 1 deletion app/webapi/webapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ func (s *Server) deleteSampleHandler(delFn func(msg string) error) func(w http.R
}
}

// reloadDynamicSamplesHandler handles PUT /samples request. It reloads dynamic samples from files
// reloadDynamicSamplesHandler handles PUT /samples request. It reloads dynamic samples from db storage.
func (s *Server) reloadDynamicSamplesHandler(w http.ResponseWriter, _ *http.Request) {
if err := s.SpamFilter.ReloadSamples(); err != nil {
w.WriteHeader(http.StatusInternalServerError)
Expand Down

0 comments on commit 8f15d8f

Please sign in to comment.