def find_user_results(): try: archive = utils.get_str_arg(request, 'archive') user = utils.get_str_arg(request, 'user') indices = get_indexes() factors = get_factors() factor_settings = utils.get_factor_settings(user, archive=archive) results = model.load_user_results(factors, indices, factor_settings.get('factors', {})) return jsonify(results) except Exception as ex: logger.info(ex) return jsonify(dict(error=str(ex), traceback=str(traceback.format_exc())))
def find_results_stats(): try: archive = utils.get_str_arg(request, 'archive') current_user = session.get('username') is_admin = current_user == 'admin' indices = get_indexes() factors = get_factors() user_settings = { user: factor_settings for user, factor_settings in utils.get_all_user_factor_settings(locked=False if is_admin else True, archive=archive) } unlocked = [user for user, settings in user_settings.items() if not settings['locked']] user_settings = {user: settings.get('factors', {}) for user, settings in user_settings.items() if settings['locked']} user_results = { user: dict(stats=model.load_results_stats(factors, indices, factor_settings)) for user, factor_settings in user_settings.items() } for user, result in user_results.items(): if is_admin: result['stats']['unlockable'] = True results = dict( users=user_results, samples=dict( stats={k: v for k, v in indices['stats'].items() if k in model.SAMPLE_INDEXES}, ), unlocked=unlocked, archives=utils.find_available_archives() if is_admin else [] ) return jsonify(results) except Exception as ex: logger.info(ex) return jsonify(dict(error=str(ex), traceback=str(traceback.format_exc())))
def archive_user_settings(): try: tag = utils.get_str_arg(request, 'tag') utils.archive_all_user_factor_settings(tag) return jsonify(dict(success=True)) except Exception as ex: logger.info(ex) return jsonify(dict(error=str(ex), traceback=str(traceback.format_exc())))
def find_cumulative_returns(): try: factors = get_factors() indices = get_indexes() archive = utils.get_str_arg(request, 'archive') user = utils.get_str_arg(request, 'user') sample_indexes = utils.get_str_arg(request, 'samples') factor_settings = utils.get_factor_settings(user, archive=archive) cum_returns = {user: model.load_cumulative_returns(factors, indices, factor_settings['factors'])} if sample_indexes is not None: for sample in sample_indexes.split(','): sample_rets = indices['returns']['cumulative'][sample] sample_rets.name = 'val' sample_rets.index.name = 'date' cum_returns[sample] = sample_rets.reset_index().to_dict(orient='records') return jsonify(cum_returns) except Exception as ex: logger.info(ex) return jsonify(dict(error=str(ex), traceback=str(traceback.format_exc())))
def unlock_factor_settings(): try: user = utils.get_str_arg(request, 'user') factor_settings = utils.get_factor_settings(user) factor_settings['locked'] = False utils.dump_factor_settings(user, factor_settings) return jsonify(utils.dict_merge(utils.get_user_counts(), dict(success=True))) except Exception as ex: logger.info(ex) return jsonify(dict(error=str(ex), traceback=str(traceback.format_exc())))
def save_factor_settings(): try: user = session.get('username') factor_settings = utils.get_str_arg(request, 'factor_settings') factor_settings = json.loads(factor_settings) if factor_settings else {} session['factor_settings'] = dict(factors=factor_settings, locked=False) utils.dump_factor_settings(user, session['factor_settings']) return jsonify(session['factor_settings']) except Exception as ex: logger.info(ex) return jsonify(dict(error=str(ex), traceback=str(traceback.format_exc())))
def find_summary_data(): try: factors = get_factors() archive = utils.get_str_arg(request, 'archive') current_user = session.get('username') is_admin = current_user == 'admin' summary = {} for factor_id, factor in factors.items(): summary[factor_id] = dict( label=factor['label'], selections=dict(HI=[], LO=[]), avg=dict(HI=0, LO=0), ethical_wt=dict(HI=0, LO=0), reason_avg=dict(HI={}, LO={}), ) users = list(utils.get_all_user_factors(archive=archive)) for user, factor_settings in users: for factor_id, inputs in factor_settings.items(): summary[factor_id]['selections'][inputs['strength']].append(utils.dict_merge(inputs, dict(user=user))) total_users = len(users) for factor in summary.values(): for strength, selections in factor['selections'].items(): factor['avg'][strength] = (sum(map(itemgetter('weight'), selections)) * 1.0) / total_users reason_avg = defaultdict(int) total_reason_users = len(selections) for s in selections: for r_id in s['reasons']: reason_avg[r_id] += 1 factor['reason_avg'][strength] = { r_id: ((total * 1.0) / total_reason_users) * 100 for r_id, total in reason_avg.items() } return jsonify(dict( data=summary, archives=utils.find_available_archives() if is_admin else [] )) except Exception as ex: logger.info(ex) return jsonify(dict(error=str(ex), traceback=str(traceback.format_exc())))
def clear_cache(): cache_name = utils.get_str_arg(request, 'cache') if cache_name: cache.clear_cache(cache_name) return jsonify(results=[])
def find_factor_data(): factor_id = utils.get_str_arg(request, 'factor') factor_data = get_factors().get(factor_id, {}) return jsonify(factor_data)