def apply_tags_to_game(query_params: Dict[str, any] = None, game_id=None, session=None): if query_params is None: return None if 'tags' not in query_params and 'private_tag_keys' not in query_params: return None tags = query_params['tags'] if 'tags' in query_params else [] private_ids = query_params[ 'private_tag_keys'] if 'private_tag_keys' in query_params else [] if len(tags) > 0: player_id = query_params['player_id'] if session.query(Player).filter( Player.platformid == player_id).first() is None: ErrorLogger.log_error(PlayerNotFound()) else: for tag in tags: created_tag = Tag.create(tag, session=session, player_id=player_id) TagWrapper.add_tag_to_game(session, game_id, created_tag.db_tag) for private_id in private_ids: tag_id, private_key = Tag.decode_tag(private_id) tag = TagWrapper.get_tag_by_id(session, tag_id) if tag.private_id is not None and tag.private_id == private_key: TagWrapper.add_tag_to_game(session, game_id, tag)
def get_current_user_id(player_id=None) -> str: if player_id is not None: return player_id try: return UserManager.get_current_user().platformid except Exception as e: ErrorLogger.log_error(e) return ""
def get_redis() -> redis.Redis: """ Tries to get redis. Does a fallback if redis is not able to be grabbed from flask. """ try: from flask import current_app return current_app.config['r'] except Exception as e: ErrorLogger.log_error(e) try: from backend.database.startup import lazy_get_redis return lazy_get_redis() except Exception as e: ErrorLogger.log_error(e)
def api_upload_proto(query_params=None): # Convert to byte files from base64 response = request.get_json() proto_in_memory = io.BytesIO( zlib.decompress(base64.b64decode(response['proto']))) protobuf_game = ProtobufManager.read_proto_out_from_file(proto_in_memory) # Process try: parsed_replay_processing(protobuf_game, query_params=query_params) except Exception as e: ErrorLogger.log_error(e, logger=logger) return jsonify({'Success': True})
def parsed_replay_processing(protobuf_game, query_params: Dict[str, any] = None, preserve_upload_date=True): logger.debug("Successfully parsed replay adding data to DB") # Process if query_params is None: match_exists = add_objects(protobuf_game, preserve_upload_date=preserve_upload_date) return query_params = parse_query_params(upload_file_query_params, query_params, add_secondary=True) if 'player_id' in query_params: protobuf_game.game_metadata.primary_player.id = query_params['player_id'] match_exists = add_objects(protobuf_game, preserve_upload_date=preserve_upload_date) logger.debug("SUCCESS: Added base data to db adding query params") if query_params is None: return if len(query_params) == 0: return try: game_id = protobuf_game.game_metadata.match_guid if game_id == "": game_id = protobuf_game.game_metadata.id except: game_id = None error_counter = [] # Add game visibility option try: apply_game_visibility(query_params=query_params, game_id=game_id, game_exists=match_exists, proto_game=protobuf_game) except CalculatedError as e: error_counter.append('visibility') ErrorLogger.log_error(e, message='Error changing visibility', logger=logger) # Add game visibility option try: apply_tags_to_game(query_params=query_params, game_id=game_id) except CalculatedError as e: error_counter.append('tags') ErrorLogger.log_error(e, message='Error adding tags', logger=logger) if len(error_counter) == 0: logger.debug("SUCCESS: Processed all query params") else: logger.warning( 'Found ' + str(len(error_counter)) + ' errors while processing query params: ' + str(error_counter))
def change_replay_visibility(game_hash: str, visibility: GameVisibilitySetting, user_id='', release_date=None) -> 'ReplayVisibility': can_change = PlayerWrapper.have_permission_to_change_game( game_hash, user_id) if can_change: try: apply_game_visibility_explicit(user_id, visibility, release_date, game_hash) except CalculatedError as e: ErrorLogger.log_error(e) raise e else: ErrorLogger.log_error(AuthorizationException()) return ReplayVisibility(game_hash, visibility)
def setup_metrics(app: Flask): logger.info('Setting up metrics') # provide app's version and deploy environment/config name to set a gauge metric ErrorLogger.add_logging_callback( MetricsHandler.log_exception_for_metrics) MetricsHandler.setup_metrics_callbacks( app, app_version=app.config['VERSION']) if 'prometheus_multiproc_dir' in os.environ: # We're in a multiprocessing environment (i.e. gunicorn) registry = CollectorRegistry() multiprocess.MultiProcessCollector(registry) if os.path.isdir("metrics"): shutil.rmtree("metrics") os.mkdir("metrics") wsgi_app = make_wsgi_app(registry) else: wsgi_app = make_wsgi_app() # Plug metrics WSGI app to your main app with dispatcher app.wsgi_app = DispatcherMiddleware(app.wsgi_app, {"/metrics": wsgi_app})
def parse_replay_wrapper(replay_to_parse_path: str, parsed_data_path: str, failed_dir: str, force_reparse: bool, logger) -> Optional[AnalysisManager]: """ Parses a replay with the given parameters. :param replay_to_parse_path: The path where the replay that is being parsed is stored :param parsed_data_path: The path where the post parsing data will be stored :param failed_dir: The path where we will store a replay that failed to parse :param force_reparse: If true the replay will parse even if it has already been parsed before. :param logger: The logger that is logging the error messages :return: An analysis manager if the replay was successfully parsed. It will return None if the replay has already been parsed. """ # Todo preparse replay ID here to save on extra parsing and on locks. (remember to delete locks older than 1 day) if os.path.isfile(parsed_data_path) and not force_reparse: return # try: try: analysis_manager = analyze_replay_file(replay_to_parse_path) # type: AnalysisManager except Exception as e: if not os.path.isdir(failed_dir): os.makedirs(failed_dir) shutil.move(replay_to_parse_path, os.path.join(failed_dir, os.path.basename(replay_to_parse_path))) with open(os.path.join(failed_dir, os.path.basename(replay_to_parse_path) + '.txt'), 'a') as f: f.write(str(e)) f.write(traceback.format_exc()) raise e try: write_replay_to_disk(analysis_manager, parsed_data_path) except Exception as e: ErrorLogger.log_error(e, logger=logger) with open(os.path.join(failed_dir, os.path.basename(replay_to_parse_path) + '.txt'), 'a') as f: f.write(str(e)) f.write(traceback.format_exc()) return analysis_manager
def get_or_download(item_path, download_lambda, open_lambda): if not os.path.isfile(item_path): try: return download_lambda() except ReplayNotFound as e: ErrorLogger.log_error(e) raise e except Exception as e: ErrorLogger.log_error(e) raise ReplayNotFound() try: return open_lambda(item_path) except Exception as e: ErrorLogger.log_error(e) raise ErrorOpeningGame(str(e))