# Path: app/models.py
# class RepoStarring(models.Model):
#     user_id = models.IntegerField()
#     user_username = models.CharField(max_length=39)
#     repo_id = models.IntegerField()
#     repo_full_name = models.CharField(max_length=140)
#     starred_at = models.DateTimeField()
# 
#     class Meta:
#         unique_together = (('user_id', 'repo_id'),)
# 
#     def __str__(self):
#         return '@{0} starred {1}'.format(self.user_username, self.repo_full_name)
# 
#     @staticmethod
#     def create_one(user_dict, repo_dict):
#         rs = RepoStarring()
#         try:
#             rs.user_id = user_dict['id']
#             rs.user_username = user_dict['login']
#             rs.repo_id = repo_dict['id']
#             rs.repo_full_name = repo_dict['full_name']
#             rs.starred_at = repo_dict['starred_at']
#         except (KeyError, TypeError) as e:
#             print(e)
#             print(user_dict)
#             print(repo_dict)
#             return
# 
#         try:
#             rs.save()
#         except IntegrityError:
#             pass


import logging
import pickle
import numpy as np
import pandas as pd
from app.models import RepoStarring
def prepare_user_item_df(min_stargazers_count):
    repos = RepoStarring.objects \
        .filter(stargazers_count__gte=min_stargazers_count) \
        .values_list('repo_full_name', flat=True) \
        .order_by('repo_full_name') \
        .distinct()
    repos_array = np.fromiter(repos.iterator(), np.dtype('U140'))
    repos_array.shape
    n_repos = repos_array.shape[0]

    users = RepoStarring.objects \
        .filter(stargazers_count__gte=min_stargazers_count) \
        .values_list('from_username', flat=True) \
        .order_by('from_username') \
        .distinct()
    users_array = np.fromiter(users.iterator(), np.dtype('U39'))
    users_array.shape
    n_users = users_array.shape[0]

    logger.info('Build the utility matrix')
    logger.info('The number of users: {0}'.format(n_users))
    logger.info('The number of items: {0}'.format(n_repos))

    filename = 'caches/df-{0}x{1}.pickle'.format(n_users, n_repos)
    try:
        user_item_df = pickle.load(open(filename, 'rb'))
    except IOError:
        shape = (n_users, n_repos)
        matrix = np.zeros(shape, dtype=np.int8)
        for i, username in enumerate(users_array):

# Path: app/models.py
# class RepoStarring(models.Model):
#     user_id = models.IntegerField()
#     user_username = models.CharField(max_length=39)
#     repo_id = models.IntegerField()
#     repo_full_name = models.CharField(max_length=140)
#     starred_at = models.DateTimeField()
# 
#     class Meta:
#         unique_together = (('user_id', 'repo_id'),)
# 
#     def __str__(self):
#         return '@{0} starred {1}'.format(self.user_username, self.repo_full_name)
# 
#     @staticmethod
#     def create_one(user_dict, repo_dict):
#         rs = RepoStarring()
#         try:
#             rs.user_id = user_dict['id']
#             rs.user_username = user_dict['login']
#             rs.repo_id = repo_dict['id']
#             rs.repo_full_name = repo_dict['full_name']
#             rs.starred_at = repo_dict['starred_at']
#         except (KeyError, TypeError) as e:
#             print(e)
#             print(user_dict)
#             print(repo_dict)
#             return
# 
#         try:
#             rs.save()
#         except IntegrityError:
#             pass


# Path: app/utils_repo.py
import logging
import pickle
import numpy as np
import pandas as pd
from app.models import RepoStarring

def prepare_user_item_df(min_stargazers_count):
    repos = RepoStarring.objects \
        .filter(stargazers_count__gte=min_stargazers_count) \
        .values_list('repo_full_name', flat=True) \
        .order_by('repo_full_name') \
        .distinct()
    repos_array = np.fromiter(repos.iterator(), np.dtype('U140'))
    repos_array.shape
    n_repos = repos_array.shape[0]

    users = RepoStarring.objects \
        .filter(stargazers_count__gte=min_stargazers_count) \
        .values_list('from_username', flat=True) \
        .order_by('from_username') \
        .distinct()
    users_array = np.fromiter(users.iterator(), np.dtype('U39'))
    users_array.shape
    n_users = users_array.shape[0]

    logger.info('Build the utility matrix')
    logger.info('The number of users: {0}'.format(n_users))
    logger.info('The number of items: {0}'.format(n_repos))

    filename = 'caches/df-{0}x{1}.pickle'.format(n_users, n_repos)
    try:
        user_item_df = pickle.load(open(filename, 'rb'))
    except IOError:
        shape = (n_users, n_repos)
        matrix = np.zeros(shape, dtype=np.int8)
        for i, username in enumerate(users_array):

            user_starred = RepoStarring.objects \