diff --git a/internal/sync/http.go b/internal/sync/http.go index 4320e92..2b59b67 100644 --- a/internal/sync/http.go +++ b/internal/sync/http.go @@ -37,33 +37,30 @@ func (w *worker) handleRoot(c *gin.Context) { "Build": version.Build, "SyncStatus": w.status(), "Stats": map[string]any{ - "Labels": getLast24Hours(), - "DNS": dns, - "Blocked": blocked, - "BlockedPercentage": fmt.Sprintf( - "%.2f", - (float64(*total.NumBlockedFiltering)*100.0)/float64(*total.NumDnsQueries), - ), - "Malware": malware, - "MalwarePercentage": fmt.Sprintf( - "%.2f", - (float64(*total.NumReplacedSafebrowsing)*100.0)/float64(*total.NumDnsQueries), - ), - "Adult": adult, - "AdultPercentage": fmt.Sprintf( - "%.2f", - (float64(*total.NumReplacedParental)*100.0)/float64(*total.NumDnsQueries), - ), - - "TotalDNS": total.NumDnsQueries, - "TotalBlocked": total.NumBlockedFiltering, - "TotalMalware": total.NumReplacedSafebrowsing, - "TotalAdult": total.NumReplacedParental, + "Labels": getLast24Hours(), + "DNS": dns, + "Blocked": blocked, + "BlockedPercentage": percent(total.NumBlockedFiltering, total.NumDnsQueries), + "Malware": malware, + "MalwarePercentage": percent(total.NumReplacedSafebrowsing, total.NumDnsQueries), + "Adult": adult, + "AdultPercentage": percent(total.NumReplacedParental, total.NumDnsQueries), + "TotalDNS": total.NumDnsQueries, + "TotalBlocked": total.NumBlockedFiltering, + "TotalMalware": total.NumReplacedSafebrowsing, + "TotalAdult": total.NumReplacedParental, }, }, ) } +func percent(a, b *int) string { + if a == nil || b == nil || *b == 0 { + return "0.00" + } + return fmt.Sprintf("%.2f", (float64(*a)*100.0)/float64(*b)) +} + func (w *worker) handleLogs(c *gin.Context) { c.Data(http.StatusOK, "text/plain", []byte(strings.Join(log.Logs(), ""))) } diff --git a/internal/sync/http_test.go b/internal/sync/http_test.go new file mode 100644 index 0000000..c95a7ab --- /dev/null +++ b/internal/sync/http_test.go @@ -0,0 +1,30 @@ +package sync + +import ( + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +var _ = Describe("Percent", func() { + DescribeTable("calculating percentage", + func(a, b *int, want string) { + Expect(percent(a, b)).To(Equal(want)) + }, + Entry("both inputs are nil", nil, nil, "0.00"), + Entry("a is nil, b is non-zero", nil, intPtr(10), "0.00"), + Entry("b is nil, a is non-zero", intPtr(10), nil, "0.00"), + Entry("b is zero", intPtr(10), intPtr(0), "0.00"), + Entry("normal case with positive int values", intPtr(25), intPtr(100), "25.00"), + Entry("a and b are equal", intPtr(50), intPtr(50), "100.00"), + Entry("a is zero, b is positive", intPtr(0), intPtr(50), "0.00"), + Entry("large positive values", intPtr(1000), intPtr(4000), "25.00"), + Entry("a greater than b", intPtr(150), intPtr(100), "150.00"), + Entry("negative values for a and b", intPtr(-25), intPtr(-50), "50.00"), + Entry("a is positive, b is negative", intPtr(25), intPtr(-50), "-50.00"), + Entry("a is negative, b is positive", intPtr(-25), intPtr(50), "-50.00"), + ) +}) + +func intPtr(i int) *int { + return &i +}