Skip to content

Commit

Permalink
WIP: Parse thresholds outside of the json.Marshal
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Jan 14, 2022
1 parent 680f6fd commit bea617a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
7 changes: 7 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ a commandline interface for interacting with it.`,
if err != nil {
return err
}
if !runtimeOptions.NoThresholds.Bool {
for _, thresholds := range conf.Options.Thresholds {
if err := thresholds.Parse(); err != nil {
return errext.WithExitCodeIfNone(err, exitcodes.InvalidConfig)
}
}
}

// Write options back to the runner too.
if err = initRunner.SetOptions(conf.Options); err != nil {
Expand Down
43 changes: 22 additions & 21 deletions stats/thresholds.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,12 @@ type Threshold struct {
parsed *thresholdExpression
}

func newThreshold(src string, abortOnFail bool, gracePeriod types.NullDuration) (*Threshold, error) {
parsedExpression, err := parseThresholdExpression(src)
if err != nil {
return nil, err
}

func newThreshold(src string, abortOnFail bool, gracePeriod types.NullDuration) *Threshold {
return &Threshold{
Source: src,
AbortOnFail: abortOnFail,
AbortGracePeriod: gracePeriod,
parsed: parsedExpression,
}, nil
}
}

func (t *Threshold) runNoTaint(sinks map[string]float64) (bool, error) {
Expand Down Expand Up @@ -143,7 +137,7 @@ type Thresholds struct {
}

// NewThresholds returns Thresholds objects representing the provided source strings
func NewThresholds(sources []string) (Thresholds, error) {
func NewThresholds(sources []string) Thresholds {
tcs := make([]thresholdConfig, len(sources))
for i, source := range sources {
tcs[i].Threshold = source
Expand All @@ -152,19 +146,16 @@ func NewThresholds(sources []string) (Thresholds, error) {
return newThresholdsWithConfig(tcs)
}

func newThresholdsWithConfig(configs []thresholdConfig) (Thresholds, error) {
func newThresholdsWithConfig(configs []thresholdConfig) Thresholds {
thresholds := make([]*Threshold, len(configs))
sinked := make(map[string]float64)

for i, config := range configs {
t, err := newThreshold(config.Threshold, config.AbortOnFail, config.AbortGracePeriod)
if err != nil {
return Thresholds{}, fmt.Errorf("threshold %d error: %w", i, err)
}
t := newThreshold(config.Threshold, config.AbortOnFail, config.AbortGracePeriod)
thresholds[i] = t
}

return Thresholds{thresholds, false, sinked}, nil
return Thresholds{thresholds, false, sinked}
}

func (ts *Thresholds) runAll(timeSpentInTest time.Duration) (bool, error) {
Expand All @@ -190,6 +181,17 @@ func (ts *Thresholds) runAll(timeSpentInTest time.Duration) (bool, error) {
return succeeded, nil
}

func (ts *Thresholds) Parse() error {
for _, t := range ts.Thresholds {
parsed, err := parseThresholdExpression(t.Source)
if err != nil {
return err
}
t.parsed = parsed
}
return nil
}

// Run processes all the thresholds with the provided Sink at the provided time and returns if any
// of them fails
func (ts *Thresholds) Run(sink Sink, duration time.Duration) (bool, error) {
Expand Down Expand Up @@ -243,10 +245,7 @@ func (ts *Thresholds) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &configs); err != nil {
return err
}
newts, err := newThresholdsWithConfig(configs)
if err != nil {
return err
}
newts := newThresholdsWithConfig(configs)
*ts = newts
return nil
}
Expand Down Expand Up @@ -279,5 +278,7 @@ func MarshalJSONWithoutHTMLEscape(t interface{}) ([]byte, error) {
return bytes, err
}

var _ json.Unmarshaler = &Thresholds{}
var _ json.Marshaler = &Thresholds{}
var (
_ json.Unmarshaler = &Thresholds{}
_ json.Marshaler = &Thresholds{}
)

0 comments on commit bea617a

Please sign in to comment.