Beispiel #1
0
    def on_participate(self, request):
        #获取分组列表
        alts = request.args.getlist('alternatives')
        # alt_fractions = request.args.getlist('alt_fractions')
        experiment_name = request.args.get('experiment')  #实验名称
        force = request.args.get('force')
        #获取record_force参数,并进行to_bool判断若为[y,ture,yes],返回Ture,否则False
        record_force = to_bool(request.args.get('record_force', 'false'))
        client_id = request.args.get('client_id')  #获取client_id
        traffic_fraction = request.args.get('traffic_fraction')  #获取参与比例

        if traffic_fraction is not None:
            traffic_fraction = float(
                traffic_fraction)  #若traffic_fraction存在则转为float()
        prefetch = to_bool(request.args.get(
            'prefetch',
            'false'))  #获取prefetch,并进行to_bool判断若为[y,ture,yes],返回Ture,否则Fals
        if client_id is None or experiment_name is None or alts is None:  #三个必要参数,否则返回400
            return json_error({'message': 'missing arguments'}, request, 400)

        dt = None
        if request.args.get(
                "datetime"):  #获取datetime参数,转化为datetime.datetime()类型,没有则默认None
            dt = dateutil.parser.parse(request.args.get("datetime"))
        try:
            if should_exclude_visitor(
                    request):  #判断user_agent是爬虫或者IP为指定排除的ip,则拒绝参与
                exp = Experiment.find(experiment_name, redis=self.redis)
                if exp.winner is not None:  #判断是否已经胜出
                    alt = exp.winner
                else:
                    alt = exp.control  #没有胜出,返回分组中的第一个赋值给alt
            else:
                alt = participate(
                    experiment_name,
                    alts,
                    client_id,
                    force=force,
                    record_force=record_force,
                    traffic_fraction=traffic_fraction,
                    # alt_fraction=alt_fractions,
                    prefetch=prefetch,
                    datetime=dt,
                    redis=self.redis)
        except ValueError as e:
            return json_error({'message': str(e)}, request, 400)

        resp = {
            'alternative': {
                'name': alt.name
            },
            'experiment': {
                'name': alt.experiment.name,
            },
            'client_id': client_id,
            'status': 'ok'
        }

        return json_success(resp, request)
Beispiel #2
0
    def on_participate(self, request):
        alts = request.args.getlist('alternatives')
        experiment_name = request.args.get('experiment')
        bucket = request.args.get('bucket')
        force = request.args.get('force')
        client_id = request.args.get('client_id')
        traffic_fraction = request.args.get('traffic_fraction')
        if traffic_fraction is not None:
            traffic_fraction = float(traffic_fraction)
        prefetch = to_bool(request.args.get('prefetch', 'false'))

        if client_id is None or experiment_name is None or alts is None:
            return json_error({'message': 'missing arguments'}, request, 400)

        dt = None
        if request.args.get("datetime"):
            dt = dateutil.parser.parse(request.args.get("datetime"))

        if should_exclude_visitor(request):
            exp = Experiment.find(experiment_name, redis=self.redis)
            if exp.winner is not None:
                alt = exp.winner
            else:
                alt = exp.control
        else:
            try:
                alt = participate(experiment_name,
                                  alts,
                                  client_id,
                                  force=force,
                                  bucket=bucket,
                                  traffic_fraction=traffic_fraction,
                                  prefetch=prefetch,
                                  datetime=dt,
                                  redis=self.redis)
            except ValueError as e:
                return json_error({'message': str(e)}, request, 400)

        resp = {
            'alternative': {
                'name': alt.name
            },
            'experiment': {
                'name': alt.experiment.name,
            },
            'client_id': client_id,
            'status': 'ok'
        }

        return json_success(resp, request)
Beispiel #3
0
    def on_participate(self, request):
        alts = request.args.getlist('alternatives')
        experiment_name = request.args.get('experiment')
        force = request.args.get('force')
        record_force = to_bool(request.args.get('record_force', 'false'))
        client_id = request.args.get('client_id')
        traffic_fraction = request.args.get('traffic_fraction')

        if traffic_fraction is not None:
            traffic_fraction = float(traffic_fraction)
        prefetch = to_bool(request.args.get('prefetch', 'false'))

        if client_id is None or experiment_name is None or alts is None:
            return json_error({'message': 'missing arguments'}, request, 400)

        dt = None
        if request.args.get("datetime"):
            dt = dateutil.parser.parse(request.args.get("datetime"))
        try:
            if should_exclude_visitor(request):
                exp = Experiment.find(experiment_name, redis=self.redis)
                if exp.winner is not None:
                    alt = exp.winner
                else:
                    alt = exp.control
            else:
                alt = participate(experiment_name, alts, client_id,
                                  force=force, record_force=record_force,
                                  traffic_fraction=traffic_fraction,
                                  prefetch=prefetch, datetime=dt, redis=self.redis)
        except ValueError as e:
            return json_error({'message': str(e)}, request, 400)

        resp = {
            'alternative': {
                'name': alt.name
            },
            'experiment': {
                'name': alt.experiment.name,
            },
            'client_id': client_id,
            'status': 'ok'
        }

        return json_success(resp, request)