Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: store created_by and updated_by for apps, modelconfigs, and sites #7613

Merged
merged 1 commit into from
Aug 28, 2024

Conversation

kurokobo
Copy link
Contributor

@kurokobo kurokobo commented Aug 25, 2024

Checklist

Important

Please review the checklist below before submitting your pull request.

  • Please open an issue before creating a PR or link to an existing issue
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods

Description

This PR adds created_by and updated_by fields to App, AppModelConfig, and Site models to store account id who created or updated the apps, model configs, or sites.

Fixes #7593

Changes:

  • Changes on models
    • New fields created_by and updated_by are added to App, AppModelConfig, and Site models
    • Workflow model is not changed since it already has *_by fileds
  • While newly creating App, AppModelConfig, or Site, both created_by and updated_by are filled with the account id who created the object
  • While modifying exising App or Site, updated_by for each models are updated with the account id who modified the object
  • However, since AppModelConfig is always replaced with new object upon update, updated_by doesn't hold much meaning. This means that updated_by and exising updated_at for AppModelConfig always equal to created_by and created_at. Still, I prepared updated_by because there was already updated_at. I think it might be a good idea for AppModelConfig to only have created_* (removing updated_*). Please let me know if any modifications are needed
  • Update JSON schema for Apps, Model Configs, and Sites to have *_by fields to respond these fields through API.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update, included: Dify Document
  • Improvement, including but not limited to code refactoring, performance optimization, and UI/UX improvement
  • Dependency upgrade

