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"
|
2024-03-03 14:28:25 +00:00
|
|
|
"slices"
|
2024-02-24 11:57:53 +00:00
|
|
|
"strings"
|
|
|
|
|
2024-02-27 11:04:05 +00:00
|
|
|
"code.tjo.space/mentos1386/zdravko/database/models"
|
2024-05-14 19:38:45 +00:00
|
|
|
"code.tjo.space/mentos1386/zdravko/internal/script"
|
2024-02-24 11:57:53 +00:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2024-02-24 21:07:49 +00:00
|
|
|
type CreateMonitor struct {
|
|
|
|
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 UpdateMonitor 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"`
|
|
|
|
}
|
|
|
|
|
2024-02-28 11:06:24 +00:00
|
|
|
type MonitorWithWorkerGroupsAndStatus struct {
|
|
|
|
*models.MonitorWithWorkerGroups
|
|
|
|
Status services.MonitorStatus
|
|
|
|
}
|
|
|
|
|
2024-02-24 11:57:53 +00:00
|
|
|
type SettingsMonitors struct {
|
|
|
|
*Settings
|
2024-03-03 14:28:25 +00:00
|
|
|
Monitors map[string][]*MonitorWithWorkerGroupsAndStatus
|
|
|
|
MonitorGroups []string
|
2024-02-24 11:57:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SettingsMonitor struct {
|
|
|
|
*Settings
|
2024-02-28 11:06:24 +00:00
|
|
|
Monitor *MonitorWithWorkerGroupsAndStatus
|
2024-02-27 11:04:05 +00:00
|
|
|
History []*models.MonitorHistory
|
2024-02-24 11:57:53 +00:00
|
|
|
}
|
|
|
|
|
2024-03-04 13:20:01 +00:00
|
|
|
type SettingsMonitorCreate struct {
|
|
|
|
*Settings
|
|
|
|
Example string
|
|
|
|
}
|
|
|
|
|
2024-02-24 11:57:53 +00:00
|
|
|
func (h *BaseHandler) SettingsMonitorsGET(c echo.Context) error {
|
|
|
|
cc := c.(AuthenticatedContext)
|
|
|
|
|
2024-02-27 11:04:05 +00:00
|
|
|
monitors, err := services.GetMonitorsWithWorkerGroups(context.Background(), h.db)
|
2024-02-24 11:57:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-28 11:06:24 +00:00
|
|
|
monitorsWithStatus := make([]*MonitorWithWorkerGroupsAndStatus, len(monitors))
|
|
|
|
for i, monitor := range monitors {
|
2024-02-29 22:42:56 +00:00
|
|
|
status, err := services.GetMonitorStatus(context.Background(), h.temporal, monitor.Id)
|
2024-02-28 11:06:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
monitorsWithStatus[i] = &MonitorWithWorkerGroupsAndStatus{
|
|
|
|
MonitorWithWorkerGroups: monitor,
|
|
|
|
Status: status,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-03 14:28:25 +00:00
|
|
|
monitorGroups := []string{}
|
|
|
|
monitorsByGroup := map[string][]*MonitorWithWorkerGroupsAndStatus{}
|
|
|
|
for _, monitor := range monitorsWithStatus {
|
|
|
|
monitorsByGroup[monitor.Group] = append(monitorsByGroup[monitor.Group], monitor)
|
|
|
|
if slices.Contains(monitorGroups, monitor.Group) == false {
|
|
|
|
monitorGroups = append(monitorGroups, monitor.Group)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-24 11:57:53 +00:00
|
|
|
return c.Render(http.StatusOK, "settings_monitors.tmpl", &SettingsMonitors{
|
|
|
|
Settings: NewSettings(
|
|
|
|
cc.Principal.User,
|
|
|
|
GetPageByTitle(SettingsPages, "Monitors"),
|
|
|
|
[]*components.Page{GetPageByTitle(SettingsPages, "Monitors")},
|
|
|
|
),
|
2024-03-03 14:28:25 +00:00
|
|
|
Monitors: monitorsByGroup,
|
|
|
|
MonitorGroups: monitorGroups,
|
2024-02-24 11:57:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *BaseHandler) SettingsMonitorsDescribeGET(c echo.Context) error {
|
|
|
|
cc := c.(AuthenticatedContext)
|
|
|
|
|
2024-02-29 22:42:56 +00:00
|
|
|
slug := c.Param("id")
|
2024-02-24 11:57:53 +00:00
|
|
|
|
2024-02-27 11:04:05 +00:00
|
|
|
monitor, err := services.GetMonitorWithWorkerGroups(context.Background(), h.db, slug)
|
2024-02-24 11:57:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-29 22:42:56 +00:00
|
|
|
status, err := services.GetMonitorStatus(context.Background(), h.temporal, monitor.Id)
|
2024-02-28 11:06:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
monitorWithStatus := &MonitorWithWorkerGroupsAndStatus{
|
|
|
|
MonitorWithWorkerGroups: monitor,
|
|
|
|
Status: status,
|
|
|
|
}
|
|
|
|
|
2024-02-27 11:04:05 +00:00
|
|
|
history, err := services.GetMonitorHistoryForMonitor(context.Background(), h.db, slug)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
maxElements := 10
|
|
|
|
if len(history) < maxElements {
|
|
|
|
maxElements = len(history)
|
|
|
|
}
|
|
|
|
|
2024-02-24 11:57:53 +00:00
|
|
|
return c.Render(http.StatusOK, "settings_monitors_describe.tmpl", &SettingsMonitor{
|
|
|
|
Settings: NewSettings(
|
|
|
|
cc.Principal.User,
|
|
|
|
GetPageByTitle(SettingsPages, "Monitors"),
|
|
|
|
[]*components.Page{
|
|
|
|
GetPageByTitle(SettingsPages, "Monitors"),
|
|
|
|
{
|
|
|
|
Path: fmt.Sprintf("/settings/monitors/%s", slug),
|
|
|
|
Title: "Describe",
|
|
|
|
Breadcrumb: monitor.Name,
|
|
|
|
},
|
|
|
|
}),
|
2024-02-28 11:06:24 +00:00
|
|
|
Monitor: monitorWithStatus,
|
2024-02-27 11:04:05 +00:00
|
|
|
History: history[:maxElements],
|
2024-02-24 11:57:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-28 15:40:24 +00:00
|
|
|
func (h *BaseHandler) SettingsMonitorsDescribeDELETE(c echo.Context) error {
|
2024-02-29 22:42:56 +00:00
|
|
|
slug := c.Param("id")
|
2024-02-28 15:40:24 +00:00
|
|
|
|
|
|
|
err := services.DeleteMonitor(context.Background(), h.db, slug)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = services.DeleteMonitorSchedule(context.Background(), h.temporal, slug)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Redirect(http.StatusSeeOther, "/settings/monitors")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *BaseHandler) SettingsMonitorsDisableGET(c echo.Context) error {
|
2024-02-29 22:42:56 +00:00
|
|
|
slug := c.Param("id")
|
2024-02-28 15:40:24 +00:00
|
|
|
|
|
|
|
monitor, err := services.GetMonitor(context.Background(), h.db, slug)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-29 22:42:56 +00:00
|
|
|
err = services.SetMonitorStatus(context.Background(), h.temporal, monitor.Id, services.MonitorStatusPaused)
|
2024-02-28 15:40:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Redirect(http.StatusSeeOther, fmt.Sprintf("/settings/monitors/%s", slug))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *BaseHandler) SettingsMonitorsEnableGET(c echo.Context) error {
|
2024-02-29 22:42:56 +00:00
|
|
|
slug := c.Param("id")
|
2024-02-28 15:40:24 +00:00
|
|
|
|
|
|
|
monitor, err := services.GetMonitor(context.Background(), h.db, slug)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-29 22:42:56 +00:00
|
|
|
err = services.SetMonitorStatus(context.Background(), h.temporal, monitor.Id, services.MonitorStatusActive)
|
2024-02-28 15:40:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Redirect(http.StatusSeeOther, fmt.Sprintf("/settings/monitors/%s", slug))
|
|
|
|
}
|
|
|
|
|
2024-02-24 11:57:53 +00:00
|
|
|
func (h *BaseHandler) SettingsMonitorsDescribePOST(c echo.Context) error {
|
|
|
|
ctx := context.Background()
|
2024-02-29 22:42:56 +00:00
|
|
|
monitorId := c.Param("id")
|
2024-02-24 11:57:53 +00:00
|
|
|
|
2024-02-24 21:07:49 +00:00
|
|
|
update := UpdateMonitor{
|
2024-03-04 13:20:01 +00:00
|
|
|
Group: strings.ToLower(c.FormValue("group")),
|
|
|
|
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-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-02-29 22:42:56 +00:00
|
|
|
monitor, err := services.GetMonitor(ctx, h.db, monitorId)
|
2024-02-24 11:57:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-03 14:28:25 +00:00
|
|
|
monitor.Group = update.Group
|
2024-02-24 21:07:49 +00:00
|
|
|
monitor.Schedule = update.Schedule
|
|
|
|
monitor.Script = update.Script
|
2024-02-24 11:57:53 +00:00
|
|
|
|
|
|
|
err = services.UpdateMonitor(
|
|
|
|
ctx,
|
2024-02-27 11:04:05 +00:00
|
|
|
h.db,
|
2024-02-24 21:07:49 +00:00
|
|
|
monitor,
|
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
|
|
|
|
|
|
|
err = services.UpdateMonitorWorkerGroups(ctx, h.db, monitor, workerGroups)
|
2024-02-24 21:07:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-27 11:04:05 +00:00
|
|
|
err = services.CreateOrUpdateMonitorSchedule(ctx, h.temporal, monitor, workerGroups)
|
2024-02-24 11:57:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-29 22:42:56 +00:00
|
|
|
return c.Redirect(http.StatusSeeOther, fmt.Sprintf("/settings/monitors/%s", monitorId))
|
2024-02-24 11:57:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *BaseHandler) SettingsMonitorsCreateGET(c echo.Context) error {
|
|
|
|
cc := c.(AuthenticatedContext)
|
|
|
|
|
2024-03-04 13:20:01 +00:00
|
|
|
return c.Render(http.StatusOK, "settings_monitors_create.tmpl", &SettingsMonitorCreate{
|
|
|
|
Settings: NewSettings(
|
|
|
|
cc.Principal.User,
|
2024-02-24 11:57:53 +00:00
|
|
|
GetPageByTitle(SettingsPages, "Monitors"),
|
2024-03-04 13:20:01 +00:00
|
|
|
[]*components.Page{
|
|
|
|
GetPageByTitle(SettingsPages, "Monitors"),
|
|
|
|
GetPageByTitle(SettingsPages, "Monitors Create"),
|
|
|
|
},
|
|
|
|
),
|
2024-05-14 19:38:45 +00:00
|
|
|
Example: h.examples.Monitor,
|
2024-03-04 13:20:01 +00:00
|
|
|
})
|
2024-02-24 11:57:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *BaseHandler) SettingsMonitorsCreatePOST(c echo.Context) error {
|
|
|
|
ctx := context.Background()
|
2024-02-29 22:42:56 +00:00
|
|
|
monitorId := slug.Make(c.FormValue("name"))
|
2024-02-24 11:57:53 +00:00
|
|
|
|
2024-02-24 21:07:49 +00:00
|
|
|
create := CreateMonitor{
|
2024-02-24 11:57:53 +00:00
|
|
|
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"))),
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
monitor := &models.Monitor{
|
2024-02-27 11:04:05 +00:00
|
|
|
Name: create.Name,
|
2024-03-03 14:28:25 +00:00
|
|
|
Group: create.Group,
|
2024-02-29 22:42:56 +00:00
|
|
|
Id: monitorId,
|
2024-02-27 11:04:05 +00:00
|
|
|
Schedule: create.Schedule,
|
|
|
|
Script: create.Script,
|
2024-02-24 21:07:49 +00:00
|
|
|
}
|
|
|
|
|
2024-02-24 11:57:53 +00:00
|
|
|
err = services.CreateMonitor(
|
|
|
|
ctx,
|
2024-02-27 11:04:05 +00:00
|
|
|
h.db,
|
2024-02-24 21:07:49 +00:00
|
|
|
monitor,
|
2024-02-24 11:57:53 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-27 11:04:05 +00:00
|
|
|
err = services.UpdateMonitorWorkerGroups(ctx, h.db, monitor, workerGroups)
|
2024-02-24 11:57:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-27 11:04:05 +00:00
|
|
|
err = services.CreateOrUpdateMonitorSchedule(ctx, h.temporal, monitor, workerGroups)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Redirect(http.StatusSeeOther, "/settings/monitors")
|
2024-02-24 11:57:53 +00:00
|
|
|
}
|