This repository has been archived by the owner on Jul 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_report.py
57 lines (41 loc) · 1.51 KB
/
log_report.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
#!/usr/bin/env python3
import psycopg2
DBNAME = "news"
def report_most_popular_articles_ever():
""" Reports the most popular articles """
db = psycopg2.connect(database=DBNAME)
print("What are the most popular three articles of all time?")
c = db.cursor()
c.execute("SELECT * FROM most_popular_articles_ever ORDER BY count DESC;")
results = c.fetchall()
db.close()
for result in results:
print("'%s' — %s views" % (result[1], result[0]))
print("\n")
def report_most_popular_authors_ever():
""" Reports the most popular authors """
db = psycopg2.connect(database=DBNAME)
print("Who are the most popular article authors of all time?")
c = db.cursor()
c.execute("SELECT * FROM most_popular_authors ORDER BY count DESC;")
results = c.fetchall()
db.close()
for result in results:
print("%s — %s views" % (result[0], result[1]))
print("\n")
def report_request_errors():
""" Reports the days with more than 1% error rate """
db = psycopg2.connect(database=DBNAME)
print("On which days did more than 1% of requests lead to errors?")
c = db.cursor()
c.execute("SELECT * FROM requests_error ORDER BY day;")
results = c.fetchall()
db.close()
for result in results:
print("%s — %s%% errors" % (result[0], result[1]))
print("\n")
if __name__ == '__main__':
print("##### Log-Analysis-Tool #####")
report_most_popular_articles_ever()
report_most_popular_authors_ever()
report_request_errors()