Files
dygou_go/internal/remote/protocol.go
T

132 lines
4.7 KiB
Go

package remote
import (
"encoding/json"
"time"
)
const (
MessageHello = "hello"
MessageHeartbeat = "heartbeat"
MessageSnapshot = "snapshot"
MessageEvents = "events"
MessageCommand = "command"
MessageCommandAck = "command_ack"
MessageCommandResult = "command_result"
)
// Snapshot is the latest state reported by one application instance.
type Snapshot struct {
MachineID string `json:"machineId"`
MachineName string `json:"machineName"`
InstanceID string `json:"instanceId"`
InstanceName string `json:"instanceName"`
Version string `json:"version"`
ActivityID string `json:"activityId,omitempty"`
State string `json:"state"`
Running bool `json:"running"`
RunID string `json:"runId,omitempty"`
Results map[string]string `json:"results,omitempty"`
AccountCount int `json:"accountCount"`
QueuedEvents uint64 `json:"queuedEvents"`
DroppedEvents uint64 `json:"droppedEvents"`
UpdatedAt time.Time `json:"updatedAt"`
}
// LogEvent is one structured event uploaded by an application instance.
type LogEvent struct {
MachineID string `json:"machineId"`
MachineName string `json:"machineName"`
InstanceID string `json:"instanceId"`
InstanceName string `json:"instanceName"`
StreamID string `json:"streamId"`
Sequence uint64 `json:"sequence"`
Time time.Time `json:"time"`
RunID string `json:"runId,omitempty"`
Category string `json:"category"`
Level string `json:"level"`
Account string `json:"account,omitempty"`
Message string `json:"message"`
}
// Command is a control instruction sent from the server to one instance.
type Command struct {
ID string `json:"id"`
Type string `json:"type"`
Payload json.RawMessage `json:"payload,omitempty"`
CreatedAt time.Time `json:"createdAt"`
ExpiresAt time.Time `json:"expiresAt"`
}
// CommandResult reports the terminal state of one command.
type CommandResult struct {
ID string `json:"id"`
Type string `json:"type"`
Success bool `json:"success"`
Message string `json:"message,omitempty"`
FinishedAt time.Time `json:"finishedAt"`
}
// Envelope is the JSON message used by the agent WebSocket.
type Envelope struct {
Type string `json:"type"`
Hello *Snapshot `json:"hello,omitempty"`
Snapshot *Snapshot `json:"snapshot,omitempty"`
Events []LogEvent `json:"events,omitempty"`
Command *Command `json:"command,omitempty"`
Result *CommandResult `json:"result,omitempty"`
Error string `json:"error,omitempty"`
}
// InstanceState is the server-side view of one connected or recently seen instance.
type InstanceState struct {
Snapshot
Online bool `json:"online"`
LastSeen time.Time `json:"lastSeen"`
RemoteAddr string `json:"remoteAddr,omitempty"`
}
// RunRecord is a compact run history entry reconstructed from snapshots.
type RunRecord struct {
RunID string `json:"runId"`
MachineID string `json:"machineId"`
MachineName string `json:"machineName"`
InstanceID string `json:"instanceId"`
InstanceName string `json:"instanceName"`
State string `json:"state"`
Running bool `json:"running"`
StartedAt time.Time `json:"startedAt"`
FinishedAt time.Time `json:"finishedAt,omitempty"`
Results map[string]string `json:"results,omitempty"`
UpdatedAt time.Time `json:"updatedAt"`
}
// ServerEvent is a persisted event with a server-side cursor.
type ServerEvent struct {
Seq uint64 `json:"seq"`
LogEvent
}
// DashboardState is the polling payload consumed by the remote web console.
type DashboardState struct {
ServerTime time.Time `json:"serverTime"`
Instances []InstanceState `json:"instances"`
Runs []RunRecord `json:"runs"`
Commands []CommandAudit `json:"commands"`
}
// CommandAudit records one command and its delivery/execution state.
type CommandAudit struct {
ID string `json:"id"`
MachineID string `json:"machineId"`
MachineName string `json:"machineName"`
InstanceID string `json:"instanceId"`
InstanceName string `json:"instanceName"`
Type string `json:"type"`
State string `json:"state"`
Message string `json:"message,omitempty"`
CreatedAt time.Time `json:"createdAt"`
DeliveredAt time.Time `json:"deliveredAt,omitempty"`
FinishedAt time.Time `json:"finishedAt,omitempty"`
}