-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueries.py
More file actions
93 lines (71 loc) · 2.57 KB
/
Queries.py
File metadata and controls
93 lines (71 loc) · 2.57 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
91
92
93
import psycopg2
def Execute_query(query):
#Establishing connection to the database
db = psycopg2.connect("news")
c = db.cursor()
c.execute(query)
# fetch results from the cursor
query_results = c.fetchall()
db.close()
return query_results
# Query 1. The most popular three articles of all time:
def get_popular_articles():
query = """
SELECT articles.title, COUNT(*) AS num
FROM articles
JOIN log
ON log.path LIKE concat('/article/%', articles.slug)
GROUP BY articles.title
ORDER BY num DESC
LIMIT 3;
"""
results = Execute_query(query)
print('Three most popular articles of all time are' )
for i in results:
print('"{title}" - {count} views'
.format(title=i[0], count=i[1]))
print()
return
# Query 2. The most popular article authors of all time:
def get_popular_authors():
query = """
SELECT authors.name, COUNT(*) AS num
FROM authors
JOIN articles
ON authors.id = articles.author
JOIN log
ON log.path like concat('/article/%', articles.slug)
GROUP BY authors.name
ORDER BY num DESC
LIMIT 3;
"""
results = Execute_query(query)
print('Most popular authors of all time')
for i in results:
print('{author} - {count} views'
.format(author=i[0], count=i[1]))
print()
return
# Query 3. On which days did more than 1% of requests lead to errors?:
def get_dates_with_errors():
query = """
SELECT TO_CHAR(date, 'FMMonth FMDD, YYYY'), err/total AS ratio
FROM (SELECT time::date AS date,
COUNT(*) AS total,
SUM((status != '200 OK')::int)::float AS err
FROM log
GROUP BY date) as errors
WHERE err/total > 0.01;
"""
results = Execute_query(query)
print(' more than 1% of requests lead to errors filtered by dates')
for i in results:
print('{date:%B %d, %Y} - {error_rate:.1f}% errors'.format(
date=i[0],
error_rate=i[1]))
print()
return
# Functions execution
get_popular_articles()
get_popular_authors()
get_dates_with_errors()