feat: Added ID field to PublicService DTO, update fields in Service model, fix CalculateUptimePercent method, transform services to Public services in goroutines
This commit is contained in:
@@ -3,8 +3,9 @@ package dto
|
||||
import "git.ostiwe.com/ostiwe-com/status/model"
|
||||
|
||||
type PublicService struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Description *string `json:"description"`
|
||||
Statuses []model.Status `json:"statuses"`
|
||||
Uptime float64 `json:"uptime"`
|
||||
}
|
||||
|
||||
@@ -18,9 +18,9 @@ type Service struct {
|
||||
// Human-readable service name
|
||||
Name string `gorm:"size:255;not null" json:"name"`
|
||||
// Human-readable service description
|
||||
Description string `gorm:"size:255" json:"description"`
|
||||
PublicDescription string `gorm:"size:255" json:"public_description"`
|
||||
Public *bool `gorm:"default:false" json:"public"`
|
||||
Description *string `gorm:"size:255" json:"description"`
|
||||
PublicDescription *string `gorm:"size:255" json:"public_description"`
|
||||
Public *bool `gorm:"default:false" json:"public"`
|
||||
// Host to check, for example 192.168.1.44
|
||||
Host string `gorm:"size:255;not null" json:"host"`
|
||||
// Type for check, for now is TCP or HTTP
|
||||
@@ -67,7 +67,7 @@ func (s Service) CalculateUptimePercent() float64 {
|
||||
return uptime
|
||||
}
|
||||
|
||||
sla := 100 - (float64(len(s.Statuses)) / float64(countOfOkStatus))
|
||||
sla := 100 / (float64(len(s.Statuses)) / float64(countOfOkStatus))
|
||||
ratio := math.Pow(10, float64(2))
|
||||
uptime = math.Round(sla*ratio) / ratio
|
||||
|
||||
|
||||
@@ -9,17 +9,18 @@ import (
|
||||
type StatusCode string
|
||||
|
||||
const (
|
||||
StatusOK StatusCode = "ok" // Means - response ok, service is alive
|
||||
StatusFailed StatusCode = "failed" // Means - response failed, all tries failed, service down
|
||||
StatusWarn StatusCode = "warn" // Means - response failed after N tries and still watched
|
||||
StatusOK StatusCode = "ok" // Means - response ok, service is alive
|
||||
StatusFailed StatusCode = "failed" // Means - response failed, all tries failed, service down
|
||||
StatusWarn StatusCode = "warn" // Means - response failed after N tries and still watched
|
||||
StatusUncheck StatusCode = "unchecked" // Means - no data
|
||||
)
|
||||
|
||||
type Status struct {
|
||||
ID int `gorm:"primary_key;auto_increment" json:"-"`
|
||||
ID int `gorm:"primary_key;auto_increment" json:"id"`
|
||||
ServiceID int `gorm:"one" json:"-"`
|
||||
Status StatusCode `gorm:"size:255;not null" json:"status"`
|
||||
Description *string `gorm:"size:255" json:"description"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
}
|
||||
|
||||
func (Status) TableName() string {
|
||||
|
||||
@@ -1,25 +1,67 @@
|
||||
package transform
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"sync"
|
||||
|
||||
"git.ostiwe.com/ostiwe-com/status/dto"
|
||||
"git.ostiwe.com/ostiwe-com/status/model"
|
||||
)
|
||||
|
||||
const maxStatuses = 60
|
||||
|
||||
func PublicServices(items ...model.Service) []dto.PublicService {
|
||||
result := make([]dto.PublicService, 0, len(items))
|
||||
mu := new(sync.Mutex)
|
||||
|
||||
chunked := slices.Chunk(items, 40)
|
||||
|
||||
for services := range chunked {
|
||||
wg := new(sync.WaitGroup)
|
||||
wg.Add(len(services))
|
||||
|
||||
for i := range services {
|
||||
go func(_wg *sync.WaitGroup, item model.Service) {
|
||||
defer _wg.Done()
|
||||
|
||||
transformed := PublicService(item)
|
||||
mu.Lock()
|
||||
result = append(result, transformed)
|
||||
mu.Unlock()
|
||||
}(wg, services[i])
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
for _, item := range items {
|
||||
result = append(result, PublicService(item))
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func PublicService(item model.Service) dto.PublicService {
|
||||
statuses := make([]model.Status, maxStatuses)
|
||||
itemStatusLen := len(item.Statuses)
|
||||
|
||||
for i := range statuses {
|
||||
if i > itemStatusLen-1 {
|
||||
break
|
||||
}
|
||||
statuses[i] = item.Statuses[i]
|
||||
}
|
||||
|
||||
for i := range statuses {
|
||||
if statuses[i].Status == "" {
|
||||
statuses[i].Status = model.StatusUncheck
|
||||
}
|
||||
}
|
||||
|
||||
slices.Reverse(statuses)
|
||||
|
||||
return dto.PublicService{
|
||||
ID: item.ID,
|
||||
Name: item.Name,
|
||||
Description: item.PublicDescription,
|
||||
Statuses: item.Statuses,
|
||||
Statuses: statuses,
|
||||
Uptime: item.CalculateUptimePercent(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user