downsampling for traffic chart #987
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/alireza0/s-ui/database"
|
"github.com/alireza0/s-ui/database"
|
||||||
@@ -93,9 +94,56 @@ func (s *StatsService) GetStats(resource string, tag string, limit int) ([]model
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result = s.downsampleStats(result, 60) // 60 rows for 30 buckets
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// downsampleStats reduces stats to maxRows rows.
|
||||||
|
// Each bucket outputs two rows (direction false and true) with average Traffic.
|
||||||
|
func (s *StatsService) downsampleStats(stats []model.Stats, maxRows int) []model.Stats {
|
||||||
|
if len(stats) <= maxRows {
|
||||||
|
return stats
|
||||||
|
}
|
||||||
|
numBuckets := int(maxRows / 2)
|
||||||
|
sort.Slice(stats, func(i, j int) bool { return stats[i].DateTime < stats[j].DateTime })
|
||||||
|
timeMin, timeMax := stats[0].DateTime, stats[len(stats)-1].DateTime
|
||||||
|
bucketSpan := (timeMax - timeMin) / int64(numBuckets)
|
||||||
|
if bucketSpan == 0 {
|
||||||
|
bucketSpan = 1
|
||||||
|
}
|
||||||
|
downsampled := make([]model.Stats, 0, maxRows)
|
||||||
|
for i := 0; i < numBuckets; i++ {
|
||||||
|
bucketStart := timeMin + int64(i)*bucketSpan
|
||||||
|
bucketEnd := timeMin + int64(i+1)*bucketSpan
|
||||||
|
if i == numBuckets-1 {
|
||||||
|
bucketEnd = timeMax + 1
|
||||||
|
}
|
||||||
|
for _, dir := range []bool{false, true} {
|
||||||
|
var sum int64
|
||||||
|
var count int
|
||||||
|
for _, r := range stats {
|
||||||
|
if r.DateTime >= bucketStart && r.DateTime < bucketEnd && r.Direction == dir {
|
||||||
|
sum += r.Traffic
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
avg := int64(0)
|
||||||
|
if count > 0 {
|
||||||
|
avg = sum / int64(count)
|
||||||
|
}
|
||||||
|
downsampled = append(downsampled, model.Stats{
|
||||||
|
DateTime: bucketStart,
|
||||||
|
Resource: stats[0].Resource,
|
||||||
|
Tag: stats[0].Tag,
|
||||||
|
Direction: dir,
|
||||||
|
Traffic: avg,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return downsampled
|
||||||
|
}
|
||||||
|
|
||||||
func (s *StatsService) GetOnlines() (onlines, error) {
|
func (s *StatsService) GetOnlines() (onlines, error) {
|
||||||
return *onlineResources, nil
|
return *onlineResources, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user