Beispiel #1
0
    def user_recommend(self, request, context):
        # 接收web后端发送过来的请求
        user_id = request.user_id
        channel_id = request.channel_id
        article_num = request.article_num
        time_stamp = request.time_stamp

        # 调用系统推荐方法获取推荐文章数据

        # 返回推荐的文章响应对象
        article_response = reco_pb2.ArticleResponse()
        # 设置曝光数据
        article_response.exposure = "exposure message"
        # 设置时间戳
        article_response.time_stamp = round(time.time() * 1000)

        article_list = []

        for i in range(article_num):
            article = reco_pb2.Article()
            article.article_id = i + 1
            # 埋点数据
            article.track.click = "click action {}".format(i + 1)
            article.track.collect = "collect action {}".format(i + 1)
            article.track.share = "share action {}".format(i + 1)
            article.track.read = "read action {}".format(i + 1)
            article_list.append(article)

        article_response.recommends.extend(article_list)

        return article_response
Beispiel #2
0
    def user_recommend(self, request, context):
        """
        伪造推荐系统
        :param request: rpc调用的请求数据对象 UserRequest对象
        :param context: 给予程序上下文环境,主要用于返回异常
                context.set_code(grpc.StatusCode.UNIMPLEMENTED)
                context.set_details('Method not implemented!')
        :return:
        """
        # 获取rpc调用的请求参数
        user_id = request.user_id
        channel_id = request.channel_id
        article_num = request.article_num
        time_stamp = request.time_stamp

        # 伪造推荐系统返回对象ArticleResponse
        response = reco_pb2.ArticleResponse()
        response.exposure = 'exposure param'
        response.time_stamp = round(time.time() * 1000)

        articles_list = []
        for i in range(article_num):
            article = reco_pb2.Article()
            article.article_id = i + 1
            article.track.click = 'click param '
            article.track.collect = 'collect param '
            article.track.share = 'share param '
            article.track.read = 'read param '
            articles_list.append(article)

        # response.recommends.append(Article())
        # 注意 在grpc中 不使用append方法 使用extend
        response.recommends.extend(articles_list)

        return response
Beispiel #3
0
    def user_recommend(self, request, context):
        # 1.解析请求对象中的参数 request
        user_id = request.user_id
        channel_id = request.channel_id
        article_num = request.article_num
        time_stamp = request.time_stamp
        print('接收到了rpc请求的参数')

        # 2 .构造响应对象,并且返回
        response = reco_pb2.ArticleResponse()  #实例化
        response.expousre = '曝光埋点数据'
        response.time_stamp = round(time.time() *
                                    1000)  #round() python内置函数,四舍五入取整

        recommends_list = []
        for i in range(article_num):
            article = reco_pb2.Article()  #实例化
            # article.track  不是间接关系,而是直接关系,所以track里面有的直接使用
            article.article_id = i + 1
            article.track.click = 'click'
            article.track.collect = 'collect'
            article.track.share = 'share'
            article.track.read = 'read'
            recommends_list.append(article)
        print(recommends_list)
        response.recommends.extend(recommends_list)  #因为是列表,所以用extend添加一个构造列表对象

        return response
Beispiel #4
0
    def user_recommend(self, request, context):
        """
        在接口中定义的用户推荐方法
        :param request:  调用时的请求参数对象  UserRequest
        :param context:  通过此对象可以设置调用返回的异常信息
        :return:
        """
        # 获取调用的参数
        user_id = request.user_id
        channel_id = request.channel_id
        article_num = request.article_num
        time_stamp = request.time_stamp

        # 决定调用返回数据
        resp = reco_pb2.ArticleResponse()
        resp.exposure = 'exposure param'
        resp.time_stamp = round(time.time() * 1000)

        _recommends = []
        for i in range(article_num):
            article = reco_pb2.Article()
            article.article_id = i+1
            article.track.click = 'click param'
            article.track.collect = 'collect param'
            article.track.share = 'share param'
            article.track.read = 'read param'
            _recommends.append(article)

        # 注意 对于列表类型的赋值使用extend
        resp.recommends.extend(_recommends)

        return resp
Beispiel #5
0
    def user_recommend(self, request, context):
        # request是调用的请求数据对象
        user_id = request.user_id
        channel_id = request.channel_id
        article_num = request.article_num
        time_stamp = request.time_stamp

        response = reco_pb2.ArticleResponse()

        # 手动构造推荐结果,后续对接真实推荐代码
        response.exposure = 'exposure param'
        response.time_stamp = round(time.time() * 1000)
        recommends = []
        for i in range(article_num):
            article = reco_pb2.Article()
            article.track.click = 'click param {}'.format(i + 1)
            article.track.collect = 'collect param {}'.format(i + 1)
            article.track.share = 'share param {}'.format(i + 1)
            article.track.read = 'read param {}'.format(i + 1)
            article.article_id = i + 1
            recommends.append(article)
        response.recommends.extend(recommends)

        # 最终要返回一个调用结果
        return response
Beispiel #6
0
    def user_recommend(self, request, context):
        # 1.解析请求对象中的参数
        user_id = request.user_id  # 用户id str类型
        channel_id = request.channel_id  # 频道id int类型
        article_num = request.article_num  # 推荐的文章数量 向推荐系统索要推荐文章的数量 int类型
        time_stamp = request.time_stamp # 推荐的时间戳 单位秒 int类型

        """这里根据请求参数做了一些操作"""

        # 2.构造相应对象,并返回
        response = reco_pb2.ArticleResponse()
        response.expousre = 'sssss'
        response.time_stamp = round(time.time()*1000) # round() py内置函数 四舍五入取整

        recommends_list = []
        for i in range(article_num):
            article = reco_pb2.Article()
            article.article_id = i+1
            article.track.click = 'click'
            article.track.collect = 'collect'
            article.track.share = 'share'
            article.track.read = 'read'
            recommends_list.append(article)

        response.recommends.extend(recommends_list)
        return response
Beispiel #7
0
    def user_recommend(self, request, context):
        """
        给用户推荐文章具体业务逻辑实现
        :param request: UserRequest类的请求参数对象
        :param context: 错误信息
        :return:
        """
        # 1.获取web后端的请求参数
        user_id = request.user_id
        channel_id = request.channel_id
        # 推荐文章的数量
        article_num = request.article_num
        time_stamp = request.time_stamp
        # 2.参数校验

        # 3.逻辑处理
        # TODO: 后续补充推荐系统的业务逻辑
        # 伪推荐 造数据
        # 4.返回值处理
        article_response = reco_pb2.ArticleResponse()
        # 曝光数据
        article_response.exposure = "exposure message"
        article_response.time_stamp = round(time.time() * 1000)

        article_list = []
        for i in range(article_num): # 0 1 2 3

            # 创建文章对象
            article = reco_pb2.Article()
            # 给文章id赋值
            article.article_id = i + 1
            # 用户行为埋点数据
            article.track.click = "click action {}".format(i+1)
            article.track.collect = "collect action {}".format(i+1)
            article.track.share = "share action {}".format(i+1)
            article.track.read = "read action {}".format(i+1)
            article_list.append(article)

        # extend 在列表尾部补充元素
        article_response.recommends.extend(article_list)

        return article_response
