mirror of
https://github.com/pushbits/server.git
synced 2025-05-14 01:16:57 +02:00
rearange code
This commit is contained in:
parent
6a0c7dc7c9
commit
101db63649
7 changed files with 91 additions and 104 deletions
|
@ -4,13 +4,11 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pushbits/server/internal/api"
|
||||
"github.com/pushbits/server/internal/authentication"
|
||||
"github.com/pushbits/server/internal/model"
|
||||
"github.com/pushbits/server/internal/pberrors"
|
||||
)
|
||||
|
||||
type AlertmanagerHandler struct {
|
||||
|
@ -23,25 +21,6 @@ type AlertmanagerHandlerSettings struct {
|
|||
MessageAnnotation string
|
||||
}
|
||||
|
||||
type hookMessage struct {
|
||||
Version string `json:"version"`
|
||||
GroupKey string `json:"groupKey"`
|
||||
Receiver string `json:"receiver"`
|
||||
GroupLabels map[string]string `json:"groupLabels"`
|
||||
CommonLabels map[string]string `json:"commonLabels"`
|
||||
CommonAnnotations map[string]string `json:"commonAnnotiations"`
|
||||
ExternalURL string `json:"externalURL"`
|
||||
Alerts []alert `json:"alerts"`
|
||||
}
|
||||
|
||||
type alert struct {
|
||||
Labels map[string]string `json:"labels"`
|
||||
Annotiations map[string]string `json:"annotiations"`
|
||||
StartsAt string `json:"startsAt"`
|
||||
EndsAt string `json:"endsAt"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
// CreateAlert godoc
|
||||
// @Summary Create an Alert
|
||||
// @Description Creates an alert that is send to the channel as a notification. This endpoint is compatible with alertmanager webhooks.
|
||||
|
@ -50,7 +29,7 @@ type alert struct {
|
|||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param token query string true "Channels token, can also be provieded in the header"
|
||||
// @Param data body hookMessage true "alertmanager webhook call"
|
||||
// @Param data body model.AlertmanagerWebhook true "alertmanager webhook call"
|
||||
// @Success 200 {object} []model.Notification
|
||||
// @Failure 500,404,403 ""
|
||||
// @Router /alert [post]
|
||||
|
@ -58,7 +37,7 @@ func (h *AlertmanagerHandler) CreateAlert(ctx *gin.Context) {
|
|||
application := authentication.GetApplication(ctx)
|
||||
log.Printf("Sending alert notification for application %s.", application.Name)
|
||||
|
||||
var hook hookMessage
|
||||
var hook model.AlertmanagerWebhook
|
||||
if err := ctx.Bind(&hook); err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -68,7 +47,7 @@ func (h *AlertmanagerHandler) CreateAlert(ctx *gin.Context) {
|
|||
notification := alert.ToNotification(h.Settings.TitleAnnotation, h.Settings.MessageAnnotation)
|
||||
notification.Sanitize(application)
|
||||
messageID, err := h.DP.SendNotification(application, ¬ification)
|
||||
if success := successOrAbort(ctx, http.StatusInternalServerError, err); !success {
|
||||
if success := api.SuccessOrAbort(ctx, http.StatusInternalServerError, err); !success {
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -78,62 +57,3 @@ func (h *AlertmanagerHandler) CreateAlert(ctx *gin.Context) {
|
|||
}
|
||||
ctx.JSON(http.StatusOK, ¬ifications)
|
||||
}
|
||||
|
||||
func (alert *alert) ToNotification(titleAnnotation, messageAnnotation string) model.Notification {
|
||||
title := strings.Builder{}
|
||||
message := strings.Builder{}
|
||||
|
||||
switch alert.Status {
|
||||
case "firing":
|
||||
title.WriteString("[FIR] ")
|
||||
case "resolved":
|
||||
title.WriteString("[RES] ")
|
||||
}
|
||||
message.WriteString("STATUS: ")
|
||||
message.WriteString(alert.Status)
|
||||
message.WriteString("\n\n")
|
||||
|
||||
if titleString, ok := alert.Annotiations[titleAnnotation]; ok {
|
||||
title.WriteString(titleString)
|
||||
} else if titleString, ok := alert.Labels[titleAnnotation]; ok {
|
||||
title.WriteString(titleString)
|
||||
} else {
|
||||
title.WriteString("Unknown Title")
|
||||
}
|
||||
|
||||
if messageString, ok := alert.Annotiations[messageAnnotation]; ok {
|
||||
message.WriteString(messageString)
|
||||
} else if messageString, ok := alert.Labels[messageAnnotation]; ok {
|
||||
message.WriteString(messageString)
|
||||
} else {
|
||||
message.WriteString("Unknown Message")
|
||||
}
|
||||
|
||||
message.WriteString("\n")
|
||||
|
||||
for labelName, labelValue := range alert.Labels {
|
||||
message.WriteString("\n")
|
||||
message.WriteString(labelName)
|
||||
message.WriteString(": ")
|
||||
message.WriteString(labelValue)
|
||||
}
|
||||
|
||||
return model.Notification{
|
||||
Message: message.String(),
|
||||
Title: title.String(),
|
||||
}
|
||||
}
|
||||
|
||||
func successOrAbort(ctx *gin.Context, code int, err error) bool {
|
||||
if err != nil {
|
||||
// If we know the error force error code
|
||||
switch err {
|
||||
case pberrors.ErrorMessageNotFound:
|
||||
ctx.AbortWithError(http.StatusNotFound, err)
|
||||
default:
|
||||
ctx.AbortWithError(code, err)
|
||||
}
|
||||
}
|
||||
|
||||
return err == nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue