Advertisement
Guest User

Untitled

a guest
May 17th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. """Write your solution in this file.
  2.  
  3. You can execute and test your answer by pressing 'Try Answer' in the side panel,
  4. or by running `python test_answer.py <test_case_path>` on the command line.
  5.  
  6. For example:
  7. python test_answer.py inputs/large.json
  8. """
  9.  
  10.  
  11. def analyze_requests(log_path):
  12. """Implement your solution here.
  13.  
  14. Arguments:
  15. log_path: String with path of Nginx access logfile.
  16.  
  17. Returns:
  18. A list of strings, representing the URLs with the largest average processing time.
  19. """
  20.  
  21. lines = []
  22. with open(log_path) as fp:
  23. line = fp.readline()
  24.  
  25. while line:
  26. lines.append(line.strip())
  27. line = fp.readline()
  28.  
  29. requests = []
  30. for line in lines:
  31. requests.append([line.split(' ')[6], float(line.split(' ')[-1])])
  32.  
  33. counts = {}
  34. for req in requests:
  35. if req[0] in counts:
  36. counts[req[0]] += 1
  37. else:
  38. counts[req[0]] = 1
  39.  
  40. duration = {}
  41. for req in requests:
  42. if req[0] in duration:
  43. duration[req[0]] += req[1]
  44. else:
  45. duration[req[0]] = req[1]
  46.  
  47. avg_time = {}
  48. for k1 in counts:
  49. for k2 in duration:
  50. if k1 == k2:
  51. avg_time[k1] = duration[k1]/counts[k1]
  52.  
  53. sorted_avg_time = sorted(avg_time.items(), key=lambda x:x[1], reverse=True)
  54.  
  55. results = []
  56. for i in range(15):
  57. results.append(sorted_avg_time[i][0])
  58.  
  59. return results
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement