Skip to content

Commit 2f41b0d

Browse files
Remove enum consts
1 parent 7b68b37 commit 2f41b0d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+265
-412
lines changed

internal/deployment/sortfilter.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ import (
88
"github.com/nais/api/internal/workload/job"
99
)
1010

11-
const (
12-
ApplicationOrderFieldDeploymentTime application.ApplicationOrderField = "DEPLOYMENT_TIME"
13-
JobOrderFieldDeploymentTime job.JobOrderField = "DEPLOYMENT_TIME"
14-
)
15-
1611
func init() {
1712
sortByTimestamp := func(ctx context.Context, wl workload.Workload) int {
1813
ts, err := latestDeploymentTimestampForWorkload(ctx, wl)
@@ -23,11 +18,15 @@ func init() {
2318
return int(ts.Unix())
2419
}
2520

26-
application.SortFilter.RegisterConcurrentOrderBy(ApplicationOrderFieldDeploymentTime, func(ctx context.Context, a *application.Application) int {
21+
application.SortFilter.RegisterConcurrentOrderBy("DEPLOYMENT_TIME", func(ctx context.Context, a *application.Application) int {
22+
return sortByTimestamp(ctx, a)
23+
})
24+
25+
job.SortFilter.RegisterConcurrentOrderBy("DEPLOYMENT_TIME", func(ctx context.Context, a *job.Job) int {
2726
return sortByTimestamp(ctx, a)
2827
})
2928

30-
job.SortFilter.RegisterConcurrentOrderBy(JobOrderFieldDeploymentTime, func(ctx context.Context, a *job.Job) int {
29+
workload.SortFilter.RegisterConcurrentOrderBy("DEPLOYMENT_TIME", func(ctx context.Context, a workload.Workload) int {
3130
return sortByTimestamp(ctx, a)
3231
})
3332
}

internal/graph/applications.resolvers.go

+3-18
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"github.com/nais/api/internal/auth/authz"
88
"github.com/nais/api/internal/graph/gengql"
9-
"github.com/nais/api/internal/graph/model"
109
"github.com/nais/api/internal/graph/pagination"
1110
"github.com/nais/api/internal/status"
1211
"github.com/nais/api/internal/team"
@@ -102,26 +101,12 @@ func (r *restartApplicationPayloadResolver) Application(ctx context.Context, obj
102101
}
103102

104103
func (r *teamResolver) Applications(ctx context.Context, obj *team.Team, first *int, after *pagination.Cursor, last *int, before *pagination.Cursor, orderBy *application.ApplicationOrder, filter *application.TeamApplicationsFilter) (*pagination.Connection[*application.Application], error) {
105-
if filter == nil {
106-
filter = &application.TeamApplicationsFilter{}
107-
}
108-
109104
page, err := pagination.ParsePage(first, after, last, before)
110105
if err != nil {
111106
return nil, err
112107
}
113108

114-
if orderBy == nil {
115-
orderBy = &application.ApplicationOrder{
116-
Field: application.ApplicationOrderFieldName,
117-
Direction: model.OrderDirectionAsc,
118-
}
119-
}
120-
121-
ret := application.ListAllForTeam(ctx, obj.Slug)
122-
ret = application.SortFilter.Filter(ctx, ret, filter)
123-
124-
application.SortFilter.Sort(ctx, ret, orderBy.Field, orderBy.Direction)
109+
ret := application.ListAllForTeam(ctx, obj.Slug, orderBy, filter)
125110
apps := pagination.Slice(ret, page)
126111
return pagination.NewConnection(apps, page, len(ret)), nil
127112
}
@@ -131,7 +116,7 @@ func (r *teamEnvironmentResolver) Application(ctx context.Context, obj *team.Tea
131116
}
132117

133118
func (r *teamInventoryCountApplicationsResolver) NotNais(ctx context.Context, obj *application.TeamInventoryCountApplications) (int, error) {
134-
apps := application.ListAllForTeam(ctx, obj.TeamSlug)
119+
apps := application.ListAllForTeam(ctx, obj.TeamSlug, nil, nil)
135120
notNais := 0
136121

137122
for _, app := range apps {
@@ -144,7 +129,7 @@ func (r *teamInventoryCountApplicationsResolver) NotNais(ctx context.Context, ob
144129
}
145130

146131
func (r *teamInventoryCountsResolver) Applications(ctx context.Context, obj *team.TeamInventoryCounts) (*application.TeamInventoryCountApplications, error) {
147-
apps := application.ListAllForTeam(ctx, obj.TeamSlug)
132+
apps := application.ListAllForTeam(ctx, obj.TeamSlug, nil, nil)
148133

149134
return &application.TeamInventoryCountApplications{
150135
Total: len(apps),

internal/graph/bigquery.resolvers.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (r *bigQueryDatasetResolver) Access(ctx context.Context, obj *bigquery.BigQ
3939

4040
if orderBy == nil {
4141
orderBy = &bigquery.BigQueryDatasetAccessOrder{
42-
Field: bigquery.BigQueryDatasetAccessOrderFieldEmail,
42+
Field: "EMAIL",
4343
Direction: model.OrderDirectionAsc,
4444
}
4545
}

internal/graph/jobs.resolvers.go

+6-20
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"github.com/nais/api/internal/auth/authz"
88
"github.com/nais/api/internal/graph/gengql"
9-
"github.com/nais/api/internal/graph/model"
109
"github.com/nais/api/internal/graph/pagination"
1110
"github.com/nais/api/internal/status"
1211
"github.com/nais/api/internal/team"
@@ -96,25 +95,12 @@ func (r *mutationResolver) TriggerJob(ctx context.Context, input job.TriggerJobI
9695
}
9796

9897
func (r *teamResolver) Jobs(ctx context.Context, obj *team.Team, first *int, after *pagination.Cursor, last *int, before *pagination.Cursor, orderBy *job.JobOrder, filter *job.TeamJobsFilter) (*pagination.Connection[*job.Job], error) {
99-
if filter == nil {
100-
filter = &job.TeamJobsFilter{}
101-
}
10298
page, err := pagination.ParsePage(first, after, last, before)
10399
if err != nil {
104100
return nil, err
105101
}
106102

107-
if orderBy == nil {
108-
orderBy = &job.JobOrder{
109-
Field: job.JobOrderFieldName,
110-
Direction: model.OrderDirectionAsc,
111-
}
112-
}
113-
114-
ret := job.ListAllForTeam(ctx, obj.Slug)
115-
ret = job.SortFilter.Filter(ctx, ret, filter)
116-
117-
job.SortFilter.Sort(ctx, ret, orderBy.Field, orderBy.Direction)
103+
ret := job.ListAllForTeam(ctx, obj.Slug, orderBy, filter)
118104
jobs := pagination.Slice(ret, page)
119105
return pagination.NewConnection(jobs, page, len(ret)), nil
120106
}
@@ -124,11 +110,11 @@ func (r *teamEnvironmentResolver) Job(ctx context.Context, obj *team.TeamEnviron
124110
}
125111

126112
func (r *teamInventoryCountJobsResolver) NotNais(ctx context.Context, obj *job.TeamInventoryCountJobs) (int, error) {
127-
jobs := job.ListAllForTeam(ctx, obj.TeamSlug)
113+
jobs := job.ListAllForTeam(ctx, obj.TeamSlug, nil, nil)
128114
notNais := 0
129115

130-
for _, job := range jobs {
131-
s := status.ForWorkload(ctx, job)
116+
for _, j := range jobs {
117+
s := status.ForWorkload(ctx, j)
132118
if s.State == status.WorkloadStateNotNais {
133119
notNais++
134120
}
@@ -137,10 +123,10 @@ func (r *teamInventoryCountJobsResolver) NotNais(ctx context.Context, obj *job.T
137123
}
138124

139125
func (r *teamInventoryCountsResolver) Jobs(ctx context.Context, obj *team.TeamInventoryCounts) (*job.TeamInventoryCountJobs, error) {
140-
apps := job.ListAllForTeam(ctx, obj.TeamSlug)
126+
jobs := job.ListAllForTeam(ctx, obj.TeamSlug, nil, nil)
141127

142128
return &job.TeamInventoryCountJobs{
143-
Total: len(apps),
129+
Total: len(jobs),
144130
TeamSlug: obj.TeamSlug,
145131
}, nil
146132
}

internal/graph/kafka.resolvers.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (r *kafkaTopicResolver) ACL(ctx context.Context, obj *kafkatopic.KafkaTopic
5050

5151
if orderBy == nil {
5252
orderBy = &kafkatopic.KafkaTopicACLOrder{
53-
Field: kafkatopic.KafkaTopicACLOrderFieldTopicName,
53+
Field: "TOPIC_NAME",
5454
Direction: model.OrderDirectionAsc,
5555
}
5656
}

internal/graph/secret.resolvers.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func (r *secretResolver) Applications(ctx context.Context, obj *secret.Secret, f
137137
return nil, err
138138
}
139139

140-
allApps := application.ListAllForTeam(ctx, obj.TeamSlug)
140+
allApps := application.ListAllForTeam(ctx, obj.TeamSlug, nil, nil)
141141

142142
ret := make([]*application.Application, 0)
143143
for _, app := range allApps {
@@ -156,7 +156,7 @@ func (r *secretResolver) Jobs(ctx context.Context, obj *secret.Secret, first *in
156156
return nil, err
157157
}
158158

159-
allJobs := job.ListAllForTeam(ctx, obj.TeamSlug)
159+
allJobs := job.ListAllForTeam(ctx, obj.TeamSlug, nil, nil)
160160

161161
ret := make([]*job.Job, 0)
162162
for _, j := range allJobs {

internal/graph/sortfilter/sortfilter.go

+29-15
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,36 @@ type orderByValue[V any] struct {
2727
}
2828

2929
type SortFilter[V any, OrderKey comparable, FilterObj comparable] struct {
30-
orderBys map[OrderKey]orderByValue[V]
31-
filters []Filter[V, FilterObj]
32-
defaultSortKey OrderKey
30+
orderBys map[OrderKey]orderByValue[V]
31+
filters []Filter[V, FilterObj]
32+
tieBreakSortKey OrderKey
33+
tieBreakOrderDirection model.OrderDirection
3334
}
3435

35-
// New creates a new SortFilter with the given defaultSortKey.
36-
// The defaultSortKey is used when two values are equal in the OrderBy function.
37-
// The defaultSortKey must not be registered as a ConcurrentOrderBy.
38-
func New[V any, OrderKey comparable, FilterObj comparable](defaultSortKey OrderKey) *SortFilter[V, OrderKey, FilterObj] {
36+
// New creates a new SortFilter with the given tieBreakSortKey and tieBreakOrderDirection.
37+
// The tieBreakSortKey is used when two values are equal in the OrderBy function.
38+
// The tieBreakSortKey must not be registered as a ConcurrentOrderBy.
39+
func New[V any, OrderKey comparable, FilterObj comparable](tieBreakSortKey OrderKey, tieBreakOrderDirection model.OrderDirection) *SortFilter[V, OrderKey, FilterObj] {
3940
return &SortFilter[V, OrderKey, FilterObj]{
40-
orderBys: make(map[OrderKey]orderByValue[V]),
41-
defaultSortKey: defaultSortKey,
41+
orderBys: make(map[OrderKey]orderByValue[V]),
42+
tieBreakSortKey: tieBreakSortKey,
43+
tieBreakOrderDirection: tieBreakOrderDirection,
4244
}
4345
}
4446

47+
func (s *SortFilter[T, OrderKey, FilterObj]) DefaultSortKey() OrderKey {
48+
return s.tieBreakSortKey
49+
}
50+
51+
func (s *SortFilter[T, OrderKey, FilterObj]) DefaultOrderDirection() model.OrderDirection {
52+
return s.tieBreakOrderDirection
53+
}
54+
55+
func (s *SortFilter[T, OrderKey, FilterObj]) Supports(key OrderKey) bool {
56+
_, exists := s.orderBys[key]
57+
return exists
58+
}
59+
4560
func (s *SortFilter[T, OrderKey, FilterObj]) RegisterFilter(filter Filter[T, FilterObj]) {
4661
s.filters = append(s.filters, filter)
4762
}
@@ -55,11 +70,6 @@ func (s *SortFilter[T, OrderKey, FilterObj]) RegisterOrderBy(key OrderKey, order
5570
}
5671
}
5772

58-
func (s *SortFilter[T, OrderKey, FilterObj]) Supports(key OrderKey) bool {
59-
_, exists := s.orderBys[key]
60-
return exists
61-
}
62-
6373
func (s *SortFilter[T, OrderKey, FilterObj]) RegisterConcurrentOrderBy(key OrderKey, orderBy ConcurrentOrderBy[T]) {
6474
if _, ok := s.orderBys[key]; ok {
6575
panic(fmt.Sprintf("OrderBy already registered for key: %v", key))
@@ -181,5 +191,9 @@ func (s *SortFilter[T, OrderKey, FilterObj]) sort(ctx context.Context, items []T
181191
}
182192

183193
func (s *SortFilter[T, OrderKey, FilterObj]) defaultSort(ctx context.Context, a, b T) int {
184-
return s.orderBys[s.defaultSortKey].orderBy(ctx, a, b)
194+
if s.tieBreakOrderDirection == model.OrderDirectionDesc {
195+
return s.orderBys[s.tieBreakSortKey].orderBy(ctx, b, a)
196+
}
197+
198+
return s.orderBys[s.tieBreakSortKey].orderBy(ctx, a, b)
185199
}

internal/graph/vulnerability.resolvers.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ func (r *teamResolver) VulnerabilitySummary(ctx context.Context, obj *team.Team,
8484
jobs = append(jobs, job.ListAllForTeamInEnvironment(ctx, obj.Slug, env)...)
8585
}
8686
} else {
87-
apps = application.ListAllForTeam(ctx, obj.Slug)
88-
jobs = job.ListAllForTeam(ctx, obj.Slug)
87+
apps = application.ListAllForTeam(ctx, obj.Slug, nil, nil)
88+
jobs = job.ListAllForTeam(ctx, obj.Slug, nil, nil)
8989
}
9090

9191
retVal := &vulnerability.TeamVulnerabilitySummary{
@@ -102,8 +102,8 @@ func (r *teamResolver) VulnerabilitySummary(ctx context.Context, obj *team.Team,
102102
wg.Go(fetchImage(app))
103103
}
104104

105-
for _, job := range jobs {
106-
wg.Go(fetchImage(job))
105+
for _, j := range jobs {
106+
wg.Go(fetchImage(j))
107107
}
108108

109109
images, err := wg.Wait()

internal/graph/workloads.resolvers.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,23 @@ func (r *teamResolver) Workloads(ctx context.Context, obj *team.Team, first *int
1818
return nil, err
1919
}
2020

21-
apps := application.ListAllForTeam(ctx, obj.Slug)
22-
jobs := job.ListAllForTeam(ctx, obj.Slug)
21+
apps := application.ListAllForTeam(ctx, obj.Slug, nil, nil)
22+
jobs := job.ListAllForTeam(ctx, obj.Slug, nil, nil)
2323

2424
workloads := make([]workload.Workload, 0, len(apps)+len(jobs))
2525
for _, app := range apps {
2626
workloads = append(workloads, app)
2727
}
28-
for _, job := range jobs {
29-
workloads = append(workloads, job)
28+
for _, j := range jobs {
29+
workloads = append(workloads, j)
3030
}
3131

3232
filtered := workload.SortFilter.Filter(ctx, workloads, filter)
3333
if orderBy == nil {
34-
orderBy = &workload.WorkloadOrder{Field: workload.WorkloadOrderFieldName, Direction: model.OrderDirectionAsc}
34+
orderBy = &workload.WorkloadOrder{
35+
Field: "NAME",
36+
Direction: model.OrderDirectionAsc,
37+
}
3538
}
3639
workload.SortFilter.Sort(ctx, filtered, orderBy.Field, orderBy.Direction)
3740

internal/persistence/bigquery/models.go

+2-20
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,8 @@ type BigQueryDatasetOrder struct {
8181

8282
type BigQueryDatasetOrderField string
8383

84-
const (
85-
BigQueryDatasetOrderFieldName BigQueryDatasetOrderField = "NAME"
86-
BigQueryDatasetOrderFieldEnvironment BigQueryDatasetOrderField = "ENVIRONMENT"
87-
)
88-
8984
func (e BigQueryDatasetOrderField) IsValid() bool {
90-
switch e {
91-
case BigQueryDatasetOrderFieldName, BigQueryDatasetOrderFieldEnvironment:
92-
return true
93-
}
94-
return false
85+
return SortFilter.Supports(e)
9586
}
9687

9788
func (e BigQueryDatasetOrderField) String() string {
@@ -122,17 +113,8 @@ type BigQueryDatasetAccessOrder struct {
122113

123114
type BigQueryDatasetAccessOrderField string
124115

125-
const (
126-
BigQueryDatasetAccessOrderFieldRole BigQueryDatasetAccessOrderField = "ROLE"
127-
BigQueryDatasetAccessOrderFieldEmail BigQueryDatasetAccessOrderField = "EMAIL"
128-
)
129-
130116
func (e BigQueryDatasetAccessOrderField) IsValid() bool {
131-
switch e {
132-
case BigQueryDatasetAccessOrderFieldRole, BigQueryDatasetAccessOrderFieldEmail:
133-
return true
134-
}
135-
return false
117+
return SortFilterAccess.Supports(e)
136118
}
137119

138120
func (e BigQueryDatasetAccessOrderField) String() string {

internal/persistence/bigquery/queries.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func ListForWorkload(ctx context.Context, teamSlug slug.Slug, datasets []nais_io
6767
func orderDatasets(ctx context.Context, datasets []*BigQueryDataset, orderBy *BigQueryDatasetOrder) {
6868
if orderBy == nil {
6969
orderBy = &BigQueryDatasetOrder{
70-
Field: BigQueryDatasetOrderFieldName,
70+
Field: "NAME",
7171
Direction: model.OrderDirectionAsc,
7272
}
7373
}

internal/persistence/bigquery/sortfilter.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,27 @@ import (
44
"context"
55
"strings"
66

7+
"github.com/nais/api/internal/graph/model"
78
"github.com/nais/api/internal/graph/sortfilter"
89
)
910

1011
var (
11-
SortFilter = sortfilter.New[*BigQueryDataset, BigQueryDatasetOrderField, struct{}](BigQueryDatasetOrderFieldName)
12-
SortFilterAccess = sortfilter.New[*BigQueryDatasetAccess, BigQueryDatasetAccessOrderField, struct{}](BigQueryDatasetAccessOrderFieldEmail)
12+
SortFilter = sortfilter.New[*BigQueryDataset, BigQueryDatasetOrderField, struct{}]("NAME", model.OrderDirectionAsc)
13+
SortFilterAccess = sortfilter.New[*BigQueryDatasetAccess, BigQueryDatasetAccessOrderField, struct{}]("EMAIL", model.OrderDirectionAsc)
1314
)
1415

1516
func init() {
16-
SortFilter.RegisterOrderBy(BigQueryDatasetOrderFieldName, func(ctx context.Context, a, b *BigQueryDataset) int {
17+
SortFilter.RegisterOrderBy("NAME", func(ctx context.Context, a, b *BigQueryDataset) int {
1718
return strings.Compare(a.GetName(), b.GetName())
1819
})
19-
SortFilter.RegisterOrderBy(BigQueryDatasetOrderFieldEnvironment, func(ctx context.Context, a, b *BigQueryDataset) int {
20+
SortFilter.RegisterOrderBy("ENVIRONMENT", func(ctx context.Context, a, b *BigQueryDataset) int {
2021
return strings.Compare(a.EnvironmentName, b.EnvironmentName)
2122
})
2223

23-
SortFilterAccess.RegisterOrderBy(BigQueryDatasetAccessOrderFieldEmail, func(ctx context.Context, a, b *BigQueryDatasetAccess) int {
24+
SortFilterAccess.RegisterOrderBy("EMAIL", func(ctx context.Context, a, b *BigQueryDatasetAccess) int {
2425
return strings.Compare(a.Email, b.Email)
2526
})
26-
SortFilterAccess.RegisterOrderBy(BigQueryDatasetAccessOrderFieldRole, func(ctx context.Context, a, b *BigQueryDatasetAccess) int {
27+
SortFilterAccess.RegisterOrderBy("ROLE", func(ctx context.Context, a, b *BigQueryDatasetAccess) int {
2728
return strings.Compare(a.Role, b.Role)
2829
})
2930
}

0 commit comments

Comments
 (0)