def main(argv): if len(argv) < 2: msg = "You must specify the environment, " \ "the product id (or --all, or --resume) and the number of days (or --complete) if using --all." log.error(msg) return {"success": False, "message": msg} try: # command-line arguments env = argv[0] product_id = argv[1] session = init_session(env) if product_id == "--all": if argv[2] == "--complete": days = None else: days = int(argv[2]) maintenance.process_products(session, days) elif product_id == "--resume": maintenance.process_products(session, resume=True) else: if len(argv) == 3 and argv[2] == '--force': force = True else: force = False maintenance.process_product(session, product_id, force_update=force) except Exception: log.exception('Exception on {0}:'.format(__name__)) return {"success": False, "message": traceback.format_exc()} return {"success": True}
def main(argv): if len(argv) < 5: msg = "You must specify the environment, the external_user_id, " \ "the external_product_id, the activity type and the activity date" log.error(msg) return {"success": False, "message": msg} try: # command-line arguments env = argv[0] user = argv[1] product = argv[2] activity_type = argv[3] activity_date = dateutil.parser.parse(argv[4]) activity = {"external_user_id": user, "external_product_id": product, "activity": activity_type, "created_at": activity_date} session = init_session(env) maintenance.update_collaborative_filtering_strengths(session, activity) return {"success": True} except Exception: log.exception('Exception on {0}:'.format(__name__)) return {"success": False, "message": traceback.format_exc()}
def main(argv): if len(argv) < 2: msg = "You must specify the environment and the document id" log.error(msg) return {"success": False, "message": msg} try: # command-line arguments env = argv[0] product = json.loads(argv[1]) product_id = product.get("external_id") if product_id is None: msg = "Product has no external_id" log.error(msg) return {"success": False, "message": msg} session = init_session(env) maintenance.process_product(session, product_id, product=product, force_update=True) except Exception: log.exception('Exception on {0}:'.format(__name__)) return {"success": False, "message": traceback.format_exc()} return {"success": True}
def recommend_products_all_users(env, count_recommendations, algorithm): result = {} session = init_session(env) all_users = session.data_proxy.fetch_all_user_ids() done = 0 for user_id in all_users: session = init_session(env, user_id, algorithm=algorithm) recommender = session.get_recommender() recommendations = recommender.recommend(count_recommendations) result[user_id] = recommendations done += 1 if done % 1000 == 0: log.debug("Processed %d users." % done) return result
def main(argv): if len(argv) < 1: msg = "You must specify the environment" log.error(msg) return {"success": False, "message": msg} try: # command-line arguments env = argv[0] session = init_session(env) session.data_proxy.ensure_indexes() return {"success": True} except Exception: log.exception('Exception on {0}:'.format(__name__)) return {"success": False, "message": traceback.format_exc()}
def main(argv): if len(argv) < 1: msg = "You must specify the environment" log.error(msg) return {"success": False, "message": msg} try: # command-line arguments env = argv[0] session = init_session(env) pttfidf.generate_templates(session) return {"success": True} except Exception: log.exception('Exception on {0}:'.format(__name__)) return {"success": False, "message": traceback.format_exc()}
def main(argv): if len(argv) < 2: msg = "You must specify the environment and the external user id" log.error(msg) return {"success": False, "message": msg} try: env = argv[0] user_id = argv[1] session = init_session(env) templates = [t for t in user_templates.get_user_templates(session, user_id)] return {"success": True, "user_id": user_id, "template_users": templates} except Exception: log.exception('Exception on {0}:'.format(__name__)) return {"success": False, "message": traceback.format_exc()}
def main(argv): if len(argv) < 1: msg = "You must specify the environment" log.error(msg) return {"success": False, "message": msg} try: # command-line arguments env = argv[0] session = init_session(env) product_ids = None if len(argv) >= 2: product_ids = argv[1] if product_ids != "--all": product_ids = argv[1].split(",") timestamp = session.get_present_date() start = time() latest_run = session.data_proxy.fetch_latest_batch_info_product_template_consolidation() if latest_run: if latest_run.get("status") == "running": msg = "An old consolidation batch is still running. Won't start another one." log.info(msg) return {"success": False, "message": msg} session.data_proxy.save_timestamp_product_template_consolidation(status="running", timestamp=timestamp) consolidate_product_templates(session, product_ids) session.data_proxy.ensure_indexes_cache() elapsed_time = time() - start session.data_proxy.save_timestamp_product_template_consolidation( status="success", timestamp=timestamp, elapsed_time=elapsed_time ) return {"success": True} except Exception: log.exception("Exception on {0}:".format(__name__)) session.data_proxy.save_timestamp_product_template_consolidation(status="failed", timestamp=timestamp) return {"success": False, "message": traceback.format_exc()}
def main(argv): if len(argv) < 2: msg = "You must specify the environment and the external product id" log.error(msg) return {"success": False, "message": msg} try: # command-line arguments env = argv[0] product_id = argv[1] session = init_session(env) maintenance.delete_product(session, product_id) except Exception: log.exception('Exception on {0}:'.format(__name__)) return {"success": False, "message": traceback.format_exc()} return {"success": True}
def main(argv): if len(argv) != 1: msg = "Environment parameter is required" log.error(msg) return {"success": False, "message": msg} try: # command-line arguments env = argv[0] session = init_session(env) session.context_filters_cache.clear() return {"success": True} except Exception: log.exception('Exception on {0}:'.format(__name__)) return {"success": False, "message": traceback.format_exc()}
def main(argv): if len(argv) < 4: msg = "You must specify the environment, the user_id, the number of recommendations " \ "and the algorithm." log.error(msg) return {"success": False, "message": msg} try: start = time() # command-line arguments env = argv[0] user_id = argv[1] count_recommendations = int(argv[2]) algorithm = argv[3] if len(argv) >= 5: context_filter_string = argv[4] else: context_filter_string = None log.info('Initializing session...') session = init_session(environment=env, user_id=user_id, context_filter_string=context_filter_string, algorithm=algorithm) log.info('Calling recommender...') recommender = session.get_recommender() results = recommender.recommend(count_recommendations) except Exception as ex: log.exception('Exception on {0}'.format(__name__)) return {"success": False, "message": ex.args[0], "stack_trace": traceback.format_exc()} if results is not None: log.info("Recommendation took [%.6f] seconds overall" % (time() - start)) return {"success": True, "products": results} else: msg = "No recommendations" log.error(msg) return {"success": False, "message": msg}
def init_session(custom_settings=None, context_filter_string=None, user_id=None, algorithm=None, use_custom_data_proxy=False): """ Inits a test session. :param custom_settings: Used to override customer settings defined in the customer config :param context_filter_string: A ContextFilter instance. :param user_id: If None, UserContext won't be created. :param algorithm: The algorithm to be used for recommendations during the tests session. :param use_custom_data_proxy: Do not reuse the singleton data proxy; instead, create a new one. :returns: the SessionContext that can be used to access settings and database """ customer_context = _init_context(custom_settings, use_custom_data_proxy) # sets write concern level to 1 - necessary for tests to pass customer_context.data_proxy.write_concern_level = 1 test_session = context.init_session(customer_ctx=customer_context, user_id=user_id, context_filter_string=context_filter_string, algorithm=algorithm) return test_session
def main(argv): if len(argv) < 4: msg = "You must specify the environment, the external_user_id, " \ "the external_product_id, and the impression date" log.error(msg) return {"success": False, "message": msg} try: # command-line arguments env = argv[0] user = argv[1] product = argv[2] impression_date = dateutil.parser.parse(argv[3]) impression = {"external_user_id": user, "external_product_id": product, "created_at": impression_date} session = init_session(env) maintenance.process_impression(session, impression) return {"success": True} except Exception: log.exception('Exception on {0}:'.format(__name__)) return {"success": False, "message": traceback.format_exc()}