Exemple #1
0
def get_statistics():
    user_ID = get_jwt_identity()
    page_no = int(request.args.get('page_no', 1))
    page_size = int(request.args.get('page_size', 5))

    action = request.args.get("action")
    entity_type = request.args.get("entity_type")

    user_obj = UserBusiness.get_by_user_ID(user_ID=user_ID)
    statistics = StatisticsBusiness.get_pagination(query={
        "action": action,
        "entity_type": entity_type,
        "caller": user_obj
    },
                                                   page_no=page_no,
                                                   page_size=page_size)
    for object in statistics.objects:
        object.app_obj_user_ID = object.app.user.user_ID

    return jsonify({
        'response': {
            "objects":
            json_utility.objs_to_json_with_args(statistics.objects,
                                                ["app", "caller"]),
            # "objects": json_utility.me_obj_list_to_json_list(statistics.objects),
            # "objects": json.loads(statistics.objects.to_json()),
            "page_size":
            statistics.page_size,
            "page_no":
            statistics.page_no,
            "count":
            statistics.count,
        }
    })
Exemple #2
0
def get_statistics():
    user_ID = get_jwt_identity()
    page_no = int(request.args.get('page_no', 1))
    page_size = int(request.args.get('page_size', 5))

    action = request.args.get("action")
    entity_type = request.args.get("entity_type")

    user_obj = UserBusiness.get_by_user_ID(user_ID=user_ID)
    statistics = StatisticsBusiness.get_pagination(
        query={
            "action": action,
            "entity_type": entity_type,
            "caller": user_obj
        },
        page_no=page_no, page_size=page_size)

    # for object in statistics.objects:
    #     # print("tom", json_utility.convert_to_json(object.app.to_mongo()))
    #     # object.app_obj = "111"
    #     app = json.dumps(object.app.to_mongo())#json_utility.convert_to_json(object.app.to_mongo())
    #     object.app_obj = app
    #
    #     # object = json_utility.convert_to_json(object.to_mongo())
    #     # object["app_obj"] = app
    print("statistics.objects", statistics.objects)

    return jsonify({
        'response': {
            "objects": json_utility.objs_to_json_with_args(statistics.objects, ["app", "caller"]),
            "page_size": statistics.page_size,
            "page_no": statistics.page_no,
            "count": statistics.count,
        }
    })
Exemple #3
0
    def create_user_request(cls, title, user_ID, **kwargs):
        # create a new user_request object
        user = UserBusiness.get_by_user_ID(user_ID)
        created_user_request = UserRequestBusiness.add_user_request(
            title=title,
            user=user,
            status=0,
            **kwargs)

        # 记录历史记录
        statistics = StatisticsBusiness.action(
            user_obj=user,
            entity_obj=created_user_request,
            entity_type="userRequest",
            action="create"
        )

        # 记录世界频道消息  # 推送消息
        world = WorldService.system_send(
            channel=CHANNEL.request,
            message=f"用户{created_user_request.user.user_ID}" +
                    f"发布了需求{created_user_request.title}")

        # return created_user_request

        # 不再记录到favor list里面
        # todo
        if created_user_request:
            user_entity = UserService. \
                action_entity(user_ID=user_ID,
                              entity_id=created_user_request.id,
                              action='star', entity='request')
            return user_entity.entity
        else:
            raise RuntimeError('Cannot create the new user_request')
Exemple #4
0
    def get_statistics(cls, user_ID, page_no, page_size, action, entity_type):
        """
        获取用户统计信息

        这里需要将 app, caller 从objectID转换成json吗?
        1. service可能被其他service调用,应该在route层转换
        2. 在其他service调用时也都需要转换,保证route调用结果一致
        :param user_ID:
        :type user_ID:
        :param page_no:
        :type page_no:
        :param page_size:
        :type page_size:
        :param action:
        :type action:
        :param entity_type:
        :type entity_type:
        :return:
        :rtype:
        """
        from server3.business.statistics_business import StatisticsBusiness
        user_obj = UserBusiness.get_by_user_ID(user_ID=user_ID)
        statistics = StatisticsBusiness.get_pagination(query={
            "action": action,
            "entity_type": entity_type,
            "caller": user_obj
        },
                                                       page_no=page_no,
                                                       page_size=page_size)

        return statistics
Exemple #5
0
    def run_app(cls, app_id, input_json, user_ID, version):
        """

        :param app_id: app id
        :param input_json:
        :param user_ID:
        :param version:
        :return:
        :rtype:
        """
        app = AppBusiness.get_by_id(project_id=app_id)
        url = '-'.join([app.user.user_ID, app.name, version])
        domin = f"http://{DOCKER_IP}:8080/function/"
        url = domin + url
        payload = json.dumps(input_json)
        headers = {
            'content-type': "application/json",
        }
        response = requests.request("POST", url, data=payload, headers=headers)
        pattern = re.compile(r'STRHEAD(.+?)STREND', flags=re.DOTALL)
        results = pattern.findall(response.text)
        print(results, 111)
        try:
            output_json = json.loads(results[0])
        except IndexError as e:
            try:
                errors = cls.business.get_service_logs(app, version)
            except IndexError as e:
                output_json = {
                    'errors': ['Service is down please deploy again!']
                }
            else:
                output_json = {'errors': errors}
        # output_json = response.json()
        # 成功调用后 在新的collection存一笔
        user_obj = UserBusiness.get_by_user_ID(user_ID=user_ID)
        # 筛选 input_json

        StatisticsBusiness.use_app(
            user_obj=user_obj,
            app_obj=app,
            output_json=output_json
            # input_json=input_json,
            # output_json=output_json
        )
        return output_json
Exemple #6
0
    def create_project(cls,
                       name,
                       description,
                       user_ID,
                       tags=None,
                       user_token='',
                       type='project',
                       **kwargs):
        """
        Create a new project

        :param name: str
        :param description: str
        :param user_ID: ObjectId
        :param type: string (app/module/dataset)
        :param tags: list of string
        :param user_token: string
        :return: a new created project object
        """
        if tags is None:
            tags = []
        project_type = type
        user = UserBusiness.get_by_user_ID(user_ID)
        # message = "{}创建了app{}".format(user.name, name)
        # world_business.system_send(channel=cls.channel, message=message)
        project = cls.business.create_project(name=name,
                                              description=description,
                                              type=type,
                                              tags=tags,
                                              user=user,
                                              user_token=user_token,
                                              **kwargs)
        from server3.service.user_service import UserService
        UserService.action_entity(user_ID=project.user.user_ID,
                                  entity_id=project.id,
                                  action='favor',
                                  entity=project.type)

        from server3.service.world_service import WorldService
        from server3.business.statistics_business import StatisticsBusiness
        # 记录历史记录
        statistics = StatisticsBusiness.action(user_obj=user,
                                               entity_obj=project,
                                               entity_type=type,
                                               action="create")
        # 记录世界频道消息  # 推送消息
        world = WorldService.system_send(
            channel=CHANNEL.request,
            message=f"用户{project.user.user_ID}创建了{project_type}: {project.name}"
        )

        return project
Exemple #7
0
    def run_app(cls, app_id, input_json, user_ID):
        """

        :param app_id: app id
        :type app_id: ObjectId
        :param input_json:
        :type input_json:
        :param user_ID:
        :type user_ID:
        :return:
        :rtype:
        """
        app = AppBusiness.get_by_id(project_id=app_id)
        url = app.user.user_ID + "-" + app.name
        domin = f"http://{DOCKER_IP}:8080/function/"
        url = domin + url
        payload = json.dumps(input_json)
        headers = {
            'content-type': "application/json",
        }
        response = requests.request("POST", url, data=payload, headers=headers)
        print(response.text)
        pattern = re.compile(r'STRHEAD(.+?)STREND', flags=re.DOTALL)
        results = pattern.findall(response.text)
        output_json = json.loads(results[0])
        print(output_json)
        # output_json = response.json()
        # 成功调用后 在新的collection存一笔
        user_obj = UserBusiness.get_by_user_ID(user_ID=user_ID)
        # 筛选 input_json

        StatisticsBusiness.use_app(
            user_obj=user_obj, app_obj=app,
            output_json=output_json
            # input_json=input_json,
            # output_json=output_json
        )
        return output_json
def create_request_answer(**data):
    # create a new request_answer object
    created_request_answer = request_answer_business. \
        add_request_answer(**data)
    if created_request_answer:
        # get user object
        user = data['answer_user']
        user_request = user_request_business. \
            get_by_user_request_id(data['user_request'])

        from server3.service.world_service import WorldService
        from server3.business.statistics_business import StatisticsBusiness
        from server3.entity.world import CHANNEL

        # 记录历史记录
        statistics = StatisticsBusiness.action(
            user_obj=user,
            entity_obj=created_request_answer,
            entity_type="requestAnswer",
            action="create")

        # 记录世界频道消息  # 推送消息
        world = WorldService.system_send(
            channel=CHANNEL.request,
            message=f"用户{created_request_answer.answer_user.user_ID}为需求"
            f"{user_request.title}创建了回答")

        # create ownership relations
        #  新建通知消息
        admin_user = user_business.get_by_user_ID('admin')

        receivers = [el for el in user_request.star_user]
        if message_service.create_message(
                sender=admin_user,
                message_type='answer',
                receivers=receivers,
                user=user,
                title='Notification',
                user_request=user_request,
        ):
            return created_request_answer
        else:
            raise RuntimeError(
                'Cannot create message of the new request_answer')

    else:
        raise RuntimeError('Cannot create the new request_answer')
def create_user_request(title, user_ID, **kwargs):
    # create a new user_request object
    user = user_business.get_by_user_ID(user_ID)
    created_user_request = user_request_business.add_user_request(title=title,
                                                                  user=user,
                                                                  status=0,
                                                                  **kwargs)

    # 记录历史记录
    statistics = StatisticsBusiness.action(user_obj=user,
                                           entity_obj=created_user_request,
                                           entity_type="userRequest",
                                           action="create")

    # 记录世界频道消息  # 推送消息
    world = WorldService.system_send(
        channel=CHANNEL.request,
        message=
        f"用户{created_user_request.user.user_ID}发布了需求{created_user_request.title}"
    )

    if created_user_request:
        # 默认发布者star
        created_user_request = user_service.update_request_star(
            created_user_request.id, user_ID)

        # get user object
        user = user_business.get_by_user_ID(user_ID=user_ID)
        # create ownership relation
        if ownership_business.add(user, user_request=created_user_request):
            return created_user_request
        else:
            raise RuntimeError(
                'Cannot create ownership of the new user_request')
    else:
        raise RuntimeError('Cannot create the new user_request')
Exemple #10
0
    def use_app(cls, user_obj, app_obj, input_json, output_json):
        statistics = StatisticsBusiness.use_app(user_obj, app_obj, input_json,
                                                output_json)

        return statistics