Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
bakito committed Dec 14, 2024
1 parent c23295d commit 05ce71c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 34 deletions.
18 changes: 10 additions & 8 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,14 @@ func initMetric(name string, metric *prometheus.GaugeVec) {
}

func Update(ims ...InstanceMetrics) {
s := OverallStats{}
for _, im := range ims {
update(im)
stats[im.HostName] = im.Stats
s[im.HostName] = im.Stats
}

stats = s

l.Debug("updated")
}

Expand Down Expand Up @@ -238,13 +241,12 @@ type InstanceMetrics struct {

type OverallStats map[string]*model.Stats

func (s OverallStats) consolidate() map[string]*model.Stats {
consolidated := OverallStats{"total": model.NewStats()}
for host, stats := range s {
consolidated[host] = stats
consolidated["total"].Add(stats)
func (s OverallStats) consolidate() *model.Stats {
total := model.NewStats()
for _, stats := range s {
total.Add(stats)
}
return consolidated
return total
}

func safeMetric[T Number](v *T) float64 {
Expand All @@ -258,6 +260,6 @@ type Number interface {
constraints.Float | constraints.Integer
}

func GetStats() map[string]*model.Stats {
func GetStats() *model.Stats {
return stats.consolidate()
}
22 changes: 12 additions & 10 deletions pkg/sync/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,18 @@ func (w *worker) handleRoot(c *gin.Context) {
"SyncStatus": w.status(),
"Stats": map[string]interface{}{
"Labels": getLast24Hours(),
"DNS": metrics.GetStats()["total"].DnsQueries,
"Blocked": metrics.GetStats()["total"].BlockedFiltering,
"BlockedPercentage": fmt.Sprintf("%.2f", (float64(*metrics.GetStats()["total"].NumBlockedFiltering)*100.0)/float64(*metrics.GetStats()["total"].NumDnsQueries)),
"Malware": metrics.GetStats()["total"].ReplacedSafebrowsing,
"Adult": metrics.GetStats()["total"].ReplacedParental,

"TotalDNS": metrics.GetStats()["total"].NumDnsQueries,
"TotalBlocked": metrics.GetStats()["total"].NumBlockedFiltering,
"TotalMalware": metrics.GetStats()["total"].NumReplacedSafebrowsing,
"TotalAdult": metrics.GetStats()["total"].NumReplacedParental,
"DNS": metrics.GetStats().DnsQueries,
"Blocked": metrics.GetStats().BlockedFiltering,
"BlockedPercentage": fmt.Sprintf("%.2f", (float64(*metrics.GetStats().NumBlockedFiltering)*100.0)/float64(*metrics.GetStats().NumDnsQueries)),
"Malware": metrics.GetStats().ReplacedSafebrowsing,
"MalwarePercentage": fmt.Sprintf("%.2f", (float64(*metrics.GetStats().NumReplacedSafebrowsing)*100.0)/float64(*metrics.GetStats().NumDnsQueries)),
"Adult": metrics.GetStats().ReplacedParental,
"AdultPercentage": fmt.Sprintf("%.2f", (float64(*metrics.GetStats().NumReplacedParental)*100.0)/float64(*metrics.GetStats().NumDnsQueries)),

"TotalDNS": metrics.GetStats().NumDnsQueries,
"TotalBlocked": metrics.GetStats().NumBlockedFiltering,
"TotalMalware": metrics.GetStats().NumReplacedSafebrowsing,
"TotalAdult": metrics.GetStats().NumReplacedParental,
},
},
)
Expand Down
31 changes: 15 additions & 16 deletions pkg/sync/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@
text-align: left;
display: flex;
flex-direction: column;
justify-content: space-between; /* Ensure the content is spaced out, with canvas at the bottom */
justify-content: space-between;
height: 100%;
}
{{- if .Metrics }}
body {
background-color: rgb(30, 30, 30); /* Dark background */
color: rgb(255, 255, 255); /* White text */
background-color: rgb(30, 30, 30);
color: rgb(255, 255, 255);
font-family: Arial, sans-serif;
}
.stat-card h3 {
Expand All @@ -70,23 +70,23 @@
.stat-card p {
margin: 5px 0;
font-size: 0.9rem;
color: rgb(78, 141, 245); /* Blue text */
color: rgb(78, 141, 245);
}
.percentage {
font-size: 0.9rem;
text-align: right;
height: 20px; /* Adjusted height to provide more space for canvas */
height: 20px;
}
canvas {
flex-grow: 1; /* Take up the available space */
height: 100px !important; /* Set a fixed height for the canvas */
flex-grow: 1;
height: 100px !important;
}
{{- end }}
.button-row {
margin-top: 20px; /* Add spacing from previous sections */
margin-top: 20px;
}
.btn-group {
margin: 5px; /* Add spacing between button groups */
margin: 5px;
}
</style>
</head>
Expand Down Expand Up @@ -120,7 +120,7 @@ <h3 style="color: rgb(255, 94, 94);">{{.Stats.TotalBlocked}}</h3>

<div class="col-12 col-md-3 d-flex">
<div class="stat-card flex-fill">
<div class="percentage"></div>
<div class="percentage" style="color: rgb(110, 224, 122);">{{.Stats.MalwarePercentage}}%</div>
<h3 style="color: rgb(110, 224, 122);">{{.Stats.TotalMalware}}</h3>
<p>Blocked malware/phishing</p>
<canvas id="malwareChart"></canvas>
Expand All @@ -129,7 +129,7 @@ <h3 style="color: rgb(110, 224, 122);">{{.Stats.TotalMalware}}</h3>

<div class="col-12 col-md-3 d-flex">
<div class="stat-card flex-fill">
<div class="percentage"></div>
<div class="percentage" style="color: rgb(232, 198, 78);">{{.Stats.AdultPercentage}}%</div>
<h3 style="color: rgb(232, 198, 78);">{{.Stats.TotalAdult}}</h3>
<p>Blocked adult websites</p>
<canvas id="adultWebsitesChart"></canvas>
Expand Down Expand Up @@ -206,14 +206,14 @@ <h3 style="color: rgb(232, 198, 78);">{{.Stats.TotalAdult}}</h3>
plugins: {
legend: { display: false },
tooltip: {
enabled: true, // Enable tooltips
enabled: true,
bodyFont: {
size: 20 // Set the font size for the body text
size: 20
},
titleFont: {
size: 20 // Set font size for the title
size: 20
},
displayColors: false, // Optionally, hide color boxes
displayColors: false,
callbacks: {
label: function(tooltipItem) {
return tooltipItem.raw;
Expand All @@ -237,7 +237,6 @@ <h3 style="color: rgb(232, 198, 78);">{{.Stats.TotalAdult}}</h3>
});
}

// Create charts with specific RGB colors
createMinimalChart('dnsQueriesChart', {{.Stats.DNS}}, 78, 141, 245); // RGB Blue
createMinimalChart('blockedFiltersChart', {{.Stats.Blocked}}, 255, 94, 94); // RGB Red
createMinimalChart('malwareChart', {{.Stats.Malware}}, 110, 224, 122); // RGB Green
Expand Down

0 comments on commit 05ce71c

Please sign in to comment.