-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
adding paging to get repositories from the database #1793
Changes from all commits
8ed8515
56ef552
239c364
670f37e
64db811
46ceeb6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,24 @@ import ( | |
"github.com/russross/meddler" | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. import grouping / ordering |
||
|
||
// indicate max of each page when paginated repo list | ||
const maxRepoPage = 999 | ||
|
||
// helper type that sort Feed List | ||
type feedHelper []*model.Feed | ||
|
||
func (slice feedHelper) Len() int { | ||
return len(slice) | ||
} | ||
|
||
func (slice feedHelper) Less(i, j int) bool { | ||
return slice[i].Created < slice[j].Created; | ||
} | ||
|
||
func (slice feedHelper) Swap(i, j int) { | ||
slice[i], slice[j] = slice[j], slice[i] | ||
} | ||
|
||
// rebind is a helper function that changes the sql | ||
// bind type from ? to $ for postgres queries. | ||
func rebind(query string) string { | ||
|
@@ -36,14 +54,32 @@ func rebind(query string) string { | |
return string(rqb) | ||
} | ||
|
||
// helper function that calculate pagination | ||
func calculatePagination(total, limit int) (pages int) { | ||
pages = total / limit | ||
if total % limit != 0 { | ||
pages++ | ||
} | ||
return | ||
} | ||
|
||
// helper function that resize list of repo to pagination | ||
func resizeList(listof []*model.RepoLite, page, limit int) []*model.RepoLite { | ||
var total = len(listof) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we avoid returning a
where
it could make sense to slice the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was my first solution... It did not seem attractive for me because the limit variable and the core logic for pagination was replicated on the repos and users functions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
this limit could be a global constant, for example:
I think this is OK There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
note that it could always get split out into a separate helper function:
|
||
var end = (page * limit) + limit | ||
if end > total{ | ||
end = total | ||
} | ||
if total > limit{ | ||
return listof[page * limit:end] | ||
} | ||
return listof | ||
} | ||
|
||
// helper function that converts a simple repsitory list | ||
// to a sql IN statment. | ||
func toList(listof []*model.RepoLite) (string, []interface{}) { | ||
var size = len(listof) | ||
if size > 999 { | ||
size = 999 | ||
listof = listof[:999] | ||
} | ||
var qs = make([]string, size, size) | ||
var in = make([]interface{}, size, size) | ||
for i, repo := range listof { | ||
|
@@ -57,10 +93,6 @@ func toList(listof []*model.RepoLite) (string, []interface{}) { | |
// to a sql IN statement compatible with postgres. | ||
func toListPosgres(listof []*model.RepoLite) (string, []interface{}) { | ||
var size = len(listof) | ||
if size > 999 { | ||
size = 999 | ||
listof = listof[:999] | ||
} | ||
var qs = make([]string, size, size) | ||
var in = make([]interface{}, size, size) | ||
for i, repo := range listof { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package datastore | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/drone/drone/model" | ||
"github.com/franela/goblin" | ||
) | ||
|
||
func TestUtils(t *testing.T) { | ||
g := goblin.Goblin(t) | ||
g.Describe("Utils", func() { | ||
|
||
g.It("Should Calculate Pagination", func() { | ||
g.Assert(calculatePagination(10, 100)).Equal(1) | ||
g.Assert(calculatePagination(10, 5)).Equal(2) | ||
g.Assert(calculatePagination(10, 3)).Equal(4) | ||
}) | ||
|
||
g.It("Should Resize a RepoLite List", func() { | ||
repoLiteList := []*model.RepoLite{ | ||
{FullName: "bradrydzewski/drone"}, | ||
{FullName: "drone/drone"}, | ||
{FullName: "octocat/hello-world-1"}, | ||
{FullName: "octocat/hello-world-2"}, | ||
{FullName: "octocat/hello-world-3"}, | ||
} | ||
notResized := resizeList(repoLiteList, 0, 10) | ||
page1 := resizeList(repoLiteList, 0, 2) | ||
page2 := resizeList(repoLiteList, 1, 2) | ||
page3 := resizeList(repoLiteList, 2, 2) | ||
|
||
g.Assert(len(notResized)).Equal(5) | ||
|
||
g.Assert(len(page1)).Equal(2) | ||
g.Assert(page1[0].FullName).Equal("bradrydzewski/drone") | ||
g.Assert(page1[1].FullName).Equal("drone/drone") | ||
|
||
g.Assert(len(page2)).Equal(2) | ||
g.Assert(page2[0].FullName).Equal("octocat/hello-world-1") | ||
g.Assert(page2[1].FullName).Equal("octocat/hello-world-2") | ||
|
||
g.Assert(len(page3)).Equal(1) | ||
g.Assert(page3[0].FullName).Equal("octocat/hello-world-3") | ||
}) | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the newer versions of the Go linter will complain when underscores are used in variable names, so we've tried to start removing these from our code. So perhaps
repoLiteList
instead?