Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mfreeman451 committed Feb 26, 2025
1 parent 1b486a7 commit 5006e04
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 167 deletions.
50 changes: 1 addition & 49 deletions pkg/cloud/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,15 @@ import (
"encoding/json"
"log"
"net/http"
"sync"
"time"

"github.com/carverauto/serviceradar/pkg/checker/snmp"
"github.com/carverauto/serviceradar/pkg/db"
srHttp "github.com/carverauto/serviceradar/pkg/http"
"github.com/carverauto/serviceradar/pkg/metrics"
"github.com/carverauto/serviceradar/pkg/models"
"github.com/gorilla/mux"
)

type ServiceStatus struct {
Name string `json:"name"`
Available bool `json:"available"`
Message string `json:"message"`
Type string `json:"type"` // e.g., "process", "port", "blockchain", etc.
Details json.RawMessage `json:"details"` // Flexible field for service-specific data
}

type NodeStatus struct {
NodeID string `json:"node_id"`
IsHealthy bool `json:"is_healthy"`
LastUpdate time.Time `json:"last_update"`
Services []ServiceStatus `json:"services"`
UpTime string `json:"uptime"`
FirstSeen time.Time `json:"first_seen"`
Metrics []models.MetricPoint `json:"metrics,omitempty"`
}

type SystemStatus struct {
TotalNodes int `json:"total_nodes"`
HealthyNodes int `json:"healthy_nodes"`
LastUpdate time.Time `json:"last_update"`
}

type NodeHistory struct {
NodeID string
Timestamp time.Time
IsHealthy bool
Services []ServiceStatus
}

type NodeHistoryPoint struct {
Timestamp time.Time `json:"timestamp"`
IsHealthy bool `json:"is_healthy"`
}

type APIServer struct {
mu sync.RWMutex
nodes map[string]*NodeStatus
router *mux.Router
nodeHistoryHandler func(nodeID string) ([]NodeHistoryPoint, error)
metricsManager metrics.MetricCollector
snmpManager snmp.SNMPManager
db db.Service
knownPollers []string
}

