-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_log_roaa.py
More file actions
81 lines (67 loc) · 2.32 KB
/
project_log_roaa.py
File metadata and controls
81 lines (67 loc) · 2.32 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
78
79
80
81
#! /usr/bin/env python3
# project1 Log Analysis by ROAA KORDI
import psycopg2
DBNAME = "news"
def main():
print("Most popular articles:")
query1 = """\
SELECT TITLE, PATH, COUNT(PATH) AS NUM
FROM ARTICLES JOIN AUTHORS ON ARTICLES.AUTHOR = AUTHORS.ID
JOIN LOG ON LOG.PATH = concat('/article/', ARTICLES.SLUG)
GROUP BY TITLE, PATH ORDER BY NUM DESC LIMIT 3 ;
"""
QueryArt(query1)
print("Most popular Authors:")
query2 = """\
SELECT AUTHORS.NAME, COUNT(*) AS NUM
FROM ARTICLES JOIN AUTHORS ON ARTICLES.AUTHOR = AUTHORS.ID
JOIN LOG ON LOG.PATH = concat('/article/', ARTICLES.SLUG)
GROUP BY AUTHORS.NAME ORDER BY NUM DESC;
"""
QueryAuth(query2)
print("Days with more than 1% errors:")
query3 = """\
SELECT DATE(TIME),ROUND(100.0* SUM(CASE LOG.STATUS
WHEN '404 NOT FOUND' THEN 1 ELSE 0 END)/COUNT(LOG.STATUS),2) AS PER
FROM LOG GROUP BY DATE(TIME) ORDER BY PER DESC;
"""
QueryLog(query3)
def QueryArt(query):
db = psycopg2.connect(database=DBNAME)
c = db.cursor()
c.execute(query)
result = c.fetchall()
for row in result:
print ("\t" + row[0] + " - " + str(row[2]) + " views")
db.close()
print ("------------------------------")
def QueryAuth(query):
db = psycopg2.connect(database=DBNAME)
c = db.cursor()
c.execute(query)
result = c.fetchall()
for row in result:
print ("\t" + row[0] + " - " + str(row[1]) + " views")
db.close()
print ("------------------------------")
def QueryLog(query):
db = psycopg2.connect(database=DBNAME)
c = db.cursor()
c.execute(query)
result = c.fetchall()
for row in result:
if float(row[1]) > 1:
print("\t" + str(row[0]) +
" -- " + str(row[1]) + "% errors")
pass
db.close()
print("------------------------------")
if __name__ == '__main__':
main()
# References used in this code:
# for round function
# https://stackoverflow.com/questions/36531361/calculate-percentage-between-
# two-columns-in-sql-query-as-another-column
# To print the result of SQL in neat way
# http://www.mikusa.com/python-mysql-docs/row_results.html
# Also https://thepythonguru.com/fetching-results/