-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogAnalysis.py
101 lines (75 loc) · 3.02 KB
/
logAnalysis.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
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
94
95
96
97
98
99
100
101
#!/usr/bin/env python
import psycopg2
# declaring database
DBNAME = "news"
def loganalysis():
# connect to the database news
try:
conn = psycopg2.connect(database=DBNAME)
except:
print "Unable to connect to database"
# make a cursor object from a connection, Cursors are used to send
# SQL statements to the database and fetch results
cursor = conn.cursor()
# calling seperate methods to answer
# eachreporting questions
popularArticle(cursor)
popularAuthors(cursor)
errorsPerDay(cursor)
# closing the connection
conn.close()
def popularArticle(cursor):
# execute SQL statement on the database
cursor.execute("""SELECT articles.title, count(*)
FROM articles INNER JOIN log ON
log.path = '/article/' || articles.slug
GROUP BY title
ORDER BY COUNT(*) DESC
LIMIT 3;""")
# Fetch all the results from the current statement
results = cursor.fetchall()
# printing the results
print "\nThe most popular three articles of all time:"
for article, views in results:
print('{0:<40}{1} views'.format(article, views))
def popularAuthors(cursor):
# execute SQL statement on the database
cursor.execute("""SELECT authors.name,count(*)
FROM articles INNER JOIN authors
on articles.author = authors.id
INNER JOIN log ON
log.path = '/article/' || articles.slug
GROUP BY authors.name
ORDER BY count(*) DESC""")
# Fetch all the results from the current statement
results = cursor.fetchall()
# printing the results
print "\nThe most popular article authors of all time:"
for author, views in results:
print('{0:<40}{1} views'.format(author, views))
def errorsPerDay(cursor):
# execute SQL statement on the database
cursor.execute("""WITH t1 as
(SELECT time::date as days,
count(*) as agg
FROM log
GROUP BY time::date
),t2 as (SELECT time::date as days,
count(*) as agg
FROM log
WHERE status like '%NOT%'
GROUP BY time::date
)SELECT to_char(t1.days,'FMMONTH DD,YYYY') as days,
to_char((cast(t2.agg as decimal)/t1.agg)*100.0,'99.99%')
as errors
FROM t1 INNER JOIN t2 ON t1.days = t2.days
where (cast(t2.agg as decimal)/t1.agg)*100.0> 1.00;""")
# Fetch all the results from the current statement
results = cursor.fetchall()
# printing the results
print "\nDays with more than 1% of requests lead to errors:"
for day, error in results:
print('{0:<40}{1} views'.format(day, error))
# this executes this python module
if __name__ == '__main__':
loganalysis()