-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogs-analysis.py
64 lines (50 loc) · 1.78 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
#!/usr/bin/env python
#
# logs-analysis.py -- analyze user logs from news database
#
import psycopg2
def connect(database_name):
"""Connect to the PostgreSQL database. Returns a database connection."""
try:
db = psycopg2.connect("dbname={}".format(database_name))
c = db.cursor()
return db, c
except psycopg2.Error as e:
print "Unable to connect to the database"
sys.exit(1)
def execute_query(query):
db, c = connect('news')
c.execute(query)
results = c.fetchall()
db.close()
return results
def top3ArticlesAllTime():
"""Returns the most popular 3 articles of all time."""
query = "select * from articles_views " \
"limit 3;"
top3 = execute_query(query)
for item in top3:
print("{0} - {1} views".format(item[0], item[2]))
def mostPopularAuthors():
"""Returns the most popular authors of all time."""
query = "select authors.name, " \
"sum(articles_views.views) as total_author_views " \
"from authors join articles_views " \
"on authors.id = articles_views.author " \
"group by authors.name " \
"order by total_author_views desc"
authors = execute_query(query)
for item in authors:
print("{0} - {1} views".format(item[0], item[1]))
def highErrorDays():
"""Returns the days where more than 1% of requests were errors."""
query = "select to_char(log_date, 'FMMonth DD, YYYY') as log_date_fmt, " \
"percent_errors from log_date_percent_errors " \
"where percent_errors > 1.00"
days = execute_query(query)
for item in days:
print("{0} - {1}% errors".format(item[0], item[1]))
if __name__ == '__main__':
top3ArticlesAllTime()
mostPopularAuthors()
highErrorDays()