-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: JeffMboya <[email protected]> Revert docs Signed-off-by: JeffMboya <[email protected]> Revert docs Signed-off-by: JeffMboya <[email protected]> Revert docs Signed-off-by: JeffMboya <[email protected]>
- Loading branch information
1 parent
b794084
commit 29fac94
Showing
7 changed files
with
139 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/absmach/propeller/worker" | ||
"github.com/go-kit/kit/endpoint" | ||
) | ||
|
||
func MakeGetWorkerStatEndpoint(svc worker.WorkerService) endpoint.Endpoint { | ||
return func(ctx context.Context, request interface{}) (interface{}, error) { | ||
req := request.(WorkerRequestDTO) | ||
return svc.GetWorkerStats(ctx, req.WorkerID) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package api | ||
|
||
type WorkerRequestDTO struct { | ||
WorkerID string `json:"worker_id"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package api | ||
|
||
import "time" | ||
|
||
type WorkerResponseDTO struct { | ||
WorkerID string `json:"worker_id"` | ||
Name string `json:"name"` | ||
State string `json:"state"` | ||
Function string `json:"function"` | ||
RestartPolicy string `json:"restart_policy"` | ||
RestartCount int `json:"restart_count"` | ||
StartTime time.Time `json:"start_time"` | ||
FinishTime time.Time `json:"finish_time"` | ||
CPUUsage float64 `json:"cpu_usage"` | ||
MemoryUsage float64 `json:"memory_usage"` | ||
TaskCount int `json:"task_count"` | ||
RunningTasks int `json:"running_tasks"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/absmach/propeller/worker" | ||
|
||
"github.com/go-chi/chi/v5" | ||
kithttp "github.com/go-kit/kit/transport/http" | ||
) | ||
|
||
func MakeHandler(svc worker.WorkerService) http.Handler { | ||
opts := []kithttp.ServerOption{} | ||
|
||
mux := chi.NewRouter() | ||
|
||
mux.Route("/workers", func(r chi.Router) { | ||
r.Get("/{worker_id}", kithttp.NewServer( | ||
MakeGetWorkerStatEndpoint(svc), | ||
decodeWorkerRequest, | ||
encodeWorkerResponse, | ||
opts..., | ||
).ServeHTTP) | ||
}) | ||
|
||
return mux | ||
} | ||
|
||
func decodeWorkerRequest(_ context.Context, r *http.Request) (interface{}, error) { | ||
if !strings.Contains(r.Header.Get("Content-Type"), "application/json") { | ||
return nil, errors.New("unsupported content type") | ||
} | ||
|
||
workerID := chi.URLParam(r, "worker_id") | ||
if workerID == "" { | ||
return nil, errors.New("worker_id is required") | ||
} | ||
|
||
return WorkerRequestDTO{WorkerID: workerID}, nil | ||
} | ||
|
||
func encodeWorkerResponse(_ context.Context, w http.ResponseWriter, response interface{}) error { | ||
w.Header().Set("Content-Type", "application/json") | ||
return json.NewEncoder(w).Encode(response) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package worker | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
type WorkerService interface { | ||
GetWorkerStats(ctx context.Context, workerID string) (WorkerResponseDTO, error) | ||
Check failure on line 9 in worker/service.go GitHub Actions / Lint and Build
|
||
} | ||
|
||
type workerService struct{} | ||
|
||
func NewWorkerService() WorkerService { | ||
return &workerService{} | ||
} | ||
|
||
func (s *workerService) GetWorkerStats(ctx context.Context, workerID string) (WorkerResponseDTO, error) { | ||
Check failure on line 18 in worker/service.go GitHub Actions / Lint and Build
|
||
return WorkerResponseDTO{ | ||
Check failure on line 19 in worker/service.go GitHub Actions / Lint and Build
|
||
WorkerID: workerID, | ||
Name: "WorkerName", | ||
State: "Running", | ||
Function: "Compute", | ||
RestartPolicy: "Always", | ||
RestartCount: 1, | ||
StartTime: time.Now().Add(-2 * time.Hour), | ||
FinishTime: time.Now().Add(1 * time.Hour), | ||
CPUUsage: 0.75, | ||
MemoryUsage: 0.65, | ||
TaskCount: 10, | ||
RunningTasks: 3, | ||
}, nil | ||
} |