# Path: edx_data_research/parsing/parse.py
# class Parse(Base):
#     __metaclass__ = abc.ABCMeta
#     
#     def __init__(self, args):
# 	super(Parse, self).__init__(args)
# 
#     @abc.abstractmethod
#     def migrate(self):
#         """Migrate data to database"""
# 	pass


from edx_data_research.parsing.parse import Parse

class ProblemIds(Parse):

    def __init__(self, args):
        super(ProblemIds, self).__init__(args)
        self.drop = args.drop
        
    def migrate(self):
        self.collections = ['problem_ids', 'tracking', 'user_id_map']
        if self.drop:
            self.collections['problem_ids'].drop()
        tracking_cursor = (self.collections['tracking']
                           .find({'event_type' : 'problem_check',
                           'event_source' : 'server'}))
        for item in tracking_cursor:
            exists = (self.collections['problem_ids'].find({'_id': item['_id']})
                      .limit(1))
            if not exists.count(True):
                document = {'_id': item['_id']}
                username = item['username']
                try:
                    username = int(username)
                except ValueError:
                    pass
                document['username'] = username
                user_id_map = (self.collections['user_id_map']
                               .find_one({'username' : username}))
                    if not user_id_map:
                        print "Username {0} not found in collection user_id_map".format(username)
                        continue

# Path: edx_data_research/parsing/parse.py
# class Parse(Base):
#     __metaclass__ = abc.ABCMeta
#     
#     def __init__(self, args):
# 	super(Parse, self).__init__(args)
# 
#     @abc.abstractmethod
#     def migrate(self):
#         """Migrate data to database"""
# 	pass


# Path: edx_data_research/parsing/parse_problem_ids.py
from edx_data_research.parsing.parse import Parse


class ProblemIds(Parse):

    def __init__(self, args):
        super(ProblemIds, self).__init__(args)
        self.drop = args.drop
        
    def migrate(self):
        self.collections = ['problem_ids', 'tracking', 'user_id_map']
        if self.drop:
            self.collections['problem_ids'].drop()
        tracking_cursor = (self.collections['tracking']
                           .find({'event_type' : 'problem_check',
                           'event_source' : 'server'}))
        for item in tracking_cursor:
            exists = (self.collections['problem_ids'].find({'_id': item['_id']})
                      .limit(1))
            if not exists.count(True):
                document = {'_id': item['_id']}
                username = item['username']
                try:
                    username = int(username)
                except ValueError:
                    pass
                document['username'] = username
                user_id_map = (self.collections['user_id_map']
                               .find_one({'username' : username}))
                    if not user_id_map:
                        print "Username {0} not found in collection user_id_map".format(username)
                        continue

                document['user_id'] = user_id_map['id']