Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search API: sort packages by title #739

Merged
merged 1 commit into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Bugfixes

* Search API: sort packages by title. [#647](https://github.com/elastic/package-registry/issues/647) [#?](https://github.com/elastic/package-registry/pull/?)

### Added

### Deprecated
Expand Down
62 changes: 45 additions & 17 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"net/http"
"sort"
"strconv"
"strings"
"time"

"github.com/Masterminds/semver/v3"
Expand Down Expand Up @@ -232,33 +231,62 @@ func filterPolicyTemplates(p util.Package, category string) util.Package {
return p
}

type sortedPackage struct {
name string
title string
version string
}

func newSortedPackage(aPackage util.Package) sortedPackage {
title := aPackage.Name
if aPackage.Title != nil {
title = *aPackage.Title
}
return sortedPackage{
name: aPackage.Name,
title: title,
version: aPackage.Version,
}
}

type sortedPackages []sortedPackage

func (s sortedPackages) Len() int {
return len(s)
}

func (s sortedPackages) Less(i, j int) bool {
titlesSorted := sort.StringsAreSorted([]string{s[i].title, s[j].title})
if s[i].title != s[j].title {
return titlesSorted
}
return sort.StringsAreSorted([]string{s[i].version, s[j].version})
}

func (s sortedPackages) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}

var _ sort.Interface = new(sortedPackages)

func getPackageOutput(ctx context.Context, packagesList map[string]map[string]util.Package) ([]byte, error) {
span, ctx := apm.StartSpan(ctx, "GetPackageOutput", "app")
defer span.End()

separator := "@"
// Packages need to be sorted to be always outputted in the same order
var keys []string
for key, k := range packagesList {
for v := range k {
keys = append(keys, key+separator+v)
var sorted sortedPackages
for _, versionPackage := range packagesList {
for _, aPackage := range versionPackage {
sorted = append(sorted, newSortedPackage(aPackage))
}
}
sort.Strings(keys)
sort.Sort(sorted)

var output []util.BasePackage

for _, k := range keys {
parts := strings.Split(k, separator)
m := packagesList[parts[0]][parts[1]]
for _, s := range sorted {
m := packagesList[s.name][s.version]
data := m.BasePackage
output = append(output, data)
}

// Instead of return `null` in case of an empty array, return []
if len(output) == 0 {
return []byte("[]"), nil
}

return util.MarshalJSONPretty(output)
}
66 changes: 33 additions & 33 deletions testdata/generated/search-all.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@
]
},
{
"name": "default_pipeline",
"name": "ecs_style_dataset",
"title": "Default pipeline Integration",
"version": "0.0.2",
"version": "0.0.1",
"release": "beta",
"description": "Tests if no pipeline is set, it defaults to the default one",
"description": "Tests the registry validations works for dataset fields using the ecs style format",
"type": "integration",
"download": "/epr/default_pipeline/default_pipeline-0.0.2.zip",
"path": "/package/default_pipeline/0.0.2",
"download": "/epr/ecs_style_dataset/ecs_style_dataset-0.0.1.zip",
"path": "/package/ecs_style_dataset/0.0.1",
"policy_templates": [
{
"name": "logs",
Expand All @@ -49,19 +49,18 @@
}
],
"categories": [
"containers",
"message_queue"
"monitoring"
]
},
{
"name": "ecs_style_dataset",
"name": "default_pipeline",
"title": "Default pipeline Integration",
"version": "0.0.1",
"version": "0.0.2",
"release": "beta",
"description": "Tests the registry validations works for dataset fields using the ecs style format",
"description": "Tests if no pipeline is set, it defaults to the default one",
"type": "integration",
"download": "/epr/ecs_style_dataset/ecs_style_dataset-0.0.1.zip",
"path": "/package/ecs_style_dataset/0.0.1",
"download": "/epr/default_pipeline/default_pipeline-0.0.2.zip",
"path": "/package/default_pipeline/0.0.2",
"policy_templates": [
{
"name": "logs",
Expand All @@ -70,7 +69,8 @@
}
],
"categories": [
"monitoring"
"containers",
"message_queue"
]
},
{
Expand Down Expand Up @@ -313,26 +313,6 @@
"custom"
]
},
{
"name": "multiple_false",
"title": "Multiple false",
"version": "0.0.1",
"release": "beta",
"description": "Tests that multiple can be set to false",
"type": "integration",
"download": "/epr/multiple_false/multiple_false-0.0.1.zip",
"path": "/package/multiple_false/0.0.1",
"policy_templates": [
{
"name": "logs",
"title": "Logs datasource",
"description": "Datasource for your log files."
}
],
"categories": [
"custom"
]
},
{
"name": "multiversion",
"title": "Multi Version",
Expand Down Expand Up @@ -411,6 +391,26 @@
"web"
]
},
{
"name": "multiple_false",
"title": "Multiple false",
"version": "0.0.1",
"release": "beta",
"description": "Tests that multiple can be set to false",
"type": "integration",
"download": "/epr/multiple_false/multiple_false-0.0.1.zip",
"path": "/package/multiple_false/0.0.1",
"policy_templates": [
{
"name": "logs",
"title": "Logs datasource",
"description": "Datasource for your log files."
}
],
"categories": [
"custom"
]
},
{
"name": "no_stream_configs",
"title": "No Stream configs",
Expand Down
40 changes: 20 additions & 20 deletions testdata/generated/search-category-custom.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,26 +175,6 @@
"custom"
]
},
{
"name": "multiple_false",
"title": "Multiple false",
"version": "0.0.1",
"release": "beta",
"description": "Tests that multiple can be set to false",
"type": "integration",
"download": "/epr/multiple_false/multiple_false-0.0.1.zip",
"path": "/package/multiple_false/0.0.1",
"policy_templates": [
{
"name": "logs",
"title": "Logs datasource",
"description": "Datasource for your log files."
}
],
"categories": [
"custom"
]
},
{
"name": "multiversion",
"title": "Multi Version Second with the same version! This one should win, because it is first.",
Expand All @@ -221,6 +201,26 @@
"web"
]
},
{
"name": "multiple_false",
"title": "Multiple false",
"version": "0.0.1",
"release": "beta",
"description": "Tests that multiple can be set to false",
"type": "integration",
"download": "/epr/multiple_false/multiple_false-0.0.1.zip",
"path": "/package/multiple_false/0.0.1",
"policy_templates": [
{
"name": "logs",
"title": "Logs datasource",
"description": "Datasource for your log files."
}
],
"categories": [
"custom"
]
},
{
"name": "no_stream_configs",
"title": "No Stream configs",
Expand Down
26 changes: 13 additions & 13 deletions testdata/generated/search-kibana652.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@
]
},
{
"name": "default_pipeline",
"name": "ecs_style_dataset",
"title": "Default pipeline Integration",
"version": "0.0.2",
"version": "0.0.1",
"release": "beta",
"description": "Tests if no pipeline is set, it defaults to the default one",
"description": "Tests the registry validations works for dataset fields using the ecs style format",
"type": "integration",
"download": "/epr/default_pipeline/default_pipeline-0.0.2.zip",
"path": "/package/default_pipeline/0.0.2",
"download": "/epr/ecs_style_dataset/ecs_style_dataset-0.0.1.zip",
"path": "/package/ecs_style_dataset/0.0.1",
"policy_templates": [
{
"name": "logs",
Expand All @@ -49,19 +49,18 @@
}
],
"categories": [
"containers",
"message_queue"
"monitoring"
]
},
{
"name": "ecs_style_dataset",
"name": "default_pipeline",
"title": "Default pipeline Integration",
"version": "0.0.1",
"version": "0.0.2",
"release": "beta",
"description": "Tests the registry validations works for dataset fields using the ecs style format",
"description": "Tests if no pipeline is set, it defaults to the default one",
"type": "integration",
"download": "/epr/ecs_style_dataset/ecs_style_dataset-0.0.1.zip",
"path": "/package/ecs_style_dataset/0.0.1",
"download": "/epr/default_pipeline/default_pipeline-0.0.2.zip",
"path": "/package/default_pipeline/0.0.2",
"policy_templates": [
{
"name": "logs",
Expand All @@ -70,7 +69,8 @@
}
],
"categories": [
"monitoring"
"containers",
"message_queue"
]
},
{
Expand Down
Loading