Advertisement
Guest User

Untitled

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