From c4609f02f7b1872450e6a084ff75df7fe5a304cc Mon Sep 17 00:00:00 2001 From: naiba Date: Mon, 2 May 2022 00:45:03 +0800 Subject: [PATCH] fix: service total uptime rate close #191 --- README.md | 2 +- model/api.go | 25 +++++++++++++++++-- resource/template/theme-daynight/service.html | 2 +- resource/template/theme-default/service.html | 2 +- resource/template/theme-hotaru/service.html | 2 +- resource/template/theme-mdui/service.html | 2 +- service/singleton/servicesentinel.go | 4 --- service/singleton/singleton.go | 2 +- 8 files changed, 29 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 5e9e6f6..61a19a0 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@
LOGO designed by 熊大 .

-    +   

:trollface: Nezha Monitoring self-hosted lightweight monitoring and operation and maintenance system. Supports system status, HTTP (SSL certificate change, upcoming expiration, expiration), TCP, Ping monitoring and alerting, execute scheduled tasks and web terminal.

diff --git a/model/api.go b/model/api.go index 50085e9..fb7a291 100644 --- a/model/api.go +++ b/model/api.go @@ -2,11 +2,32 @@ package model type ServiceItemResponse struct { Monitor *Monitor - TotalUp uint64 - TotalDown uint64 CurrentUp uint64 CurrentDown uint64 Delay *[30]float32 Up *[30]int Down *[30]int } + +func sum[T int | float32](slice *[30]T) T { + if slice == nil { + return 0 + } + var sum T + for _, v := range *slice { + sum += v + } + return sum +} + +func (r ServiceItemResponse) TotalUp() int { + return sum(r.Up) +} + +func (r ServiceItemResponse) TotalDown() int { + return sum(r.Down) +} + +func (r ServiceItemResponse) TotalUptime() float32 { + return float32(r.TotalUp()) / (float32(r.TotalUp() + r.TotalDown())) * 100 +} diff --git a/resource/template/theme-daynight/service.html b/resource/template/theme-daynight/service.html index d3a5da7..68ba858 100644 --- a/resource/template/theme-daynight/service.html +++ b/resource/template/theme-daynight/service.html @@ -66,7 +66,7 @@ {{statusName (divU64 $service.CurrentUp (addU64 $service.CurrentUp $service.CurrentDown))}}
{{$service.Monitor.Name}}
-
{{float32f (divU64 $service.TotalUp (addU64 $service.TotalUp $service.TotalDown))}}%
+
{{$service.TotalUptime|float32f}}%
{{range $i,$d := $service.Delay}} diff --git a/resource/template/theme-default/service.html b/resource/template/theme-default/service.html index 8272b4a..71e1aae 100644 --- a/resource/template/theme-default/service.html +++ b/resource/template/theme-default/service.html @@ -21,7 +21,7 @@ {{$service.Monitor.Name}} - {{float32f (divU64 $service.TotalUp (addU64 $service.TotalUp $service.TotalDown))}}% + {{$service.TotalUptime|float32f}}% {{range $i,$d := $service.Delay}}
{{$service.Monitor.Name}} - {{float32f (divU64 $service.TotalUp (addU64 $service.TotalUp $service.TotalDown))}}% + {{$service.TotalUptime|float32f}}% {{range $i,$d := $service.Delay}}
{{$service.Monitor.Name}} - {{float32f (divU64 $service.TotalUp (addU64 $service.TotalUp $service.TotalDown))}}% + {{$service.TotalUptime|float32f}}% {{range $i,$d := $service.Delay}} {{$percent := (div (index $service.Up $i) (add (index $service.Up $i) (index $service.Down $i)))}} diff --git a/service/singleton/servicesentinel.go b/service/singleton/servicesentinel.go index bd5ac75..25ebe3c 100644 --- a/service/singleton/servicesentinel.go +++ b/service/singleton/servicesentinel.go @@ -188,11 +188,9 @@ func (ss *ServiceSentinel) loadMonitorHistory() { for i := 0; i < len(mhs); i++ { dayIndex := 28 - (int(today.Sub(mhs[i].CreatedAt).Hours()) / 24) if mhs[i].Successful { - ServiceSentinelShared.monthlyStatus[mhs[i].MonitorID].TotalUp++ ServiceSentinelShared.monthlyStatus[mhs[i].MonitorID].Delay[dayIndex] = (ServiceSentinelShared.monthlyStatus[mhs[i].MonitorID].Delay[dayIndex]*float32(ss.monthlyStatus[mhs[i].MonitorID].Up[dayIndex]) + mhs[i].Delay) / float32(ss.monthlyStatus[mhs[i].MonitorID].Up[dayIndex]+1) ServiceSentinelShared.monthlyStatus[mhs[i].MonitorID].Up[dayIndex]++ } else { - ServiceSentinelShared.monthlyStatus[mhs[i].MonitorID].TotalDown++ ServiceSentinelShared.monthlyStatus[mhs[i].MonitorID].Down[dayIndex]++ } } @@ -263,8 +261,6 @@ func (ss *ServiceSentinel) LoadStats() map[uint64]*model.ServiceItemResponse { v := ss.serviceStatusToday[k] ss.monthlyStatus[k].Up[29] = v.Up ss.monthlyStatus[k].Down[29] = v.Down - ss.monthlyStatus[k].TotalUp += uint64(v.Up) - ss.monthlyStatus[k].TotalDown += uint64(v.Down) ss.monthlyStatus[k].Delay[29] = v.Delay } // 最后 5 分钟的状态 与 monitor 对象填充 diff --git a/service/singleton/singleton.go b/service/singleton/singleton.go index 35fc4d2..48e5b2a 100644 --- a/service/singleton/singleton.go +++ b/service/singleton/singleton.go @@ -12,7 +12,7 @@ import ( "github.com/naiba/nezha/pkg/utils" ) -var Version = "v0.13.6" // !!记得修改 README 中的 badge 版本!! +var Version = "v0.13.7" // !!记得修改 README 中的 badge 版本!! var ( Conf *model.Config