Ejemplo n.º 1
0
    def from_problem_logs(self):
        problem_log_query = ProblemLog.all()
        problem_logs = problem_log_query.fetch(1000)

        problem_logs.sort(key=lambda log: log.time_taken)
        grouped = dict((k, sum(1 for i in v))
                       for (k, v)
                       in groupby(problem_logs,
                                  key=lambda log: log.time_taken))
        return self.render_hist(grouped)
Ejemplo n.º 2
0
def fancy_stats_deferred(exid, start_dt, end_dt, cursor, uid, i):
    key_name = ExerciseStatisticShard.make_key(exid, start_dt, end_dt, cursor)
    if cursor and ExerciseStatisticShard.get_by_key_name(key_name):
        # We've already run, die.
        return

    query = ProblemLog.all()
    query.filter('exercise =', exid)
    query.filter('correct =', True)
    query.filter('time_done >=', start_dt)
    query.filter('time_done <', end_dt)
    query.order('-time_done')

    if cursor:
        query.with_cursor(cursor)

    problem_logs = query.fetch(1000)
    if len(problem_logs) > 0:
        logging.info("processing %d logs for %s" % (len(problem_logs), exid))

        stats = fancy_stats_from_logs(problem_logs)
        pickled = pickle_util.dump(stats)

        shard = ExerciseStatisticShard(
            key_name=key_name,
            exid=exid,
            start_dt=start_dt,
            end_dt=end_dt,
            cursor=cursor,
            blob_val=pickled)
        shard.put()

        enqueue_task(exid, start_dt, end_dt, query.cursor(), uid, i + 1)
    else:
        # No more problem logs left to process
        logging.info("Summing all stats for %s", exid)

        all_stats = fancy_stats_shard_reducer(exid, start_dt, end_dt)

        model = ExerciseStatistic(
            key_name=ExerciseStatistic.make_key(exid, start_dt, end_dt),
            exid=exid,
            start_dt=start_dt,
            end_dt=end_dt,
            blob_val=pickle_util.dump(all_stats),
            log_count=all_stats['log_count'])
        model.put()

        logging.info("done processing %d logs for %s",
                     all_stats['log_count'], exid)
