Prechádzať zdrojové kódy

fix #1158 : group by value and scope and get only longest validity

bui 3 rokov pred
rodič
commit
034aef81f5
1 zmenil súbory, kde vykonal 6 pridanie a 0 odobranie
  1. 6 0
      pkg/database/decisions.go

+ 6 - 0
pkg/database/decisions.go

@@ -137,6 +137,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
 }
 
@@ -162,6 +163,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)