forked from elastic/package-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
categories.go
190 lines (156 loc) · 4.78 KB
/
categories.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package main
import (
"context"
"fmt"
"net/http"
"net/url"
"sort"
"strconv"
"time"
"github.com/Masterminds/semver/v3"
"go.elastic.co/apm"
"github.com/elastic/package-registry/packages"
"github.com/elastic/package-registry/util"
)
type Category struct {
Id string `yaml:"id" json:"id"`
Title string `yaml:"title" json:"title"`
Count int `yaml:"count" json:"count"`
}
// categoriesHandler is a dynamic handler as it will also allow filtering in the future.
func categoriesHandler(indexer Indexer, cacheTime time.Duration) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
filter, err := newCategoriesFilterFromQuery(query)
if err != nil {
badRequest(w, err.Error())
return
}
includePolicyTemplates := false
if v := query.Get("include_policy_templates"); v != "" {
includePolicyTemplates, err = strconv.ParseBool(v)
if err != nil {
badRequest(w, fmt.Sprintf("invalid 'include_policy_templates' query param: '%s'", v))
return
}
}
opts := packages.GetOptions{
Filter: filter,
}
packages, err := indexer.Get(r.Context(), &opts)
if err != nil {
notFoundError(w, err)
return
}
categories := getCategories(r.Context(), packages, includePolicyTemplates)
data, err := getCategoriesOutput(r.Context(), categories)
if err != nil {
notFoundError(w, err)
return
}
cacheHeaders(w, cacheTime)
jsonHeader(w)
w.Write(data)
}
}
func newCategoriesFilterFromQuery(query url.Values) (*packages.Filter, error) {
var filter packages.Filter
if len(query) == 0 {
return &filter, nil
}
var err error
if v := query.Get("kibana.version"); v != "" {
filter.KibanaVersion, err = semver.NewVersion(v)
if err != nil {
return nil, fmt.Errorf("invalid Kibana version '%s': %w", v, err)
}
}
// Deprecated: release tags to be removed.
if v := query.Get("experimental"); v != "" {
filter.Experimental, err = strconv.ParseBool(v)
if err != nil {
return nil, fmt.Errorf("invalid 'experimental' query param: '%s'", v)
}
// For compatibility with older versions of Kibana.
if filter.Experimental {
filter.Prerelease = true
}
}
if v := query.Get("prerelease"); v != "" {
// In case of error, keep it false
filter.Prerelease, err = strconv.ParseBool(v)
if err != nil {
return nil, fmt.Errorf("invalid 'prerelease' query param: '%s'", v)
}
}
return &filter, nil
}
func getCategories(ctx context.Context, packages packages.Packages, includePolicyTemplates bool) map[string]*Category {
span, ctx := apm.StartSpan(ctx, "FilterCategories", "app")
defer span.End()
categories := map[string]*Category{}
for _, p := range packages {
for _, c := range p.Categories {
if _, ok := categories[c]; !ok {
categories[c] = &Category{
Id: c,
Title: c,
Count: 0,
}
}
categories[c].Count = categories[c].Count + 1
}
if includePolicyTemplates {
// /categories counts policies and packages separately, but packages are counted too
// if they don't match but any of their policies does (for the AWS case this would mean that
// the count for "datastore" would be 3: the Package and the RDS and DynamoDB policies).
var extraPackageCategories []string
for _, t := range p.PolicyTemplates {
// Skip when policy template level `categories` is empty and there is only one policy template
if t.Categories == nil && len(p.PolicyTemplates) == 1 {
break
}
for _, c := range p.Categories {
categories[c].Count = categories[c].Count + 1
}
// Add policy template level categories.
for _, c := range t.Categories {
if _, ok := categories[c]; !ok {
categories[c] = &Category{
Id: c,
Title: c,
Count: 0,
}
}
if !p.HasCategory(c) && !util.StringsContains(extraPackageCategories, c) {
extraPackageCategories = append(extraPackageCategories, c)
categories[c].Count = categories[c].Count + 1
}
categories[c].Count = categories[c].Count + 1
}
}
}
}
return categories
}
func getCategoriesOutput(ctx context.Context, categories map[string]*Category) ([]byte, error) {
span, ctx := apm.StartSpan(ctx, "GetCategoriesOutput", "app")
defer span.End()
var keys []string
for k := range categories {
keys = append(keys, k)
}
sort.Strings(keys)
var outputCategories []*Category
for _, k := range keys {
c := categories[k]
if title, ok := packages.CategoryTitles[c.Title]; ok {
c.Title = title
}
outputCategories = append(outputCategories, c)
}
return util.MarshalJSONPretty(outputCategories)
}