@@ -4,131 +4,15 @@ import (
44 "net/http"
55
66 "github.com/go-chi/chi/v5"
7-
8- "github.com/slok/sloth/internal/http/backend/app"
9- "github.com/slok/sloth/internal/http/ui/htmx"
107)
118
129func (u ui ) handlerServiceDetails () http.HandlerFunc {
13- // Available components
14- const (
15- componentSLOList = "slo-list"
16- )
17-
18- type tplDataSLO struct {
19- Name string
20- BurningBudgetPercent float64
21- RemainingBudgetWindowPercent float64
22- DetailsURL string
23- CriticalAlertName string
24- WarningAlertName string
25- GroupLabels map [string ]string
26- }
27-
28- type tplData struct {
29- ServiceID string
30- SLOs []tplDataSLO
31- AutoReloadSLOListSeconds int
32- AutoReloadSLOListURL string
33- SLOPagination tplPaginationData
34- }
35-
36- mapSLOsToTPL := func (s []app.RealTimeSLODetails ) []tplDataSLO {
37- var slos []tplDataSLO
38- for _ , slo := range s {
39- critAlert := ""
40- if slo .Alerts .FiringPage != nil {
41- critAlert = slo .Alerts .FiringPage .Name
42- }
43- warnAlert := ""
44- if slo .Alerts .FiringWarning != nil {
45- warnAlert = slo .Alerts .FiringWarning .Name
46- }
47- slos = append (slos , tplDataSLO {
48- Name : slo .SLO .Name ,
49- BurningBudgetPercent : slo .Budget .BurningBudgetPercent ,
50- RemainingBudgetWindowPercent : 100 - slo .Budget .BurnedBudgetWindowPercent ,
51- DetailsURL : urls .AppURL ("/slos/" + slo .SLO .ID ),
52- CriticalAlertName : critAlert ,
53- WarningAlertName : warnAlert ,
54- GroupLabels : slo .SLO .GroupLabels ,
55- })
56- }
57- return slos
58- }
59-
6010 return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
61- ctx := r .Context ()
62- isHTMXCall := htmx .NewRequest (r .Header ).IsHTMXRequest ()
63- component := urls .ComponentFromRequest (r )
64-
6511 svcID := chi .URLParam (r , URLParamServiceID )
66- data := tplData {
67- ServiceID : svcID ,
68- AutoReloadSLOListSeconds : 30 ,
69- AutoReloadSLOListURL : urls .URLWithComponent (urls .AppURL ("/services/" + svcID ), componentSLOList ),
70- }
71-
72- nextCursor := urls .ForwardCursorFromRequest (r )
73- prevCursor := urls .BackwardCursorFromRequest (r )
74-
75- switch {
76- // Snippet SLO list next.
77- case isHTMXCall && component == componentSLOList && nextCursor != "" :
78- // Get SLOs for service.
79- slosResp , err := u .serviceApp .ListSLOs (ctx , app.ListSLOsRequest {
80- FilterServiceID : data .ServiceID ,
81- Cursor : nextCursor ,
82- })
83- if err != nil {
84- u .logger .Errorf ("could not get service SLOs: %s" , err )
85- http .Error (w , "could not get service SLOs" , http .StatusInternalServerError )
86- return
87- }
88-
89- data .SLOs = mapSLOsToTPL (slosResp .SLOs )
90- data .SLOPagination = mapPaginationToTPL (slosResp .PaginationCursors , urls .URLWithComponent (urls .AppURL ("/services/" + data .ServiceID ), componentSLOList ))
91-
92- u .tplRenderer .RenderResponse (ctx , w , r , "app_service_comp_slo_list" , data )
93-
94- // Snippet SLO list previous.
95- case isHTMXCall && component == componentSLOList && prevCursor != "" :
96- // Get SLOs for service.
97- slosResp , err := u .serviceApp .ListSLOs (ctx , app.ListSLOsRequest {
98- FilterServiceID : data .ServiceID ,
99- Cursor : prevCursor ,
100- })
101- if err != nil {
102- u .logger .Errorf ("could not get service SLOs: %s" , err )
103- http .Error (w , "could not get service SLOs" , http .StatusInternalServerError )
104- return
105- }
106-
107- data .SLOs = mapSLOsToTPL (slosResp .SLOs )
108- data .SLOPagination = mapPaginationToTPL (slosResp .PaginationCursors , urls .URLWithComponent (urls .AppURL ("/services/" + data .ServiceID ), componentSLOList ))
109-
110- u .tplRenderer .RenderResponse (ctx , w , r , "app_service_comp_slo_list" , data )
111-
112- // Unknown snippet.
113- case isHTMXCall :
114- http .Error (w , "Unknown component" , http .StatusBadRequest )
115-
116- // Full page load.
117- default :
118- // Get SLOs for service.
119- slosResp , err := u .serviceApp .ListSLOs (ctx , app.ListSLOsRequest {
120- FilterServiceID : data .ServiceID ,
121- })
122- if err != nil {
123- u .logger .Errorf ("could not get service SLOs: %s" , err )
124- http .Error (w , "could not get service SLOs" , http .StatusInternalServerError )
125- return
126- }
12712
128- data . SLOs = mapSLOsToTPL ( slosResp . SLOs )
129- data . SLOPagination = mapPaginationToTPL ( slosResp . PaginationCursors , urls .URLWithComponent ( urls . AppURL ( "/services/" + data . ServiceID ), componentSLOList ) )
13+ currentURL := urls . AppURL ( "/slos" )
14+ currentURL = urls .AddQueryParm ( currentURL , queryParamSLOServiceID , svcID )
13015
131- u .tplRenderer .RenderResponse (ctx , w , r , "app_service" , data )
132- }
16+ http .Redirect (w , r , currentURL , http .StatusSeeOther )
13317 })
13418}
0 commit comments