Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion backend/internal/bootstrap/services_bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func initializeServices(ctx context.Context, db *database.DB, cfg *config.Config
svcs.BuildWorkspace = services.NewBuildWorkspaceService(svcs.Settings)
svcs.Project = services.NewProjectService(db, svcs.Settings, svcs.Event, svcs.Image, svcs.Docker, svcs.Build, cfg)
svcs.Container = services.NewContainerService(db, svcs.Event, svcs.Docker, svcs.Image, svcs.Settings, svcs.Project)
svcs.Dashboard = services.NewDashboardService(db, svcs.Docker, svcs.Container, svcs.Settings, svcs.Vulnerability)
svcs.Dashboard = services.NewDashboardService(db, svcs.Docker, svcs.Container, svcs.Settings, svcs.Vulnerability, svcs.Environment)
svcs.Volume = services.NewVolumeService(db, svcs.Docker, svcs.Event, svcs.Settings, svcs.Container, svcs.Image, cfg.BackupVolumeName)
svcs.Network = services.NewNetworkService(db, svcs.Docker, svcs.Event)
svcs.Port = services.NewPortService(svcs.Docker)
Expand Down
48 changes: 48 additions & 0 deletions backend/internal/huma/handlers/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ type GetDashboardActionItemsOutput struct {
Body base.ApiResponse[dashboardtypes.ActionItems]
}

type GetDashboardEnvironmentsOverviewInput struct {
DebugAllGood bool `query:"debugAllGood" default:"false" doc:"Debug mode: force empty action item lists"`
}

type GetDashboardEnvironmentsOverviewOutput struct {
Body base.ApiResponse[dashboardtypes.EnvironmentsOverview]
}

func RegisterDashboard(api huma.API, dashboardService *services.DashboardService) {
h := &DashboardHandler{dashboardService: dashboardService}

Expand Down Expand Up @@ -60,6 +68,19 @@ func RegisterDashboard(api huma.API, dashboardService *services.DashboardService
{"ApiKeyAuth": {}},
},
}, h.GetActionItems)

huma.Register(api, huma.Operation{
OperationID: "get-dashboard-environments-overview",
Method: http.MethodGet,
Path: "/dashboard/environments",
Summary: "Get aggregate environments dashboard overview",
Description: "Returns dashboard summary data for all visible environments",
Tags: []string{"Dashboard"},
Security: []map[string][]string{
{"BearerAuth": {}},
{"ApiKeyAuth": {}},
},
}, h.GetEnvironmentsOverview)
Comment thread
kmendell marked this conversation as resolved.
}

func (h *DashboardHandler) GetDashboard(ctx context.Context, input *GetDashboardInput) (*GetDashboardOutput, error) {
Expand Down Expand Up @@ -117,3 +138,30 @@ func (h *DashboardHandler) GetActionItems(ctx context.Context, input *GetDashboa
},
}, nil
}

func (h *DashboardHandler) GetEnvironmentsOverview(
ctx context.Context,
input *GetDashboardEnvironmentsOverviewInput,
) (*GetDashboardEnvironmentsOverviewOutput, error) {
if h.dashboardService == nil {
return nil, huma.Error500InternalServerError("service not available")
}

overview, err := h.dashboardService.GetEnvironmentsOverview(ctx, services.DashboardActionItemsOptions{
DebugAllGood: input.DebugAllGood,
})
if err != nil {
return nil, huma.Error500InternalServerError(err.Error())
}

if overview == nil {
return nil, huma.Error500InternalServerError("dashboard environments overview not available")
}

return &GetDashboardEnvironmentsOverviewOutput{
Body: base.ApiResponse[dashboardtypes.EnvironmentsOverview]{
Success: true,
Data: *overview,
},
}, nil
}
29 changes: 27 additions & 2 deletions backend/internal/huma/handlers/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func setupDashboardHandlerTestDB(t *testing.T) (*database.DB, *services.Settings

db, err := gorm.Open(glsqlite.Open("file:"+t.Name()+"?mode=memory&cache=shared"), &gorm.Config{})
require.NoError(t, err)
require.NoError(t, db.AutoMigrate(&models.ApiKey{}, &models.ImageUpdateRecord{}, &models.Project{}, &models.SettingVariable{}))
require.NoError(t, db.AutoMigrate(&models.ApiKey{}, &models.Environment{}, &models.ImageUpdateRecord{}, &models.Project{}, &models.SettingVariable{}))

databaseDB := &database.DB{DB: db}
settingsSvc, err := services.NewSettingsService(context.Background(), databaseDB)
Expand Down Expand Up @@ -111,7 +111,7 @@ func TestDashboardHandlerGetDashboardReturnsSnapshot(t *testing.T) {

dockerSvc := newDashboardHandlerTestDockerService(t, settingsSvc, containers, images)
handler := &DashboardHandler{
dashboardService: services.NewDashboardService(db, dockerSvc, nil, settingsSvc, nil),
dashboardService: services.NewDashboardService(db, dockerSvc, nil, settingsSvc, nil, nil),
}

output, err := handler.GetDashboard(context.Background(), &GetDashboardInput{EnvironmentID: "0"})
Expand All @@ -131,3 +131,28 @@ func TestDashboardHandlerGetDashboardReturnsSnapshot(t *testing.T) {
{Kind: dashboardtypes.ActionItemKindExpiringKeys, Count: 1, Severity: dashboardtypes.ActionItemSeverityWarning},
}, snapshot.ActionItems.Items)
}

