Revert "Policy rules remove redundant function (#193)" (#199)

This reverts commit c4afeee5b3.
This commit is contained in:
Igor Gov 2021-08-10 18:04:30 +03:00 committed by GitHub
parent 59dec1a547
commit cbe04af801
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 27 deletions

View File

@ -54,9 +54,17 @@ type BaseEntryDetails struct {
}
type ApplicableRules struct {
Latency int64 `json:"latency,omitempty"`
Status bool `json:"status,omitempty"`
NumberOfRules int `json:"numberOfRules,omitempty"`
Latency int64 `json:"latency,omitempty"`
Status bool `json:"status,omitempty"`
NumberOfRules int `json:"numberOfRules,omitempty"`
}
func NewApplicableRules(status bool, latency int64, number int) ApplicableRules {
ar := ApplicableRules{}
ar.Status = status
ar.Latency = latency
ar.NumberOfRules = number
return ar
}
type FullEntryDetails struct {
@ -211,6 +219,8 @@ func (fewp *FullEntryWithPolicy) UnmarshalData(entry *MizuEntry) error {
}
func RunValidationRulesState(harEntry har.Entry, service string) ApplicableRules {
_, resultPolicyToSend := rules.MatchRequestPolicy(harEntry, service)
return rules.PassedValidationRules(resultPolicyToSend)
numberOfRules, resultPolicyToSend := rules.MatchRequestPolicy(harEntry, service)
statusPolicyToSend, latency, numberOfRules := rules.PassedValidationRules(resultPolicyToSend, numberOfRules)
ar := NewApplicableRules(statusPolicyToSend, latency, numberOfRules)
return ar
}

View File

@ -3,7 +3,6 @@ package rules
import (
"encoding/json"
"fmt"
"mizuserver/pkg/models"
"reflect"
"regexp"
"strings"
@ -93,35 +92,19 @@ func MatchRequestPolicy(harEntry har.Entry, service string) (int, []RulesMatched
return len(enforcePolicy.Rules), resultPolicyToSend
}
func PassedValidationRules(rulesMatched []RulesMatched) models.ApplicableRules {
func PassedValidationRules(rulesMatched []RulesMatched, numberOfRules int) (bool, int64, int) {
if len(rulesMatched) == 0 {
return models.ApplicableRules{
Status: false,
Latency: 0,
NumberOfRules: 0,
}
return false, 0, 0
}
for _, rule := range rulesMatched {
if rule.Matched == false {
return models.ApplicableRules{
Status: false,
Latency: -1,
NumberOfRules: len(rulesMatched),
}
return false, -1, len(rulesMatched)
}
}
for _, rule := range rulesMatched {
if strings.ToLower(rule.Rule.Type) == "latency" {
return models.ApplicableRules{
Status: true,
Latency: rule.Rule.Latency,
NumberOfRules: len(rulesMatched),
}
return true, rule.Rule.Latency, len(rulesMatched)
}
}
return models.ApplicableRules{
Status: true,
Latency: -1,
NumberOfRules: len(rulesMatched),
}
return true, -1, len(rulesMatched)
}