def recompute_for_users(): """ recomputes only those caches that are already in the table and belong to a user. if multiple users have the same preferences the computation is donne only for the first because this is how recompute_recommender_cache_if_needed does. To think about: - what happens when this script is triggered simultaneously with triggering recompute_recommender_cache_if_needed from the UI? will there end up be duplicated recommendations? should we add a uninque constraint on (hash x article)? Note: in theory, the recomputing should be doable independent of users in practice, the recompute_recommender_cache takes the user as input. for that function to become independent of the user we need to be able to recover the ids of the languages, topics, searchers, etc. from the content_hash to do this their ids would need to be comma separated OTOH, in the future we might still want to have a per-user cache because the recommendations might be different for each user since every user has different language levels!!! :param existing_hashes: :return: """ already_done = [] for user_id in User.all_recent_user_ids(): try: user = User.find_by_id(user_id) reading_pref_hash = reading_preferences_hash(user) if reading_pref_hash not in already_done: recompute_recommender_cache_if_needed(user, session) zeeguu_core.log_n_print( f"Success for {reading_pref_hash} and {user}") already_done.append(reading_pref_hash) else: zeeguu_core.log_n_print( f"nno need to do for {user}. hash {reading_pref_hash} already done" ) except Exception as e: zeeguu_core.log_n_print(f"Failed for user {user}")
#!/usr/bin/env python """ Script that lists recent users To be called from a cron job. """ from zeeguu_core.model import User for user_id in User.all_recent_user_ids(): user = User.find_by_id(user_id) print(user.name) print(user.email)
def active_users_today(): from zeeguu_core.model import User return len(User.all_recent_user_ids(days=1))