2024-02-16 12:07:29 +00:00
|
|
|
package activities
|
|
|
|
|
|
|
|
import (
|
2024-02-21 22:15:21 +00:00
|
|
|
"bytes"
|
2024-02-16 12:07:29 +00:00
|
|
|
"context"
|
2024-02-21 22:15:21 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-02-21 13:09:22 +00:00
|
|
|
"log/slog"
|
2024-02-16 12:07:29 +00:00
|
|
|
"net/http"
|
2024-02-21 09:06:54 +00:00
|
|
|
|
2024-02-29 22:42:56 +00:00
|
|
|
"code.tjo.space/mentos1386/zdravko/database/models"
|
2024-02-21 22:15:21 +00:00
|
|
|
"code.tjo.space/mentos1386/zdravko/pkg/api"
|
2024-02-21 09:06:54 +00:00
|
|
|
"code.tjo.space/mentos1386/zdravko/pkg/k6"
|
2024-02-16 12:07:29 +00:00
|
|
|
)
|
|
|
|
|
2024-02-21 09:06:54 +00:00
|
|
|
type HealtcheckParam struct {
|
|
|
|
Script string
|
2024-02-16 12:07:29 +00:00
|
|
|
}
|
|
|
|
|
2024-02-24 11:57:53 +00:00
|
|
|
type MonitorResult struct {
|
2024-02-21 22:15:21 +00:00
|
|
|
Success bool
|
|
|
|
Note string
|
2024-02-16 12:07:29 +00:00
|
|
|
}
|
|
|
|
|
2024-02-24 11:57:53 +00:00
|
|
|
func (a *Activities) Monitor(ctx context.Context, param HealtcheckParam) (*MonitorResult, error) {
|
2024-02-21 13:09:22 +00:00
|
|
|
execution := k6.NewExecution(slog.Default(), param.Script)
|
2024-02-16 12:07:29 +00:00
|
|
|
|
2024-02-21 22:15:21 +00:00
|
|
|
result, err := execution.Run(ctx)
|
2024-02-16 12:07:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-24 11:57:53 +00:00
|
|
|
return &MonitorResult{Success: result.Success, Note: result.Note}, nil
|
2024-02-17 19:19:18 +00:00
|
|
|
}
|
|
|
|
|
2024-02-21 09:06:54 +00:00
|
|
|
type HealtcheckAddToHistoryParam struct {
|
2024-02-29 22:42:56 +00:00
|
|
|
MonitorId string
|
|
|
|
Status models.MonitorStatus
|
|
|
|
Note string
|
|
|
|
WorkerGroupId string
|
2024-02-17 19:19:18 +00:00
|
|
|
}
|
|
|
|
|
2024-02-24 11:57:53 +00:00
|
|
|
type MonitorAddToHistoryResult struct {
|
2024-02-17 19:19:18 +00:00
|
|
|
}
|
|
|
|
|
2024-02-24 11:57:53 +00:00
|
|
|
func (a *Activities) MonitorAddToHistory(ctx context.Context, param HealtcheckAddToHistoryParam) (*MonitorAddToHistoryResult, error) {
|
2024-02-29 22:42:56 +00:00
|
|
|
url := fmt.Sprintf("%s/api/v1/monitors/%s/history", a.config.ApiUrl, param.MonitorId)
|
2024-02-21 22:15:21 +00:00
|
|
|
|
2024-02-24 11:57:53 +00:00
|
|
|
body := api.ApiV1MonitorsHistoryPOSTBody{
|
2024-02-29 22:42:56 +00:00
|
|
|
Status: param.Status,
|
|
|
|
Note: param.Note,
|
|
|
|
WorkerGroupId: param.WorkerGroupId,
|
2024-02-21 22:15:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
jsonBody, err := json.Marshal(body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := api.NewRequest(http.MethodPost, url, a.config.Token, bytes.NewReader(jsonBody))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
if response.StatusCode != http.StatusCreated {
|
|
|
|
return nil, fmt.Errorf("unexpected status code: %d", response.StatusCode)
|
|
|
|
}
|
2024-02-17 19:19:18 +00:00
|
|
|
|
2024-02-24 11:57:53 +00:00
|
|
|
return &MonitorAddToHistoryResult{}, nil
|
2024-02-16 12:07:29 +00:00
|
|
|
}
|