-
-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathdashboard.go
More file actions
167 lines (140 loc) · 5.08 KB
/
dashboard.go
File metadata and controls
167 lines (140 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package handlers
import (
"context"
"net/http"
"github.com/danielgtaylor/huma/v2"
"github.com/getarcaneapp/arcane/backend/internal/services"
"github.com/getarcaneapp/arcane/types/base"
dashboardtypes "github.com/getarcaneapp/arcane/types/dashboard"
)
type DashboardHandler struct {
dashboardService *services.DashboardService
}
type GetDashboardInput struct {
EnvironmentID string `path:"id" doc:"Environment ID"`
DebugAllGood bool `query:"debugAllGood" default:"false" doc:"Debug mode: force an empty action item list"`
}
type GetDashboardOutput struct {
Body base.ApiResponse[dashboardtypes.Snapshot]
}
type GetDashboardActionItemsInput struct {
EnvironmentID string `path:"id" doc:"Environment ID"`
DebugAllGood bool `query:"debugAllGood" default:"false" doc:"Debug mode: force an empty action item list"`
}
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}
huma.Register(api, huma.Operation{
OperationID: "get-dashboard",
Method: http.MethodGet,
Path: "/environments/{id}/dashboard",
Summary: "Get dashboard snapshot",
Description: "Returns the dashboard first-paint snapshot in a single response",
Tags: []string{"Dashboard"},
Security: []map[string][]string{
{"BearerAuth": {}},
{"ApiKeyAuth": {}},
},
}, h.GetDashboard)
huma.Register(api, huma.Operation{
OperationID: "get-dashboard-action-items",
Method: http.MethodGet,
Path: "/environments/{id}/dashboard/action-items",
Summary: "Get dashboard action items",
Description: "Returns only dashboard action items that currently need attention",
Tags: []string{"Dashboard"},
Security: []map[string][]string{
{"BearerAuth": {}},
{"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)
}
func (h *DashboardHandler) GetDashboard(ctx context.Context, input *GetDashboardInput) (*GetDashboardOutput, error) {
if h.dashboardService == nil {
return nil, huma.Error500InternalServerError("service not available")
}
// EnvironmentID is consumed by env proxy/auth middleware for routing/validation.
_ = input.EnvironmentID
snapshot, err := h.dashboardService.GetSnapshot(ctx, services.DashboardActionItemsOptions{
DebugAllGood: input.DebugAllGood,
})
if err != nil {
return nil, huma.Error500InternalServerError(err.Error())
}
if snapshot == nil {
return nil, huma.Error500InternalServerError("dashboard snapshot not available")
}
return &GetDashboardOutput{
Body: base.ApiResponse[dashboardtypes.Snapshot]{
Success: true,
Data: *snapshot,
},
}, nil
}
func (h *DashboardHandler) GetActionItems(ctx context.Context, input *GetDashboardActionItemsInput) (*GetDashboardActionItemsOutput, error) {
if h.dashboardService == nil {
return nil, huma.Error500InternalServerError("service not available")
}
// EnvironmentID is consumed by env proxy/auth middleware for routing/validation.
_ = input.EnvironmentID
actionItems, err := h.dashboardService.GetActionItems(ctx, services.DashboardActionItemsOptions{
DebugAllGood: input.DebugAllGood,
})
if err != nil {
return nil, huma.Error500InternalServerError(err.Error())
}
if actionItems == nil {
actionItems = &dashboardtypes.ActionItems{Items: []dashboardtypes.ActionItem{}}
} else if actionItems.Items == nil {
actionItems.Items = []dashboardtypes.ActionItem{}
}
return &GetDashboardActionItemsOutput{
Body: base.ApiResponse[dashboardtypes.ActionItems]{
Success: true,
Data: *actionItems,
},
}, 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
}