Skip to content

Commit

Permalink
Merge pull request #25 from gforcada/flake8
Browse files Browse the repository at this point in the history
Flake8
  • Loading branch information
gforcada committed Jul 6, 2017
2 parents ead9e6a + 5b70e8a commit ada92ee
Show file tree
Hide file tree
Showing 14 changed files with 196 additions and 140 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ python:
- "3.5"
install:
- pip install -r requirements.txt
- pip install zc.buildout
- pip install -r requirements-dev.txt
- buildout -c qa.cfg
script:
- bin/code-analysis
- flake8 *.py haproxy/*.py haproxy/*/*.py
- nosetests --with-coverage --cover-package=haproxy
after_success:
- pip install -q coveralls
Expand Down
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ include *.cfg
include .coveragerc
include .travis.yml
include LICENSE
include requirements.txt
include *.txt
exclude pyvenv.cfg
recursive-include docs *.py
recursive-include docs *.rst
Expand Down
2 changes: 1 addition & 1 deletion haproxy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

START_REGEX = re.compile(
r'(?P<day>\d+)/(?P<month>\w+)/(?P<year>\d+)'
r'(:(?P<hour>\d+)|)(:(?P<minute>\d+)|)(:(?P<second>\d+)|)'
r'(:(?P<hour>\d+)|)(:(?P<minute>\d+)|)(:(?P<second>\d+)|)',
)
2 changes: 1 addition & 1 deletion haproxy/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def _date_str_to_datetime(date):
raw_date_input = '{0}/{1}/{2}'.format(
matches.group('day'),
matches.group('month'),
matches.group('year')
matches.group('year'),
)
date_format = '%d/%b/%Y'
if matches.group('hour'):
Expand Down
8 changes: 4 additions & 4 deletions haproxy/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@
r'(?P<headers>{.*})\s+|)'
# "GET /path/to/image HTTP/1.1"
r'"(?P<http_request>.*)"'
r'\Z' # end of line
r'\Z', # end of line
)

HTTP_REQUEST_REGEX = re.compile(
r'(?P<method>\w+)\s+'
r'(?P<path>(/[`´\\<>/\w:,;\.#$!?=&@%_+\'\*^~|\(\)\[\]\{\}-]*)+)'
r'\s+(?P<protocol>\w+/\d\.\d)'
r'\s+(?P<protocol>\w+/\d\.\d)',
)


Expand Down Expand Up @@ -235,6 +235,6 @@ def handle_bad_http_request(self):
if self.raw_http_request != '<BADREQ>':
print(
'Could not process HTTP request {0}'.format(
self.raw_http_request
)
self.raw_http_request,
),
)
78 changes: 48 additions & 30 deletions haproxy/logfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ def __init__(self, logfile=None):
self.logfile = logfile
self._pickle_file = '{0}.pickle'.format(logfile)
# only this attributes will be pickled
self._pickle_attributes = ['_valid_lines',
'_invalid_lines',
'total_lines', ]
self._pickle_attributes = [
'_valid_lines',
'_invalid_lines',
'total_lines',
]

self.total_lines = 0

Expand Down Expand Up @@ -109,9 +111,6 @@ def filter(self, filter_func, reverse=False):
:returns: a new instance of Log containing only log lines
that passed the filter function.
:rtype: :class:`Log`
TODO:
Deep copy implementation.
"""
new_log_file = Log()
new_log_file.logfile = self.logfile
Expand Down Expand Up @@ -183,7 +182,7 @@ def cmd_top_ips(self):
"""
return self._sort_and_trim(
self.cmd_ip_counter(),
reverse=True
reverse=True,
)

def cmd_status_codes_counter(self):
Expand All @@ -210,7 +209,7 @@ def cmd_top_request_paths(self):
"""
return self._sort_and_trim(
self.cmd_request_path_counter(),
reverse=True
reverse=True,
)

def cmd_slow_requests(self):
Expand All @@ -222,14 +221,20 @@ def cmd_slow_requests(self):
command line interface to allow to send parameters to each command
or globally.
"""
slow_requests = [line.time_wait_response for line in self._valid_lines
if line.time_wait_response > 1000]
slow_requests = [
line.time_wait_response
for line in self._valid_lines
if line.time_wait_response > 1000
]
return slow_requests

def cmd_average_response_time(self):
"""Returns the average response time of all, non aborted, requests."""
average = [line.time_wait_response for line in self._valid_lines
if line.time_wait_response >= 0]
average = [
line.time_wait_response
for line in self._valid_lines
if line.time_wait_response >= 0
]

