Skip to content
This repository has been archived by the owner on Nov 12, 2023. It is now read-only.

Commit

Permalink
order worker states for API (#17)
Browse files Browse the repository at this point in the history
* order worker states for API

* change pagination
  • Loading branch information
Fabio1988 authored Jun 13, 2023
1 parent 8f5142b commit 2a74d88
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ config.toml
logs/*

# Binary
Flygon
Flygon
flygon
6 changes: 3 additions & 3 deletions routes/paginator.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ func getValueByPath[S any](object S, path []reflect.StructField) reflect.Value {
}

func paginateAndSort[S any](context *gin.Context, collection []S) {
collectionLength := len(collection)
pageParam := context.DefaultQuery("page", "0")
perPageParam := context.DefaultQuery("perPage", "50")
perPageParam := context.DefaultQuery("perPage", strconv.Itoa(collectionLength))
sortByParam := context.DefaultQuery("sortBy", "")
orderParam := context.DefaultQuery("order", "DESC")

Expand Down Expand Up @@ -101,9 +102,8 @@ func paginateAndSort[S any](context *gin.Context, collection []S) {
}
})

collectionLength := len(collection)
firstIndex := page * perPage
lastIndex := min((page+1)*perPage, len(collection))
lastIndex := min((page+1)*perPage, collectionLength)

context.JSON(http.StatusOK, gin.H{
"data": collection[firstIndex:lastIndex],
Expand Down
10 changes: 5 additions & 5 deletions routes/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

type ApiWorkerState struct {
Id int `json:"id"`
Id string `json:"id"`
Uuid string `json:"uuid"`
Username string `json:"username"`
AreaId int `json:"area_id"`
Expand All @@ -26,16 +26,16 @@ func buildWorkerResponse() []ApiWorkerState {
workers := worker.GetWorkers()

workerList := []ApiWorkerState{}
for i, w := range workers {
workerList = append(workerList, buildSingleWorker(w, i))
for _, w := range workers {
workerList = append(workerList, buildSingleWorker(w))
}

return workerList
}

func buildSingleWorker(s *worker.State, i int) ApiWorkerState {
func buildSingleWorker(s *worker.State) ApiWorkerState {
return ApiWorkerState{
Id: i,
Id: s.Uuid,
Uuid: s.Uuid,
Username: s.Username,
AreaId: s.AreaId,
Expand Down
14 changes: 12 additions & 2 deletions worker/workerState.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package worker

import (
"sort"
"sync"
"time"
)
Expand Down Expand Up @@ -76,8 +77,17 @@ func GetWorkersWithArea(areaId int) []*State {

func GetWorkers() (results []*State) {
statesMutex.Lock()
for _, v := range states {
results = append(results, v)

keys := make([]string, 0, len(states))
for k := range states {
keys = append(keys, k)
}
sort.Strings(keys)
// When iterating over a map with a range loop, the iteration order is not specified and is not guaranteed
// to be the same from one iteration to the next
// therefore we need to sort keys first and access states by key to return an ordered list
for _, k := range keys {
results = append(results, states[k])
}
statesMutex.Unlock()
return results
Expand Down

0 comments on commit 2a74d88

Please sign in to comment.