Skip to content

Commit 1a22aee

Browse files
authored
Create Logs-Analysis.py
1 parent aef09d3 commit 1a22aee

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

Logs-Analysis.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,54 @@
77
import psycopg2
88

99
def main():
10+
# Connect to an existing database
11+
conn = psycopg2.connect("dbname=news")
12+
13+
# Open a cursor to perform database operations
14+
cur = conn.cursor()
1015

16+
# Question 1
1117
sql_popular_articles = """
12-
SELECT *
18+
SELECT article_view.title, article_view.view
1319
FROM article_view
1420
ORDER BY article_view.view DESC
1521
LIMIT 3;
1622
"""
23+
cur.execute(sql_popular_articles)
24+
print("Most popular articles:")
25+
for (title, view) in cur.fetchall():
26+
print(" {} - {} views".format(title, view))
27+
print("-" * 70)
1728

29+
30+
# Question 2
1831
sql_popular_authors = """
1932
SELECT article_view.name, SUM(article_view.view) AS author_view
2033
FROM article_view
2134
GROUP BY article_view.name
2235
ORDER BY author_view DESC
2336
LIMIT 1;
2437
"""
38+
cur.execute(sql_popular_authors)
39+
print("Most popular authors:")
40+
for (name, view) in cur.fetchall():
41+
print(" {} - {} views".format(name, view))
42+
print("-" * 70)
2543

44+
# Question 3
2645
sql_more_than_one_percent_errors = """
2746
SELECT *
2847
FROM error_rate
2948
WHERE error_rate.percentage > 1
3049
ORDER BY error_rate.percentage DESC;
3150
"""
32-
33-
# Connect to an existing database
34-
conn = psycopg2.connect("dbname=news")
35-
36-
# Open a cursor to perform database operations
37-
cur = conn.cursor()
38-
39-
51+
cur.execute(sql_more_than_one_percent_errors)
52+
print("Days with more than 1% errors:")
53+
for (date, percentage) in cur.fetchall():
54+
print(" {} - {}% errors".format(date, percentage))
55+
print("-" * 70)
4056

4157

42-
43-
44-
45-
46-
4758
# Close communication with the database
4859
cur.close()
4960
conn.close()

0 commit comments

Comments
 (0)