Files
ITWorx-Pulse-Public/internal/network/provider.go
T
ITWorx Pulse release export bd774932d5
Public source validation / validate (push) Failing after 3m8s
Publish ITWorx Pulse source
2026-09-03 02:09:19 +02:00

56 lines
1.4 KiB
Go

package network
import (
"context"
"errors"
"time"
"github.com/itworx/pulse/internal/host"
"github.com/itworx/pulse/internal/service"
)
type Aggregator struct {
Host host.Provider
Services service.Provider
Limits Limits
Now func() time.Time
}
func (a Aggregator) Snapshot(ctx context.Context) (Snapshot, error) {
if ctx == nil {
return Snapshot{}, errors.New("network context is nil")
}
if err := ctx.Err(); err != nil {
return Snapshot{}, err
}
now := time.Now().UTC()
if a.Now != nil {
now = a.Now().UTC()
}
hostSnapshot := host.UnknownSnapshot(now, "host", "agent", "source_unavailable")
if a.Host != nil {
next, err := a.Host.Snapshot(ctx)
if err != nil {
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return Snapshot{}, err
}
hostSnapshot = host.UnknownSnapshot(now, "host", "agent", "source_unavailable")
} else {
hostSnapshot = next
}
}
serviceSnapshot := service.UnknownSnapshot(now, "source_unavailable")
if a.Services != nil {
next, err := a.Services.Snapshot(ctx)
if err != nil {
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return Snapshot{}, err
}
serviceSnapshot = service.UnknownSnapshot(now, "source_unavailable")
} else {
serviceSnapshot = next
}
}
return BuildSnapshot(now, hostSnapshot, serviceSnapshot, nil, nil, a.Limits)
}