-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogs_analysis.py
170 lines (146 loc) · 4.99 KB
/
logs_analysis.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python3
'''Log Analysis Project for Full Stack Nanodegree by Udacity'''
import psycopg2
# To make Connection with the news database and return the cursor
def connect_to_db():
try:
db = psycopg2.connect("dbname=news")
cursor = db.cursor()
except ConnectionError:
print("Failed to connect to the database.")
return None
else:
return cursor
# END connect_to_db
# To query top three articles from the database
def top_articles(db_cursor, f):
query = '''
SELECT articles.title, count(*) AS views
FROM articles INNER JOIN log
ON log.path = '/article/' || articles.slug
WHERE log.status = '200 OK'
GROUP BY articles.title
ORDER BY views DESC
LIMIT 3;
'''
db_cursor.execute(query)
results = db_cursor.fetchall()
print('\n\n Three most popular articles of all time')
print('=====================================================')
for result in results:
print(
'\n "{title}" -- {views} views'
.format(title=result[0], views=result[1])
)
print('\n')
prompt = input("Do you want to save these results in text file (y/n): ")
if prompt == 'y':
f.write('\n Three most popular articles of all time\n')
f.write('=====================================================')
for result in results:
f.write(
'\n "{title}" -- {views} views'
.format(title=result[0], views=result[1])
)
f.write('\n')
return
# END top_articles()
# To query top three authors from the database
def top_authors(db_cursor, f):
query = '''
SELECT authors.name, count(*) AS views
FROM articles, authors, log
WHERE log.path = '/article/' || articles.slug
AND articles.author = authors.id
GROUP BY authors.name
ORDER BY count(*) DESC;
'''
db_cursor.execute(query)
results = db_cursor.fetchall()
print('\n\n Most popular authors of all time')
print('============================================')
for result in results:
print(
'\n "{name}" -- {views} views'
.format(name=result[0], views=result[1])
)
print('\n')
prompt = input("Do you want to save these results in text file (y/n): ")
if prompt == 'y':
f.write('\n\n Most popular authors of all time\n')
f.write('============================================')
for result in results:
f.write(
'\n "{name}" -- {views} views'
.format(name=result[0], views=result[1])
)
f.write('\n')
return
# END top_authors()
# To query the day with percentage error > 1
def too_many_errors(db_cursor, f):
query = '''
WITH error_rate AS (
SELECT date,
round((errors::float / requests::float * 100.00)::numeric, 2)
AS error_prc
FROM daily_request_stats)
SELECT date, error_prc FROM error_rate WHERE error_prc > 1;
'''
db_cursor.execute(query)
results = db_cursor.fetchall()
print('\n\n Day with more than 1 perc. failed requests')
print('=============================================')
for result in results:
print(
'\n "{date}" -- {error_prc} views'
.format(date=result[0], error_prc=result[1])
)
print('\n')
prompt = input("Do you want to save these results in text file (y/n): ")
if prompt == 'y':
f.write('\n\n Day with more than 1 perc. failed requests\n')
f.write('=============================================')
for result in results:
f.write(
'\n "{date}" -- {error_prc} views'
.format(date=result[0], error_prc=result[1])
)
f.write('\n')
return
# END too_many_errors()
# To create the view daily_request_stats
def create_view(db_cursor):
query = '''
CREATE OR REPLACE VIEW daily_request_stats AS
WITH daily_hits AS (
SELECT date(time) AS date, count(*) AS d_hits
FROM log
GROUP BY date
ORDER BY date
), failed_hits AS (
SELECT date(time) AS date, count(*) AS f_hits
FROM log
WHERE status != '200 OK'
GROUP BY date
ORDER BY date
)
SELECT daily_hits.date, d_hits AS requests,
d_hits - f_hits AS success, f_hits AS errors
FROM daily_hits INNER JOIN failed_hits
ON daily_hits.date = failed_hits.date;
'''
db_cursor.execute(query)
print('View created !!!')
if __name__ == "__main__":
file = open('results.txt', 'w+')
cursor = connect_to_db()
if cursor:
# uncomment the line below to create the view
# required for getting the HTTP requests error report
# create_view(cursor)
top_articles(cursor, file)
top_authors(cursor, file)
too_many_errors(cursor, file)
cursor.close()
file.close()