2024-02-24 11:57:53 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-02-27 11:04:05 +00:00
|
|
|
"database/sql"
|
2024-02-24 11:57:53 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2024-05-23 16:33:30 +00:00
|
|
|
"time"
|
2024-02-24 11:57:53 +00:00
|
|
|
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
|
|
"github.com/gosimple/slug"
|
|
|
|
"github.com/labstack/echo/v4"
|
2024-05-23 16:33:30 +00:00
|
|
|
"github.com/mentos1386/zdravko/database/models"
|
|
|
|
"github.com/mentos1386/zdravko/internal/server/services"
|
|
|
|
"github.com/mentos1386/zdravko/pkg/script"
|
|
|
|
"github.com/mentos1386/zdravko/web/templates/components"
|
2024-02-24 11:57:53 +00:00
|
|
|
)
|
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
type CreateCheck struct {
|
2024-02-24 21:07:49 +00:00
|
|
|
Name string `validate:"required"`
|
|
|
|
WorkerGroups string `validate:"required"`
|
|
|
|
Schedule string `validate:"required,cron"`
|
|
|
|
Script string `validate:"required"`
|
2024-05-23 16:33:30 +00:00
|
|
|
Filter string `validate:"required"`
|
2024-02-24 21:07:49 +00:00
|
|
|
}
|
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
type UpdateCheck struct {
|
2024-02-24 21:07:49 +00:00
|
|
|
WorkerGroups string `validate:"required"`
|
|
|
|
Schedule string `validate:"required,cron"`
|
|
|
|
Script string `validate:"required"`
|
2024-05-23 16:33:30 +00:00
|
|
|
Filter string `validate:"required"`
|
2024-02-24 21:07:49 +00:00
|
|
|
}
|
|
|
|
|
2024-05-18 20:18:28 +00:00
|
|
|
type CheckWithWorkerGroupsAndState struct {
|
2024-05-16 20:15:14 +00:00
|
|
|
*models.CheckWithWorkerGroups
|
2024-05-18 20:18:28 +00:00
|
|
|
State models.CheckState
|
2024-02-28 11:06:24 +00:00
|
|
|
}
|
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
type SettingsChecks struct {
|
2024-02-24 11:57:53 +00:00
|
|
|
*Settings
|
2024-05-23 16:33:30 +00:00
|
|
|
Checks []*CheckWithWorkerGroupsAndState
|
|
|
|
History []struct {
|
|
|
|
CreatedAt time.Time
|
|
|
|
Status string
|
|
|
|
Note string
|
|
|
|
}
|
2024-02-24 11:57:53 +00:00
|
|
|
}
|
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
type SettingsCheck struct {
|
2024-02-24 11:57:53 +00:00
|
|
|
*Settings
|
2024-05-18 20:18:28 +00:00
|
|
|
Check *CheckWithWorkerGroupsAndState
|
2024-05-23 16:33:30 +00:00
|
|
|
History []*services.CheckHistory
|
2024-02-24 11:57:53 +00:00
|
|
|
}
|
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
type SettingsCheckCreate struct {
|
2024-03-04 13:20:01 +00:00
|
|
|
*Settings
|
2024-05-23 16:33:30 +00:00
|
|
|
ExampleScript string
|
|
|
|
ExampleFilter string
|
2024-03-04 13:20:01 +00:00
|
|
|
}
|
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
func (h *BaseHandler) SettingsChecksGET(c echo.Context) error {
|
2024-02-24 11:57:53 +00:00
|
|
|
cc := c.(AuthenticatedContext)
|
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
checks, err := services.GetChecksWithWorkerGroups(context.Background(), h.db)
|
2024-02-24 11:57:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-18 20:18:28 +00:00
|
|
|
checksWithState := make([]*CheckWithWorkerGroupsAndState, len(checks))
|
2024-05-16 20:15:14 +00:00
|
|
|
for i, check := range checks {
|
2024-05-18 20:18:28 +00:00
|
|
|
state, err := services.GetCheckState(context.Background(), h.temporal, check.Id)
|
2024-02-28 11:06:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-18 20:18:28 +00:00
|
|
|
checksWithState[i] = &CheckWithWorkerGroupsAndState{
|
2024-05-16 20:15:14 +00:00
|
|
|
CheckWithWorkerGroups: check,
|
2024-05-18 20:18:28 +00:00
|
|
|
State: state,
|
2024-02-28 11:06:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
return c.Render(http.StatusOK, "settings_checks.tmpl", &SettingsChecks{
|
2024-02-24 11:57:53 +00:00
|
|
|
Settings: NewSettings(
|
|
|
|
cc.Principal.User,
|
2024-05-15 15:25:39 +00:00
|
|
|
GetPageByTitle(SettingsPages, "Checks"),
|
|
|
|
[]*components.Page{GetPageByTitle(SettingsPages, "Checks")},
|
2024-02-24 11:57:53 +00:00
|
|
|
),
|
2024-05-23 16:33:30 +00:00
|
|
|
Checks: checksWithState,
|
2024-02-24 11:57:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
func (h *BaseHandler) SettingsChecksDescribeGET(c echo.Context) error {
|
2024-02-24 11:57:53 +00:00
|
|
|
cc := c.(AuthenticatedContext)
|
|
|
|
|
2024-02-29 22:42:56 +00:00
|
|
|
slug := c.Param("id")
|
2024-02-24 11:57:53 +00:00
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
check, err := services.GetCheckWithWorkerGroups(context.Background(), h.db, slug)
|
2024-02-24 11:57:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-18 20:18:28 +00:00
|
|
|
status, err := services.GetCheckState(context.Background(), h.temporal, check.Id)
|
2024-02-28 11:06:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-18 20:18:28 +00:00
|
|
|
checkWithStatus := &CheckWithWorkerGroupsAndState{
|
2024-05-16 20:15:14 +00:00
|
|
|
CheckWithWorkerGroups: check,
|
2024-05-18 20:18:28 +00:00
|
|
|
State: status,
|
2024-02-28 11:06:24 +00:00
|
|
|
}
|
|
|
|
|
2024-05-23 16:33:30 +00:00
|
|
|
history, err := services.GetCheckHistoryForCheck(context.Background(), h.temporal, slug)
|
2024-02-27 11:04:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
maxElements := 10
|
|
|
|
if len(history) < maxElements {
|
|
|
|
maxElements = len(history)
|
|
|
|
}
|
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
return c.Render(http.StatusOK, "settings_checks_describe.tmpl", &SettingsCheck{
|
2024-02-24 11:57:53 +00:00
|
|
|
Settings: NewSettings(
|
|
|
|
cc.Principal.User,
|
2024-05-15 15:25:39 +00:00
|
|
|
GetPageByTitle(SettingsPages, "Checks"),
|
2024-02-24 11:57:53 +00:00
|
|
|
[]*components.Page{
|
2024-05-15 15:25:39 +00:00
|
|
|
GetPageByTitle(SettingsPages, "Checks"),
|
2024-02-24 11:57:53 +00:00
|
|
|
{
|
2024-05-16 20:15:14 +00:00
|
|
|
Path: fmt.Sprintf("/settings/checks/%s", slug),
|
2024-02-24 11:57:53 +00:00
|
|
|
Title: "Describe",
|
2024-05-16 20:15:14 +00:00
|
|
|
Breadcrumb: check.Name,
|
2024-02-24 11:57:53 +00:00
|
|
|
},
|
|
|
|
}),
|
2024-05-18 20:18:28 +00:00
|
|
|
Check: checkWithStatus,
|
2024-02-27 11:04:05 +00:00
|
|
|
History: history[:maxElements],
|
2024-02-24 11:57:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
func (h *BaseHandler) SettingsChecksDescribeDELETE(c echo.Context) error {
|
2024-02-29 22:42:56 +00:00
|
|
|
slug := c.Param("id")
|
2024-02-28 15:40:24 +00:00
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
err := services.DeleteCheck(context.Background(), h.db, slug)
|
2024-02-28 15:40:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
err = services.DeleteCheckSchedule(context.Background(), h.temporal, slug)
|
2024-02-28 15:40:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
return c.Redirect(http.StatusSeeOther, "/settings/checks")
|
2024-02-28 15:40:24 +00:00
|
|
|
}
|
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
func (h *BaseHandler) SettingsChecksDisableGET(c echo.Context) error {
|
2024-02-29 22:42:56 +00:00
|
|
|
slug := c.Param("id")
|
2024-02-28 15:40:24 +00:00
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
check, err := services.GetCheck(context.Background(), h.db, slug)
|
2024-02-28 15:40:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-18 20:18:28 +00:00
|
|
|
err = services.SetCheckState(context.Background(), h.temporal, check.Id, models.CheckStatePaused)
|
2024-02-28 15:40:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
return c.Redirect(http.StatusSeeOther, fmt.Sprintf("/settings/checks/%s", slug))
|
2024-02-28 15:40:24 +00:00
|
|
|
}
|
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
func (h *BaseHandler) SettingsChecksEnableGET(c echo.Context) error {
|
2024-02-29 22:42:56 +00:00
|
|
|
slug := c.Param("id")
|
2024-02-28 15:40:24 +00:00
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
check, err := services.GetCheck(context.Background(), h.db, slug)
|
2024-02-28 15:40:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-18 20:18:28 +00:00
|
|
|
err = services.SetCheckState(context.Background(), h.temporal, check.Id, models.CheckStateActive)
|
2024-02-28 15:40:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
return c.Redirect(http.StatusSeeOther, fmt.Sprintf("/settings/checks/%s", slug))
|
2024-02-28 15:40:24 +00:00
|
|
|
}
|
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
func (h *BaseHandler) SettingsChecksDescribePOST(c echo.Context) error {
|
2024-02-24 11:57:53 +00:00
|
|
|
ctx := context.Background()
|
2024-05-16 20:15:14 +00:00
|
|
|
checkId := c.Param("id")
|
2024-02-24 11:57:53 +00:00
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
update := UpdateCheck{
|
2024-03-04 13:20:01 +00:00
|
|
|
WorkerGroups: strings.ToLower(strings.TrimSpace(c.FormValue("workergroups"))),
|
2024-02-24 11:57:53 +00:00
|
|
|
Schedule: c.FormValue("schedule"),
|
2024-05-14 19:38:45 +00:00
|
|
|
Script: script.EscapeString(c.FormValue("script")),
|
2024-05-23 16:33:30 +00:00
|
|
|
Filter: c.FormValue("filter"),
|
2024-02-24 11:57:53 +00:00
|
|
|
}
|
2024-02-24 21:07:49 +00:00
|
|
|
err := validator.New(validator.WithRequiredStructEnabled()).Struct(update)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-24 11:57:53 +00:00
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
check, err := services.GetCheck(ctx, h.db, checkId)
|
2024-02-24 11:57:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-16 20:15:14 +00:00
|
|
|
check.Schedule = update.Schedule
|
|
|
|
check.Script = update.Script
|
2024-05-23 16:33:30 +00:00
|
|
|
check.Filter = update.Filter
|
2024-02-24 11:57:53 +00:00
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
err = services.UpdateCheck(
|
2024-02-24 11:57:53 +00:00
|
|
|
ctx,
|
2024-02-27 11:04:05 +00:00
|
|
|
h.db,
|
2024-05-16 20:15:14 +00:00
|
|
|
check,
|
2024-02-24 11:57:53 +00:00
|
|
|
)
|
|
|
|
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
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
err = services.UpdateCheckWorkerGroups(ctx, h.db, check, workerGroups)
|
2024-02-24 21:07:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
err = services.CreateOrUpdateCheckSchedule(ctx, h.temporal, check, workerGroups)
|
2024-02-24 11:57:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
return c.Redirect(http.StatusSeeOther, fmt.Sprintf("/settings/checks/%s", checkId))
|
2024-02-24 11:57:53 +00:00
|
|
|
}
|
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
func (h *BaseHandler) SettingsChecksCreateGET(c echo.Context) error {
|
2024-02-24 11:57:53 +00:00
|
|
|
cc := c.(AuthenticatedContext)
|
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
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
|
|
|
},
|
|
|
|
),
|
2024-05-23 16:33:30 +00:00
|
|
|
ExampleScript: h.examples.Check,
|
|
|
|
ExampleFilter: h.examples.Filter,
|
2024-03-04 13:20:01 +00:00
|
|
|
})
|
2024-02-24 11:57:53 +00:00
|
|
|
}
|
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
func (h *BaseHandler) SettingsChecksCreatePOST(c echo.Context) error {
|
2024-02-24 11:57:53 +00:00
|
|
|
ctx := context.Background()
|
2024-05-16 20:15:14 +00:00
|
|
|
checkId := slug.Make(c.FormValue("name"))
|
2024-02-24 11:57:53 +00:00
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
create := CreateCheck{
|
2024-02-24 11:57:53 +00:00
|
|
|
Name: c.FormValue("name"),
|
2024-03-04 13:20:01 +00:00
|
|
|
WorkerGroups: strings.ToLower(strings.TrimSpace(c.FormValue("workergroups"))),
|
2024-02-24 11:57:53 +00:00
|
|
|
Schedule: c.FormValue("schedule"),
|
2024-05-14 19:38:45 +00:00
|
|
|
Script: script.EscapeString(c.FormValue("script")),
|
2024-05-23 16:33:30 +00:00
|
|
|
Filter: c.FormValue("filter"),
|
2024-02-24 11:57:53 +00:00
|
|
|
}
|
2024-02-24 21:07:49 +00:00
|
|
|
err := validator.New(validator.WithRequiredStructEnabled()).Struct(create)
|
2024-02-24 11:57:53 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
check := &models.Check{
|
2024-05-23 16:33:30 +00:00
|
|
|
Name: create.Name,
|
|
|
|
Id: checkId,
|
|
|
|
Schedule: create.Schedule,
|
|
|
|
Script: create.Script,
|
|
|
|
Filter: create.Filter,
|
2024-02-24 21:07:49 +00:00
|
|
|
}
|
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
err = services.CreateCheck(
|
2024-02-24 11:57:53 +00:00
|
|
|
ctx,
|
2024-02-27 11:04:05 +00:00
|
|
|
h.db,
|
2024-05-16 20:15:14 +00:00
|
|
|
check,
|
2024-02-24 11:57:53 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
err = services.UpdateCheckWorkerGroups(ctx, h.db, check, workerGroups)
|
2024-02-24 11:57:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
err = services.CreateOrUpdateCheckSchedule(ctx, h.temporal, check, workerGroups)
|
2024-02-27 11:04:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-16 20:15:14 +00:00
|
|
|
return c.Redirect(http.StatusSeeOther, "/settings/checks")
|
2024-02-24 11:57:53 +00:00
|
|
|
}
|