Skip to content

Commit

Permalink
Fix: アクティビティモニタが空の場合のavg算出処理誤りを修正
Browse files Browse the repository at this point in the history
  • Loading branch information
yamamoto-febc committed Jan 15, 2024
1 parent 88d7aab commit c8018b4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
5 changes: 4 additions & 1 deletion resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ func (r *Resource) toMetrics() map[string]interface{} {
log.Printf("%s zone:%s %s %s:%f time:%s", r.Name, r.Zone, strInfo, r.Label, p.Value, p.Time.String())
}

avg := sum / float64(len(r.Monitors))
avg := 0.
if len(r.Monitors) > 0 && len(monitors) > 0 {
avg = sum / float64(len(r.Monitors))
}
log.Printf("%s average_%s:%f", r.Name, r.Label, avg)

metrics := map[string]interface{}{
Expand Down
31 changes: 31 additions & 0 deletions resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,37 @@ func TestResources_Metrics(t *testing.T) {
"label": []interface{}{},
},
},
{
name: "single resource - no value",
args: args{
resources: &Resources{
Label: "routers",
Resources: []*Resource{
{
ID: 1,
Name: "test1",
Zone: "is1a",
Monitors: nil,
Label: "traffic",
AdditionalInfo: nil,
},
},
},
},
want: map[string]interface{}{
"avg": 0.,
"max": 0.,
"min": 0.,
"routers": []interface{}{
map[string]interface{}{
"name": "test1",
"zone": "is1a",
"avg": 0.,
"monitors": []interface{}{},
},
},
},
},
{
name: "single resource - single value",
args: args{
Expand Down

0 comments on commit c8018b4

Please sign in to comment.