zdravko/internal/activities/healthcheck.go

74 lines
1.5 KiB
Go
Raw Normal View History

package activities
import (
"bytes"
"context"
"encoding/json"
"fmt"
2024-02-21 13:09:22 +00:00
"log/slog"
"net/http"
"code.tjo.space/mentos1386/zdravko/pkg/api"
"code.tjo.space/mentos1386/zdravko/pkg/k6"
)
type HealtcheckParam struct {
Script string
}
type HealthcheckResult struct {
Success bool
Note string
}
func (a *Activities) Healthcheck(ctx context.Context, param HealtcheckParam) (*HealthcheckResult, error) {
2024-02-21 13:09:22 +00:00
execution := k6.NewExecution(slog.Default(), param.Script)
result, err := execution.Run(ctx)
if err != nil {
return nil, err
}
return &HealthcheckResult{Success: result.Success, Note: result.Note}, nil
2024-02-17 19:19:18 +00:00
}
type HealtcheckAddToHistoryParam struct {
Slug string
Status string
Note string
2024-02-17 19:19:18 +00:00
}
type HealthcheckAddToHistoryResult struct {
2024-02-17 19:19:18 +00:00
}
func (a *Activities) HealthcheckAddToHistory(ctx context.Context, param HealtcheckAddToHistoryParam) (*HealthcheckAddToHistoryResult, error) {
url := fmt.Sprintf("%s/api/v1/healthchecks/%s/history", a.config.ApiUrl, param.Slug)
body := api.ApiV1HealthchecksHistoryPOSTBody{
Status: param.Status,
Note: param.Note,
}
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
return &HealthcheckAddToHistoryResult{}, nil
}