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

mendersoftware / reporting / 732508278

pending completion
732508278

Pull #87

gitlab-ci

Fabio Tranchitella
refac: rename internal search end-point to `/tenants/{tenant_id}/devices/search`
Pull Request #87: MEN-5930: index device deployment objects

331 of 402 new or added lines in 12 files covered. (82.34%)

135 existing lines in 5 files now uncovered.

2196 of 2600 relevant lines covered (84.46%)

17.72 hits per line

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

95.56
/model/aggregations.go
1
// Copyright 2022 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 validation "github.com/go-ozzo/ozzo-validation/v4"
18

19
const defaultAggregationLimit = 10
20
const maxAggregationTerms = 100
21

22
type AggregateParams struct {
23
        Aggregations []AggregationTerm `json:"aggregations"`
24
        Filters      []FilterPredicate `json:"filters"`
25
        Groups       []string          `json:"-"`
26
        TenantID     string            `json:"-"`
27
}
28

29
type AggregationTerm struct {
30
        Name         string            `json:"name"`
31
        Attribute    string            `json:"attribute"`
32
        Scope        string            `json:"scope"`
33
        Limit        int               `json:"limit"`
34
        Aggregations []AggregationTerm `json:"aggregations"`
35
}
36

37
func (sp AggregateParams) Validate() error {
12✔
38
        err := validation.ValidateStruct(&sp,
12✔
39
                validation.Field(&sp.Aggregations, validation.Required,
12✔
40
                        validation.Length(1, maxAggregationTerms)))
12✔
41
        if err != nil {
16✔
42
                return err
4✔
43
        }
4✔
44

45
        for _, f := range sp.Filters {
13✔
46
                err := f.Validate()
5✔
47
                if err != nil {
7✔
48
                        return err
2✔
49
                }
2✔
50
        }
51
        return nil
6✔
52
}
53

54
func (f AggregationTerm) Validate() error {
14✔
55
        return validation.ValidateStruct(&f,
14✔
56
                validation.Field(&f.Name, validation.Required),
14✔
57
                validation.Field(&f.Attribute, validation.Required),
14✔
58
                validation.Field(&f.Scope, validation.Required),
14✔
59
                validation.Field(&f.Limit, validation.Min(0)),
14✔
60
                validation.Field(&f.Aggregations, validation.When(
14✔
61
                        len(f.Aggregations) > 0, validation.Length(0, maxAggregationTerms))),
14✔
62
        )
14✔
63
}
14✔
64

65
type Aggregations map[string]interface{}
66

67
func BuildAggregations(terms []AggregationTerm) (*Aggregations, error) {
10✔
68
        aggs := Aggregations{}
10✔
69
        for _, term := range terms {
20✔
70
                terms := map[string]interface{}{
10✔
71
                        "field": ToAttr(term.Scope, term.Attribute, TypeStr),
10✔
72
                }
10✔
73
                limit := term.Limit
10✔
74
                if limit <= 0 {
16✔
75
                        limit = defaultAggregationLimit
6✔
76
                }
6✔
77
                terms["size"] = limit
10✔
78
                agg := map[string]interface{}{
10✔
79
                        "terms": terms,
10✔
80
                }
10✔
81
                if len(term.Aggregations) > 0 {
12✔
82
                        subaggs, err := BuildAggregations(term.Aggregations)
2✔
83
                        if err != nil {
2✔
UNCOV
84
                                return nil, err
×
85
                        }
×
86
                        agg["aggs"] = subaggs
2✔
87
                }
88
                aggs[term.Name] = agg
10✔
89
        }
90
        return &aggs, nil
10✔
91
}
92

93
type DeviceAggregation struct {
94
        Name       string                  `json:"name"`
95
        Items      []DeviceAggregationItem `json:"items"`
96
        OtherCount int                     `json:"other_count"`
97
}
98

99
type DeviceAggregationItem struct {
100
        Key          string              `json:"key"`
101
        Count        int                 `json:"count"`
102
        Aggregations []DeviceAggregation `json:"aggregations,omitempty"`
103
}
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