Skip to content

Commit

Permalink
remove unused code: HandleDeletedDecisions() (#3301)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc authored Oct 29, 2024
1 parent 53f9bc5 commit 96d4da7
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 69 deletions.
31 changes: 0 additions & 31 deletions pkg/apiserver/apic.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,37 +423,6 @@ func (a *apic) CAPIPullIsOld(ctx context.Context) (bool, error) {
return true, nil
}

func (a *apic) HandleDeletedDecisions(deletedDecisions []*models.Decision, deleteCounters map[string]map[string]int) (int, error) {
ctx := context.TODO()
nbDeleted := 0

for _, decision := range deletedDecisions {
filter := map[string][]string{
"value": {*decision.Value},
"origin": {*decision.Origin},
}
if strings.ToLower(*decision.Scope) != "ip" {
filter["type"] = []string{*decision.Type}
filter["scopes"] = []string{*decision.Scope}
}

dbCliRet, _, err := a.dbClient.ExpireDecisionsWithFilter(ctx, filter)
if err != nil {
return 0, fmt.Errorf("expiring decisions error: %w", err)
}

dbCliDel, err := strconv.Atoi(dbCliRet)
if err != nil {
return 0, fmt.Errorf("converting db ret %d: %w", dbCliDel, err)
}

updateCounterForDecision(deleteCounters, decision.Origin, decision.Scenario, dbCliDel)
nbDeleted += dbCliDel
}

return nbDeleted, nil
}

func (a *apic) HandleDeletedDecisionsV3(ctx context.Context, deletedDecisions []*modelscapi.GetDecisionsStreamResponseDeletedItem, deleteCounters map[string]map[string]int) (int, error) {
var nbDeleted int

Expand Down
38 changes: 0 additions & 38 deletions pkg/apiserver/apic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,44 +255,6 @@ func TestNewAPIC(t *testing.T) {
}
}

func TestAPICHandleDeletedDecisions(t *testing.T) {
ctx := context.Background()
api := getAPIC(t, ctx)
_, deleteCounters := makeAddAndDeleteCounters()

decision1 := api.dbClient.Ent.Decision.Create().
SetUntil(time.Now().Add(time.Hour)).
SetScenario("crowdsec/test").
SetType("ban").
SetScope("IP").
SetValue("1.2.3.4").
SetOrigin(types.CAPIOrigin).
SaveX(context.Background())

api.dbClient.Ent.Decision.Create().
SetUntil(time.Now().Add(time.Hour)).
SetScenario("crowdsec/test").
SetType("ban").
SetScope("IP").
SetValue("1.2.3.4").
SetOrigin(types.CAPIOrigin).
SaveX(context.Background())

assertTotalDecisionCount(t, ctx, api.dbClient, 2)

nbDeleted, err := api.HandleDeletedDecisions([]*models.Decision{{
Value: ptr.Of("1.2.3.4"),
Origin: ptr.Of(types.CAPIOrigin),
Type: &decision1.Type,
Scenario: ptr.Of("crowdsec/test"),
Scope: ptr.Of("IP"),
}}, deleteCounters)

require.NoError(t, err)
assert.Equal(t, 2, nbDeleted)
assert.Equal(t, 2, deleteCounters[types.CAPIOrigin]["all"])
}

func TestAPICGetMetrics(t *testing.T) {
ctx := context.Background()

Expand Down

0 comments on commit 96d4da7

Please sign in to comment.