Advertisement
Guest User

Untitled

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