zdravko/internal/server/handlers/settings.go

152 lines
4.2 KiB
Go
Raw Normal View History

package handlers
import (
"net/http"
"github.com/labstack/echo/v4"
"github.com/mentos1386/zdravko/internal/server/services"
"github.com/mentos1386/zdravko/web/templates/components"
)
2024-05-15 15:25:39 +00:00
type SettingsSidebarGroup struct {
Group string
Pages []*components.Page
}
type Settings struct {
*components.Base
2024-05-17 19:54:14 +00:00
SettingsGroupName string
SettingsSidebarActive *components.Page
2024-05-15 15:25:39 +00:00
SettingsSidebar []SettingsSidebarGroup
User *AuthenticatedUser
SettingsBreadcrumbs []*components.Page
}
2024-05-17 19:54:14 +00:00
func findGroupForPage(groups []SettingsSidebarGroup, page *components.Page) *SettingsSidebarGroup {
for _, group := range groups {
for _, p := range group.Pages {
if p == page {
return &group
}
}
}
return nil
}
func NewSettings(user *AuthenticatedUser, page *components.Page, breadCrumbs []*components.Page) *Settings {
2024-05-17 19:54:14 +00:00
groupName := ""
group := findGroupForPage(SettingsSidebar, page)
if group != nil {
groupName = group.Group
}
return &Settings{
Base: &components.Base{
NavbarActive: GetPageByTitle(Pages, "Settings"),
Navbar: Pages,
},
2024-05-17 19:54:14 +00:00
SettingsGroupName: groupName,
SettingsSidebarActive: page,
2024-05-15 15:25:39 +00:00
SettingsSidebar: SettingsSidebar,
SettingsBreadcrumbs: breadCrumbs,
User: user,
}
}
var SettingsPages = []*components.Page{
2024-05-17 19:54:14 +00:00
{Path: "/settings", Title: "Home", Breadcrumb: "Home"},
{Path: "/settings/incidents", Title: "Incidents", Breadcrumb: "Incidents"},
2024-05-15 15:25:39 +00:00
{Path: "/settings/targets", Title: "Targets", Breadcrumb: "Targets"},
{Path: "/settings/targets/create", Title: "Targets Create", Breadcrumb: "Create"},
{Path: "/settings/hooks", Title: "Hooks", Breadcrumb: "Hooks"},
{Path: "/settings/hooks/create", Title: "Hooks Create", Breadcrumb: "Create"},
{Path: "/settings/checks", Title: "Checks", Breadcrumb: "Checks"},
{Path: "/settings/checks/create", Title: "Checks Create", Breadcrumb: "Create"},
2024-02-24 21:07:49 +00:00
{Path: "/settings/worker-groups", Title: "Worker Groups", Breadcrumb: "Worker Groups"},
{Path: "/settings/worker-groups/create", Title: "Worker Groups Create", Breadcrumb: "Create"},
2024-02-23 11:18:02 +00:00
{Path: "/settings/notifications", Title: "Notifications", Breadcrumb: "Notifications"},
{Path: "/settings/notifications/create", Title: "Notifications Create", Breadcrumb: "Create"},
{Path: "/settings/triggers", Title: "Triggers", Breadcrumb: "Triggers"},
{Path: "/settings/triggers/create", Title: "Triggers Create", Breadcrumb: "Create"},
{Path: "/settings/temporal", Title: "Temporal", Breadcrumb: "Temporal"},
{Path: "/oauth2/logout", Title: "Logout", Breadcrumb: "Logout"},
}
2024-05-15 15:25:39 +00:00
var SettingsSidebar = []SettingsSidebarGroup{
{
Group: "Overview",
Pages: []*components.Page{
2024-05-17 19:54:14 +00:00
GetPageByTitle(SettingsPages, "Home"),
2024-05-15 15:25:39 +00:00
},
},
{
Group: "Monitor",
Pages: []*components.Page{
GetPageByTitle(SettingsPages, "Targets"),
GetPageByTitle(SettingsPages, "Checks"),
GetPageByTitle(SettingsPages, "Hooks"),
2024-05-15 15:25:39 +00:00
},
},
{
Group: "Alert",
2024-05-15 15:25:39 +00:00
Pages: []*components.Page{
GetPageByTitle(SettingsPages, "Triggers"),
},
},
{
Group: "Notify",
Pages: []*components.Page{
GetPageByTitle(SettingsPages, "Incidents"),
GetPageByTitle(SettingsPages, "Notifications"),
},
},
{
Group: "Manage",
Pages: []*components.Page{
GetPageByTitle(SettingsPages, "Worker Groups"),
GetPageByTitle(SettingsPages, "Temporal"),
GetPageByTitle(SettingsPages, "Logout"),
},
},
}
2024-05-17 19:54:14 +00:00
type SettingsHome struct {
2024-02-29 22:42:56 +00:00
*Settings
WorkerGroupsCount int
ChecksCount int
2024-02-29 22:42:56 +00:00
NotificationsCount int
History []*services.CheckHistory
2024-02-29 22:42:56 +00:00
}
2024-05-17 19:54:14 +00:00
func (h *BaseHandler) SettingsHomeGET(c echo.Context) error {
cc := c.(AuthenticatedContext)
2024-02-29 22:42:56 +00:00
ctx := c.Request().Context()
workerGroups, err := services.CountWorkerGroups(ctx, h.db)
if err != nil {
return err
}
checks, err := services.CountChecks(ctx, h.db)
2024-02-29 22:42:56 +00:00
if err != nil {
return err
}
history, err := services.GetLastNCheckHistory(ctx, h.temporal, 10)
2024-02-29 22:42:56 +00:00
if err != nil {
return err
}
2024-05-17 19:54:14 +00:00
return c.Render(http.StatusOK, "settings_home.tmpl", SettingsHome{
2024-02-29 22:42:56 +00:00
Settings: NewSettings(
cc.Principal.User,
2024-05-17 19:54:14 +00:00
GetPageByTitle(SettingsPages, "Home"),
[]*components.Page{GetPageByTitle(SettingsPages, "Home")},
2024-02-29 22:42:56 +00:00
),
WorkerGroupsCount: workerGroups,
ChecksCount: checks,
2024-02-29 22:42:56 +00:00
NotificationsCount: 42,
History: history,
})
}