Skip to content

Commit

Permalink
fix(api): load workers per hatcheries (#3028)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsamin committed Jul 11, 2018
1 parent 6bf8b45 commit 7874418
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 16 deletions.
2 changes: 1 addition & 1 deletion engine/api/api_routes.go
Expand Up @@ -395,7 +395,7 @@ func (api *API) InitRouter() {
r.Handle("/auth/mode", r.GET(api.authModeHandler, Auth(false)))

// Workers
r.Handle("/worker", r.GET(api.getWorkersHandler, Auth(false)), r.POST(api.registerWorkerHandler, Auth(false)))
r.Handle("/worker", r.GET(api.getWorkersHandler), r.POST(api.registerWorkerHandler, Auth(false)))
r.Handle("/worker/refresh", r.POST(api.refreshWorkerHandler))
r.Handle("/worker/checking", r.POST(api.workerCheckingHandler))
r.Handle("/worker/waiting", r.POST(api.workerWaitingHandler))
Expand Down
6 changes: 3 additions & 3 deletions engine/api/hatchery/hatchery.go
Expand Up @@ -126,9 +126,9 @@ func LoadDeadHatcheries(db gorp.SqlExecutor, timeout float64) ([]sdk.Hatchery, e
// LoadHatchery fetch hatchery info from database given UID
func LoadHatchery(db gorp.SqlExecutor, uid, name string) (*sdk.Hatchery, error) {
query := `SELECT id, uid, name, last_beat, group_id, worker_model_id
FROM hatchery
LEFT JOIN hatchery_model ON hatchery_model.hatchery_id = hatchery.id
WHERE uid = $1 AND name = $2`
FROM hatchery
LEFT JOIN hatchery_model ON hatchery_model.hatchery_id = hatchery.id
WHERE uid = $1 AND name = $2`

var h sdk.Hatchery
var wmID sql.NullInt64
Expand Down
7 changes: 6 additions & 1 deletion engine/api/worker.go
Expand Up @@ -53,7 +53,12 @@ func (api *API) getWorkersHandler() Handler {
return sdk.WrapError(err, "getWorkerModels> cannot parse form")
}

workers, errl := worker.LoadWorkers(api.mustDB())
var hatcheryName string
h := getHatchery(ctx)
if h != nil {
hatcheryName = h.Name
}
workers, errl := worker.LoadWorkers(api.mustDB(), hatcheryName)
if errl != nil {
return sdk.WrapError(errl, "getWorkerModels> cannot load workers")
}
Expand Down
13 changes: 10 additions & 3 deletions engine/api/worker/worker.go
Expand Up @@ -144,12 +144,19 @@ func LoadWorker(db gorp.SqlExecutor, id string) (*sdk.Worker, error) {
}

// LoadWorkers load all workers in db
func LoadWorkers(db gorp.SqlExecutor) ([]sdk.Worker, error) {
func LoadWorkers(db gorp.SqlExecutor, hatcheryName string) ([]sdk.Worker, error) {
w := []sdk.Worker{}
var statusS string
query := `SELECT id, name, last_beat, group_id, model, status, hatchery_id, hatchery_name FROM worker WHERE 1 = 1 ORDER BY name ASC`
query := `SELECT id, name, last_beat, group_id, model, status, hatchery_id, hatchery_name FROM worker ORDER BY name ASC`
args := []interface{}{}

rows, err := db.Query(query)
if hatcheryName != "" {
// TODO: remove the hatchery name from worker worker table !
query = `SELECT id, name, last_beat, group_id, model, status, hatchery_id, hatchery_name FROM worker WHERE hatchery_name = $1 ORDER BY name ASC`
args = []interface{}{hatcheryName}
}

rows, err := db.Query(query, args...)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions engine/api/worker/worker_test.go
Expand Up @@ -11,7 +11,7 @@ import (
func TestInsertWorker(t *testing.T) {
db, _ := test.SetupPG(t, bootstrap.InitiliazeDB)

workers, err := LoadWorkers(db)
workers, err := LoadWorkers(db, "")
test.NoError(t, err)
for _, w := range workers {
DeleteWorker(db, w.ID)
Expand All @@ -31,7 +31,7 @@ func TestInsertWorker(t *testing.T) {
func TestDeletetWorker(t *testing.T) {
db, _ := test.SetupPG(t, bootstrap.InitiliazeDB)

workers, errl := LoadWorkers(db)
workers, errl := LoadWorkers(db, "")
test.NoError(t, errl)
for _, w := range workers {
DeleteWorker(db, w.ID)
Expand All @@ -54,7 +54,7 @@ func TestDeletetWorker(t *testing.T) {
func TestLoadWorkers(t *testing.T) {
db, _ := test.SetupPG(t, bootstrap.InitiliazeDB)

workers, errl := LoadWorkers(db)
workers, errl := LoadWorkers(db, "")
test.NoError(t, errl)
for _, w := range workers {
DeleteWorker(db, w.ID)
Expand All @@ -78,7 +78,7 @@ func TestLoadWorkers(t *testing.T) {
}

var errlw error
workers, errlw = LoadWorkers(db)
workers, errlw = LoadWorkers(db, "")
if errlw != nil {
t.Fatalf("Cannot load workers: %s", errlw)
}
Expand Down
8 changes: 4 additions & 4 deletions engine/api/worker_test.go
Expand Up @@ -20,7 +20,7 @@ func Test_workerCheckingHandler(t *testing.T) {
api, _, router := newTestAPI(t, bootstrap.InitiliazeDB)

//1. Load all workers and hatcheries
workers, err := worker.LoadWorkers(api.mustDB())
workers, err := worker.LoadWorkers(api.mustDB(), "")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -101,7 +101,7 @@ func Test_workerCheckingHandler(t *testing.T) {

assert.Equal(t, 204, w.Code)

workers, err = worker.LoadWorkers(api.mustDB())
workers, err = worker.LoadWorkers(api.mustDB(), "")
if err != nil {
t.Fatal(err)
}
Expand All @@ -115,7 +115,7 @@ func Test_workerWaitingHandler(t *testing.T) {
api, _, router := newTestAPI(t, bootstrap.InitiliazeDB)

//1. Load all workers and hatcheries
workers, errlw := worker.LoadWorkers(api.mustDB())
workers, errlw := worker.LoadWorkers(api.mustDB(), "")
if errlw != nil {
t.Fatal(errlw)
}
Expand Down Expand Up @@ -197,7 +197,7 @@ func Test_workerWaitingHandler(t *testing.T) {

assert.Equal(t, 204, w.Code)

workers, err = worker.LoadWorkers(api.mustDB())
workers, err = worker.LoadWorkers(api.mustDB(), "")
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 7874418

Please sign in to comment.