-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathLogsAnalysis.py
57 lines (48 loc) · 1.46 KB
/
LogsAnalysis.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
#!/usr/bin/env python3
import psycopg2
def main():
# Connect to an existing database
conn = psycopg2.connect("dbname=news")
# Open a cursor to perform database operations
cur = conn.cursor()
# Question 1
sql_popular_articles = """
SELECT article_view.title, article_view.view
FROM article_view
ORDER BY article_view.view DESC
LIMIT 3;
"""
cur.execute(sql_popular_articles)
print("Most popular articles:")
for (title, view) in cur.fetchall():
print(" {} - {} views".format(title, view))
print("-" * 70)
# Question 2
sql_popular_authors = """
SELECT article_view.name, SUM(article_view.view) AS author_view
FROM article_view
GROUP BY article_view.name
ORDER BY author_view DESC;
"""
cur.execute(sql_popular_authors)
print("Most popular authors:")
for (name, view) in cur.fetchall():
print(" {} - {} views".format(name, view))
print("-" * 70)
# Question 3
sql_more_than_one_percent_errors = """
SELECT *
FROM error_rate
WHERE error_rate.percentage > 1
ORDER BY error_rate.percentage DESC;
"""
cur.execute(sql_more_than_one_percent_errors)
print("Days with more than 1% errors:")
for (date, percentage) in cur.fetchall():
print(" {} - {}% errors".format(date, percentage))
print("-" * 70)
# Close communication with the database
cur.close()
conn.close()
if __name__ == "__main__":
main()