func TestDashboardHandlerGetEnvironmentsOverviewReturnsAggregateSummary(t *testing.T) {
db, settingsSvc := setupDashboardHandlerTestDB(t)

require.NoError(t, db.WithContext(context.Background()).Create(&models.Environment{
BaseModel: models.BaseModel{ID: "0", CreatedAt: time.Now()},
Name: "Local Docker",
ApiUrl: "http://local.test",
Status: string(models.EnvironmentStatusOffline),
Enabled: true,
}).Error)

handler := &DashboardHandler{
dashboardService: services.NewDashboardService(db, nil, nil, settingsSvc, nil, services.NewEnvironmentService(db, http.DefaultClient, nil, nil, settingsSvc, nil)),
}

output, err := handler.GetEnvironmentsOverview(context.Background(), &GetDashboardEnvironmentsOverviewInput{})
require.NoError(t, err)
require.NotNil(t, output)
require.True(t, output.Body.Success)
require.Equal(t, 1, output.Body.Data.Summary.TotalEnvironments)
require.Len(t, output.Body.Data.Environments, 1)
require.Equal(t, "0", output.Body.Data.Environments[0].Environment.ID)
require.Equal(t, dashboardtypes.EnvironmentSnapshotStateSkipped, output.Body.Data.Environments[0].SnapshotState)
}
38 changes: 1 addition & 37 deletions backend/internal/huma/handlers/environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,43 +619,7 @@ func (h *EnvironmentHandler) UpdateEnvironment(ctx context.Context, input *Updat
}

func (h *EnvironmentHandler) applyEdgeRuntimeState(env *environment.Environment) {
if env == nil || !env.IsEdge {
return
}

connected := false
env.Connected = &connected
env.ConnectedAt = nil
env.LastHeartbeat = nil
env.LastPollAt = nil
env.EdgeTransport = nil

if pollState, ok := edge.GetPollRuntimeRegistry().Get(env.ID, time.Now()); ok {
env.LastPollAt = pollState.LastPollAt
}

if runtimeState, ok := edge.GetTunnelRuntimeState(env.ID); ok {
connected = true
env.Connected = &connected
env.Status = string(models.EnvironmentStatusOnline)
env.ConnectedAt = runtimeState.ConnectedAt
env.LastHeartbeat = runtimeState.LastHeartbeat
if transport, ok := edge.GetActiveTunnelTransport(env.ID); ok {
env.EdgeTransport = &transport
} else if runtimeState.Transport != "" {
env.EdgeTransport = &runtimeState.Transport
}
return
}

if env.LastPollAt != nil {
env.Status = string(models.EnvironmentStatusStandby)
return
}

if env.Status != string(models.EnvironmentStatusPending) {
env.Status = string(models.EnvironmentStatusOffline)
}
services.ApplyEnvironmentRuntimeState(env)
}

// DeleteEnvironment deletes an environment.
Expand Down
Loading
Loading