Implement Monlet MVP stack and UI updates

This commit is contained in:
Stanislav Rossovskii
2026-05-27 10:01:59 +04:00
parent dcea096327
commit edc51e9c59
145 changed files with 15618 additions and 248 deletions

View File

@@ -21,26 +21,28 @@ type Client struct {
baseURL string
token string
agentID string
version string
http *http.Client
}
func New(baseURL, token, agentID, version string) *Client {
func New(baseURL, token, agentID string) *Client {
return &Client{
baseURL: strings.TrimRight(baseURL, "/"),
token: token,
agentID: agentID,
version: version,
http: &http.Client{Timeout: 15 * time.Second},
}
}
type AgentFeatures struct {
Push bool `json:"push"`
Metrics bool `json:"metrics"`
}
type HeartbeatRequest struct {
AgentID string `json:"agent_id"`
ObservedAt time.Time `json:"observed_at"`
Hostname string `json:"hostname"`
Version string `json:"version"`
Mode string `json:"mode,omitempty"`
Features AgentFeatures `json:"features"`
Labels map[string]string `json:"labels,omitempty"`
}
@@ -65,7 +67,11 @@ func (c *Client) SendEvents(ctx context.Context, events []scheduler.Event) (*Eve
if len(events) > MaxBatchEvents {
return nil, fmt.Errorf("batch exceeds %d events", MaxBatchEvents)
}
req := EventBatchRequest{AgentID: c.agentID, Events: events}
wireEvents := make([]scheduler.Event, len(events))
for i, ev := range events {
wireEvents[i] = ev.WireEvent()
}
req := EventBatchRequest{AgentID: c.agentID, Events: wireEvents}
var resp EventBatchResponse
if err := c.post(ctx, "/api/v1/events", req, &resp); err != nil {
return nil, err

View File

@@ -24,8 +24,13 @@ func TestSendHeartbeatSuccess(t *testing.T) {
_, _ = io.WriteString(w, `{"accepted":true}`)
}))
defer srv.Close()
c := New(srv.URL, "tok", "a1", "v0")
err := c.SendHeartbeat(context.Background(), HeartbeatRequest{AgentID: "a1", ObservedAt: time.Now().UTC(), Hostname: "h", Version: "v0"})
c := New(srv.URL, "tok", "a1")
err := c.SendHeartbeat(context.Background(), HeartbeatRequest{
AgentID: "a1",
ObservedAt: time.Now().UTC(),
Hostname: "h",
Features: AgentFeatures{Push: true, Metrics: false},
})
if err != nil {
t.Fatal(err)
}
@@ -42,7 +47,7 @@ func TestSendEventsBatch(t *testing.T) {
_, _ = io.WriteString(w, `{"accepted":2,"deduplicated":0}`)
}))
defer srv.Close()
c := New(srv.URL, "tok", "a1", "v0")
c := New(srv.URL, "tok", "a1")
resp, err := c.SendEvents(context.Background(), []scheduler.Event{{EventID: "x"}, {EventID: "y"}})
if err != nil {
t.Fatal(err)
@@ -53,7 +58,7 @@ func TestSendEventsBatch(t *testing.T) {
}
func TestSendEventsTooBig(t *testing.T) {
c := New("http://x", "t", "a1", "v0")
c := New("http://x", "t", "a1")
big := make([]scheduler.Event, MaxBatchEvents+1)
_, err := c.SendEvents(context.Background(), big)
if err == nil {