func NewAPIServer(options ...func(server *APIServer)) *APIServer {
s := &APIServer{
nodes: make(map[string]*NodeStatus),
Expand Down Expand Up @@ -273,6 +224,7 @@ func (s *APIServer) getNodes(w http.ResponseWriter, _ *http.Request) {
for _, known := range s.knownPollers {
if id == known {
nodes = append(nodes, node)

break
}
}
Expand Down
60 changes: 60 additions & 0 deletions pkg/cloud/api/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package api

import (
"encoding/json"
"sync"
"time"

"github.com/carverauto/serviceradar/pkg/checker/snmp"
"github.com/carverauto/serviceradar/pkg/db"
"github.com/carverauto/serviceradar/pkg/metrics"
"github.com/carverauto/serviceradar/pkg/models"
"github.com/gorilla/mux"
)

type ServiceStatus struct {
Name string `json:"name"`
Available bool `json:"available"`
Message string `json:"message"`
Type string `json:"type"` // e.g., "process", "port", "blockchain", etc.
Details json.RawMessage `json:"details"` // Flexible field for service-specific data
}

type NodeStatus struct {
NodeID string `json:"node_id"`
IsHealthy bool `json:"is_healthy"`
LastUpdate time.Time `json:"last_update"`
Services []ServiceStatus `json:"services"`
UpTime string `json:"uptime"`
FirstSeen time.Time `json:"first_seen"`
Metrics []models.MetricPoint `json:"metrics,omitempty"`
}

type SystemStatus struct {
TotalNodes int `json:"total_nodes"`
HealthyNodes int `json:"healthy_nodes"`
LastUpdate time.Time `json:"last_update"`
}

type NodeHistory struct {
NodeID string
Timestamp time.Time
IsHealthy bool
Services []ServiceStatus
}

type NodeHistoryPoint struct {
Timestamp time.Time `json:"timestamp"`
IsHealthy bool `json:"is_healthy"`
}

type APIServer struct {
mu sync.RWMutex
nodes map[string]*NodeStatus
router *mux.Router
nodeHistoryHandler func(nodeID string) ([]NodeHistoryPoint, error)
metricsManager metrics.MetricCollector
snmpManager snmp.SNMPManager
db db.Service
knownPollers []string
}
91 changes: 0 additions & 91 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,98 +5,12 @@ import (
"encoding/json"
"fmt"
"os"
"time"

"github.com/carverauto/serviceradar/pkg/models"
)

var (
errInvalidDuration = fmt.Errorf("invalid duration")
)

type Duration time.Duration

func (d *Duration) UnmarshalJSON(b []byte) error {
var v interface{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}

switch value := v.(type) {
case float64:
// parse numeric as nanoseconds
*d = Duration(time.Duration(value))
return nil
case string:
dur, err := time.ParseDuration(value)
if err != nil {
return fmt.Errorf("invalid duration: %w", err)
}

*d = Duration(dur)

return nil
default:
return errInvalidDuration
}
}

// AgentConfig represents the configuration for an agent instance.
type AgentConfig struct {
CheckersDir string `json:"checkers_dir"` // e.g., /etc/serviceradar/checkers
ListenAddr string `json:"listen_addr"` // e.g., :50051
ServiceName string `json:"service_name"` // e.g., "agent"
Security *models.SecurityConfig `json:"security"`
}

// Check represents a generic service check configuration.
type Check struct {
ServiceType string `json:"service_type"` // e.g., "grpc", "process", "port"
ServiceName string `json:"service_name"`
Details string `json:"details,omitempty"` // Service-specific details
Port int32 `json:"port,omitempty"` // For port checkers
Config json.RawMessage `json:"config,omitempty"` // Checker-specific configuration
}

// AgentDefinition represents a remote agent and its checks.
type AgentDefinition struct {
Address string `json:"address"` // gRPC address of the agent
Checks []Check `json:"checks"` // List of checks to run on this agent
}

// PollerConfig represents the configuration for a poller instance.
type PollerConfig struct {
Agents map[string]AgentDefinition `json:"agents"` // Map of agent ID to agent definition
CloudAddress string `json:"cloud_address"` // Address of cloud service
PollInterval Duration `json:"poll_interval"` // How often to poll agents
PollerID string `json:"poller_id"` // Unique identifier for this poller
}

// WebhookConfig represents a webhook notification configuration.
type WebhookConfig struct {
Enabled bool `json:"enabled"`
URL string `json:"url"`
Cooldown Duration `json:"cooldown"`
Template string `json:"template"`
Headers []Header `json:"headers,omitempty"` // Optional custom headers
}

// Header represents a custom HTTP header.
type Header struct {
Key string `json:"key"`
Value string `json:"value"`
}

// CloudConfig represents the configuration for the cloud service.
type CloudConfig struct {
ListenAddr string `json:"listen_addr"`
GrpcAddr string `json:"grpc_addr,omitempty"`
DBPath string `json:"db_path"`
AlertThreshold Duration `json:"alert_threshold"`
KnownPollers []string `json:"known_pollers"`
Webhooks []WebhookConfig `json:"webhooks,omitempty"`
}

// LoadFile is a generic helper that loads a JSON file from path into
// the struct pointed to by dst.
func LoadFile(path string, dst interface{}) error {
Expand All @@ -112,11 +26,6 @@ func LoadFile(path string, dst interface{}) error {
return nil
}

// Validator interface for configurations that need validation.
type Validator interface {
Validate() error
}

// ValidateConfig validates a configuration if it implements Validator.
func ValidateConfig(cfg interface{}) error {
if v, ok := cfg.(Validator); ok {
Expand Down
6 changes: 6 additions & 0 deletions pkg/config/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package config

// Validator interface for configurations that need validation.
type Validator interface {
Validate() error
}
92 changes: 92 additions & 0 deletions pkg/config/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package config

import (
"encoding/json"
"fmt"
"time"

"github.com/carverauto/serviceradar/pkg/models"
)

type Duration time.Duration

func (d *Duration) UnmarshalJSON(b []byte) error {
var v interface{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}

switch value := v.(type) {
case float64:
// parse numeric as nanoseconds
*d = Duration(time.Duration(value))
return nil
case string:
dur, err := time.ParseDuration(value)
if err != nil {
return fmt.Errorf("invalid duration: %w", err)
}

*d = Duration(dur)

return nil
default:
return errInvalidDuration
}
}

// AgentConfig represents the configuration for an agent instance.
type AgentConfig struct {
CheckersDir string `json:"checkers_dir"` // e.g., /etc/serviceradar/checkers
ListenAddr string `json:"listen_addr"` // e.g., :50051
ServiceName string `json:"service_name"` // e.g., "agent"
Security *models.SecurityConfig `json:"security"`
}

// Check represents a generic service check configuration.
type Check struct {
ServiceType string `json:"service_type"` // e.g., "grpc", "process", "port"
ServiceName string `json:"service_name"`
Details string `json:"details,omitempty"` // Service-specific details
Port int32 `json:"port,omitempty"` // For port checkers
Config json.RawMessage `json:"config,omitempty"` // Checker-specific configuration
}

// AgentDefinition represents a remote agent and its checks.
type AgentDefinition struct {
Address string `json:"address"` // gRPC address of the agent
Checks []Check `json:"checks"` // List of checks to run on this agent
}

// PollerConfig represents the configuration for a poller instance.
type PollerConfig struct {
Agents map[string]AgentDefinition `json:"agents"` // Map of agent ID to agent definition
CloudAddress string `json:"cloud_address"` // Address of cloud service
PollInterval Duration `json:"poll_interval"` // How often to poll agents
PollerID string `json:"poller_id"` // Unique identifier for this poller
}

// WebhookConfig represents a webhook notification configuration.
type WebhookConfig struct {
Enabled bool `json:"enabled"`
URL string `json:"url"`
Cooldown Duration `json:"cooldown"`
Template string `json:"template"`
Headers []Header `json:"headers,omitempty"` // Optional custom headers
}

// Header represents a custom HTTP header.
type Header struct {
Key string `json:"key"`
Value string `json:"value"`
}

// CloudConfig represents the configuration for the cloud service.
type CloudConfig struct {
ListenAddr string `json:"listen_addr"`
GrpcAddr string `json:"grpc_addr,omitempty"`
DBPath string `json:"db_path"`
AlertThreshold Duration `json:"alert_threshold"`
KnownPollers []string `json:"known_pollers"`
Webhooks []WebhookConfig `json:"webhooks,omitempty"`
}
Loading

0 comments on commit 5006e04

Please sign in to comment.