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

draft(issue_match): initial ordering proposal based on entity tags #356

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
4 changes: 3 additions & 1 deletion internal/api/graphql/graph/baseResolver/issue_match.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func SingleIssueMatchBaseResolver(app app.Heureka, ctx context.Context, parent *
return &issueMatch, nil
}

func IssueMatchBaseResolver(app app.Heureka, ctx context.Context, filter *model.IssueMatchFilter, first *int, after *string, parent *model.NodeParent) (*model.IssueMatchConnection, error) {
func IssueMatchBaseResolver(app app.Heureka, ctx context.Context, filter *model.IssueMatchFilter, first *int, after *string, order []*model.IssueMatchOrderBy, parent *model.NodeParent) (*model.IssueMatchConnection, error) {
requestedFields := GetPreloads(ctx)
logrus.WithFields(logrus.Fields{
"requestedFields": requestedFields,
Expand Down Expand Up @@ -120,6 +120,8 @@ func IssueMatchBaseResolver(app app.Heureka, ctx context.Context, filter *model.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume line 97 is not required anymore?

       issue_match_ids := []*int64{}
	for _, issue_match_id := range filter.ID {
                filterById, err := ParseCursor(issue_match_id)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe ParseCursor should be renamed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True

opt := GetListOptions(requestedFields)

opt.Order = model.NewIssueMatchOrder(order)

issueMatches, err := app.ListIssueMatches(f, opt)

if err != nil {
Expand Down
30 changes: 30 additions & 0 deletions internal/api/graphql/graph/model/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,36 @@ func NewIssueMatchEntity(im *IssueMatchInput) entity.IssueMatch {
}
}

func NewIssueMatchOrder(imos []*IssueMatchOrderBy) []entity.Order {
fieldMap := map[IssueMatchOrderByValues]entity.OrderByValue{
IssueMatchOrderByValuesSeverity: entity.IssueMatchOrderValuesSeverity,
IssueMatchOrderByValuesComponentInstanceCcrn: entity.IssueMatchOrderValuesComponentInstanceCCRN,
IssueMatchOrderByValuesTargetRemediationDate: entity.IssueMatchOrderValuesTargetRemediationDate,
}

directionMap := map[OrderDirection]entity.OrderDirectionValue{
OrderDirectionAsc: entity.OrderDirectionValueAsc,
OrderDirectionDesc: entity.OrderDirectionValueDesc,
}

order := make([]entity.Order, len(imos))
for i, imo := range imos {
if _, exists := fieldMap[*imo.Field]; !exists {
return nil
}
if _, exists := directionMap[*imo.Direction]; !exists {
return nil
}

order[i] = entity.Order{
By: fieldMap[*imo.Field],
Direction: directionMap[*imo.Direction],
}
}

return order
}

func NewIssueMatchChange(imc *entity.IssueMatchChange) IssueMatchChange {
action := IssueMatchChangeAction(imc.Action)
return IssueMatchChange{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/api/graphql/graph/resolver/evidence.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/api/graphql/graph/resolver/issue.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions internal/api/graphql/graph/resolver/query.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions internal/api/graphql/graph/schema/common.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ type Page {
pageCount: Int
}

enum OrderDirection {
ASC,
DESC
}

type PageInfo {
hasNextPage: Boolean
hasPreviousPage: Boolean
Expand Down
11 changes: 11 additions & 0 deletions internal/api/graphql/graph/schema/issue_match.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,14 @@ enum IssueMatchStatusValues {
false_positive
mitigated
}

input IssueMatchOrderBy {
field: IssueMatchOrderByValues
direction: OrderDirection
}

enum IssueMatchOrderByValues {
targetRemediationDate,
componentInstanceCCRN,
severity
}
4 changes: 3 additions & 1 deletion internal/api/graphql/graph/schema/query.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

type Query {
Issues(filter: IssueFilter, first: Int, after: String): IssueConnection
IssueMatches(filter: IssueMatchFilter, first: Int, after: String): IssueMatchConnection

IssueMatches(filter: IssueMatchFilter, first: Int, after: String, order: [IssueMatchOrderBy]): IssueMatchConnection

IssueMatchChanges(filter: IssueMatchChangeFilter, first: Int, after: String): IssueMatchChangeConnection
Services(filter: ServiceFilter, first: Int, after: String): ServiceConnection
Components(filter: ComponentFilter, first: Int, after: String): ComponentConnection
Expand Down
20 changes: 12 additions & 8 deletions internal/app/activity/activity_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,9 @@ var _ = Describe("When updating Activity", Label("app", "UpdateService"), func()
after = 0
filter = &entity.ActivityFilter{
Paginated: entity.Paginated{
First: &first,
After: &after,
First: &first,
After: &after,
Cursor: lo.ToPtr(""),
},
}
})
Expand Down Expand Up @@ -180,8 +181,9 @@ var _ = Describe("When deleting Activity", Label("app", "DeleteActivity"), func(
after = 0
filter = &entity.ActivityFilter{
Paginated: entity.Paginated{
First: &first,
After: &after,
First: &first,
After: &after,
Cursor: lo.ToPtr(""),
},
}
})
Expand Down Expand Up @@ -218,8 +220,9 @@ var _ = Describe("When modifying relationship of Service and Activity", Label("a
after = 0
filter = &entity.ActivityFilter{
Paginated: entity.Paginated{
First: &first,
After: &after,
First: &first,
After: &after,
Cursor: lo.ToPtr(""),
},
Id: []*int64{&activity.Id},
}
Expand Down Expand Up @@ -262,8 +265,9 @@ var _ = Describe("When modifying relationship of Issue and Activity", Label("app
after = 0
filter = &entity.ActivityFilter{
Paginated: entity.Paginated{
First: &first,
After: &after,
First: &first,
After: &after,
Cursor: lo.ToPtr(""),
},
Id: []*int64{&activity.Id},
}
Expand Down
93 changes: 71 additions & 22 deletions internal/app/common/pagination_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package common

import (
"fmt"
"github.com/samber/lo"

"github.com/cloudoperators/heureka/internal/entity"
"github.com/cloudoperators/heureka/pkg/util"
Expand All @@ -24,15 +25,82 @@ func PreparePagination(filter *entity.Paginated, options *entity.ListOptions) {

func EnsurePaginated(filter *entity.Paginated) {
if filter.First == nil {
first := 10
filter.First = &first
filter.First = lo.ToPtr(10)
}
if filter.After == nil {
var after int64 = 0
filter.After = &after
}
if filter.Cursor == nil {
filter.Cursor = lo.ToPtr("")
}
}

func GetCursorPages(firstCursor *string, cursors []string, pageSize int) ([]entity.Page, entity.Page) {
var currentCursor = ""
var pages []entity.Page
var currentPage entity.Page
var i = 0
var pN = 0
var page entity.Page
for _, c := range cursors {
i++
if i == 1 {
pN++
page = entity.Page{
After: &currentCursor,
IsCurrent: false,
}
}
if c == *firstCursor {
page.IsCurrent = true
}
page.PageCount = util.Ptr(i)
if i >= pageSize {
currentCursor = c
page.PageNumber = util.Ptr(pN)
pages = append(pages, page)
i = 0
if page.IsCurrent {
currentPage = page
}
}
}
if len(cursors)%pageSize != 0 {
page.PageNumber = util.Ptr(pN)
pages = append(pages, page)
if page.IsCurrent {
currentPage = page
}
}
return pages, currentPage
}

func GetCursorPageInfo[T entity.HasCursor](res []T, cursors []string, pageSize int, currentCursor *string) *entity.PageInfo {

var nextPageAfter *string
currentAfter := currentCursor
firstCursor := res[0].Cursor()

if len(res) > 1 {
nextPageAfter = res[len(res)-1].Cursor()
} else {
nextPageAfter = firstCursor
}

pages, currentPage := GetCursorPages(firstCursor, cursors, pageSize)

return &entity.PageInfo{
HasNextPage: util.Ptr(currentPage.PageNumber != nil && *currentPage.PageNumber < len(pages)),
HasPreviousPage: util.Ptr(currentPage.PageNumber != nil && *currentPage.PageNumber > 1),
IsValidPage: util.Ptr(currentPage.After != nil && currentAfter != nil && *currentPage.After == *currentAfter),
PageNumber: currentPage.PageNumber,
NextPageAfter: nextPageAfter,
Pages: pages,
}
}

// Deprecated do not use for new code
func GetPages(firstCursor *string, ids []int64, pageSize int) ([]entity.Page, entity.Page) {
var currentCursor = util.Ptr("0")
var pages []entity.Page
Expand Down Expand Up @@ -73,6 +141,7 @@ func GetPages(firstCursor *string, ids []int64, pageSize int) ([]entity.Page, en
return pages, currentPage
}

// Deprecated do not use for new code
func GetPageInfo[T entity.HasCursor](res []T, ids []int64, pageSize int, currentCursor int64) *entity.PageInfo {
var nextPageAfter *string
currentAfter := util.Ptr(fmt.Sprintf("%d", currentCursor))
Expand All @@ -94,23 +163,3 @@ func GetPageInfo[T entity.HasCursor](res []T, ids []int64, pageSize int, current
Pages: pages,
}
}

func FinalizePagination[T entity.HasCursor](results []T, filter *entity.Paginated, options *entity.ListOptions) (*entity.PageInfo, []T) {
var pageInfo entity.PageInfo
count := len(results)
if options.ShowPageInfo && len(results) > 0 {
hasNextPage := len(results) == *filter.First
if hasNextPage {
results = results[:count-1]
}
firstCursor := results[0].Cursor()
lastCursor := results[len(results)-1].Cursor()
pageInfo = entity.PageInfo{
HasNextPage: &hasNextPage,
StartCursor: firstCursor,
EndCursor: lastCursor,
}

}
return &pageInfo, results
}
10 changes: 6 additions & 4 deletions internal/app/component/component_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ var _ = Describe("When creating Component", Label("app", "CreateComponent"), fun
after = 0
filter = &entity.ComponentFilter{
Paginated: entity.Paginated{
First: &first,
After: &after,
First: &first,
After: &after,
Cursor: lo.ToPtr(""),
},
}
})
Expand Down Expand Up @@ -154,8 +155,9 @@ var _ = Describe("When updating Component", Label("app", "UpdateComponent"), fun
after = 0
filter = &entity.ComponentFilter{
Paginated: entity.Paginated{
First: &first,
After: &after,
First: &first,
After: &after,
Cursor: lo.ToPtr(""),
},
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,9 @@ var _ = Describe("When updating ComponentInstance", Label("app", "UpdateComponen
after = 0
filter = &entity.ComponentInstanceFilter{
Paginated: entity.Paginated{
First: &first,
After: &after,
First: &first,
After: &after,
Cursor: lo.ToPtr(""),
},
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,9 @@ var _ = Describe("When updating ComponentVersion", Label("app", "UpdateComponent
after = 0
filter = &entity.ComponentVersionFilter{
Paginated: entity.Paginated{
First: &first,
After: &after,
First: &first,
After: &after,
Cursor: lo.ToPtr(""),
},
}
})
Expand Down
5 changes: 3 additions & 2 deletions internal/app/evidence/evidence_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,9 @@ var _ = Describe("When updating Evidence", Label("app", "UpdateEvidence"), func(
after = 0
filter = &entity.EvidenceFilter{
Paginated: entity.Paginated{
First: &first,
After: &after,
First: &first,
After: &after,
Cursor: lo.ToPtr(""),
},
}
})
Expand Down
Loading
Loading