Skip to content

Commit 695cd8c

Browse files
authored
Add files via upload
1 parent ddd8f86 commit 695cd8c

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed

LogsProject.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/python3
2+
3+
import psycopg2
4+
5+
DBNAME = "news"
6+
7+
8+
def popular_articles():
9+
query = """
10+
SELECT title, x
11+
FROM articles4, shortened2
12+
WHERE articles4.concat = shortened2.path
13+
ORDER BY x DESC;
14+
"""
15+
db = psycopg2.connect(database=DBNAME)
16+
c = db.cursor()
17+
c.execute(query)
18+
results = c.fetchall()
19+
for r in results:
20+
print r[0], "--", r[1], "views"
21+
print ""
22+
db.close()
23+
24+
25+
def popular_authors():
26+
query = """
27+
SELECT name, sum(number_of_views)
28+
AS number_of_views
29+
FROM z, y
30+
WHERE z.concat = y.path
31+
GROUP BY name
32+
ORDER BY number_of_views desc;
33+
"""
34+
db = psycopg2.connect(database=DBNAME)
35+
c = db.cursor()
36+
c.execute(query)
37+
results = c.fetchall()
38+
for r in results:
39+
print r[0], "--", r[1], "views"
40+
print ""
41+
db.close()
42+
43+
44+
def errors():
45+
query = """SELECT date,
46+
CONCAT(round(percentage::decimal, 2),'%')
47+
FROM divide
48+
WHERE percentage > 1;"""
49+
db = psycopg2.connect(database=DBNAME)
50+
c = db.cursor()
51+
c.execute(query)
52+
results = c.fetchall()
53+
for r in results:
54+
print r[0], "--", r[1], "views"
55+
db.close()
56+
57+
58+
popular_articles()
59+
popular_authors()
60+
errors()

LogsProjectOutput.txt

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
Candidate is jerk, alleges rival -- 338647 views
3+
Bears love berries, alleges bear -- 253801 views
4+
Bad things gone, say good people -- 170098 views
5+
6+
Ursula La Multa -- 507594 views
7+
Rudolf von Treppenwitz -- 423457 views
8+
Anonymous Contributor -- 170098 views
9+
Markoff Chaney -- 84557 views
10+
11+
2016-07-17 -- 2.32% error

README.MD

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#Requirements
2+
-User must have virtual environment running
3+
4+
-In order to run, user needs
5+
6+
-Vagrant
7+
-Virtual Box
8+
Get Vagrant file here: `https://github.com/udacity/fullstack-nanodegree-vm/blob/master/vagrant/Vagrantfile`
9+
Download database here: `https://d17h27t6h515a5.cloudfront.net/topher/2016/August/57b5f748_newsdata/newsdata.zip`
10+
11+
Place logsproject.py and newsdata.sql in vagrant directory
12+
13+
If psycopg2 is not already installed, use command `pip install psycopg2`
14+
15+
#How to run
16+
in terminal make vagrant current directory and run
17+
18+
`vagrant up` and `vagrant ssh`
19+
20+
use following command to open database:
21+
`psql news`
22+
23+
enter the following queries to create required views:
24+
25+
`
26+
CREATE VIEW shortened2 AS
27+
SELECT path,
28+
COUNT(id) AS x
29+
FROM log
30+
GROUP BY path
31+
ORDER BY x DESC
32+
LIMIT 3 OFFSET 1;
33+
`
34+
`
35+
CREATE VIEW articles4 AS
36+
SELECT *,
37+
CONCAT('/article/', slug)
38+
FROM articles;
39+
`
40+
`
41+
CREATE VIEW z AS
42+
SELECT concat, title, name
43+
FROM articles4, authors
44+
WHERE articles4.author = authors.id;
45+
`
46+
`
47+
CREATE VIEW y AS
48+
SELECT path, count(path) AS number_of_views
49+
FROM log
50+
GROUP BY path
51+
ORDER BY number_of_views desc;
52+
`
53+
`
54+
CREATE VIEW ok AS
55+
SELECT time::date AS date,
56+
COUNT(status) AS no_error
57+
FROM log
58+
WHERE status = '200 OK'
59+
GROUP BY date;
60+
`
61+
`
62+
CREATE VIEW error AS
63+
SELECT time::date AS date,
64+
COUNT(status) AS error
65+
FROM log
66+
WHERE status = '404 NOT FOUND'
67+
GROUP BY date;
68+
`
69+
`
70+
CREATE VIEW together AS
71+
SELECT ok.date, error, no_error
72+
FROM ok, error
73+
WHERE ok.date = error.date;
74+
`
75+
`
76+
CREATE VIEW divide AS
77+
SELECT date, 100*(error::decimal / no_error) AS percentage
78+
FROM together;
79+
`
80+
81+
exit psql and run command:
82+
`python logsproject.py`

0 commit comments

Comments
 (0)