Beispiel #8
0
    def user_recommend(self, request, context):
        """
      理应由推荐系统编写的 推荐业务逻辑
      为了跑通整个rpc通讯流程,暂时由web来完成 伪推荐
      :param request: rpc的请求对象 在当前的rpc 首页新闻推荐中,按照proto文件指明,应该是UserRequset类对象
      :param context: 在当前被执行的函数中 如果出现问题,可以通过context设置 返回给调用一方的错误信息
         context.set_code(grpc.StatusCode.UNIMPLEMENTED)
         context.set_details('Method not implemented!')
      :return:
        """
        # 获取调用的请求参数
        user_id = request.user_id  # 用户id
        channel_id = request.channel_id  # 频道id
        article_num = request.article_num  # 文章数量
        time_stamp = request.time_stamp  # 时间戳

        # 构建文章id和伪造埋点参数 返回
        resp = reco_pb2.ArticleResponse()#构建响应对象
        resp.exposure = 'exposure param'#曝光参数
        resp.time_stamp = round(time.time() * 1000)#时间戳,毫秒整数


        # 在grpc中 repeated 列表参数的构建 需要使用extend,不用append
        #列表参数有埋点数
        # resp.recommends.append()
        articles = []
        for i in range(article_num):
            #Article对象
            article = reco_pb2.Article()
            article.article_id = i + 1
            article.track.click = 'click param {}'.format(i + 1)
            article.track.collect = 'collect param {}'.format(i + 1)
            article.track.share = 'share param {}'.format(i + 1)
            article.track.read = 'read param {}'.format(i + 1)

            articles.append(article)
        #扩展给列表属性 extend
        resp.recommends.extend(articles)

        return resp
Beispiel #9
0
 def user_recommends(self, request, context):
     # 用户推荐
     # 获取请求信息
     user_id = request.user_id
     channel_id = request.channel_id
     article_num = request.article_num
     time_stamp = request.time_stamp
     # 此处代码为伪推荐,后续系统推荐课程中,可以补全此处代码
     resp = reco_pb2.ArticleResponse()
     resp.exposure = 'exposure data'
     resp.time_stamp = round(time.time() * 1000)
     content = []
     for i in range(article_num):
         article = reco_pb2.Article()
         article.article_id = i
         article.track.click = 'click data {}'.format(i + 1)
         article.track.collect = 'collect data {}'.format(i + 1)
         article.track.share = 'share data {}'.format(i + 1)
         article.track.read = 'read data {}'.format(i + 1)
         content.append(article)
     resp.recommends.extend(content)
     # 返回响应
     return resp
Beispiel #10
0
    def user_recommend(self, request, context):
        """
        用户文章推荐 实际是由推荐系统编写
        :param request: rpc调用的请求数据对象  UserRequest对象
        :param context: 如果被调用的这个方法出现了异常,可以通过context提供的方法来告知rpc调用的一方错误信息
            context.set_code(grpc.StatusCode.UNIMPLEMENTED)
            context.set_details('Method not implemented!')
        :return:
        """
        # 获取rpc调用的请求参数
        user_id = request.user_id
        channel_id = request.channel_id
        article_num = request.article_num
        time_stamp = request.time_stamp

        # 为了看到效果 先伪推荐
        # 构建调用返回值对象
        response = reco_pb2.ArticleResponse()
        response.exposure = 'exposure param'
        response.time_stamp = round(time.time() * 1000)

        articles_list = []
        for i in range(article_num):
            article = reco_pb2.Article()
            article.article_id = i + 1
            article.track.click = 'click param '
            article.track.collect = 'collect param '
            article.track.share = 'share param '
            article.track.read = 'read param '
            articles_list.append(article)

        # response.recommends.append(Article())
        # 注意 在grpc中 不使用append方法 使用extend
        response.recommends.extend(articles_list)

        return response
Beispiel #11
0
    def user_recommend(self, request, context):
        # 接收请求,request不是Flask请求上下文对象
        user_id = request.user_id
        channel_id = request.channel_id
        article_num = request.article_num
        time_stamp = request.time_stamp

        # 返回响应,后续需要对接推荐系统,获取推荐的文章列表,现在模拟推荐
        # 实例化文章对象
        resp = reco_pb2.ArticleResponse()
        resp.exposure = 'exposure params'
        resp.time_stamp = round(time.time() * 1000)
        # 默认返回的文章使用article_num
        data_list = []
        for i in range(article_num):
            article = reco_pb2.Article()
            article.article_id = i + 1
            article.track.click = 'click params {}'.format(i + 1)
            article.track.collect = 'collect params {}'.format(i + 1)
            article.track.share = 'share params {}'.format(i + 1)
            article.track.read = 'read params {}'.format(i + 1)
            data_list.append(article)
        resp.recommends.extend(data_list)
        return resp