mirror of
https://github.com/TwiN/gatus.git
synced 2024-12-15 17:51:09 +00:00
Refactor duplicate functions
This commit is contained in:
parent
8b5e5f54cc
commit
627173e64f
4 changed files with 15 additions and 30 deletions
|
@ -24,3 +24,14 @@ var (
|
|||
// EventUnhealthy is a type of event that represents a service failing one or more of its conditions
|
||||
EventUnhealthy EventType = "UNHEALTHY"
|
||||
)
|
||||
|
||||
// NewEventFromResult creates an Event from a Result
|
||||
func NewEventFromResult(result *Result) *Event {
|
||||
event := &Event{Timestamp: result.Timestamp}
|
||||
if result.Success {
|
||||
event.Type = EventHealthy
|
||||
} else {
|
||||
event.Type = EventUnhealthy
|
||||
}
|
||||
return event
|
||||
}
|
||||
|
|
|
@ -233,7 +233,7 @@ func (s *Store) Insert(service *core.Service, result *core.Result) {
|
|||
// Silently fail
|
||||
log.Printf("[database][Insert] Failed to insert event=%s for group=%s; service=%s: %s", core.EventStart, service.Group, service.Name, err.Error())
|
||||
}
|
||||
event := generateEventBasedOnResult(result)
|
||||
event := core.NewEventFromResult(result)
|
||||
if err = s.insertEvent(tx, serviceID, event); err != nil {
|
||||
// Silently fail
|
||||
log.Printf("[database][Insert] Failed to insert event=%s for group=%s; service=%s: %s", event.Type, service.Group, service.Name, err.Error())
|
||||
|
@ -249,7 +249,7 @@ func (s *Store) Insert(service *core.Service, result *core.Result) {
|
|||
// that the service either went from Healthy to Unhealthy or Unhealthy -> Healthy, therefore, we'll add
|
||||
// an event to mark the change in state
|
||||
if lastResultSuccess != result.Success {
|
||||
event := generateEventBasedOnResult(result)
|
||||
event := core.NewEventFromResult(result)
|
||||
if err = s.insertEvent(tx, serviceID, event); err != nil {
|
||||
// Silently fail
|
||||
log.Printf("[database][Insert] Failed to insert event=%s for group=%s; service=%s: %s", event.Type, service.Group, service.Name, err.Error())
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
package database
|
||||
|
||||
import "github.com/TwinProduction/gatus/core"
|
||||
|
||||
func generateEventBasedOnResult(result *core.Result) *core.Event {
|
||||
var eventType core.EventType
|
||||
if result.Success {
|
||||
eventType = core.EventHealthy
|
||||
} else {
|
||||
eventType = core.EventUnhealthy
|
||||
}
|
||||
return &core.Event{
|
||||
Type: eventType,
|
||||
Timestamp: result.Timestamp,
|
||||
}
|
||||
}
|
|
@ -62,7 +62,7 @@ func AddResult(ss *core.ServiceStatus, result *core.Result) {
|
|||
if len(ss.Results) > 0 {
|
||||
// Check if there's any change since the last result
|
||||
if ss.Results[len(ss.Results)-1].Success != result.Success {
|
||||
ss.Events = append(ss.Events, generateEventBasedOnResultSuccess(result))
|
||||
ss.Events = append(ss.Events, core.NewEventFromResult(result))
|
||||
if len(ss.Events) > core.MaximumNumberOfEvents {
|
||||
// Doing ss.Events[1:] would usually be sufficient, but in the case where for some reason, the slice has
|
||||
// more than one extra element, we can get rid of all of them at once and thus returning the slice to a
|
||||
|
@ -72,7 +72,7 @@ func AddResult(ss *core.ServiceStatus, result *core.Result) {
|
|||
}
|
||||
} else {
|
||||
// This is the first result, so we need to add the first healthy/unhealthy event
|
||||
ss.Events = append(ss.Events, generateEventBasedOnResultSuccess(result))
|
||||
ss.Events = append(ss.Events, core.NewEventFromResult(result))
|
||||
}
|
||||
ss.Results = append(ss.Results, result)
|
||||
if len(ss.Results) > core.MaximumNumberOfResults {
|
||||
|
@ -83,13 +83,3 @@ func AddResult(ss *core.ServiceStatus, result *core.Result) {
|
|||
}
|
||||
processUptimeAfterResult(ss.Uptime, result)
|
||||
}
|
||||
|
||||
func generateEventBasedOnResultSuccess(result *core.Result) *core.Event {
|
||||
event := &core.Event{Timestamp: result.Timestamp}
|
||||
if result.Success {
|
||||
event.Type = core.EventHealthy
|
||||
} else {
|
||||
event.Type = core.EventUnhealthy
|
||||
}
|
||||
return event
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue