admin.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import logging
  2. from flask import Blueprint, render_template
  3. from lakesuperior.api import admin as admin_api
  4. # Admin interface and REST API.
  5. logger = logging.getLogger(__name__)
  6. admin = Blueprint('admin', __name__)
  7. @admin.route('/stats', methods=['GET'])
  8. def stats():
  9. '''
  10. Get repository statistics.
  11. '''
  12. def fsize_fmt(num, suffix='b'):
  13. '''
  14. Format an integer into 1024-block file size format.
  15. Adapted from Python 2 code on
  16. https://stackoverflow.com/a/1094933/3758232
  17. @param num (int) Size value in bytes.
  18. @param suffix (string) Suffix label (defaults to `B`).
  19. @return string Formatted size to largest fitting unit.
  20. '''
  21. for unit in ['','K','M','G','T','P','E','Z']:
  22. if abs(num) < 1024.0:
  23. return "{:3.1f} {}{}".format(num, unit, suffix)
  24. num /= 1024.0
  25. return "{:.1f} {}{}".format(num, 'Y', suffix)
  26. repo_stats = admin_api.stats()
  27. return render_template(
  28. 'stats.html', fsize_fmt=fsize_fmt, **repo_stats)
  29. @admin.route('/tools', methods=['GET'])
  30. def admin_tools():
  31. '''
  32. Admin tools.
  33. @TODO stub.
  34. '''
  35. return render_template('admin_tools.html')