-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
70 lines (52 loc) · 2.02 KB
/
main.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
#!/usr/bin/env python
import psycopg2
def print_popular_articles(c):
print("1. What are the most popular three articles of all time?\n")
c.execute("""SELECT articles.title, count(log.id) FROM articles
LEFT JOIN log
ON log.path=CONCAT('/article/', articles.slug)
AND status='200 OK'
GROUP BY articles.title
ORDER BY count DESC
LIMIT 3;""")
for i in c.fetchall():
print('"{}" - {} views'.format(i[0], i[1]))
def print_author_ranking(c):
print("\n2. Who are the most popular article authors of all time?\n")
c.execute("""SELECT authors.name, sum(view_count) AS view_sum
from authors,
(SELECT articles.author AS author_id, count(log.id) AS view_count
FROM articles
LEFT JOIN log
ON log.path=CONCAT('/article/', articles.slug)
AND status='200 OK'
GROUP BY articles.author) AS VC
WHERE authors.id=author_id
GROUP BY authors.name
ORDER BY view_sum DESC;""")
for i in c.fetchall():
print('{} - {} views'.format(i[0], i[1]))
def print_error_percentage(c):
print("\n3. On which days did more than 1% of requests lead to errors?\n")
c.execute("""SELECT success.date,
(error.count/(CAST(success.count AS FLOAT)+error.count)*100)
FROM (SELECT CAST(time AS DATE) AS date, count(*) FROM log
WHERE status='200 OK'
GROUP BY date) AS success,
(SELECT CAST(time AS DATE) AS date, count(*) FROM log
WHERE status='404 NOT FOUND'
GROUP BY date) AS error
WHERE success.date=error.date
AND (error.count/(CAST(success.count AS FLOAT)+error.count)*100)>1;""")
for i in c.fetchall():
print("{0:%B %d, %Y} - {1:.2f}% errors".format(i[0], i[1]))
def main():
DBNAME = "news"
db = psycopg2.connect(database=DBNAME)
c = db.cursor()
print_popular_articles(c)
print_author_ranking(c)
print_error_percentage(c)
db.close()
if __name__ == "__main__":
main()