divisor = float(len(average))
if divisor > 0:
Expand All @@ -238,8 +243,11 @@ def cmd_average_response_time(self):

def cmd_average_waiting_time(self):
"""Returns the average queue time of all, non aborted, requests."""
average = [line.time_wait_queues for line in self._valid_lines
if line.time_wait_queues >= 0]
average = [
line.time_wait_queues
for line in self._valid_lines
if line.time_wait_queues >= 0
]

divisor = float(len(average))
if divisor > 0:
Expand Down Expand Up @@ -295,10 +303,13 @@ def cmd_queue_peaks(self):
first_on_queue = line.accept_date

if current_queue == 0 and current_peak > threshold:
peaks.append({'peak': current_peak,
'span': current_span,
'first': first_on_queue,
'last': line.accept_date, })
data = {
'peak': current_peak,
'span': current_span,
'first': first_on_queue,
'last': line.accept_date,
}
peaks.append(data)
current_peak = 0
current_span = 0
first_on_queue = None
Expand All @@ -308,10 +319,13 @@ def cmd_queue_peaks(self):

# case of a series that does not end
if current_queue > 0 and current_peak > threshold:
peaks.append({'peak': current_peak,
'span': current_span,
'first': first_on_queue,
'last': line.accept_date, })
data = {
'peak': current_peak,
'span': current_span,
'first': first_on_queue,
'last': line.accept_date,
}
peaks.append(data)

return peaks

Expand Down Expand Up @@ -353,7 +367,7 @@ def cmd_requests_per_minute(self):
def format_and_append(append_to, date, counter):
seconds_and_micro = timedelta(
seconds=date.second,
microseconds=date.microsecond
microseconds=date.microsecond,
)
minute_formatted = date - seconds_and_micro
append_to.append((minute_formatted, counter))
Expand All @@ -369,7 +383,7 @@ def format_and_append(append_to, date, counter):
format_and_append(
requests,
current_minute,
current_minute_counter
current_minute_counter,
)
current_minute_counter = 1
current_minute = line_date
Expand All @@ -378,7 +392,7 @@ def format_and_append(append_to, date, counter):
format_and_append(
requests,
current_minute,
current_minute_counter
current_minute_counter,
)

return requests
Expand All @@ -399,8 +413,10 @@ def _sort_lines(self):
This method sorts all valid log lines by their acceptance date,
providing the real order in which connections where made to the server.
"""
self._valid_lines = sorted(self._valid_lines,
key=lambda line: line.accept_date)
self._valid_lines = sorted(
self._valid_lines,
key=lambda line: line.accept_date,
)

@staticmethod
def _sort_and_trim(data, reverse=False):
Expand All @@ -413,7 +429,9 @@ def _sort_and_trim(data, reverse=False):
"""
threshold = 10
data_list = data.items()
data_list = sorted(data_list,
key=lambda data_info: data_info[1],
reverse=reverse)
data_list = sorted(
data_list,
key=lambda data_info: data_info[1],
reverse=reverse,
)
return data_list[:threshold]
10 changes: 6 additions & 4 deletions haproxy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def create_parser():
'format (e.g. 11/Dec/2013 or 11/Dec/2013:19:31:41). '
'At least provide the day/month/year. Values not specified will '
'use their base value (e.g. 00 for hour). Use in conjunction '
'with -d to limit the number of entries to process.'
'with -d to limit the number of entries to process.',
)

parser.add_argument(
Expand All @@ -43,7 +43,7 @@ def create_parser():
'as a number and a time unit, e.g.: 1s, 10m, 3h or 4d (for 1 '
'second, 10 minutes, 3 hours or 4 days). Use in conjunction with '
'-s to only analyze certain time delta. If no start time is '
'given, the time on the first line will be used instead.'
'given, the time on the first line will be used instead.',
)

parser.add_argument(
Expand Down Expand Up @@ -267,8 +267,10 @@ def main(args):
for filter_data in args['filters']:
arg = filter_data[1] or ''
filter_func = getattr(filters, 'filter_{0}'.format(filter_data[0]))
log_file = log_file.filter(filter_func(arg),
reverse=args['negate_filter'])
log_file = log_file.filter(
filter_func(arg),
reverse=args['negate_filter'],
)

# run all commands
for command in args['commands']:
Expand Down

0 comments on commit ada92ee

Please sign in to comment.