-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogdb.py
More file actions
90 lines (80 loc) · 3.17 KB
/
logdb.py
File metadata and controls
90 lines (80 loc) · 3.17 KB
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
86
87
88
89
90
#!/usr/bin/env python2.7
import psycopg2
def db_connect():
"""initiate connection to database"""
db = psycopg2.connect("dbname=news")
return db
def most_accessed_article():
"""Most accessed articles of all time sorted in descending other."""
try:
db = db_connect() # create database connection
cursor = db.cursor()
# query : articles and the amount of time they have been accessed
cursor.execute("""select title, count(*) as views from articles, log
where log.path
like '/article/' || articles.slug and status = '200 OK'
group by title
order by views desc
limit 3;""")
rows = cursor.fetchall() # fetch results from database
# loop through to display retrieved records
for i in rows:
# query : retrieve article titles via known path in log
print('"{}" - {} views'.format(i[0], i[1])) # output text
finally:
db.close() # close database connection
def most_popular_authors():
"""Most popular authors based on total article views per author."""
try:
db = db_connect() # create database connection
cursor = db.cursor()
# query : retrieve author, find articles by them and count
cursor.execute("""select authors.name, count(*) as views from authors
inner join articles on authors.id = articles.author
inner join log
on status = '200 OK' and '/article/' || articles.slug = log.path
group by authors.name order by views desc;""")
leaderboard = cursor.fetchall() # fetch results from database
# loop through to display retrieved records
for i in leaderboard:
print('{} - {} views'.format(i[0], i[1])) # output text
finally:
db.close() # close database connection
def days_with_errors():
"""Percentage error of total requests per day."""
try:
db = db_connect() # create database connection
cursor = db.cursor()
# query:use log_report view to calculate percent daily errors > than 1
cursor.execute("""select day,
round(error * 100.0 / requests, 1) as percent from log_report
group by day,error,requests
having round(error * 100.0 / requests, 1) > 1
order by percent desc;""")
rows = cursor.fetchall() # fetch results from database
# loop through to display retrieved records
for i in rows:
print('{} - {}% errors'.format(i[0], i[1])) # output text
finally:
db.close() # close database connection
# report header format
print("\n")
print(54*"*")
print('**************** NEWS DATABASE REPORT ****************')
print(54*"*")
# call method to display most accessed articles
print("\n")
print('The most popular three articles of all time:')
print(54*"_")
most_accessed_article()
# call method to display most popular authors
print("\n")
print('The most popular article authors of all time:')
print(54*"_")
most_popular_authors()
# call method to display days with over 1% error requests
print("\n")
print('Days with more than 1% of requests leading to errors:')
print(54*"_")
days_with_errors()
print("\n")