-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogs.py
65 lines (48 loc) · 2.02 KB
/
logs.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
#!/usr/bin/env python
import psycopg2
DBNAME = "news"
# making a function to handle the queries
def make_query(query):
db = psycopg2.connect(database=DBNAME)
c = db.cursor()
c.execute(query)
return c.fetchall()
db.close()
# 1- What are the most popular three articles of all time?
query1 = (""" select title,count(*) as num from articles,log where
log.path=CONCAT('/article/',articles.slug)
group by articles.title order by
num DESC limit 3; """)
# 2- Who are the most popular article authors of all time?
query2 = ("SELECT authors.name, count(*) as views"
" FROM articles JOIN authors ON"
" articles.author = authors.id"
" JOIN log ON articles.slug = substring(log.path, 10)"
" WHERE log.status LIKE '200 OK' "
" GROUP BY authors.name ORDER BY views DESC;")
# 3-which days did more than 1% of requests lead to errors?
query3 = (" select * from ("
" SELECT TO_CHAR(date, 'Mon DD, YYYY'),round(100.0*sum(case log.status"
" when '200 OK' then 0 else 1 end)/count(log.status),3) as error"
" from log group by date(time)"
" order by error desc) as subq where error > 1; ")
# Functions for print each query
def print_query_1(query):
results = make_query(query)
print('\n1. The 3 most popular articles of all time are:\n')
for result in results:
print ('\t' + str(result[0]) + ' - ' + str(result[1]) + ' views')
def print_query_2(query):
results = make_query(query)
print('\n2. The most popular article authors of all time are:\n')
for result in results:
print ('\t' + str(result[0]) + ' - ' + str(result[1]) + ' views')
def print_query_3(query):
results = make_query(query)
print('\n3. Days with more than 1% of request that lead to an error:\n')
for result in results:
print ('\t' + str(result[0]) + ' - ' + str(result[1]) + ' %')
# print the result
print_query_1(query1)
print_query_2(query2)
print_query_3(query3)