Просмотр исходного кода

allow user to disable decision deduplication (#1687)

* allow user to disable decision deduplication
Thibault "bui" Koechlin 2 лет назад
Родитель
Сommit
bacea50485
2 измененных файлов с 27 добавлено и 8 удалено
  1. 9 4
      pkg/apiserver/controllers/v1/decisions.go
  2. 18 4
      pkg/database/decisions.go

+ 9 - 4
pkg/apiserver/controllers/v1/decisions.go

@@ -140,6 +140,11 @@ func (c *Controller) StreamDecision(gctx *gin.Context) {
 		filters["scopes"] = []string{"ip,range"}
 	}
 
+	dedup := true
+	if v, ok := filters["dedup"]; ok && v[0] == "false" {
+		dedup = false
+	}
+
 	// if the blocker just start, return all decisions
 	if val, ok := gctx.Request.URL.Query()["startup"]; ok {
 		if val[0] == "true" {
@@ -150,7 +155,7 @@ func (c *Controller) StreamDecision(gctx *gin.Context) {
 				return
 			}
 			//data = KeepLongestDecision(data)
-			ret["new"], err = FormatDecisions(data, true)
+			ret["new"], err = FormatDecisions(data, dedup)
 			if err != nil {
 				log.Errorf("unable to format expired decision for '%s' : %v", bouncerInfo.Name, err)
 				gctx.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
@@ -164,7 +169,7 @@ func (c *Controller) StreamDecision(gctx *gin.Context) {
 				gctx.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
 				return
 			}
-			ret["deleted"], err = FormatDecisions(data, true)
+			ret["deleted"], err = FormatDecisions(data, dedup)
 			if err != nil {
 				log.Errorf("unable to format expired decision for '%s' : %v", bouncerInfo.Name, err)
 				gctx.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
@@ -193,7 +198,7 @@ func (c *Controller) StreamDecision(gctx *gin.Context) {
 		return
 	}
 	//data = KeepLongestDecision(data)
-	ret["new"], err = FormatDecisions(data, true)
+	ret["new"], err = FormatDecisions(data, dedup)
 	if err != nil {
 		log.Errorf("unable to format new decision for '%s' : %v", bouncerInfo.Name, err)
 		gctx.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
@@ -207,7 +212,7 @@ func (c *Controller) StreamDecision(gctx *gin.Context) {
 		gctx.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
 		return
 	}
-	ret["deleted"], err = FormatDecisions(data, true)
+	ret["deleted"], err = FormatDecisions(data, dedup)
 	if err != nil {
 		log.Errorf("unable to format expired decision for '%s' : %v", bouncerInfo.Name, err)
 		gctx.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})

+ 18 - 4
pkg/database/decisions.go

@@ -97,8 +97,12 @@ func BuildDecisionRequestWithFilter(query *ent.DecisionQuery, filter map[string]
 func (c *Client) QueryAllDecisionsWithFilters(filters map[string][]string) ([]*ent.Decision, error) {
 	query := c.Ent.Decision.Query().Where(
 		decision.UntilGT(time.Now().UTC()),
-		longestDecisionForScopeTypeValue,
 	)
+	//Allow a bouncer to ask for non-deduplicated results
+	if v, ok := filters["dedup"]; !ok || v[0] != "false" {
+		query = query.Where(longestDecisionForScopeTypeValue)
+	}
+
 	query, err := BuildDecisionRequestWithFilter(query, filters)
 
 	if err != nil {
@@ -117,8 +121,12 @@ func (c *Client) QueryAllDecisionsWithFilters(filters map[string][]string) ([]*e
 func (c *Client) QueryExpiredDecisionsWithFilters(filters map[string][]string) ([]*ent.Decision, error) {
 	query := c.Ent.Decision.Query().Where(
 		decision.UntilLT(time.Now().UTC()),
-		longestDecisionForScopeTypeValue,
 	)
+	//Allow a bouncer to ask for non-deduplicated results
+	if v, ok := filters["dedup"]; !ok || v[0] != "false" {
+		query = query.Where(longestDecisionForScopeTypeValue)
+	}
+
 	query, err := BuildDecisionRequestWithFilter(query, filters)
 
 	if err != nil {
@@ -219,8 +227,11 @@ func (c *Client) QueryExpiredDecisionsSinceWithFilters(since time.Time, filters
 	query := c.Ent.Decision.Query().Where(
 		decision.UntilLT(time.Now().UTC()),
 		decision.UntilGT(since),
-		longestDecisionForScopeTypeValue,
 	)
+	//Allow a bouncer to ask for non-deduplicated results
+	if v, ok := filters["dedup"]; !ok || v[0] != "false" {
+		query = query.Where(longestDecisionForScopeTypeValue)
+	}
 	query, err := BuildDecisionRequestWithFilter(query, filters)
 	if err != nil {
 		c.Log.Warningf("QueryExpiredDecisionsSinceWithFilters : %s", err)
@@ -240,8 +251,11 @@ func (c *Client) QueryNewDecisionsSinceWithFilters(since time.Time, filters map[
 	query := c.Ent.Decision.Query().Where(
 		decision.CreatedAtGT(since),
 		decision.UntilGT(time.Now().UTC()),
-		longestDecisionForScopeTypeValue,
 	)
+	//Allow a bouncer to ask for non-deduplicated results
+	if v, ok := filters["dedup"]; !ok || v[0] != "false" {
+		query = query.Where(longestDecisionForScopeTypeValue)
+	}
 	query, err := BuildDecisionRequestWithFilter(query, filters)
 	if err != nil {
 		c.Log.Warningf("QueryNewDecisionsSinceWithFilters : %s", err)