Skip to content

Commit

Permalink
Python is always handy to use
Browse files Browse the repository at this point in the history
  • Loading branch information
gforcada committed Dec 20, 2013
1 parent ef7a244 commit 99e9fad
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
12 changes: 2 additions & 10 deletions haproxy/haproxy_logfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,9 @@ def cmd_top_ips(self):
interface to allow to send parameters to each command or globally.
"""
threshold = 10
ips_dict = self.cmd_ip_counter()
ips_list = []

for ip in ips_dict:
ips_list.append(
{'ip': ip,
'repetitions': ips_dict[ip], }
)

ips_list = self.cmd_ip_counter().items()
ips_list = sorted(ips_list,
key=lambda ip_info: ip_info['repetitions'],
key=lambda ip_info: ip_info[1],
reverse=True)
return ips_list[:threshold]

Expand Down
18 changes: 9 additions & 9 deletions haproxy/tests/test_haproxy_log_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,14 @@ def test_haproxy_log_file_cmd_top_ips(self):
top_ips = log_file.cmd_top_ips()

self.assertEqual(len(top_ips), 10)
self.assertEqual(top_ips[0], {'ip': '1.1.1.15', 'repetitions': 6})
self.assertEqual(top_ips[1], {'ip': '1.1.1.11', 'repetitions': 5})
self.assertEqual(top_ips[0], ('1.1.1.15', 6))
self.assertEqual(top_ips[1], ('1.1.1.11', 5))

# as the 3rd and 4th have the same repetitions their order is unknown
self.assertEqual(top_ips[2]['repetitions'], 4)
self.assertEqual(top_ips[3]['repetitions'], 4)
self.assertTrue(top_ips[2]['ip'] in ('1.1.1.10', '1.1.1.19'))
self.assertTrue(top_ips[3]['ip'] in ('1.1.1.10', '1.1.1.19'))
self.assertEqual(top_ips[2][1], 4)
self.assertEqual(top_ips[3][1], 4)
self.assertTrue(top_ips[2][0] in ('1.1.1.10', '1.1.1.19'))
self.assertTrue(top_ips[3][0] in ('1.1.1.10', '1.1.1.19'))

# the same as above for all the others
other_ips = [
Expand All @@ -300,12 +300,12 @@ def test_haproxy_log_file_cmd_top_ips(self):
'1.1.1.18',
]
for ip_info in top_ips[4:]:
self.assertEqual(ip_info['repetitions'], 2)
self.assertTrue(ip_info['ip'] in other_ips)
self.assertEqual(ip_info[1], 2)
self.assertTrue(ip_info[0] in other_ips)

# remove the other_ips to ensure all ips are there
for position, current in enumerate(other_ips):
if current == ip_info['ip']:
if current == ip_info[0]:
del other_ips[position]
break

Expand Down

0 comments on commit 99e9fad

Please sign in to comment.