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

Cleanup of sortfilter #106

Merged
merged 4 commits into from
Mar 4, 2025
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
32 changes: 32 additions & 0 deletions internal/deployment/sortfilter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package deployment

import (
"context"

"github.com/nais/api/internal/workload"
"github.com/nais/api/internal/workload/application"
"github.com/nais/api/internal/workload/job"
)

func init() {
sortByTimestamp := func(ctx context.Context, wl workload.Workload) int {
ts, err := latestDeploymentTimestampForWorkload(ctx, wl)
if err != nil {
return -1
}

return int(ts.Unix())
}

application.SortFilter.RegisterConcurrentSort("DEPLOYMENT_TIME", func(ctx context.Context, a *application.Application) int {
return sortByTimestamp(ctx, a)
})

job.SortFilter.RegisterConcurrentSort("DEPLOYMENT_TIME", func(ctx context.Context, a *job.Job) int {
return sortByTimestamp(ctx, a)
})

workload.SortFilter.RegisterConcurrentSort("DEPLOYMENT_TIME", func(ctx context.Context, a workload.Workload) int {
return sortByTimestamp(ctx, a)
})
}
36 changes: 0 additions & 36 deletions internal/deployment/sortorder.go

This file was deleted.

13 changes: 3 additions & 10 deletions internal/github/repository/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package repository
import (
"fmt"
"io"
"slices"
"strconv"
"strings"

Expand Down Expand Up @@ -58,11 +59,8 @@ type TeamRepositoryFilter struct {
Name *string `json:"name"`
}

// Ordering options when fetching repositories.
type RepositoryOrder struct {
// The field to order items by.
Field RepositoryOrderField `json:"field"`
// The direction to order items by.
Field RepositoryOrderField `json:"field"`
Direction model.OrderDirection `json:"direction"`
}

Expand All @@ -77,7 +75,6 @@ func (o *RepositoryOrder) String() string {
type RepositoryOrderField string

const (
// Order repositories by name.
RepositoryOrderFieldName RepositoryOrderField = "NAME"
)

Expand All @@ -86,11 +83,7 @@ var AllRepositoryOrderField = []RepositoryOrderField{
}

func (e RepositoryOrderField) IsValid() bool {
switch e {
case RepositoryOrderFieldName:
return true
}
return false
return slices.Contains(AllRepositoryOrderField, e)
}

func (e RepositoryOrderField) String() string {
Expand Down
21 changes: 3 additions & 18 deletions internal/graph/applications.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/nais/api/internal/auth/authz"
"github.com/nais/api/internal/graph/gengql"
"github.com/nais/api/internal/graph/model"
"github.com/nais/api/internal/graph/pagination"
"github.com/nais/api/internal/status"
"github.com/nais/api/internal/team"
Expand Down Expand Up @@ -102,26 +101,12 @@ func (r *restartApplicationPayloadResolver) Application(ctx context.Context, obj
}

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) {
if filter == nil {
filter = &application.TeamApplicationsFilter{}
}

page, err := pagination.ParsePage(first, after, last, before)
if err != nil {
return nil, err
}

if orderBy == nil {
orderBy = &application.ApplicationOrder{
Field: application.ApplicationOrderFieldName,
Direction: model.OrderDirectionAsc,
}
}

ret := application.ListAllForTeam(ctx, obj.Slug)
ret = application.SortFilter.Filter(ctx, ret, filter)

application.SortFilter.Sort(ctx, ret, orderBy.Field, orderBy.Direction)
ret := application.ListAllForTeam(ctx, obj.Slug, orderBy, filter)
apps := pagination.Slice(ret, page)
return pagination.NewConnection(apps, page, len(ret)), nil
}
Expand All @@ -131,7 +116,7 @@ func (r *teamEnvironmentResolver) Application(ctx context.Context, obj *team.Tea
}

func (r *teamInventoryCountApplicationsResolver) NotNais(ctx context.Context, obj *application.TeamInventoryCountApplications) (int, error) {
apps := application.ListAllForTeam(ctx, obj.TeamSlug)
apps := application.ListAllForTeam(ctx, obj.TeamSlug, nil, nil)
notNais := 0

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

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

return &application.TeamInventoryCountApplications{
Total: len(apps),
Expand Down
2 changes: 1 addition & 1 deletion internal/graph/bigquery.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (r *bigQueryDatasetResolver) Access(ctx context.Context, obj *bigquery.BigQ

if orderBy == nil {
orderBy = &bigquery.BigQueryDatasetAccessOrder{
Field: bigquery.BigQueryDatasetAccessOrderFieldEmail,
Field: "EMAIL",
Direction: model.OrderDirectionAsc,
}
}
Expand Down
26 changes: 6 additions & 20 deletions internal/graph/jobs.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/nais/api/internal/auth/authz"
"github.com/nais/api/internal/graph/gengql"
"github.com/nais/api/internal/graph/model"
"github.com/nais/api/internal/graph/pagination"
"github.com/nais/api/internal/status"
"github.com/nais/api/internal/team"
Expand Down Expand Up @@ -96,25 +95,12 @@ func (r *mutationResolver) TriggerJob(ctx context.Context, input job.TriggerJobI
}

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) {
if filter == nil {
filter = &job.TeamJobsFilter{}
}
page, err := pagination.ParsePage(first, after, last, before)
if err != nil {
return nil, err
}

if orderBy == nil {
orderBy = &job.JobOrder{
Field: job.JobOrderFieldName,
Direction: model.OrderDirectionAsc,
}
}

ret := job.ListAllForTeam(ctx, obj.Slug)
ret = job.SortFilter.Filter(ctx, ret, filter)

job.SortFilter.Sort(ctx, ret, orderBy.Field, orderBy.Direction)
ret := job.ListAllForTeam(ctx, obj.Slug, orderBy, filter)
jobs := pagination.Slice(ret, page)
return pagination.NewConnection(jobs, page, len(ret)), nil
}
Expand All @@ -124,11 +110,11 @@ func (r *teamEnvironmentResolver) Job(ctx context.Context, obj *team.TeamEnviron
}

func (r *teamInventoryCountJobsResolver) NotNais(ctx context.Context, obj *job.TeamInventoryCountJobs) (int, error) {
jobs := job.ListAllForTeam(ctx, obj.TeamSlug)
jobs := job.ListAllForTeam(ctx, obj.TeamSlug, nil, nil)
notNais := 0

for _, job := range jobs {
s := status.ForWorkload(ctx, job)
for _, j := range jobs {
s := status.ForWorkload(ctx, j)
if s.State == status.WorkloadStateNotNais {
notNais++
}
Expand All @@ -137,10 +123,10 @@ func (r *teamInventoryCountJobsResolver) NotNais(ctx context.Context, obj *job.T
}

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

return &job.TeamInventoryCountJobs{
Total: len(apps),
Total: len(jobs),
TeamSlug: obj.TeamSlug,
}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/graph/kafka.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (r *kafkaTopicResolver) ACL(ctx context.Context, obj *kafkatopic.KafkaTopic

if orderBy == nil {
orderBy = &kafkatopic.KafkaTopicACLOrder{
Field: kafkatopic.KafkaTopicACLOrderFieldTopicName,
Field: "TOPIC_NAME",
Direction: model.OrderDirectionAsc,
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/graph/secret.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (r *secretResolver) Applications(ctx context.Context, obj *secret.Secret, f
return nil, err
}

allApps := application.ListAllForTeam(ctx, obj.TeamSlug)
allApps := application.ListAllForTeam(ctx, obj.TeamSlug, nil, nil)

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

allJobs := job.ListAllForTeam(ctx, obj.TeamSlug)
allJobs := job.ListAllForTeam(ctx, obj.TeamSlug, nil, nil)

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