2024-02-11 19:28:00 +00:00
|
|
|
package handlers
|
2024-02-10 17:35:16 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2024-02-20 10:24:04 +00:00
|
|
|
"github.com/labstack/echo/v4"
|
2024-05-23 16:33:30 +00:00
|
|
|
"github.com/mentos1386/zdravko/internal/server/services"
|
|
|
|
"github.com/mentos1386/zdravko/web/templates/components"
|
2024-02-10 17:35:16 +00:00
|
|
|
)
|
|
|
|
|
2024-05-15 15:25:39 +00:00
|
|
|
type SettingsSidebarGroup struct {
|
|
|
|
Group string
|
|
|
|
Pages []*components.Page
|
|
|
|
}
|
|
|
|
|
2024-02-12 10:22:14 +00:00
|
|
|
type Settings struct {
|
|
|
|
*components.Base
|
2024-05-17 19:54:14 +00:00
|
|
|
SettingsGroupName string
|
2024-02-15 22:47:56 +00:00
|
|
|
SettingsSidebarActive *components.Page
|
2024-05-15 15:25:39 +00:00
|
|
|
SettingsSidebar []SettingsSidebarGroup
|
2024-02-15 22:47:56 +00:00
|
|
|
User *AuthenticatedUser
|
|
|
|
SettingsBreadcrumbs []*components.Page
|
2024-02-12 10:22:14 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-02-15 22:47:56 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-02-15 22:47:56 +00:00
|
|
|
return &Settings{
|
2024-02-12 13:20:38 +00:00
|
|
|
Base: &components.Base{
|
2024-02-15 22:47:56 +00:00
|
|
|
NavbarActive: GetPageByTitle(Pages, "Settings"),
|
|
|
|
Navbar: Pages,
|
2024-02-12 13:20:38 +00:00
|
|
|
},
|
2024-05-17 19:54:14 +00:00
|
|
|
SettingsGroupName: groupName,
|
2024-02-15 22:47:56 +00:00
|
|
|
SettingsSidebarActive: page,
|
2024-05-15 15:25:39 +00:00
|
|
|
SettingsSidebar: SettingsSidebar,
|
2024-02-15 22:47:56 +00:00
|
|
|
SettingsBreadcrumbs: breadCrumbs,
|
|
|
|
User: user,
|
2024-02-12 13:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-15 22:47:56 +00:00
|
|
|
var SettingsPages = []*components.Page{
|
2024-05-17 19:54:14 +00:00
|
|
|
{Path: "/settings", Title: "Home", Breadcrumb: "Home"},
|
2024-05-16 20:15:14 +00:00
|
|
|
{Path: "/settings/incidents", Title: "Incidents", Breadcrumb: "Incidents"},
|
2024-05-15 15:25:39 +00:00
|
|
|
{Path: "/settings/targets", Title: "Targets", Breadcrumb: "Targets"},
|
2024-05-14 19:38:45 +00:00
|
|
|
{Path: "/settings/targets/create", Title: "Targets Create", Breadcrumb: "Create"},
|
2024-05-16 20:15:14 +00:00
|
|
|
{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"},
|
2024-03-01 09:53:36 +00:00
|
|
|
{Path: "/settings/notifications/create", Title: "Notifications Create", Breadcrumb: "Create"},
|
2024-04-28 19:24:00 +00:00
|
|
|
{Path: "/settings/triggers", Title: "Triggers", Breadcrumb: "Triggers"},
|
|
|
|
{Path: "/settings/triggers/create", Title: "Triggers Create", Breadcrumb: "Create"},
|
2024-02-19 14:23:17 +00:00
|
|
|
{Path: "/settings/temporal", Title: "Temporal", Breadcrumb: "Temporal"},
|
2024-02-15 22:47:56 +00:00
|
|
|
{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"),
|
2024-05-16 20:15:14 +00:00
|
|
|
GetPageByTitle(SettingsPages, "Hooks"),
|
2024-05-15 15:25:39 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-05-16 20:15:14 +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-02-15 22:47:56 +00:00
|
|
|
}
|
|
|
|
|
2024-05-17 19:54:14 +00:00
|
|
|
type SettingsHome struct {
|
2024-02-29 22:42:56 +00:00
|
|
|
*Settings
|
|
|
|
WorkerGroupsCount int
|
2024-05-16 20:15:14 +00:00
|
|
|
ChecksCount int
|
2024-02-29 22:42:56 +00:00
|
|
|
NotificationsCount int
|
2024-05-23 16:33:30 +00:00
|
|
|
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 {
|
2024-02-20 10:24:04 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
checks, err := services.CountChecks(ctx, h.db)
|
2024-02-29 22:42:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-23 16:33:30 +00:00
|
|
|
history, err := services.GetLastNCheckHistory(ctx, h.temporal, 10)
|
2024-02-29 22:42:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-10 17:35:16 +00:00
|
|
|
|
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,
|
2024-05-16 20:15:14 +00:00
|
|
|
ChecksCount: checks,
|
2024-02-29 22:42:56 +00:00
|
|
|
NotificationsCount: 42,
|
|
|
|
History: history,
|
|
|
|
})
|
2024-02-10 17:35:16 +00:00
|
|
|
}
|