-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_tool.py
More file actions
77 lines (64 loc) · 2.34 KB
/
log_tool.py
File metadata and controls
77 lines (64 loc) · 2.34 KB
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
#!/usr/bin/env python3
import datetime
import psycopg2
"""
This application is used to collect aggregated information from
an sql database and displaying in the terminal output
"""
def get_query_results(query):
"""
Helper query to perform a database query towards the
postgreSQL database
"""
db = psycopg2.connect(database="news")
c = db.cursor()
c.execute(query)
result = c.fetchall()
db.close()
return result
"""
The Functions below are responsible for querying the database and
formatting and printing the information collected from the SQL database
"""
def print_error_ratio_days():
query = """select to_char(daily_requests.f_date, 'FMMonth FMDD, YYYY') as date_1,
round(
(daily_errors.error_count * 100. /
daily_requests.request_count)::numeric,1
) as error_perc
from daily_requests, daily_errors
where daily_requests.f_date = daily_errors.f_date
and round(
(daily_errors.error_count * 100. /
daily_requests.request_count)::numeric,1
) >= 1.0;"""
rows = get_query_results(query)
print("****** Biggest Error Ratio ******")
for date, perc in rows:
print("{} -- {}% errors".format(date, perc))
print("\n")
def print_most_popular_authors():
query = """select authors.name,count(articles_log.article_title) as views
from articles_log ,authors
where authors.id = articles_log.articles_author
group by authors.name
order by views desc;"""
rows = get_query_results(query)
print("****** Most Popular Authors ******")
for author, views in rows:
print("{} -- {} views".format(author, views))
print("\n")
def print_most_viewed_articles():
query = """select article_title,count(article_title) as views
from articles_log
group by article_title
order by views desc limit 3;"""
rows = get_query_results(query)
print("****** Most Viewed Articles ******")
for title, views in rows:
print("{} -- {} views".format(title, views))
print("\n")
if __name__ == "__main__":
print_most_viewed_articles()
print_most_popular_authors()
print_error_ratio_days()