Compare commits

...

2 commits

Author SHA1 Message Date
bui
1ec70fa5e0 deduplicate decisions 2022-01-13 11:52:30 +01:00
bui
034aef81f5 fix #1158 : group by value and scope and get only longest validity 2022-01-12 15:46:02 +01:00
2 changed files with 45 additions and 3 deletions

View file

@ -37,6 +37,7 @@ func (c *Controller) GetDecision(gctx *gin.Context) {
var results []*models.Decision
var data []*ent.Decision
log.Printf("hey yoyoy #1")
data, err = c.DBClient.QueryDecisionWithFilter(gctx.Request.URL.Query())
if err != nil {
c.HandleDBErrors(gctx, err)

View file

@ -2,6 +2,7 @@ package database
import (
"fmt"
"log"
"strings"
"time"
@ -137,6 +138,7 @@ func BuildDecisionRequestWithFilter(query *ent.DecisionQuery, filter map[string]
} else if ip_sz != 0 {
return nil, errors.Wrapf(InvalidFilter, "Unknown ip size %d", ip_sz)
}
return query, nil
}
@ -151,7 +153,7 @@ func (c *Client) QueryDecisionWithFilter(filter map[string][]string) ([]*ent.Dec
if err != nil {
return []*ent.Decision{}, err
}
log.Printf("here yo")
err = decisions.Select(
decision.FieldID,
decision.FieldUntil,
@ -162,6 +164,11 @@ func (c *Client) QueryDecisionWithFilter(filter map[string][]string) ([]*ent.Dec
decision.FieldValue,
decision.FieldScope,
decision.FieldOrigin,
).GroupBy( /*group by value + scope, we don't want duplicate*/
decision.FieldValue,
decision.FieldScope,
).Aggregate( /*and we want the decision with longest validity*/
ent.Max(decision.FieldUntil),
).Scan(c.CTX, &data)
if err != nil {
c.Log.Warningf("QueryDecisionWithFilter : %s", err)
@ -172,6 +179,8 @@ func (c *Client) QueryDecisionWithFilter(filter map[string][]string) ([]*ent.Dec
}
func (c *Client) QueryAllDecisionsWithFilters(filters map[string][]string) ([]*ent.Decision, error) {
var data []*ent.Decision
query := c.Ent.Decision.Query().Where(decision.UntilGT(time.Now()))
query, err := BuildDecisionRequestWithFilter(query, filters)
@ -180,7 +189,22 @@ func (c *Client) QueryAllDecisionsWithFilters(filters map[string][]string) ([]*e
return []*ent.Decision{}, errors.Wrap(QueryFail, "get all decisions with filters")
}
data, err := query.All(c.CTX)
err = query.Select(
decision.FieldID,
decision.FieldUntil,
decision.FieldScenario,
decision.FieldType,
decision.FieldStartIP,
decision.FieldEndIP,
decision.FieldValue,
decision.FieldScope,
decision.FieldOrigin,
).GroupBy( /*group by value + scope, we don't want duplicate*/
decision.FieldValue,
decision.FieldScope,
).Aggregate( /*and we want the decision with longest validity*/
ent.Max(decision.FieldUntil),
).Scan(c.CTX, &data)
if err != nil {
c.Log.Warningf("QueryAllDecisionsWithFilters : %s", err)
return []*ent.Decision{}, errors.Wrap(QueryFail, "get all decisions with filters")
@ -222,13 +246,30 @@ func (c *Client) QueryExpiredDecisionsSinceWithFilters(since time.Time, filters
}
func (c *Client) QueryNewDecisionsSinceWithFilters(since time.Time, filters map[string][]string) ([]*ent.Decision, error) {
var data []*ent.Decision
query := c.Ent.Decision.Query().Where(decision.CreatedAtGT(since)).Where(decision.UntilGT(time.Now()))
query, err := BuildDecisionRequestWithFilter(query, filters)
if err != nil {
c.Log.Warningf("QueryNewDecisionsSinceWithFilters : %s", err)
return []*ent.Decision{}, errors.Wrapf(QueryFail, "new decisions since '%s'", since.String())
}
data, err := query.All(c.CTX)
err = query.Select(
decision.FieldID,
decision.FieldUntil,
decision.FieldScenario,
decision.FieldType,
decision.FieldStartIP,
decision.FieldEndIP,
decision.FieldValue,
decision.FieldScope,
decision.FieldOrigin,
).GroupBy( /*group by value + scope, we don't want duplicate*/
decision.FieldValue,
decision.FieldScope,
).Aggregate( /*and we want the decision with longest validity*/
ent.Max(decision.FieldUntil),
).Scan(c.CTX, &data)
if err != nil {
c.Log.Warningf("QueryNewDecisionsSinceWithFilters : %s", err)
return []*ent.Decision{}, errors.Wrapf(QueryFail, "new decisions since '%s'", since.String())