Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """Write your solution in this file.
- You can execute and test your answer by pressing 'Try Answer' in the side panel,
- or by running `python test_answer.py <test_case_path>` on the command line.
- For example:
- python test_answer.py inputs/large.json
- """
- def analyze_requests(log_path):
- """Implement your solution here.
- Arguments:
- log_path: String with path of Nginx access logfile.
- Returns:
- A list of strings, representing the URLs with the largest average processing time.
- """
- lines = []
- with open(log_path) as fp:
- line = fp.readline()
- while line:
- lines.append(line.strip())
- line = fp.readline()
- requests = []
- for line in lines:
- requests.append([line.split(' ')[6], float(line.split(' ')[-1])])
- counts = {}
- for req in requests:
- if req[0] in counts:
- counts[req[0]] += 1
- else:
- counts[req[0]] = 1
- duration = {}
- for req in requests:
- if req[0] in duration:
- duration[req[0]] += req[1]
- else:
- duration[req[0]] = req[1]
- avg_time = {}
- for k1 in counts:
- for k2 in duration:
- if k1 == k2:
- avg_time[k1] = duration[k1]/counts[k1]
- sorted_avg_time = sorted(avg_time.items(), key=lambda x:x[1], reverse=True)
- results = []
- for i in range(15):
- results.append(sorted_avg_time[i][0])
- return results
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement