Skip to content

Commit

Permalink
Allow showing just the last requests made
Browse files Browse the repository at this point in the history
  • Loading branch information
pipeline committed Oct 6, 2023
1 parent e813237 commit 2ba6d18
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions pkg/project/request_model_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type RequestSearchResult struct {
type PartialRequestResponse struct {
TotalRequests uint64
Requests []Request
Offset int
}

// CompareRequests godoc
Expand Down Expand Up @@ -617,7 +618,8 @@ func isInSlice(slice []string, val string) bool {
// @Param sort_dir query string false "Column direction to sort by (default asc)"
// @Param last query int false "Limit to the last n requests (sorted by time)"
// @Param limit query int false "Maximum number of rows to return"
// @Param offset query int false "Offset X rows from the start"
// @Param limit_last query bool false "When limiting the number of rows to return, return the last n rows instead of the first n"
// @Param offset query int false "Offset X rows from the start (if limit_last is not set)"
// @Security ApiKeyAuth
// @Success 200 {array} project.Request
// @Failure 500 {string} string Error
Expand Down Expand Up @@ -692,7 +694,9 @@ func GetRequests(w http.ResponseWriter, r *http.Request) {

limit := r.FormValue("limit")
offset := r.FormValue("offset")
limitLast := r.FormValue("limit_last")
var requestCount int64 = 0
offsetInt := 0
if limit != "" {
tx = tx.Table("requests")
tx.Count(&requestCount)
Expand All @@ -701,11 +705,20 @@ func GetRequests(w http.ResponseWriter, r *http.Request) {
limitInt, err := strconv.Atoi(limit)
if err == nil {
tx = tx.Limit(limitInt)

if limitLast == "true" {
offsetInt = (int)(requestCount - int64(limitInt))
if offsetInt < 0 {
offsetInt = 0
}
tx = tx.Offset(offsetInt)
}
}
}

if offset != "" {
offsetInt, err := strconv.Atoi(offset)
if offset != "" && limitLast != "true" {
var err error
offsetInt, err = strconv.Atoi(offset)
if err == nil {
tx = tx.Offset(offsetInt)
}
Expand Down Expand Up @@ -735,6 +748,7 @@ func GetRequests(w http.ResponseWriter, r *http.Request) {
response, err = json.Marshal(PartialRequestResponse{
TotalRequests: uint64(requestCount),
Requests: requests,
Offset: offsetInt,
})
} else {
response, err = json.Marshal(requests)
Expand Down

0 comments on commit 2ba6d18

Please sign in to comment.