zdravko/internal/handlers/settingschecks.go

338 lines
8.3 KiB
Go
Raw Normal View History

package handlers
import (
"context"
2024-02-27 11:04:05 +00:00
"database/sql"
"fmt"
"net/http"
2024-03-03 14:28:25 +00:00
"slices"
"strings"
2024-02-27 11:04:05 +00:00
"code.tjo.space/mentos1386/zdravko/database/models"
"code.tjo.space/mentos1386/zdravko/internal/script"
"code.tjo.space/mentos1386/zdravko/internal/services"
"code.tjo.space/mentos1386/zdravko/web/templates/components"
"github.com/go-playground/validator/v10"
"github.com/gosimple/slug"
"github.com/labstack/echo/v4"
)
type CreateCheck struct {
2024-02-24 21:07:49 +00:00
Name string `validate:"required"`
2024-03-03 14:28:25 +00:00
Group string `validate:"required"`
2024-02-24 21:07:49 +00:00
WorkerGroups string `validate:"required"`
Schedule string `validate:"required,cron"`
Script string `validate:"required"`
}
type UpdateCheck struct {
2024-03-03 14:28:25 +00:00
Group string `validate:"required"`
2024-02-24 21:07:49 +00:00
WorkerGroups string `validate:"required"`
Schedule string `validate:"required,cron"`
Script string `validate:"required"`
}
type CheckWithWorkerGroupsAndStatus struct {
*models.CheckWithWorkerGroups
Status services.CheckStatus
}
type SettingsChecks struct {
*Settings
Checks map[string][]*CheckWithWorkerGroupsAndStatus
CheckGroups []string
}
type SettingsCheck struct {
*Settings
Check *CheckWithWorkerGroupsAndStatus
History []*models.CheckHistory
}
type SettingsCheckCreate struct {
2024-03-04 13:20:01 +00:00
*Settings
Example string
}
func (h *BaseHandler) SettingsChecksGET(c echo.Context) error {
cc := c.(AuthenticatedContext)
checks, err := services.GetChecksWithWorkerGroups(context.Background(), h.db)
if err != nil {
return err
}
checksWithStatus := make([]*CheckWithWorkerGroupsAndStatus, len(checks))
for i, check := range checks {
status, err := services.GetCheckStatus(context.Background(), h.temporal, check.Id)
if err != nil {
return err
}
checksWithStatus[i] = &CheckWithWorkerGroupsAndStatus{
CheckWithWorkerGroups: check,
Status: status,
}
}
checkGroups := []string{}
checksByGroup := map[string][]*CheckWithWorkerGroupsAndStatus{}
for _, check := range checksWithStatus {
checksByGroup[check.Group] = append(checksByGroup[check.Group], check)
if slices.Contains(checkGroups, check.Group) == false {
checkGroups = append(checkGroups, check.Group)
2024-03-03 14:28:25 +00:00
}
}
return c.Render(http.StatusOK, "settings_checks.tmpl", &SettingsChecks{
Settings: NewSettings(
cc.Principal.User,
2024-05-15 15:25:39 +00:00
GetPageByTitle(SettingsPages, "Checks"),
[]*components.Page{GetPageByTitle(SettingsPages, "Checks")},
),
Checks: checksByGroup,
CheckGroups: checkGroups,
})
}
func (h *BaseHandler) SettingsChecksDescribeGET(c echo.Context) error {
cc := c.(AuthenticatedContext)
2024-02-29 22:42:56 +00:00
slug := c.Param("id")
check, err := services.GetCheckWithWorkerGroups(context.Background(), h.db, slug)
if err != nil {
return err
}
status, err := services.GetCheckStatus(context.Background(), h.temporal, check.Id)
if err != nil {
return err
}
checkWithStatus := &CheckWithWorkerGroupsAndStatus{
CheckWithWorkerGroups: check,
Status: status,
}
history, err := services.GetCheckHistoryForCheck(context.Background(), h.db, slug)
2024-02-27 11:04:05 +00:00
if err != nil {
return err
}
maxElements := 10
if len(history) < maxElements {
maxElements = len(history)
}
return c.Render(http.StatusOK, "settings_checks_describe.tmpl", &SettingsCheck{
Settings: NewSettings(
cc.Principal.User,
2024-05-15 15:25:39 +00:00
GetPageByTitle(SettingsPages, "Checks"),
[]*components.Page{
2024-05-15 15:25:39 +00:00
GetPageByTitle(SettingsPages, "Checks"),
{
Path: fmt.Sprintf("/settings/checks/%s", slug),
Title: "Describe",
Breadcrumb: check.Name,
},
}),
Check: checkWithStatus,
2024-02-27 11:04:05 +00:00
History: history[:maxElements],
})
}
func (h *BaseHandler) SettingsChecksDescribeDELETE(c echo.Context) error {
2024-02-29 22:42:56 +00:00
slug := c.Param("id")
err := services.DeleteCheck(context.Background(), h.db, slug)
if err != nil {
return err
}
err = services.DeleteCheckSchedule(context.Background(), h.temporal, slug)
if err != nil {
return err
}
return c.Redirect(http.StatusSeeOther, "/settings/checks")
}
func (h *BaseHandler) SettingsChecksDisableGET(c echo.Context) error {
2024-02-29 22:42:56 +00:00
slug := c.Param("id")
check, err := services.GetCheck(context.Background(), h.db, slug)
if err != nil {
return err
}
err = services.SetCheckStatus(context.Background(), h.temporal, check.Id, services.CheckStatusPaused)
if err != nil {
return err
}
return c.Redirect(http.StatusSeeOther, fmt.Sprintf("/settings/checks/%s", slug))
}
func (h *BaseHandler) SettingsChecksEnableGET(c echo.Context) error {
2024-02-29 22:42:56 +00:00
slug := c.Param("id")
check, err := services.GetCheck(context.Background(), h.db, slug)
if err != nil {
return err
}
err = services.SetCheckStatus(context.Background(), h.temporal, check.Id, services.CheckStatusActive)
if err != nil {
return err
}
return c.Redirect(http.StatusSeeOther, fmt.Sprintf("/settings/checks/%s", slug))
}
func (h *BaseHandler) SettingsChecksDescribePOST(c echo.Context) error {
ctx := context.Background()
checkId := c.Param("id")
update := UpdateCheck{
2024-03-04 13:20:01 +00:00
Group: strings.ToLower(c.FormValue("group")),
WorkerGroups: strings.ToLower(strings.TrimSpace(c.FormValue("workergroups"))),
Schedule: c.FormValue("schedule"),
Script: script.EscapeString(c.FormValue("script")),
}
2024-02-24 21:07:49 +00:00
err := validator.New(validator.WithRequiredStructEnabled()).Struct(update)
if err != nil {
return err
}
check, err := services.GetCheck(ctx, h.db, checkId)
if err != nil {
return err
}
check.Group = update.Group
check.Schedule = update.Schedule
check.Script = update.Script
err = services.UpdateCheck(
ctx,
2024-02-27 11:04:05 +00:00
h.db,
check,
)
if err != nil {
return err
}
2024-02-24 21:07:49 +00:00
workerGroups := []*models.WorkerGroup{}
for _, group := range strings.Split(update.WorkerGroups, " ") {
if group == "" {
continue
}
2024-02-27 11:04:05 +00:00
workerGroup, err := services.GetWorkerGroup(ctx, h.db, slug.Make(group))
2024-02-24 21:07:49 +00:00
if err != nil {
2024-02-27 11:04:05 +00:00
if err == sql.ErrNoRows {
2024-02-29 22:42:56 +00:00
workerGroup = &models.WorkerGroup{Name: group, Id: slug.Make(group)}
2024-02-27 11:04:05 +00:00
err = services.CreateWorkerGroup(ctx, h.db, workerGroup)
if err != nil {
return err
}
} else {
return err
}
2024-02-24 21:07:49 +00:00
}
workerGroups = append(workerGroups, workerGroup)
}
2024-02-27 11:04:05 +00:00
err = services.UpdateCheckWorkerGroups(ctx, h.db, check, workerGroups)
2024-02-24 21:07:49 +00:00
if err != nil {
return err
}
err = services.CreateOrUpdateCheckSchedule(ctx, h.temporal, check, workerGroups)
if err != nil {
return err
}
return c.Redirect(http.StatusSeeOther, fmt.Sprintf("/settings/checks/%s", checkId))
}
func (h *BaseHandler) SettingsChecksCreateGET(c echo.Context) error {
cc := c.(AuthenticatedContext)
return c.Render(http.StatusOK, "settings_checks_create.tmpl", &SettingsCheckCreate{
2024-03-04 13:20:01 +00:00
Settings: NewSettings(
cc.Principal.User,
2024-05-15 15:25:39 +00:00
GetPageByTitle(SettingsPages, "Checks"),
2024-03-04 13:20:01 +00:00
[]*components.Page{
2024-05-15 15:25:39 +00:00
GetPageByTitle(SettingsPages, "Checks"),
GetPageByTitle(SettingsPages, "Checks Create"),
2024-03-04 13:20:01 +00:00
},
),
Example: h.examples.Check,
2024-03-04 13:20:01 +00:00
})
}
func (h *BaseHandler) SettingsChecksCreatePOST(c echo.Context) error {
ctx := context.Background()
checkId := slug.Make(c.FormValue("name"))
create := CreateCheck{
Name: c.FormValue("name"),
2024-03-04 13:20:01 +00:00
Group: strings.ToLower(c.FormValue("group")),
WorkerGroups: strings.ToLower(strings.TrimSpace(c.FormValue("workergroups"))),
Schedule: c.FormValue("schedule"),
Script: script.EscapeString(c.FormValue("script")),
}
2024-02-24 21:07:49 +00:00
err := validator.New(validator.WithRequiredStructEnabled()).Struct(create)
if err != nil {
return err
}
2024-02-27 11:04:05 +00:00
workerGroups := []*models.WorkerGroup{}
2024-02-24 21:07:49 +00:00
for _, group := range strings.Split(create.WorkerGroups, " ") {
2024-02-27 11:04:05 +00:00
if group == "" {
continue
}
workerGroup, err := services.GetWorkerGroup(ctx, h.db, slug.Make(group))
if err != nil {
if err == sql.ErrNoRows {
2024-02-29 22:42:56 +00:00
workerGroup = &models.WorkerGroup{Name: group, Id: slug.Make(group)}
2024-02-27 11:04:05 +00:00
err = services.CreateWorkerGroup(ctx, h.db, workerGroup)
if err != nil {
return err
}
} else {
return err
}
}
workerGroups = append(workerGroups, workerGroup)
2024-02-24 21:07:49 +00:00
}
check := &models.Check{
2024-02-27 11:04:05 +00:00
Name: create.Name,
2024-03-03 14:28:25 +00:00
Group: create.Group,
Id: checkId,
2024-02-27 11:04:05 +00:00
Schedule: create.Schedule,
Script: create.Script,
2024-02-24 21:07:49 +00:00
}
err = services.CreateCheck(
ctx,
2024-02-27 11:04:05 +00:00
h.db,
check,
)
if err != nil {
return err
}
err = services.UpdateCheckWorkerGroups(ctx, h.db, check, workerGroups)
if err != nil {
return err
}
err = services.CreateOrUpdateCheckSchedule(ctx, h.temporal, check, workerGroups)
2024-02-27 11:04:05 +00:00
if err != nil {
return err
}
return c.Redirect(http.StatusSeeOther, "/settings/checks")
}