2015-03-16 22:03:17 +00:00
|
|
|
import logging
|
|
|
|
|
2015-09-15 23:08:31 +00:00
|
|
|
from data.database import ImageStorage, Image, db, db_for_update
|
2015-03-16 22:03:17 +00:00
|
|
|
from app import app
|
|
|
|
|
2015-09-15 23:08:31 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2015-03-16 22:03:17 +00:00
|
|
|
|
|
|
|
def backfill_aggregate_sizes():
|
|
|
|
""" Generates aggregate sizes for any image storage entries without them """
|
2015-09-15 23:08:31 +00:00
|
|
|
logger.debug('Aggregate sizes backfill: Began execution')
|
2015-03-16 22:03:17 +00:00
|
|
|
while True:
|
2015-09-15 23:08:31 +00:00
|
|
|
batch_image_ids = list(Image
|
|
|
|
.select(Image.id)
|
|
|
|
.where(Image.aggregate_size >> None)
|
|
|
|
.limit(100))
|
2015-03-16 22:03:17 +00:00
|
|
|
|
2015-09-15 23:08:31 +00:00
|
|
|
if len(batch_image_ids) == 0:
|
2015-03-16 22:03:17 +00:00
|
|
|
# There are no storages left to backfill. We're done!
|
2015-09-15 23:08:31 +00:00
|
|
|
logger.debug('Aggregate sizes backfill: Backfill completed')
|
2015-03-16 22:03:17 +00:00
|
|
|
return
|
|
|
|
|
2015-09-15 23:08:31 +00:00
|
|
|
logger.debug('Aggregate sizes backfill: Found %s records to update', len(batch_image_ids))
|
|
|
|
for image_id in batch_image_ids:
|
|
|
|
logger.debug('Updating image : %s', image_id.id)
|
2015-03-16 22:03:17 +00:00
|
|
|
|
|
|
|
with app.config['DB_TRANSACTION_FACTORY'](db):
|
|
|
|
try:
|
2015-09-15 23:08:31 +00:00
|
|
|
image = (Image
|
|
|
|
.select(Image, ImageStorage)
|
|
|
|
.join(ImageStorage)
|
|
|
|
.where(Image.id == image_id)
|
|
|
|
.get())
|
|
|
|
|
|
|
|
aggregate_size = image.storage.image_size
|
2015-03-16 22:03:17 +00:00
|
|
|
|
|
|
|
image_ids = image.ancestors.split('/')[1:-1]
|
|
|
|
for image_id in image_ids:
|
2015-09-15 23:08:31 +00:00
|
|
|
to_add = db_for_update(Image
|
|
|
|
.select(Image, ImageStorage)
|
|
|
|
.join(ImageStorage)
|
|
|
|
.where(Image.id == image_id)).get()
|
|
|
|
aggregate_size += to_add.storage.image_size
|
|
|
|
|
|
|
|
image.aggregate_size = aggregate_size
|
|
|
|
image.save()
|
2015-03-16 22:03:17 +00:00
|
|
|
except Image.DoesNotExist:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
logging.getLogger('peewee').setLevel(logging.CRITICAL)
|
|
|
|
backfill_aggregate_sizes()
|