Skip to content

Commit

Permalink
Merge pull request #9 from sjtug/wip-api-short
Browse files Browse the repository at this point in the history
implement GET /manager/summary
  • Loading branch information
htfy96 authored Jan 16, 2018
2 parents d1ed18a + 98d404b commit b3ba729
Showing 1 changed file with 44 additions and 4 deletions.
48 changes: 44 additions & 4 deletions manager/json_rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
log "github.com/Sirupsen/logrus"
"github.com/ant0ine/go-json-rest/rest"
"net/http"
"time"
)

// RestfulAPI is a JSON-like API of given manager
Expand All @@ -23,7 +24,8 @@ func (r *RestfulAPI) GetAPIHandler() http.Handler {
api := rest.NewApi()
api.Use(rest.DefaultDevStack...)
router, err := rest.MakeRouter(
rest.Get("/lug/v1/manager", r.getManagerStatus),
rest.Get("/lug/v1/manager", r.getManagerStatusDetail),
rest.Get("/lug/v1/manager/summary", r.getManagerStatusSummary),
rest.Post("/lug/v1/manager/start", r.startManager),
rest.Post("/lug/v1/manager/stop", r.stopManager),
rest.Delete("/lug/v1/manager", r.exitManager),
Expand All @@ -35,9 +37,47 @@ func (r *RestfulAPI) GetAPIHandler() http.Handler {
return api.MakeHandler()
}

func (r *RestfulAPI) getManagerStatus(w rest.ResponseWriter, req *rest.Request) {
status := r.manager.GetStatus()
w.WriteJson(status)
type WorkerStatusSimple struct {
// Result is true if sync succeed, else false
Result bool
// LastFinished indicates last success time
LastFinished time.Time
// Idle stands for whether worker is idle, false if syncing
Idle bool
}

type MangerStatusSimple struct {
Running bool
WorkerStatus map[string]WorkerStatusSimple
}

func (r *RestfulAPI) getManagerStatusCommon(w rest.ResponseWriter, req *rest.Request, detailed bool) {
raw_status := r.manager.GetStatus()
if detailed {
w.WriteJson(raw_status)
return
}
manager_status_simple := MangerStatusSimple{
Running: raw_status.Running,
WorkerStatus: map[string]WorkerStatusSimple{},
}
// summary mode
for worker_key, raw_worker_status := range raw_status.WorkerStatus {
manager_status_simple.WorkerStatus[worker_key] = WorkerStatusSimple{
Result: raw_worker_status.Result,
LastFinished: raw_worker_status.LastFinished,
Idle: raw_worker_status.Idle,
}
}
w.WriteJson(manager_status_simple)
}

func (r *RestfulAPI) getManagerStatusDetail(w rest.ResponseWriter, req *rest.Request) {
r.getManagerStatusCommon(w, req, true)
}

func (r *RestfulAPI) getManagerStatusSummary(w rest.ResponseWriter, req *rest.Request) {
r.getManagerStatusCommon(w, req, false)
}

func (r *RestfulAPI) startManager(w rest.ResponseWriter, req *rest.Request) {
Expand Down

0 comments on commit b3ba729

Please sign in to comment.