-
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 1 commit
8ed8515
56ef552
239c364
670f37e
64db811
46ceeb6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
… by Feed.Created.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,10 @@ package datastore | |
|
||
import ( | ||
"fmt" | ||
"sort" | ||
|
||
"github.com/drone/drone/model" | ||
"github.com/russross/meddler" | ||
"sort" | ||
) | ||
|
||
func (db *datastore) GetUser(id int64) (*model.User, error) { | ||
|
@@ -30,7 +30,7 @@ func (db *datastore) GetUserFeed(listof []*model.RepoLite) ([]*model.Feed, error | |
var ( | ||
args []interface{} | ||
err error | ||
feed = model.Feeds{} | ||
feed = Feeds{} | ||
) | ||
_stmt, _args := toList(listof) | ||
|
||
|
@@ -45,11 +45,12 @@ func (db *datastore) GetUserFeed(listof []*model.RepoLite) ([]*model.Feed, error | |
feed = append(feed, _feed...) | ||
} | ||
} | ||
sort.Sort(sort.Reverse(feed)) | ||
limit := 50 | ||
if len(feed) < limit{ | ||
limit = len(feed) | ||
// avoid sorting for less than 50 | ||
if len(feed) <= limit{ | ||
return feed, err | ||
} | ||
sort.Sort(sort.Reverse(feed)) | ||
return feed[:limit], err | ||
} | ||
|
||
|
@@ -95,6 +96,20 @@ func (db *datastore) DeleteUser(user *model.User) error { | |
return err | ||
} | ||
|
||
type Feeds []*model.Feed | ||
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 can probably be a private type since it won't be exported |
||
|
||
func (slice Feeds) Len() int { | ||
return len(slice) | ||
} | ||
|
||
func (slice Feeds) Less(i, j int) bool { | ||
return slice[i].Created < slice[j].Created; | ||
} | ||
|
||
func (slice Feeds) Swap(i, j int) { | ||
slice[i], slice[j] = slice[j], slice[i] | ||
} | ||
|
||
const userTable = "users" | ||
|
||
const userLoginQuery = ` | ||
|
@@ -125,7 +140,6 @@ SELECT | |
repo_owner | ||
,repo_name | ||
,repo_full_name | ||
,build_id | ||
,build_number | ||
,build_event | ||
,build_status | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
package datastore | ||
|
||
import ( | ||
"math" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/drone/drone/model" | ||
"github.com/russross/meddler" | ||
"math" | ||
) | ||
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 |
||
|
||
// rebind is a helper function that changes the sql | ||
|
@@ -40,8 +40,8 @@ func rebind(query string) string { | |
// helper function that converts a simple repsitory list | ||
// to a sql IN statment. | ||
func toList(listof []*model.RepoLite) ([]string, [][]interface{}) { | ||
const limit = 999 | ||
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 limit = 999 | ||
var pagesCount = int(math.Ceil(float64(total) / float64(limit))) | ||
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 the float64 allocation and conversion here? 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. I'm not sure, because |
||
|
||
var qs = make([]string, pagesCount, pagesCount) | ||
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. shouldn't we have |
||
|
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.
imports should be grouped and sorted based on Go style guide. See https://github.com/golang/go/wiki/CodeReviewComments#imports
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.
Sorry, I'm newbie in go.
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.
no worries :)
appreciate you taking the time to fix the issue and learn Go along the way!