Testing Instructions

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  1. Start new instance of Dify 0.7.1

  2. Invite User-A and User-B as Editor, activate them, then gather ids for these users

    # /console/api/workspaces/current/members
    {
        "accounts": [
            ...
            {
                "id": "aabcc2cb-d6de-4101-bc4f-ad97c469c0b0",
                "name": "user-b",
                "avatar": null,
                "email": "[email protected]",
                ...
            },
            {
                "id": "f989e9ff-1a46-4d44-84c9-006a83389691",
                "name": "user-a",
                "avatar": null,
                "email": "[email protected]",
                ...
            }
        ]
    }
  3. Logged in as User-A and create App-A and publish it

  4. Logged in as User-A and create Workflow-A and connect End block, then publish it

  5. Ensure App-A and Workflow-A has no created_by nor updated_by fields

    # /console/api/apps
    {
        "page": 1,
        "limit": 30,
        "total": 2,
        "has_more": false,
        "data": [
            {
                "id": "e4747810-2e59-4b11-8f31-dadee4359cf2",
                "name": "Workflow-A",
                "max_active_requests": null,
                "description": "",
                "mode": "workflow",
                "icon_type": "emoji",
                "icon": "\ud83e\udd16",
                "icon_background": "#FFEAD5",
                "icon_url": null,
                "model_config": null,
                "created_at": 1724563892,
                "tags": []
            },
            {
                "id": "10e067bc-c62b-4124-9565-ec15ba0b7e91",
                "name": "App-A",
                "max_active_requests": null,
                "description": "",
                "mode": "chat",
                "icon_type": "emoji",
                "icon": "\ud83e\udd16",
                "icon_background": "#FFEAD5",
                "icon_url": null,
                "model_config": {
                    "model": {
                        "provider": "openai",
                        "name": "gpt-4o",
                        "mode": "chat",
                        "completion_params": {
                            "stop": []
                        }
                    },
                    "pre_prompt": ""
                },
                "created_at": 1724563880,
                "tags": []
            }
        ]
    }
  6. Build custom dify-api image with this PR

  7. Upgrade the instance to the new image

  8. Ensure the migrations are happened

    $ docker compose logs -f api worker
    worker-1  | Running migrations
    api-1     | Running migrations
    worker-1  | None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.
    api-1     | None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.
    worker-1  | INFO:matplotlib.font_manager:generated new fontManager
    api-1     | INFO:matplotlib.font_manager:generated new fontManager
    worker-1  | Preparing database migration...
    worker-1  | Start database migration.
    worker-1  | INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
    worker-1  | INFO  [alembic.runtime.migration] Will assume transactional DDL.
    worker-1  | INFO  [alembic.runtime.migration] Running upgrade a6be81136580 -> 2dbe42621d96, rename workflow__conversation_variables to workflow_conversation_variables
    worker-1  | INFO  [alembic.runtime.migration] Running upgrade 2dbe42621d96 -> d0187d6a88dd, add created_by and updated_by to app, modelconfig, and site
    worker-1  | Database migration successful!
  9. Logged in as User-B and create App-B and Workflow-B

  10. Ensure that all apps has additional fields, App-A and Workflow-A have empty created_by and updated_by, App-B and Workflow-B have valid created_by and updated_by for User-B

    {
        "page": 1,
        "limit": 30,
        "total": 4,
        "has_more": false,
        "data": [
            {
                "id": "c1461e18-978d-4d14-bcc9-debcaa5afd49",
                "name": "Workflow-B",
                ...
                "workflow": {
                    "id": "6b2cbb50-2a8b-4559-a743-a2ba2000298a",
                    ✅ "created_by": "aabcc2cb-d6de-4101-bc4f-ad97c469c0b0",
                    ✅ "created_at": 1724564348,
                    ✅ "updated_by": null,
                    ✅ "updated_at": null
                },
                ✅ "created_by": "aabcc2cb-d6de-4101-bc4f-ad97c469c0b0",
                ✅ "created_at": 1724564341,
                ✅ "updated_by": "aabcc2cb-d6de-4101-bc4f-ad97c469c0b0",
                ✅ "updated_at": 1724564341,
                "tags": []
            },
            {
                "id": "90b00b29-3762-4c09-99af-c919abd323d1",
                "name": "App-B",
                ...
                "model_config": {
                    "model": {
                        ...
                    },
                    "pre_prompt": "",
                    ✅ "created_by": "aabcc2cb-d6de-4101-bc4f-ad97c469c0b0",
                    ✅ "created_at": 1724564334,
                    ✅ "updated_by": "aabcc2cb-d6de-4101-bc4f-ad97c469c0b0",
                    ✅ "updated_at": 1724564334
                },
                "workflow": null,
                ✅ "created_by": "aabcc2cb-d6de-4101-bc4f-ad97c469c0b0",
                ✅ "created_at": 1724564332,
                ✅ "updated_by": "aabcc2cb-d6de-4101-bc4f-ad97c469c0b0",
                ✅ "updated_at": 1724564332,
                "tags": []
            },
            {
                "id": "e4747810-2e59-4b11-8f31-dadee4359cf2",
                "name": "Workflow-A",
                ...
                "workflow": {
                    "id": "a53b09b7-49bb-41fe-ba55-a21e6a492c58",
                    ✅ "created_by": "f989e9ff-1a46-4d44-84c9-006a83389691",
                    ✅ "created_at": 1724563896,
                    ✅ "updated_by": null,
                    ✅ "updated_at": null
                },
                ✅ "created_by": null,
                ✅ "created_at": 1724563892,
                ✅ "updated_by": null,
                ✅ "updated_at": 1724563892,
                "tags": []
            },
            {
                "id": "10e067bc-c62b-4124-9565-ec15ba0b7e91",
                "name": "App-A",
                ...
                "model_config": {
                    "model": {
                        ...
                    },
                    "pre_prompt": "",
                    ✅ "created_by": null,
                    ✅ "created_at": 1724563882,
                    ✅ "updated_by": null,
                    ✅ "updated_at": 1724563882
                },
                "workflow": null,
                ✅ "created_by": null,
                ✅ "created_at": 1724563880,
                ✅ "updated_by": null,
                ✅ "updated_at": 1724563880,
                "tags": []
            }
        ]
    }
  11. Make any changes on any apps by any users

  12. Ensure appropreate fields are updated

    {
        "page": 1,
        "limit": 30,
        "total": 4,
        "has_more": false,
        "data": [
            {
                "id": "c1461e18-978d-4d14-bcc9-debcaa5afd49",
                "name": "Workflow-B",
                ...
                "workflow": {
                    "id": "6d7ff2da-18e8-4a69-8fb5-43acac6dcbad",
                    ✅ "created_by": "f989e9ff-1a46-4d44-84c9-006a83389691",
                    ✅ "created_at": 1724564706,
                    "updated_by": null,
                    "updated_at": null
                },
                "created_by": "aabcc2cb-d6de-4101-bc4f-ad97c469c0b0",
                "created_at": 1724564341,
                "updated_by": "aabcc2cb-d6de-4101-bc4f-ad97c469c0b0",
                "updated_at": 1724564341,
                "tags": []
            },
            {
                "id": "90b00b29-3762-4c09-99af-c919abd323d1",
                "name": "App-B",
                ...
                "model_config": {
                    "model": {
                        ...
                    },
                    "pre_prompt": "Say hello",
                    ✅ "created_by": "f989e9ff-1a46-4d44-84c9-006a83389691",
                    ✅ "created_at": 1724564681,
                    ✅ "updated_by": "f989e9ff-1a46-4d44-84c9-006a83389691",
                    ✅ "updated_at": 1724564681
                },
                "workflow": null,
                "created_by": "aabcc2cb-d6de-4101-bc4f-ad97c469c0b0",
                "created_at": 1724564332,
                "updated_by": "aabcc2cb-d6de-4101-bc4f-ad97c469c0b0",
                "updated_at": 1724564332,
                "tags": []
            },
            {
                "id": "e4747810-2e59-4b11-8f31-dadee4359cf2",
                "name": "Workflow-A",
                ...
                "workflow": {
                    "id": "82f852b9-bf22-42f3-ae07-ba17119ba088",
                    ✅ "created_by": "aabcc2cb-d6de-4101-bc4f-ad97c469c0b0",
                    ✅ "created_at": 1724564611,
                    "updated_by": null,
                    "updated_at": null
                },
                "created_by": null,
                "created_at": 1724563892,
                "updated_by": null,
                "updated_at": 1724563892,
                "tags": []
            },
            {
                "id": "10e067bc-c62b-4124-9565-ec15ba0b7e91",
                "name": "App-A",
                ...
                "model_config": {
                    "model": {
                        ...
                    },
                    "pre_prompt": "Always say greetings",
                    ✅ "created_by": "aabcc2cb-d6de-4101-bc4f-ad97c469c0b0",
                    ✅ "created_at": 1724564438,
                    ✅ "updated_by": "aabcc2cb-d6de-4101-bc4f-ad97c469c0b0",
                    ✅ "updated_at": 1724564438
                },
                "workflow": null,
                "created_by": null,
                "created_at": 1724563880,
                ✅ "updated_by": "aabcc2cb-d6de-4101-bc4f-ad97c469c0b0",
                ✅ "updated_at": 1724564436,
                "tags": []
            }
        ]
    }
  13. Ensure per-apps endpoints return additional metadata

    # /console/api/apps/90b00b29-3762-4c09-99af-c919abd323d1
    {
        "id": "90b00b29-3762-4c09-99af-c919abd323d1",
        "name": "App-B",
        ...
        "model_config": {
                ...
            },
            ✅ "created_by": "f989e9ff-1a46-4d44-84c9-006a83389691",
            ✅ "created_at": 1724564681,
            ✅ "updated_by": "f989e9ff-1a46-4d44-84c9-006a83389691",
            ✅ "updated_at": 1724564681
        },
        "workflow": null,
        "site": {
            ...
            ✅ "created_by": "aabcc2cb-d6de-4101-bc4f-ad97c469c0b0",
            ✅ "created_at": 1724564332,
            ✅ "updated_by": "aabcc2cb-d6de-4101-bc4f-ad97c469c0b0",
            ✅ "updated_at": 1724564332
        },
        ...
        ✅ "created_by": "aabcc2cb-d6de-4101-bc4f-ad97c469c0b0",
        ✅ "created_at": 1724564332,
        ✅ "updated_by": "aabcc2cb-d6de-4101-bc4f-ad97c469c0b0",
        ✅ "updated_at": 1724564332,
        "deleted_tools": []
    }
    # /console/api/apps/e4747810-2e59-4b11-8f31-dadee4359cf2
    {
        "id": "e4747810-2e59-4b11-8f31-dadee4359cf2",
        "name": "Workflow-A",
        ...
        "workflow": {
            "id": "82f852b9-bf22-42f3-ae07-ba17119ba088",
            ✅ "created_by": "aabcc2cb-d6de-4101-bc4f-ad97c469c0b0",
            ✅ "created_at": 1724564611,
            ✅ "updated_by": null,
            ✅ "updated_at": null
        },
        "site": {
            ...
            ✅ "created_by": null,
            ✅ "created_at": 1724563892,
            ✅ "updated_by": null,
            ✅ "updated_at": 1724563892
        },
        ...
        ✅ "created_by": null,
        ✅ "created_at": 1724563892,
        ✅ "updated_by": null,
        ✅ "updated_at": 1724563892,
        "deleted_tools": []
    }

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. 💪 enhancement New feature or request labels Aug 25, 2024
@crazywoola crazywoola requested a review from laipz8200 August 25, 2024 06:44
@kurokobo
Copy link
Contributor Author

@laipz8200
I've just solved the conflicts and force pushed, thanks 😃

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Aug 27, 2024
@crazywoola crazywoola merged commit bc3a8e0 into langgenius:main Aug 28, 2024
7 checks passed
@kurokobo kurokobo deleted the apps_created_by branch August 28, 2024 01:30
cuiks pushed a commit to cuiks/dify that referenced this pull request Sep 2, 2024
idonotknow pushed a commit to AceDataCloud/Dify that referenced this pull request Nov 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💪 enhancement New feature or request lgtm This PR has been approved by a maintainer size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RFE] Store the account id that created or updated the applications
2 participants