Skip to content

Commit

Permalink
Fix host to run assignment
Browse files Browse the repository at this point in the history
A entry in /hosts can span multiple runs due to start and endrun not
being the same.
  • Loading branch information
daenney committed May 9, 2021
1 parent 31a59f9 commit eb9527d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
12 changes: 7 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (c *Client) Schedule(ev *Event) (*Schedule, error) {
return nil, err
}

var hosts = hostsResp{}
var hosts = []host{}
err = json.Unmarshal(results[2], &hosts)
if err != nil {
return nil, err
Expand All @@ -134,11 +134,13 @@ func (c *Client) Schedule(ev *Event) (*Schedule, error) {
}

for _, host := range hosts {
r, ok := runsByID[host.Fields.Run]
if !ok {
continue
for _, run := range host.Runs {
r, ok := runsByID[run]
if !ok {
continue
}
r.Hosts = append(r.Hosts, host.Handle)
}
r.Hosts = append(r.Hosts, host.Fields.Handle)
}

schedule := NewScheduleFrom(finalRuns)
Expand Down
35 changes: 32 additions & 3 deletions host.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
package gdq

import (
"encoding/json"
)

type host struct {
Handle string `json:"handle"`
Runs []uint `json:"runs"`
}

type hostResp struct {
Fields struct {
Run uint `json:"start_run"`
Handle string `json:"name"`
StartRun uint `json:"start_run"`
EndRun uint `json:"end_run"`
Handle string `json:"name"`
} `json:"fields"`
}

type hostsResp []hostResp
func (h *host) UnmarshalJSON(b []byte) error {
var v hostResp
if err := json.Unmarshal(b, &v); err != nil {
return err
}

h.Handle = v.Fields.Handle

if v.Fields.StartRun == v.Fields.EndRun {
h.Runs = []uint{v.Fields.StartRun}
} else {
seq := make([]uint, v.Fields.EndRun-v.Fields.StartRun+1)
for i := range seq {
seq[i] = v.Fields.StartRun + uint(i)
}
h.Runs = seq
}

return nil
}

0 comments on commit eb9527d

Please sign in to comment.