Rewrite how historical stats are stored and calculated.

Closes #310.
This commit is contained in:
Eugene Bujak
2018-09-06 02:11:36 +03:00
parent c7a5275d42
commit 04562dece3
3 changed files with 142 additions and 180 deletions

View File

@@ -51,33 +51,14 @@ func ensureDELETE(handler func(http.ResponseWriter, *http.Request)) func(http.Re
// --------------------------
// helper functions for stats
// --------------------------
func computeRate(input []float64) []float64 {
func getSlice(input [statsHistoryElements]float64, start int, end int) []float64 {
output := make([]float64, 0)
for i := len(input) - 2; i >= 0; i-- {
value := input[i]
diff := value - input[i+1]
output = append([]float64{diff}, output...)
for i := start; i <= end; i++ {
output = append(output, input[i])
}
return output
}
func generateMapFromSnap(snap statsSnapshot) map[string]interface{} {
var avgProcessingTime float64
if snap.processingTimeCount > 0 {
avgProcessingTime = snap.processingTimeSum / snap.processingTimeCount
}
result := map[string]interface{}{
"dns_queries": snap.totalRequests,
"blocked_filtering": snap.filteredLists,
"replaced_safebrowsing": snap.filteredSafebrowsing,
"replaced_safesearch": snap.filteredSafesearch,
"replaced_parental": snap.filteredParental,
"avg_processing_time": avgProcessingTime,
}
return result
}
func generateMapFromStats(stats *periodicStats, start int, end int) map[string]interface{} {
// clamp
start = clamp(start, 0, statsHistoryElements)
@@ -85,8 +66,8 @@ func generateMapFromStats(stats *periodicStats, start int, end int) map[string]i
avgProcessingTime := make([]float64, 0)
count := computeRate(stats.processingTimeCount[start:end])
sum := computeRate(stats.processingTimeSum[start:end])
count := getSlice(stats.entries[processingTimeCount], start, end)
sum := getSlice(stats.entries[processingTimeSum], start, end)
for i := 0; i < len(count); i++ {
var avg float64
if count[i] != 0 {
@@ -97,11 +78,11 @@ func generateMapFromStats(stats *periodicStats, start int, end int) map[string]i
}
result := map[string]interface{}{
"dns_queries": computeRate(stats.totalRequests[start:end]),
"blocked_filtering": computeRate(stats.filteredLists[start:end]),
"replaced_safebrowsing": computeRate(stats.filteredSafebrowsing[start:end]),
"replaced_safesearch": computeRate(stats.filteredSafesearch[start:end]),
"replaced_parental": computeRate(stats.filteredParental[start:end]),
"dns_queries": getSlice(stats.entries[totalRequests], start, end),
"blocked_filtering": getSlice(stats.entries[filteredLists], start, end),
"replaced_safebrowsing": getSlice(stats.entries[filteredSafebrowsing], start, end),
"replaced_safesearch": getSlice(stats.entries[filteredSafesearch], start, end),
"replaced_parental": getSlice(stats.entries[filteredParental], start, end),
"avg_processing_time": avgProcessingTime,
}
return result