fix: service total uptime rate close #191

This commit is contained in:
naiba 2022-05-02 00:45:03 +08:00
parent 52eede82a1
commit c4609f02f7
8 changed files with 29 additions and 12 deletions

View File

@ -4,7 +4,7 @@
<br>
<small><i>LOGO designed by <a href="https://xio.ng" target="_blank">熊大</a> .</i></small>
<br><br>
<img src="https://img.shields.io/github/workflow/status/naiba/nezha/Dashboard%20image?label=Dash%20v0.13.6&logo=github&style=for-the-badge">&nbsp;<img src="https://img.shields.io/github/v/release/naiba/nezha?color=brightgreen&label=Agent&style=for-the-badge&logo=github">&nbsp;<img src="https://img.shields.io/github/workflow/status/naiba/nezha/Agent%20release?label=Agent%20CI&logo=github&style=for-the-badge">&nbsp;<img src="https://img.shields.io/badge/Installer-v0.9.1-brightgreen?style=for-the-badge&logo=linux">
<img src="https://img.shields.io/github/workflow/status/naiba/nezha/Dashboard%20image?label=Dash%20v0.13.7&logo=github&style=for-the-badge">&nbsp;<img src="https://img.shields.io/github/v/release/naiba/nezha?color=brightgreen&label=Agent&style=for-the-badge&logo=github">&nbsp;<img src="https://img.shields.io/github/workflow/status/naiba/nezha/Agent%20release?label=Agent%20CI&logo=github&style=for-the-badge">&nbsp;<img src="https://img.shields.io/badge/Installer-v0.9.1-brightgreen?style=for-the-badge&logo=linux">
<br>
<br>
<p>:trollface: <b>Nezha Monitoring</b> self-hosted lightweight monitoring and operation and maintenance system. Supports system status, HTTP (SSL certificate change, upcoming expiration, expiration), TCP, Ping <b>monitoring</b> and <b>alerting</b>, execute scheduled tasks and <b>web terminal</b>.</p>

View File

@ -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
}

View File

@ -66,7 +66,7 @@
<span class="monitor-state-text">{{statusName (divU64 $service.CurrentUp (addU64 $service.CurrentUp $service.CurrentDown))}}</span>
</div>
<div class="monitor-name">{{$service.Monitor.Name}}</div>
<div class="monitor-uptime">{{float32f (divU64 $service.TotalUp (addU64 $service.TotalUp $service.TotalDown))}}%</div>
<div class="monitor-uptime">{{$service.TotalUptime|float32f}}%</div>
<div class="corner-container">
{{range $i,$d := $service.Delay}}

View File

@ -21,7 +21,7 @@
<tr>
<td class="ui center aligned">{{$service.Monitor.Name}}</td>
<td class="ui center aligned">
{{float32f (divU64 $service.TotalUp (addU64 $service.TotalUp $service.TotalDown))}}%
{{$service.TotalUptime|float32f}}%
</td>
<td class="ui center aligned">{{range $i,$d := $service.Delay}}
<div class="ui icon button {{className (div (index $service.Up $i) (add (index $service.Up $i) (index $service.Down $i)))}}"

View File

@ -22,7 +22,7 @@
<tr>
<td class="ui center aligned">{{$service.Monitor.Name}}</td>
<td class="ui center aligned">
{{float32f (divU64 $service.TotalUp (addU64 $service.TotalUp $service.TotalDown))}}%
{{$service.TotalUptime|float32f}}%
</td>
<td class="ui center aligned">{{range $i,$d := $service.Delay}}
<div class="ui icon button {{className (div (index $service.Up $i) (add (index $service.Up $i) (index $service.Down $i)))}}"

View File

@ -36,7 +36,7 @@
{{range $service := .Services}}
<tr>
<td class="mdui-text-center"><strong>{{$service.Monitor.Name}}</strong></td>
<td class="mdui-text-center">{{float32f (divU64 $service.TotalUp (addU64 $service.TotalUp $service.TotalDown))}}%</td>
<td class="mdui-text-center">{{$service.TotalUptime|float32f}}%</td>
<td>
{{range $i,$d := $service.Delay}}
{{$percent := (div (index $service.Up $i) (add (index $service.Up $i) (index $service.Down $i)))}}

View File

@ -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 对象填充

View File

@ -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