Advertisement
Guest User

Untitled

a guest
Jun 30th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. import psycopg2
  2. import datetime
  3.  
  4. try:
  5. dbname = "news"
  6. user = "postgres"
  7. host = "localhost"
  8. password = "pass"
  9.  
  10. conn = psycopg2.connect(str.join("dbname='{0}'",
  11. "user='{1}'",
  12. "host='{2}'",
  13. "password='{3}'".format(dbname,
  14. user,
  15. host,
  16. password)))
  17. except:
  18. print "Cannot connect to 'news' database"
  19.  
  20.  
  21. def top_articles(limit):
  22. """ Top news articles sorted by popularity """
  23. cursor = conn.cursor()
  24. cursor.execute(str.join("SELECT COUNT(id), path",
  25. "FROM log WHERE status = '200 OK'",
  26. " AND NOT path = '/' GROUP BY".
  27. " path ORDER BY COUNT(id)",
  28. " DESC LIMIT {0};".format(limit)))
  29.  
  30. print "Top {0} articles sorted by popularity: ".format(limit)
  31. print
  32.  
  33. articles = cursor.fetchall()
  34. for article in articles:
  35. cursor.execute(str.join("SELECT title FROM",
  36. "articles WHERE",
  37. "('/article/' || slug) "
  38. "='" + article[1] + "';"))
  39. title = cursor.fetchone()
  40. print '"{0}" - {1} views'.format(title[0], article[0])
  41.  
  42.  
  43. def top_authors(limit):
  44. """ Top authors sorted by total article views """
  45. cursor = conn.cursor()
  46. cursor.execute("SELECT name, id FROM authors LIMIT {0};".format(limit))
  47.  
  48. print "Top {0} authors sorted by page views: ".format(limit)
  49. print
  50.  
  51. authors = cursor.fetchall()
  52. for author in authors:
  53. cursor.execute(
  54. "SELECT slug FROM articles WHERE author ='{0}';".format(author[1]))
  55. slugs = cursor.fetchall()
  56. total_views = 0
  57. for slug in slugs:
  58. cursor.execute(str.join("SELECT COUNT(id) FROM",
  59. "log WHERE status = '200 OK'",
  60. "AND substring(path, 10)",
  61. "= '{0}';".format(slug[0])))
  62.  
  63. art_count = cursor.fetchone()
  64. total_views += art_count[0]
  65. print "{0} - {1} views".format(author[0], total_views)
  66.  
  67.  
  68. def bad_request_days():
  69. """ Days where there were bad requests over 1% """
  70. cursor = conn.cursor()
  71. cursor.execute(str.join("SELECT COUNT(id), time, status FROM",
  72. "log GROUP BY time, status ORDER BY time;"))
  73.  
  74. last_row = None
  75. total_bad = 0.0
  76. total_good = 0.0
  77.  
  78. print "Days with more than 1% errors:"
  79. print
  80.  
  81. for row in cursor:
  82. if last_row is None:
  83. last_row = row[1]
  84.  
  85. if last_row.month == row[1].month and last_row.day == row[1].day:
  86. if row[2] == "404 NOT FOUND":
  87. total_bad += row[0]
  88. elif row[2] == "200 OK":
  89. total_good += row[0]
  90. else:
  91. result = float(total_bad / (total_bad + total_good) * 100)
  92. if result > 1.0:
  93. print str.join("{0}/{1}/{2} -",
  94. "{3:.2f}%".format(last_row.month, last_row.day,
  95. last_row.year, result))
  96.  
  97. last_row = None
  98. total_bad = 0.0
  99. total_good = 0.0
  100.  
  101.  
  102. if __name__ == "__main__":
  103. top_articles(3)
  104. print
  105. top_authors(4)
  106. print
  107. bad_request_days()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement