• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / reporting / 743020307

pending completion
743020307

Pull #87

gitlab-ci

Fabio Tranchitella
chore(tests): add unit test case for too many terms in nested aggregations
Pull Request #87: MEN-5930: index device deployment objects

825 of 929 new or added lines in 17 files covered. (88.81%)

1 existing line in 1 file now uncovered.

2735 of 3182 relevant lines covered (85.95%)

17.25 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.67
/model/aggregations_deployments.go
1
// Copyright 2023 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
package model
16

17
import (
18
        validation "github.com/go-ozzo/ozzo-validation/v4"
19
        "github.com/pkg/errors"
20
)
21

22
type AggregateDeploymentsParams struct {
23
        Aggregations []DeploymentsAggregationTerm `json:"aggregations"`
24
        Filters      []DeploymentsFilterPredicate `json:"filters"`
25
        TenantID     string                       `json:"-"`
26
}
27

28
type DeploymentsAggregationTerm struct {
29
        Name         string                       `json:"name"`
30
        Attribute    string                       `json:"attribute"`
31
        Limit        int                          `json:"limit"`
32
        Aggregations []DeploymentsAggregationTerm `json:"aggregations"`
33
}
34

35
func checkMaxNestedDeploymentsAggregationsWithLimit(value interface{}, limit uint) error {
14✔
36
        if limit <= 0 {
16✔
37
                return errors.Errorf("too many nested aggregations, limit is %d", maxNestedAggregations)
2✔
38
        }
2✔
39
        if aggs, ok := value.([]DeploymentsAggregationTerm); ok {
24✔
40
                for _, agg := range aggs {
24✔
41
                        if len(agg.Aggregations) > 0 {
22✔
42
                                return checkMaxNestedDeploymentsAggregationsWithLimit(agg.Aggregations, limit-1)
10✔
43
                        }
10✔
44
                }
45
        }
46
        return nil
2✔
47
}
48

49
func checkMaxNestedDeploymentsAggregations(value interface{}) error {
4✔
50
        return checkMaxNestedDeploymentsAggregationsWithLimit(value, maxNestedAggregations)
4✔
51
}
4✔
52

53
func (sp AggregateDeploymentsParams) Validate() error {
16✔
54
        err := validation.ValidateStruct(&sp,
16✔
55
                validation.Field(&sp.Aggregations, validation.Required,
16✔
56
                        validation.Length(1, maxAggregationTerms)))
16✔
57
        if err != nil {
24✔
58
                return err
8✔
59
        }
8✔
60

61
        for _, f := range sp.Filters {
14✔
62
                err := f.Validate()
6✔
63
                if err != nil {
8✔
64
                        return err
2✔
65
                }
2✔
66
        }
67
        return nil
6✔
68
}
69

70
func (f DeploymentsAggregationTerm) Validate() error {
18✔
71
        return validation.ValidateStruct(&f,
18✔
72
                validation.Field(&f.Name, validation.Required),
18✔
73
                validation.Field(&f.Attribute, validation.Required),
18✔
74
                validation.Field(&f.Limit, validation.Min(0)),
18✔
75
                validation.Field(&f.Aggregations, validation.When(
18✔
76
                        len(f.Aggregations) > 0,
18✔
77
                        validation.Length(0, maxAggregationTerms),
18✔
78
                        validation.By(checkMaxNestedDeploymentsAggregations),
18✔
79
                )),
18✔
80
        )
18✔
81
}
18✔
82

83
func BuildDeploymentsAggregations(terms []DeploymentsAggregationTerm) (*Aggregations, error) {
10✔
84
        aggs := Aggregations{}
10✔
85
        for _, term := range terms {
20✔
86
                terms := map[string]interface{}{
10✔
87
                        "field": term.Attribute,
10✔
88
                }
10✔
89
                limit := term.Limit
10✔
90
                if limit <= 0 {
16✔
91
                        limit = defaultAggregationLimit
6✔
92
                }
6✔
93
                terms["size"] = limit
10✔
94
                agg := map[string]interface{}{
10✔
95
                        "terms": terms,
10✔
96
                }
10✔
97
                if len(term.Aggregations) > 0 {
12✔
98
                        subaggs, err := BuildDeploymentsAggregations(term.Aggregations)
2✔
99
                        if err != nil {
2✔
NEW
100
                                return nil, err
×
NEW
101
                        }
×
102
                        agg["aggs"] = subaggs
2✔
103
                }
104
                aggs[term.Name] = agg
10✔
105
        }
106
        return &aggs, nil
10✔
107
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc