Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle blank AgentLastRuleUpdate #66

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1857,7 +1857,7 @@ type Agent struct {
AgentDecisionTime95th float64 `json:"agent.decision_time_95th"`
AgentDecisionTime99th float64 `json:"agent.decision_time_99th"`
AgentEnabled bool `json:"agent.enabled"`
AgentLastRuleUpdate time.Time `json:"agent.last_rule_update"`
AgentLastRuleUpdate emptyTime `json:"agent.last_rule_update"`
AgentLastSeen time.Time `json:"agent.last_seen"`
AgentLatencyTime50th float64 `json:"agent.latency_time_50th"`
AgentLatencyTime95th float64 `json:"agent.latency_time_95th"`
Expand Down Expand Up @@ -1900,6 +1900,36 @@ type Agent struct {
RuntimeNumGoroutines int `json:"num_goroutines"`
}

// emptyTime wraps time.Time and handles empty string values during unmarshaling.
type emptyTime struct {
time.Time
}

// UnmarshalJSON implements custom unmarshaling for emptyTime.
// Handles cases where the value is either a valid time string or an empty string.
func (ct *emptyTime) UnmarshalJSON(data []byte) error {
// Unmarshaling as a string
var str string
if err := json.Unmarshal(data, &str); err != nil {
return err
}

// If empty, leave the time as a zero value.
if str == "" {
*ct = emptyTime{time.Time{}}
return nil
}

// Attempt to parse the time string in the expected format.
parsedTime, err := time.Parse(time.RFC3339, str)
if err != nil {
return err
}

*ct = emptyTime{parsedTime}
return nil
}

// agentsResponse is the response for the list agents endpoint
type agentsResponse struct {
Data []Agent
Expand Down