diff --git a/stats/stats.go b/stats/stats.go index 85d0e149..68e53b3c 100644 --- a/stats/stats.go +++ b/stats/stats.go @@ -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) } diff --git a/stats/stats_test.go b/stats/stats_test.go index 3fa4ecd3..4b2a8750 100644 --- a/stats/stats_test.go +++ b/stats/stats_test.go @@ -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) diff --git a/stats/stats_unit.go b/stats/stats_unit.go index 9cbd67d0..0bedb334 100644 --- a/stats/stats_unit.go +++ b/stats/stats_unit.go @@ -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 +}