<s> import datetime <EOL> from django . core . management . base import BaseCommand <EOL> from django . db import connection <EOL> import commonware . log <EOL> from olympia . stats . models import ( <EOL> ThemeUpdateCount , ThemeUpdateCountBulk , ThemeUserCount ) <EOL> log = commonware . log . getLogger ( '<STR_LIT>' ) <EOL> class Command ( BaseCommand ) : <EOL> """<STR_LIT>""" <EOL> help = __doc__ <EOL> def handle ( self , * args , ** options ) : <EOL> start = datetime . datetime . now ( ) <EOL> today = datetime . date . today ( ) <EOL> yesterday = today - datetime . timedelta ( days = <NUM_LIT:1> ) <EOL> last_week_avgs = ThemeUpdateCount . objects . get_range_days_avg ( <EOL> yesterday - datetime . timedelta ( days = <NUM_LIT:6> ) , <EOL> yesterday , <EOL> '<STR_LIT>' ) <EOL> prev_3_weeks_avgs = dict ( ThemeUpdateCount . objects . get_range_days_avg ( <EOL> yesterday - datetime . timedelta ( days = <NUM_LIT> ) , <EOL> yesterday - datetime . timedelta ( days = <NUM_LIT:7> ) ) ) <EOL> temp_update_counts = [ ] <EOL> theme_user_counts = [ ] <EOL> for addon_id , persona_id , popularity in last_week_avgs : <EOL> if not persona_id or not addon_id : <EOL> continue <EOL> prev_3_weeks_avg = prev_3_weeks_avgs . get ( addon_id , <NUM_LIT:0> ) <EOL> theme_update_count_bulk = ThemeUpdateCountBulk ( <EOL> persona_id = persona_id , <EOL> popularity = popularity , <EOL> movers = <NUM_LIT:0> ) <EOL> if popularity > <NUM_LIT:100> and prev_3_weeks_avg > <NUM_LIT:1> : <EOL> theme_update_count_bulk . movers = ( <EOL> popularity - prev_3_weeks_avg ) / prev_3_weeks_avg <EOL> theme_user_count = ThemeUserCount ( <EOL> addon_id = addon_id , <EOL> count = popularity , <EOL> date = today <EOL> ) <EOL> temp_update_counts . append ( theme_update_count_bulk ) <EOL> theme_user_counts . append ( theme_user_count ) <EOL> ThemeUpdateCountBulk . objects . all ( ) . delete ( ) <EOL> ThemeUpdateCountBulk . objects . bulk_create ( temp_update_counts , <NUM_LIT:100> ) <EOL> ThemeUserCount . objects . bulk_create ( theme_user_counts , <NUM_LIT:100> ) <EOL> raw_query = """<STR_LIT>""" <EOL> cursor = connection . cursor ( ) <EOL> cursor . execute ( raw_query ) <EOL> log . debug ( '<STR_LIT>' % ( <EOL> datetime . datetime . now ( ) - start ) ) </s>
