-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreporter.py
155 lines (124 loc) · 4.36 KB
/
reporter.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python3.7
import psycopg2
# first question
def top_articals(num=99):
"""
the most popular ? articles sorted by most viewed
return list of articles and views for each
"""
# create connection
db = psycopg2.connect("dbname=news")
cur = db.cursor()
# preparing reqierd table of
# articles.title , views_count
# whith code 200 statue
# path contain /articales/<article slug>
cur.execute('''
select a.title , count(log.path)
from log
right join articles as a
on (log.status like '%%200%%')
and (log.path like concat('%%/article/%%',a.slug))
group by a.title
order by count desc
limit %s;''', (num, ))
res = cur.fetchall()
# commit and closer connection
db.commit()
cur.close()
db.close()
return res
# second question
def top_author(num=99):
"""the most popular ? author sorted by page views"""
# create connection
db = psycopg2.connect("dbname=news")
cur = db.cursor()
# creating list of author and sum of view_count for thier articles
cur.execute('''SELECT authors.name, count(log.path)
from authors,articles,log
where (log.status like '%%200%%')
and (log.path like concat('%%/article/%%',articles.slug))
and authors.id = articles.author
group by authors.name
order by count desc
limit %s''', (num, ))
res = cur.fetchall()
# commit and close connection
db.commit()
cur.close()
db.close()
return res
# third question
def error_find(precentage=.01):
""" error precentage higher than 1% default"""
# create connection
db = psycopg2.connect("dbname=news")
cur = db.cursor()
# counting error by day
# ignoring redirection statues 3xx
# counting server and client error 4xx , 5xx
# and date(time) = date(%s)
cur.execute('''CREATE OR REPLACE view error_count as
select date(time) as date, count(*) as error
from log
where (status like '%%4%%' or status like '%%5%%')
group by date;''')
# counting sucess by day
# count suceess 2xx
cur.execute('''CREATE OR REPLACE view success_count as
select date(time) as date , count(*) as success
from log
where (status like '%%2%%')
group by date;''')
# fing date where error higher than precentage
# multiplying first to convert ro float
cur.execute('''SELECT er.date , (100.0*er.error)/sucs.success
from error_count as er ,success_count as sucs
where er.date = sucs.date
and cast(er.error as float)/sucs.success >= %s;''',
(precentage, ))
res = cur.fetchall()
# commit and close connection
db.commit()
cur.close()
db.close()
return res
def view_data(res):
'''
print two coulomn data in formated way
'''
if len(res) == 0:
print('no data')
return
width = max([len(str(i[0])) for i in res])+4
for i in res:
print('{:<{width}}{:>10}'.format(str(i[0]), i[1], width=width))
if __name__ == "__main__":
import sys
if len(sys.argv) == 1:
print('\ntop articles:')
res = top_articals()
view_data(res)
print('\ntop authors:')
res = top_author()
view_data(res)
print('\nerror report:')
res = error_find()
view_data(res)
elif len(sys.argv) == 2:
if (sys.argv[1] == 'articles'):
res = top_articals()
elif(sys.argv[1] == 'author'):
res = top_author()
elif(sys.argv[1] == 'error'):
res = error_find()
view_data(res)
elif len(sys.argv) == 3:
if (sys.argv[1] == 'articles'):
res = top_articals(int(sys.argv[2]))
elif(sys.argv[1] == 'author'):
res = top_author(int(sys.argv[2]))
elif(sys.argv[1] == 'error'):
res = error_find(float(sys.argv[2])/100.0)
view_data(res)