Skip to content

Commit cd66f4c

Browse files
authored
Create Logs-Analysis.py
1 parent a3a82ed commit cd66f4c

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

Logs-Analysis.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
#!/usr/bin/env python3
22

3-
'''
3+
"""
44
psycopg is the PostgreSQL adapter for the Python programming language.
55
Some database code and comments are adapted from the psycopg documentation(http://initd.org/psycopg/)
6-
'''
6+
"""
77
import psycopg2
88

99
def main():
1010

11-
question1 = """
11+
question1 = '''
1212
1. What are the most popular three articles of all time?
1313
Which articles have been accessed the most?
1414
Present this information as a sorted list with the most popular article at the top
15-
"""
16-
15+
'''
16+
1717
q1_SQL = """
1818
SELECT articles.title, COUNT(log.path) as num_access
1919
FROM articles, log
20-
WHERE log.path = '/article/' || articles.slug
20+
WHERE log.path = concat('/article/', articles.slug)
2121
GROUP BY articles.title
2222
ORDER BY num_access DESC
2323
LIMIT 3;
@@ -41,8 +41,23 @@ def main():
4141
3. On which days did more than 1% of requests lead to errors?
4242
The log table includes a column status that indicates the HTTP status code that the news site sent to the user's browser.
4343
"""
44+
45+
SELECT Total.date, (Error.ViewCount*1.0/Total.ViewCount) as ErrorPercentage
46+
FROM
47+
(SELECT date(time), COUNT(*) as ViewCount
48+
FROM log
49+
GROUP BY date(time)
50+
ORDER BY date(time)) as Total
51+
,(SELECT date(time), COUNT(*) as ViewCount
52+
FROM log WHERE status = '404 NOT FOUND'
53+
GROUP BY date(time)
54+
ORDER BY date(time)) as Error
55+
WHERE Total.date = Error.date;
4456

4557
q3_SQL = """
58+
SELECT
59+
FROM
60+
WHERE
4661
4762
"""
4863

0 commit comments

Comments
 (0)