-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.py
60 lines (51 loc) · 1.7 KB
/
log.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
#!/usr/bin/env python
import psycopg2
def connect(dbname="news"):
"""Connect to the PostgreSQL database and returns a database connection."""
try:
db = psycopg2.connect("dbname={}".format(dbname))
c = db.cursor()
return db, c
except:
print("Error in connecting to database")
def popular_article():
"""Prints most popular three articles of all time"""
db, c = connect()
query = "select * from popular_articles limit 3"
c.execute(query)
result = c.fetchall()
db.close()
print "\nPopular Articles:\n"
for i in range(0, len(result), 1):
print "\"" + result[i][0] + "\" - " + str(result[i][1]) + " views"
def popular_authors():
"""Prints most popular article authors of all time"""
db, c = connect()
query = "select * from popular_authors"
c.execute(query)
result = c.fetchall()
db.close()
print "\nPopular Authors:\n"
for i in range(0, len(result), 1):
print "\"" + result[i][0] + "\" - " + str(result[i][1]) + " views"
def log_status():
"""Print days on which more than 1% of requests lead to errors"""
db, c = connect()
query = "select * from log_status"
c.execute(query)
result = c.fetchall()
db.close()
print "\nDays with more than 1% of errors:\n"
for i in range(0, len(result), 1):
print str(result[i][0])+ " - "+str(round(result[i][3], 2))+"% errors"
if __name__ == '__main__':
# uncomment the below code to make views
'''
view_popular_article()
view_popular_authors()
view_log_status()
'''
popular_article()
popular_authors()
log_status()
print "\nSuccess!\n"