Files

54 lines
1.5 KiB
Go
Raw Permalink Normal View History

2025-06-10 23:46:40 +02:00
package controller
import (
"context"
2025-06-12 14:49:48 +02:00
log "github.com/sirupsen/logrus"
2025-06-10 23:46:40 +02:00
"github.com/netbirdio/management-refactor/internals/controllers/network_map"
"github.com/netbirdio/management-refactor/internals/shared/db"
appmetrics "github.com/netbirdio/management-refactor/internals/shared/metrics"
)
type Controller struct {
repo Repository
metrics *metrics
2025-06-11 15:31:09 +02:00
UpdateChannel network_map.UpdateChannel
2025-06-10 23:46:40 +02:00
}
2025-06-11 15:31:09 +02:00
func NewController(store *db.Store, metrics *appmetrics.AppMetrics, updateChannel network_map.UpdateChannel) *Controller {
2025-06-10 23:46:40 +02:00
cMetrics, err := appmetrics.RegisterMetrics(metrics, newMetrics)
if err != nil {
2025-06-12 14:49:48 +02:00
log.Fatalf("Failed to register app metrics: %v", err)
2025-06-10 23:46:40 +02:00
}
return &Controller{
repo: newRepository(store, cMetrics),
metrics: cMetrics,
2025-06-11 15:31:09 +02:00
UpdateChannel: updateChannel,
2025-06-10 23:46:40 +02:00
}
}
2025-06-12 20:50:53 +02:00
func (c *Controller) CalculateNetworkMap(ctx context.Context, accountID string) (*network_map.NetworkMap, error) {
2025-06-11 15:31:09 +02:00
_, err := c.repo.GetNetworkMapData(accountID)
if err != nil {
// usually return error
}
2025-06-10 23:46:40 +02:00
// Do calc on data
2025-06-12 20:50:53 +02:00
log.WithContext(ctx).Tracef("Calculating network map for account on public")
2025-06-11 15:31:09 +02:00
2025-06-10 23:46:40 +02:00
return &network_map.NetworkMap{}, nil
}
func (c *Controller) UpdatePeers(ctx context.Context, accountID string) error {
2025-06-12 20:50:53 +02:00
_, err := c.CalculateNetworkMap(ctx, accountID)
2025-06-10 23:46:40 +02:00
if err != nil {
2025-06-12 14:49:48 +02:00
log.Errorf("Failed to calculate network map for account %s: %v", accountID, err)
2025-06-11 15:31:09 +02:00
return err
2025-06-10 23:46:40 +02:00
}
2025-06-11 15:31:09 +02:00
c.UpdateChannel.SendUpdate(ctx, accountID, &network_map.UpdateMessage{})
2025-06-10 23:46:40 +02:00
return nil
}