controller.Users.calculate_storage() method to calculate the total bytes used by a particular user.
This commit is contained in:
parent
8e7749d512
commit
2e75521ac2
|
@ -306,7 +306,6 @@ class Users( object ):
|
|||
|
||||
notebooks += user.notebooks
|
||||
|
||||
|
||||
yield dict(
|
||||
user = user,
|
||||
notebooks = notebooks,
|
||||
|
@ -315,4 +314,33 @@ class Users( object ):
|
|||
login_url = login_url,
|
||||
)
|
||||
|
||||
def calculate_storage( self, user ):
|
||||
"""
|
||||
Calculate total storage utilization for all notebooks and all notes of the given user,
|
||||
including storage for all past revisions.
|
||||
@type user: User
|
||||
@param user: user for which to calculate storage utilization
|
||||
@rtype: int
|
||||
@return: total bytes used for storage
|
||||
"""
|
||||
total_bytes = 0
|
||||
|
||||
def sum_revisions( obj ):
|
||||
return \
|
||||
self.__database.size( obj.object_id ) + \
|
||||
sum( [ self.__database.size( obj.object_id, revision ) or 0 for revision in obj.revisions_list ], 0 )
|
||||
|
||||
def sum_notebook( notebook ):
|
||||
return \
|
||||
sum_revisions( notebook ) + \
|
||||
sum( [ sum_revisions( note ) for note in notebook.notes ], 0 )
|
||||
|
||||
for notebook in user.notebooks:
|
||||
total_bytes += sum_notebook( notebook )
|
||||
|
||||
if notebook.trash:
|
||||
total_bytes += sum_notebook( notebook.trash )
|
||||
|
||||
return total_bytes
|
||||
|
||||
scheduler = property( lambda self: self.__scheduler )
|
||||
|
|
|
@ -201,3 +201,27 @@ class Test_users( Test_controller ):
|
|||
|
||||
def test_current_with_startup_notes_without_login( self ):
|
||||
self.test_current_without_login( include_startup_notes = True )
|
||||
|
||||
def test_calculate_user_storage( self ):
|
||||
size = cherrypy.root.users.calculate_storage( self.user )
|
||||
notebooks = self.user.notebooks
|
||||
|
||||
# expected a sum of the sizes of all of this user's notebooks, notes, and revisions
|
||||
expected_size = \
|
||||
self.database.size( notebooks[ 0 ].object_id ) + \
|
||||
self.database.size( notebooks[ 0 ].object_id, notebooks[ 0 ].revision ) + \
|
||||
self.database.size( notebooks[ 1 ].object_id ) + \
|
||||
self.database.size( notebooks[ 1 ].object_id, notebooks[ 1 ].revision )
|
||||
|
||||
assert size == expected_size
|
||||
|
||||
def test_calculate_anon_storage( self ):
|
||||
size = cherrypy.root.users.calculate_storage( self.anonymous )
|
||||
|
||||
expected_size = \
|
||||
self.database.size( self.anon_notebook.object_id ) + \
|
||||
self.database.size( self.anon_notebook.object_id, self.anon_notebook.revision ) + \
|
||||
self.database.size( self.anon_notebook.notes[ 0 ].object_id ) + \
|
||||
self.database.size( self.anon_notebook.notes[ 0 ].object_id, self.anon_notebook.notes[ 0 ].revision )
|
||||
|
||||
assert size == expected_size
|
||||
|
|
Reference in New Issue