-
Notifications
You must be signed in to change notification settings - Fork 0
/
apache_print.py
126 lines (96 loc) · 4.42 KB
/
apache_print.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
'''CPSC 254 - Fall 2015 - Apache Log Analyzer
This module is to handle different print functions.
'''
import datetime
def to_human_readable_size(size):
'''Given a integer `size` in bytes, return the tuple of human-readable-size
floating number and its corresponding unit. See the humansize.py in the
textbook to how to convert to human-readable size.
'''
# +++your code here+++
suffixes = {1000: ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']}
if size < 0:
raise ValueError('number must be non-negative')
multiple = 1000
for suffix in suffixes[multiple]:
size /= multiple
if size < multiple:
return '{0:.1f} {1}'.format(size, suffix)
raise ValueError('number too large')
def print_header(file_report):
'''Given the path of the report file, (over)write to the file the header
of the report. The header should be similar to below except the date and
time. It should be close to the date and time you run the script.
************************************
* APACHE LOG REPORT *
* Generated on 2015-04-19 09:04 PM *
************************************
Note:
- See datetime module reference:
https://docs.python.org/3.4/library/datetime.html
- To get the current date and time, see datetime.now function
- To return formatted date and time, see datetime.strftime function
'''
# +++your code here+++
from datetime import datetime
datetime = datetime.now()
f = open(file_report, 'w')
f.write('\
************************************\n\
* APACHE LOG REPORT *\n\
* Generated on ' + datetime.strftime("%d-%m-%y %H:%M") + ' *\n\
************************************\n')
f.close()
def print_total_sent(file_report, bytes_sent):
'''Given file path of the report `file_report` and the total bytes
transferred to remote hosts `bytes_sent`, write to the file after the
header the total of bytes that is cumulated from all the apache log files.
See the print-format below:
Total transferred: xx.xx GB
'''
# +++your code here+++
size = to_human_readable_size(bytes_sent)
with open(file_report, "a") as f: f.write('Total transferred: ' + size + 'GB\n')
def print_top_cumulative_size_requests(file_report, cumulative_size_requests):
'''Given the path of a report file `file_report` and cumulative size of
each request, write to the given file the top 100 largest-size requests
and their sizes in decending order of the size from largest to less. See
the print-format below:
Top 100 Largest Total-Sent Requests
/shuttle/missions/sts-71/movies/sts-71-launch.mpg 3.19 GB
/shuttle/missions/sts-71/movies/sts-71-mir-dock.mpg 1.41 GB
...
'''
# +++your code here+++
with open(file_report, "a") as f: f.write('Top 100 Largest Total-Sent Requests \n')
for request in cumulative_size_requests:
with open(file_report, "a") as f: f.write(str(request['request']) + ' ' + to_human_readable_size(request['size']) + 'GB\n')
def print_weekly_unique_hosts(file_report, weekly_uniq_hosts):
'''Given the path of a report file `file_report` and all the unique hosts
of each week `weekly_uniq_hosts`, write to the given file the week number
and the number of the unique hosts in the corresponding week. The week
must be sorted in ascending order from smallest to biggest. See the
print-format below:
Weekly Number of Unique Hosts
Week 26: 9413
Week 27: 32509
...
'''
# +++your code here+++
with open(file_report, "a") as f: f.write('Weekly Number of Unique Hosts \n')
for request in status_404_requests:
with open(file_report, "a") as f: f.write('Week ' + str(request['week']) + ': ' + '\n')
def print_status_404_requests(file_report, status_404_requests):
'''Given the path of a report file `file_report` and all the unique
404-status requests `status_404_requests`, write to the given file all
these requests in decending order of its frequency (from largest to
smallest). See the print-format below:
List of Frequent 404-status Requests
/pub/winvn/readme.txt
/pub/winvn/release.txt
...
'''
# +++your code here+++
with open(file_report, "a") as f: f.write('List of Frequent 404-status Requests \n')
for request in status_404_requests:
with open(file_report, "a") as f: f.write(str(request['request']) + '\n')