def parsed_replay_processing(protobuf_game, query_params: Dict[str, any] = None, preserve_upload_date=True) -> bool: logger.debug("Successfully parsed replay adding data to DB") # Process if query_params is None or len(query_params) == 0: match_exists = add_objects(protobuf_game, preserve_upload_date=preserve_upload_date) return match_exists 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 or len(query_params) == 0: return match_exists 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)) return match_exists
def get_current_user_id(player_id=None) -> str: if player_id is not None: return player_id try: user = UserManager.get_current_user() if user is None: return "" return 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_admin_get_logs(query_params=None): search = None if 'search' in query_params and query_params['search'] != "": search = query_params['search'] return jsonify( ErrorLogger.get_logs(query_params['page'], query_params['limit'], search))
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) 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 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 parse_replay_wrapper(replay_to_parse_path: str, parsed_data_path: str, failed_dir: str, force_reparse: bool, logger, query_params=None) -> 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()) payload = { 'replay_uuid': os.path.basename(replay_to_parse_path), 'error_type': type(e).__name__, 'stack': traceback.format_exc(), 'game_hash': None } ErrorLogger.log_replay_error(payload, query_params) 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), game_data=item_path)
def api_upload_proto(session=None, query_params=None): # Convert to byte files from base64 payload = request.get_json() proto_in_memory = io.BytesIO(zlib.decompress(base64.b64decode(payload['proto']))) protobuf_game = ProtobufManager.read_proto_out_from_file(proto_in_memory) # Process try: match_exists = parsed_replay_processing(protobuf_game, query_params=query_params) add_saved_replay(match_exists) except Exception as e: payload['stack'] = traceback.format_exc() payload['error_type'] = type(e).__name__ ErrorLogger.log_replay_error(payload, query_params, proto_game=protobuf_game) ErrorLogger.log_error(e, logger=logger) return jsonify({'Success': False}) ErrorLogger.log_replay(payload, query_params, proto_game=protobuf_game) return jsonify({'Success': True})
def api_upload_proto_error(query_params=None): payload = request.get_json() ErrorLogger.log_replay_error(payload, query_params) return jsonify({'Success': True})