+ stats: GetTopData()

This commit is contained in:
Simon Zolin 2019-10-07 15:56:33 +03:00
parent 4165688f25
commit 418baa608f
3 changed files with 28 additions and 0 deletions

View File

@ -42,6 +42,9 @@ type Stats interface {
// Update counters
Update(e Entry)
// Get IP addresses of the clients with the most number of requests
GetTopClientsIP(limit uint) []string
// WriteDiskConfig - write configuration
WriteDiskConfig(dc *DiskConfig)
}

View File

@ -75,6 +75,9 @@ func TestStats(t *testing.T) {
assert.True(t, d["num_replaced_parental"].(uint64) == 0)
assert.True(t, d["avg_processing_time"].(float64) == 0.123456)
topClients := s.GetTopClientsIP(2)
assert.True(t, topClients[0] == "127.0.0.1")
s.clear()
s.Close()
os.Remove(conf.Filename)

View File

@ -697,3 +697,25 @@ func (s *statsCtx) getData(timeUnit TimeUnit) map[string]interface{} {
return d
}
func (s *statsCtx) GetTopClientsIP(limit uint) []string {
lastID := s.conf.UnitID()
units := s.loadUnits(lastID)
if units == nil {
return nil
}
// top clients
m := map[string]uint64{}
for _, u := range units {
for _, it := range u.Clients {
m[it.Name] += it.Count
}
}
a := convertMapToArray(m, int(limit))
d := []string{}
for _, it := range a {
d = append(d, it.Name)
}
return d
}