def profiled(*args, **kwargs): logger = get_logger(method.__module__) if logger.isEnabledFor(system_logging.DEBUG): msg = "{0}...".format(method.__name__) logger.debug(msg) temp_start_time = time.time() result = method(*args, **kwargs) end_time = time.time() msg = "{0} (duration = {1:2.6f}s)".format(method.__name__, end_time - temp_start_time) logger.debug(msg) else: result = method(*args, **kwargs) return result
import concurrent.futures import datetime as dt from random import shuffle from time import time import barbante.config as config from barbante.maintenance.template_consolidation import consolidate_product_templates from barbante.utils.profiling import profile from barbante.context.context_manager import wrap import barbante.utils.logging as barbante_logging log = barbante_logging.get_logger(__name__) CONSERVATIVE = 0 AGGRESSIVE = 1 MIN_ACCEPTABLE_PP_STRENGTH = 0.0001 def generate_templates(session_context): generate_strengths(session_context) consolidate_product_templates(session_context, collaborative=True, tfidf=False) @profile def generate_strengths(session_context): """ Computes product x product strengths (from scratch) based on the users' activities. It uses the context data proxy to read input data and write the strengths back to the database. :param session_context: The session context.
import barbante.api.cache_stats as cache_stats import barbante.api.clear_cache as clear_cache import barbante.api.recommend as recommend import barbante.api.consolidate_product_templates as consolidate_product_templates import barbante.api.consolidate_user_templates as consolidate_user_templates import barbante.api.get_user_templates as user_templates import barbante.utils.logging as barbante_logging from barbante.config import is_valid_customer_identifier from barbante.context.context_manager import new_context _TRACER_ID_HEADER = "tracerid" # HTTP header is case-insensitive """ The HTTP header carrying the UUID used to identify log messages. """ log = barbante_logging.get_logger("barbante.server.reel") class FutureHandler(tornado.web.RequestHandler): """ Implements the async calls for POST and GET. """ endpoint_pattern = re.compile(r"^/([^/]+)") """ A pattern used for obtaining the endpoint name. """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.hostname = socket.gethostname() def do_get(self, *args):
import sys import os import contextlib import inspect import logging as system_logging import functools from barbante.utils.logging import get_logger from barbante.context.context_manager import global_context_manager as gcm _TRACE_BEGIN = 'B' _TRACE_END = 'E' tracer_logger = get_logger('barbante.utils.logging.trace') def _log(msg, tracer_id, endpoint, method_name, class_name, stage): tracer_logger.info(msg if msg is not None else '', extra={'stage': stage, 'tracerid': tracer_id, 'endpoint': endpoint, 'method_name': method_name, 'class_name': class_name}) def _doublewrap(f): """ A decorator decorator, allowing the decorator to be used as: @decorator(with, arguments, and=kwargs) or @decorator See http://stackoverflow.com/a/14412901/778272 """ @functools.wraps(f) def new_dec(*args, **kwargs):
with new_session(): do_some_task() """ import threading import contextlib import uuid from functools import partial from tornado.stack_context import run_with_stack_context, StackContext from tornado.concurrent import wrap as tornado_wrap from barbante.utils.logging import get_logger log = get_logger(__name__) class RequestContext: """ An object responsible for keeping data that must be globally accessible relative to a certain request. """ def __init__(self, tracer_id: str='UNAVAILABLE', endpoint: str='UNAVAILABLE', environment: str='UNAVAILABLE'): self.tracer_id = tracer_id self.endpoint = endpoint self.environment = environment class GlobalContextManager(threading.local): """ Keeps a stack context for each thread in Barbante.