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"
|
import "git.ostiwe.com/ostiwe-com/status/model"
|
||||||
|
|
||||||
type PublicService struct {
|
type PublicService struct {
|
||||||
|
ID int `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Description string `json:"description"`
|
Description *string `json:"description"`
|
||||||
Statuses []model.Status `json:"statuses"`
|
Statuses []model.Status `json:"statuses"`
|
||||||
Uptime float64 `json:"uptime"`
|
Uptime float64 `json:"uptime"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,8 +18,8 @@ type Service struct {
|
|||||||
// Human-readable service name
|
// Human-readable service name
|
||||||
Name string `gorm:"size:255;not null" json:"name"`
|
Name string `gorm:"size:255;not null" json:"name"`
|
||||||
// Human-readable service description
|
// Human-readable service description
|
||||||
Description string `gorm:"size:255" json:"description"`
|
Description *string `gorm:"size:255" json:"description"`
|
||||||
PublicDescription string `gorm:"size:255" json:"public_description"`
|
PublicDescription *string `gorm:"size:255" json:"public_description"`
|
||||||
Public *bool `gorm:"default:false" json:"public"`
|
Public *bool `gorm:"default:false" json:"public"`
|
||||||
// Host to check, for example 192.168.1.44
|
// Host to check, for example 192.168.1.44
|
||||||
Host string `gorm:"size:255;not null" json:"host"`
|
Host string `gorm:"size:255;not null" json:"host"`
|
||||||
@@ -67,7 +67,7 @@ func (s Service) CalculateUptimePercent() float64 {
|
|||||||
return uptime
|
return uptime
|
||||||
}
|
}
|
||||||
|
|
||||||
sla := 100 - (float64(len(s.Statuses)) / float64(countOfOkStatus))
|
sla := 100 / (float64(len(s.Statuses)) / float64(countOfOkStatus))
|
||||||
ratio := math.Pow(10, float64(2))
|
ratio := math.Pow(10, float64(2))
|
||||||
uptime = math.Round(sla*ratio) / ratio
|
uptime = math.Round(sla*ratio) / ratio
|
||||||
|
|
||||||
|
|||||||
@@ -12,14 +12,15 @@ const (
|
|||||||
StatusOK StatusCode = "ok" // Means - response ok, service is alive
|
StatusOK StatusCode = "ok" // Means - response ok, service is alive
|
||||||
StatusFailed StatusCode = "failed" // Means - response failed, all tries failed, service down
|
StatusFailed StatusCode = "failed" // Means - response failed, all tries failed, service down
|
||||||
StatusWarn StatusCode = "warn" // Means - response failed after N tries and still watched
|
StatusWarn StatusCode = "warn" // Means - response failed after N tries and still watched
|
||||||
|
StatusUncheck StatusCode = "unchecked" // Means - no data
|
||||||
)
|
)
|
||||||
|
|
||||||
type Status struct {
|
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:"-"`
|
ServiceID int `gorm:"one" json:"-"`
|
||||||
Status StatusCode `gorm:"size:255;not null" json:"status"`
|
Status StatusCode `gorm:"size:255;not null" json:"status"`
|
||||||
Description *string `gorm:"size:255" json:"description"`
|
Description *string `gorm:"size:255" json:"description"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"createdAt"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (Status) TableName() string {
|
func (Status) TableName() string {
|
||||||
|
|||||||
@@ -1,25 +1,67 @@
|
|||||||
package transform
|
package transform
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"slices"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"git.ostiwe.com/ostiwe-com/status/dto"
|
"git.ostiwe.com/ostiwe-com/status/dto"
|
||||||
"git.ostiwe.com/ostiwe-com/status/model"
|
"git.ostiwe.com/ostiwe-com/status/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const maxStatuses = 60
|
||||||
|
|
||||||
func PublicServices(items ...model.Service) []dto.PublicService {
|
func PublicServices(items ...model.Service) []dto.PublicService {
|
||||||
result := make([]dto.PublicService, 0, len(items))
|
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
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func PublicService(item model.Service) dto.PublicService {
|
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{
|
return dto.PublicService{
|
||||||
|
ID: item.ID,
|
||||||
Name: item.Name,
|
Name: item.Name,
|
||||||
Description: item.PublicDescription,
|
Description: item.PublicDescription,
|
||||||
Statuses: item.Statuses,
|
Statuses: statuses,
|
||||||
Uptime: item.CalculateUptimePercent(),
|
Uptime: item.CalculateUptimePercent(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user