1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- import logging
- from flask import Blueprint, render_template
- from lakesuperior.api import admin as admin_api
- from lakesuperior.exceptions import (
- ChecksumValidationError, ResourceNotExistsError, TombstoneError)
- logger = logging.getLogger(__name__)
- admin = Blueprint('admin', __name__)
- @admin.route('/stats', methods=['GET'])
- def stats():
- """
- Get repository statistics.
- """
- def fsize_fmt(num, suffix='b'):
- """
- Format an integer into 1024-block file size format.
- Adapted from Python 2 code on
- https://stackoverflow.com/a/1094933/3758232
- :param int num: Size value in bytes.
- :param str suffix: Suffix label (defaults to ``b``).
- :rtype: str
- :return: Formatted size to largest fitting unit.
- """
- for unit in ['','K','M','G','T','P','E','Z']:
- if abs(num) < 1024.0:
- return f'{num:3.1f} {unit}{suffix}'
- num /= 1024.0
- return f'{num:.1f} Y{suffix}'
- repo_stats = admin_api.stats()
- return render_template(
- 'stats.html', fsize_fmt=fsize_fmt, **repo_stats)
- @admin.route('/tools', methods=['GET'])
- def admin_tools():
- """
- Admin tools.
- @TODO stub.
- """
- return render_template('admin_tools.html')
- @admin.route('/<path:uid>/fixity', methods=['GET'])
- def fixity_check(uid):
- """
- Check the fixity of a resource.
- """
- uid = '/' + uid.strip('/')
- try:
- admin_api.fixity_check(uid)
- except ResourceNotExistsError as e:
- return str(e), 404
- except TombstoneError as e:
- return str(e), 410
- except ChecksumValidationError as e:
- return str(e), 412
- return f'Checksum for {uid} passed.', 200
|