Ejemplo n.º 3
0
    def get(self):
        if not hasattr(secrets, 'ka_api_consumer_key') or    \
           not hasattr(secrets, 'ka_api_consumer_secret') or \
           not hasattr(secrets_dev, 'ka_api_token_key') or   \
           not hasattr(secrets_dev, 'ka_api_token_secret'):
            return self.redirect("/")

        self.setup_oauth()

        self.email = self.request_string("email")
        if not self.email:
            raise "Must supply email for user to import"

        params = copy.copy(self._default_kinds)
        params.update(self.request.params)

        # get proper user from addition 1 userexercise
        user_id_json = json.loads(
            self.api("/api/v1/user/exercises/addition_1"))
        user = users.User(user_id_json['user'])

        # UserData
        user_data_json_raw = self.api("/api/v1/user")
        user_data = UserData.from_json(json.loads(user_data_json_raw),
                                       user=user)
        self.output('user_data', user_data, user_data_json_raw)
        user_data.put()

        if 'UserVideo' in params:
            user_videos_json = json.loads(self.api("/api/v1/user/videos"))
            user_videos = []
            for user_video_json in user_videos_json[:params['UserVideo']]:
                user_video = UserVideo.from_json(user_video_json,
                                                 user_data=user_data)
                user_videos.append(user_video)
                self.output('user_video', user_video, jsonify(user_video_json))

            video_logs = defaultdict(list)
            if 'VideoLog' in params:
                for user_video in user_videos:
                    ytid = user_video.video.youtube_id
                    video_logs_json = json.loads(
                        self.api("/api/v1/user/videos/%s/log" % ytid))
                    problem_log_limit = params['ProblemLog']
                    for video_log_json in video_logs_json[:problem_log_limit]:
                        video_log = VideoLog.from_json(video_log_json,
                                                       user_video.video, user)
                        video_logs[user_video].append(video_log)
                        self.output("video_log", video_log,
                                    jsonify(video_log_json))

                # delete old video logs
                query = VideoLog.all(keys_only=True)
                query.filter('user ='******'UserExercise' in params:
            user_exercises_json = json.loads(
                self.api("/api/v1/user/exercises"))
            user_exercises = []
            exercise_limit = params['UserExercise']
            for user_exercise_json in user_exercises_json[:exercise_limit]:
                user_exercise = UserExercise.from_json(user_exercise_json,
                                                       user_data)
                if user_exercise:
                    user_exercises.append(user_exercise)
                    self.output("user_exercise", user_exercise,
                                jsonify(user_exercise_json))

            problem_logs = defaultdict(list)
            if 'ProblemLog' in params:
                for user_exercise in user_exercises:
                    problem_logs_json = json.loads(
                        self.api("/api/v1/user/exercises/%s/log" %
                                 user_exercise.exercise))
                    problem_log_limit = params['ProblemLog']
                    problem_logs_json = problem_logs_json[:problem_log_limit]
                    for problem_log_json in problem_logs_json:
                        problem_log = ProblemLog.from_json(
                            problem_log_json,
                            user_data=user_data,
                            exercise=user_exercise.exercise_model)
                        problem_logs[user_exercise].append(problem_log)
                        self.output("problem_log", problem_log,
                                    jsonify(problem_log_json))

            db.put(user_exercises)
            for k, v in problem_logs.iteritems():
                db.put(v)

        if 'Goal' in params:
            with AutoNowDisabled(Goal):
                goals_json = json.loads(self.api("/api/v1/user/goals"))
                goals = []
                for goal_json in goals_json[:params['Goal']]:
                    goal = Goal.from_json(goal_json, user_data=user_data)
                    goals.append(goal)
                    self.output("goal", goal, jsonify(goal_json))

                db.put(goals)

                # need to tell the userdata that it has goals
                user_data.has_current_goals = not all(
                    [g.completed for g in goals])
                user_data.put()
Ejemplo n.º 4
0
    def get(self):
        if (
            not hasattr(secrets, "ka_api_consumer_key")
            or not hasattr(secrets, "ka_api_consumer_secret")
            or not hasattr(secrets_dev, "ka_api_token_key")
            or not hasattr(secrets_dev, "ka_api_token_secret")
        ):
            return self.redirect("/")

        self.setup_oauth()

        self.email = self.request_string("email")
        if not self.email:
            raise "Must supply email for user to import"

        params = copy.copy(self._default_kinds)
        params.update(self.request.params)

        # get proper user from addition 1 userexercise
        user_id_json = json.loads(self.api("/api/v1/user/exercises/addition_1"))
        user = users.User(user_id_json["user"])

        # UserData
        user_data_json_raw = self.api("/api/v1/user")
        user_data = UserData.from_json(json.loads(user_data_json_raw), user=user)
        self.output("user_data", user_data, user_data_json_raw)
        user_data.put()

        if "UserVideo" in params:
            user_videos_json = json.loads(self.api("/api/v1/user/videos"))
            user_videos = []
            for user_video_json in user_videos_json[: params["UserVideo"]]:
                user_video = UserVideo.from_json(user_video_json, user_data=user_data)
                user_videos.append(user_video)
                self.output("user_video", user_video, jsonify(user_video_json))

            video_logs = defaultdict(list)
            if "VideoLog" in params:
                for user_video in user_videos:
                    ytid = user_video.video.youtube_id
                    video_logs_json = json.loads(self.api("/api/v1/user/videos/%s/log" % ytid))
                    problem_log_limit = params["ProblemLog"]
                    for video_log_json in video_logs_json[:problem_log_limit]:
                        video_log = VideoLog.from_json(video_log_json, user_video.video, user)
                        video_logs[user_video].append(video_log)
                        self.output("video_log", video_log, jsonify(video_log_json))

                # delete old video logs
                query = VideoLog.all(keys_only=True)
                query.filter("user ="******"UserExercise" in params:
            user_exercises_json = json.loads(self.api("/api/v1/user/exercises"))
            user_exercises = []
            exercise_limit = params["UserExercise"]
            for user_exercise_json in user_exercises_json[:exercise_limit]:
                user_exercise = UserExercise.from_json(user_exercise_json, user_data)
                if user_exercise:
                    user_exercises.append(user_exercise)
                    self.output("user_exercise", user_exercise, jsonify(user_exercise_json))

            problem_logs = defaultdict(list)
            if "ProblemLog" in params:
                for user_exercise in user_exercises:
                    problem_logs_json = json.loads(self.api("/api/v1/user/exercises/%s/log" % user_exercise.exercise))
                    problem_log_limit = params["ProblemLog"]
                    problem_logs_json = problem_logs_json[:problem_log_limit]
                    for problem_log_json in problem_logs_json:
                        problem_log = ProblemLog.from_json(
                            problem_log_json, user_data=user_data, exercise=user_exercise.exercise_model
                        )
                        problem_logs[user_exercise].append(problem_log)
                        self.output("problem_log", problem_log, jsonify(problem_log_json))

            db.put(user_exercises)
            for k, v in problem_logs.iteritems():
                db.put(v)

        if "Goal" in params:
            with AutoNowDisabled(Goal):
                goals_json = json.loads(self.api("/api/v1/user/goals"))
                goals = []
                for goal_json in goals_json[: params["Goal"]]:
                    goal = Goal.from_json(goal_json, user_data=user_data)
                    goals.append(goal)
                    self.output("goal", goal, jsonify(goal_json))

                db.put(goals)

                # need to tell the userdata that it has goals
                user_data.has_current_goals = not all([g.completed for g in goals])
                user_data.put()