# Fixes usercache processing
# If there are any errors in the usercache processing, fix them and reload the data
# Basic flow
# - Copy data back to user cache
# - Attempt to moveToLongTerm
# - Find errors
# - Fix errors
# - Repeat until no errors are found
import sys
import logging
logging.basicConfig(level=logging.DEBUG)

import uuid
import datetime as pydt
import json
import bson.json_util as bju

import emission.core.get_database as edb
import emission.net.usercache.abstract_usercache_handler as euah
import emission.net.usercache.abstract_usercache as enua

def fix_usercache_errors():
    copy_to_usercache()
    move_to_long_term()
    
def copy_to_usercache():       
    # Step 1: Copy data back to user cache
    error_it = edb.get_timeseries_error_db().find()
    logging.info("Found %d errors in this round" % error_it.count())
    for error in error_it:
        logging.debug("Copying entry %s" % error["metadata"])
        save_result = edb.get_usercache_db().save(error)
        remove_result = edb.get_timeseries_error_db().remove(error["_id"])    
        logging.debug("save_result = %s, remove_result = %s" % (save_result, remove_result))
    logging.info("step copy_to_usercache DONE")
    
def move_to_long_term():
    cache_uuid_list = enua.UserCache.get_uuid_list()
    logging.info("cache UUID list = %s" % cache_uuid_list)

    for uuid in cache_uuid_list:
        logging.info("*" * 10 + "UUID %s: moving to long term" % uuid + "*" * 10)
        uh = euah.UserCacheHandler.getUserCacheHandler(uuid)
        uh.moveToLongTerm()


if __name__ == '__main__':
    fix_usercache_errors()

Check in a script to debug usercache processing
Check in a script to debug usercache processing

Currently, if there are errors in processing the usercache, we store the
erroneous values into a separate usercache error database. But then, we want to
evaluate the errors and fix them if they reflect bugs. This is a script that
allows us to do that.

It basically copies the values back to the usercache and reruns the new move to
long term code on them. The idea is that you can rerun until you get zero
errors, or at least, zero fixable errors.
