stat.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """ Running statistics.
  2. """
  3. import logging
  4. import redis
  5. from texttable import Texttable
  6. from reddrip.commands import Command
  7. from reddrip.util.config import Configuration
  8. log = logging.getLogger(__name__)
  9. class StatCommand(Command):
  10. """ Get the statistics from Redis.
  11. """
  12. help = "Reddrip statistics."
  13. @classmethod
  14. def setup_parser(cls, parser):
  15. parser.add_argument(
  16. "--config", "-c", help="configuration file", required=True
  17. )
  18. def _human_readable(self, size):
  19. size = float(size)
  20. sizes = [(1024, "Gb"), (1024, "Mb"), (1024, "Kb"), (1, "b")]
  21. divider, suffix = sizes.pop()
  22. while size > 1024:
  23. try:
  24. divider, suffix = sizes.pop()
  25. size = round(size / divider, 2)
  26. except IndexError:
  27. break
  28. return "%s %s" % (size, suffix)
  29. def execute(self, options):
  30. conf = Configuration(options.config)
  31. redis_conn = redis.StrictRedis(
  32. host=conf.glob["redis.host"],
  33. port=int(conf.glob["redis.port"]),
  34. db=conf.glob["redis.db"],
  35. )
  36. subreddits = redis_conn.smembers("subreddits")
  37. print "Total of %d subreddits in database, following %s." % (
  38. len(subreddits), len(conf)
  39. )
  40. print
  41. table = Texttable(max_width=0)
  42. table.set_deco(Texttable.HEADER)
  43. table.header([
  44. "Name",
  45. "Type",
  46. "Processed",
  47. "Saved",
  48. "Size"
  49. ])
  50. table.set_cols_align(["l", "l", "r", "r", "r"])
  51. size = 0
  52. processed = 0
  53. saved = 0
  54. for subreddit in subreddits:
  55. try:
  56. dl_type = conf.subreddit(subreddit)["type"]
  57. except KeyError:
  58. continue
  59. sub_processed = len(
  60. redis_conn.smembers("stat:%s:processed:all" % subreddit)
  61. )
  62. sub_saved = redis_conn.get("stat:%s:saved:count" % subreddit)
  63. sub_size = redis_conn.get("stat:%s:saved:size" % subreddit)
  64. processed += int(sub_processed)
  65. saved += int(sub_saved)
  66. size += int(sub_size)
  67. table.add_row([
  68. subreddit,
  69. dl_type,
  70. sub_processed,
  71. sub_saved,
  72. self._human_readable(sub_size)
  73. ])
  74. table.add_row([
  75. "TOTAL",
  76. "",
  77. processed,
  78. saved,
  79. self._human_readable(size)
  80. ])
  81. print table.draw()