-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogs_analysis.py
85 lines (66 loc) · 2.25 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
# !/usr/bin/env python
import psycopg2
# connect to db
db = psycopg2.connect('dbname=news')
c = db.cursor()
def popular_three_articles():
query = """
SELECT articles.title, count(*) AS views
FROM log,
articles
WHERE log.path = '/article/' || articles.slug
GROUP BY articles.title
ORDER BY views DESC
LIMIT 3;
"""
c.execute(query)
results = c.fetchall()
print('\n1) What are the most popular three articles of all time?\n')
for result in results:
print('"{}" - {} views'
.format(result[0], result[1]))
def popular_authors():
query = '''
SELECT authors.name, count(*) AS views
FROM log, articles, authors
WHERE log.path = '/article/' || articles.slug
AND articles.author = authors.id
GROUP BY authors.name
ORDER BY views DESC;
'''
c.execute(query)
results = c.fetchall()
print('\n2) Who are the most popular article authors of all time?\n')
for result in results:
print('{} - {} views'.format(result[0], result[1]))
def requests_errors():
query = """
WITH requests AS (
SELECT date(TIME) AS day, count(*)
FROM log
GROUP BY day
ORDER BY day
), errors AS (
SELECT date(TIME) AS day, count(*)
FROM log
WHERE status LIKE '404%'
GROUP BY day
ORDER BY day
), error_rate AS (
SELECT requests.day,
CAST(errors.count AS float) / CAST(requests.count AS float) * 100
AS error_percent
FROM requests, errors
WHERE requests.day = errors.day
)
SELECT * FROM error_rate WHERE error_percent > 1;
"""
c.execute(query)
results = c.fetchall()
print('\n3) On which days did more than 1% of requests lead to errors?\n')
for result in results:
print('{} - {}\n'.format(result[0].strftime('%B %d, %Y'), str(round(result[1], 1)) + '% errors'))
if __name__ == '__main__':
popular_three_articles()
popular_authors()
requests_errors()