def update_stopstalk_rating(user_id, user_submissions, custom): global commit_to_db_counter final_rating = utilities.get_stopstalk_user_stats(user_submissions)["rating_history"] final_rating = dict(final_rating) if final_rating == {}: print user_id, custom, "No submissions" return atable = db.auth_user cftable = db.custom_friend today = str(datetime.datetime.now().date()) current_rating = sum(final_rating[today]) print user_id, custom, current_rating update_params = dict(stopstalk_rating=int(current_rating)) if custom: cftable(user_id).update_record(**update_params) else: atable(user_id).update_record(**update_params) commit_to_db_counter += 1 if commit_to_db_counter > 10: # Don't hold the records in memory, keep commiting after every 10 records # to avoid the lock wait timeouts db.commit() commit_to_db_counter = 0
def update_stopstalk_rating(user_id, stopstalk_handle, custom): atable = db.auth_user cftable = db.custom_friend column_name = "custom_user_id" if custom else "user_id" query = """ SELECT time_stamp, problem_link, status, site, problem_id FROM submission WHERE %(column_name)s = %(user_id)d ORDER BY time_stamp """ % { "column_name": column_name, "user_id": user_id } all_submissions = db.executesql(query) user_submissions = [] for submission in all_submissions: user_submissions.append({ column_name: user_id, "time_stamp": submission[0], "problem_link": submission[1], "status": submission[2], "site": submission[3], "problem_id": submission[4] }) final_rating = utilities.get_stopstalk_user_stats( stopstalk_handle, custom, user_submissions)["rating_history"] final_rating = dict(final_rating) today = str(datetime.datetime.now().date()) current_rating = int(sum(final_rating[today])) update_params = dict(stopstalk_rating=current_rating) if custom: cftable(user_id).update_record(**update_params) else: current.REDIS_CLIENT.delete( utilities.get_user_record_cache_key(user_id)) atable(user_id).update_record(**update_params) db.commit() # Update global leaderboard cache current_value = current.REDIS_CLIENT.get("global_leaderboard_cache") if current_value is None: # Global leaderboard cache not present return current_rating import json current_value = json.loads(current_value) for row in current_value: if row[1] == stopstalk_handle: row[3] = current_rating current_value = reorder_leaderboard_data(current_value) current.REDIS_CLIENT.set("global_leaderboard_cache", json.dumps(current_value), ex=1 * 60 * 60) return current_rating
def get_stopstalk_user_stats(): if request.extension != "json": raise HTTP(400) return user_id = request.vars.get("user_id", None) custom = request.vars.get("custom", None) final_data = dict(rating_history=[], curr_accepted_streak=0, max_accepted_streak=0, curr_day_streak=0, max_day_streak=0, solved_counts={}, status_percentages=[], site_accuracies={}, solved_problems_count=0, total_problems_count=0, calendar_data={}) if user_id is None or custom is None: return final_data user_id = int(user_id) custom = (custom == "True") stopstalk_handle = utilities.get_stopstalk_handle(user_id, custom) redis_cache_key = "profile_page:user_stats_" + stopstalk_handle # Check if data is present in REDIS data = current.REDIS_CLIENT.get(redis_cache_key) if data: result = json.loads(data) if not auth.is_logged_in(): del result["rating_history"] return result stable = db.submission query = (stable["custom_user_id" if custom else "user_id"] == user_id) rows = db(query).select(stable.time_stamp, stable.problem_link, stable.problem_id, stable.status, stable.site, orderby=stable.time_stamp) # Returns rating history, accepted & max streak (day and accepted), result = utilities.get_stopstalk_user_stats(rows.as_list()) if auth.is_logged_in(): current.REDIS_CLIENT.set(redis_cache_key, json.dumps(result, separators=(",", ":")), ex=1 * 60 * 60) else: del result["rating_history"] return result
def update_stopstalk_rating(user_id, stopstalk_handle, custom): atable = db.auth_user cftable = db.custom_friend column_name = "custom_user_id" if custom else "user_id" query = """ SELECT time_stamp, problem_link, status, site, problem_id FROM submission WHERE %(column_name)s = %(user_id)d ORDER BY time_stamp """ % { "column_name": column_name, "user_id": user_id } all_submissions = db.executesql(query) user_submissions = [] for submission in all_submissions: user_submissions.append({ column_name: user_id, "time_stamp": submission[0], "problem_link": submission[1], "status": submission[2], "site": submission[3], "problem_id": submission[4] }) final_rating = utilities.get_stopstalk_user_stats( stopstalk_handle, custom, user_submissions)["rating_history"] final_rating = dict(final_rating) today = str(datetime.datetime.now().date()) current_rating = int(sum(final_rating[today])) update_params = dict(stopstalk_rating=current_rating) record = None if custom: record = cftable(user_id) else: record = atable(user_id) current.REDIS_CLIENT.delete( utilities.get_user_record_cache_key(user_id)) record.update_record(**update_params) db.commit() if custom == True: # Don't need to do anything on global_leaderboard_cache if custom is true return current_rating # Update global leaderboard cache current_value = current.REDIS_CLIENT.get(GLOBAL_LEADERBOARD_CACHE_KEY) if current_value is None: # Global leaderboard cache not present return current_rating import json current_value = json.loads(current_value) reorder_leaderboard = False for row in current_value: if row[1] == stopstalk_handle: row[3] = current_rating reorder_leaderboard = True break if not reorder_leaderboard: reorder_leaderboard = True cf_count = db(cftable.user_id == record.id).count() current_value.append( (record.first_name + " " + record.last_name, record.stopstalk_handle, record.institute, record.stopstalk_rating, float(record.per_day_change), utilities.get_country_details(record.country), cf_count, 0)) current_value = reorder_leaderboard_data(current_value) current.REDIS_CLIENT.set(GLOBAL_LEADERBOARD_CACHE_KEY, json.dumps(current_value), ex=ONE_HOUR) return current_rating