草庐IT

用ChatGPT,绘制一个账号系统的C4架构图

peida 2023-07-25 原文

hi,我是熵减,见字如面。

昨天我们用ChatGPT来设计一个账号系统,并尝试输出:模型表,类关系图,序列图,状态图等常用的架构设计中常用的元素。

今天,我们继续向更高层级延伸一下,看ChatGPT能不能更进一步释放工程师的生产力,创造更多的可能性。

所以,这次我们就来尝试用ChatGPT,来构建账号系统的C4模型的架构图啦。

由于ChatGPT不能直接输出图像,所以我们转化为用Mermaid的脚本,通过markdown的方式输出,再用支持Mermaid渲染的工具,将图片显示出来。

生成数据库初始化SQL

我们首先来试试,生成数据库初始化SQL吧。

提示:将以上账号系统的数据模型,生成MySQL数据库的SQL

ChatGPT:

-- 创建表:Account
CREATE TABLE Account (
  id INT PRIMARY KEY,
  username VARCHAR(50) NOT NULL,
  password VARCHAR(50) NOT NULL,
  email VARCHAR(50),
  phone_number VARCHAR(20),
  state VARCHAR(20),
  email_ver BOOLEAN DEFAULT false,
  phone_ver BOOLEAN DEFAULT false,
  created_at DATETIME,
  updated_at DATETIME
);

-- 创建表:EmailVerification
CREATE TABLE EmailVerification (
  id INT PRIMARY KEY,
  token VARCHAR(50) NOT NULL,
  expired_at DATETIME,
  account_id INT,
  FOREIGN KEY (account_id) REFERENCES Account(id)
);

-- 创建表:PhoneVerification
CREATE TABLE PhoneVerification (
  id INT PRIMARY KEY,
  token VARCHAR(50) NOT NULL,
  expired_at DATETIME,
  phone_number VARCHAR(20),
  account_id INT,
  FOREIGN KEY (account_id) REFERENCES Account(id)
);

-- 创建表:ThirdPartySocialAccount
CREATE TABLE ThirdPartySocialAccount (
  id INT PRIMARY KEY,
  provider VARCHAR(20) NOT NULL,
  provider_id VARCHAR(50) NOT NULL,
  account_id INT,
  FOREIGN KEY (account_id) REFERENCES Account(id)
);

-- 创建表:LoginRecord
CREATE TABLE LoginRecord (
  id INT PRIMARY KEY,
  account_id INT,
  login_at DATETIME,
  FOREIGN KEY (account_id) REFERENCES Account(id)
);

以上SQL语句可以在MySQL数据库中创建相应的表,包括Account、EmailVerification、PhoneVerification、ThirdPartySocialAccount和LoginRecord五个表,并设置它们之间的关联关系。

直接输出C4 Diagram

接下来我们直接尝试其是否可以输出系统的C4模型的代码吧。

提示:对于系统架构图,用Mermaid's c4 diagram是非常有效的,接下来,我们需要开始构建c4 模型的Mermaid脚本,并用markdown

ChatGPT:

好的,以下是用Mermaid's C4 Diagram构建系统架构图的Mermaid脚本,并用markdown格式输出:

## C4 Model for Account System

### Level 1: System Context Diagram

title Account System - System Context Diagram

actor User

User -> AccountSystem: Use

### Level 2: Container Diagram

title Account System - Container Diagram

AccountSystem -> AccountService: Uses
AccountSystem -> EmailService: Uses
AccountSystem -> PhoneService: Uses
AccountSystem -> ThirdPartyService: Uses
AccountSystem -> Database: Uses

AccountService -> Database: Reads/Writes
EmailService -> Database: Reads/Writes
PhoneService -> Database: Reads/Writes
ThirdPartyService -> Database: Reads/Writes

### Level 3: Component Diagram

title Account System - Component Diagram

AccountSystem --> AccountService: Manages Accounts
AccountSystem --> EmailService: Sends Email
AccountSystem --> PhoneService: Sends SMS
AccountSystem --> ThirdPartyService: Interacts with Social Platforms

AccountService --> Account: Accesses Account Data
EmailService --> EmailVerification: Validates Email
PhoneService --> PhoneVerification: Validates Phone Number

Account --> PasswordSecurity: Manages Passwords
Account --> LoginRecord: Records Login Information

虽然给出了完整的代码,但在我的vscode的插件中,无法渲染出正确的结果图。

我推测由于ChatGPT训练的时间是21年的,所以其给出的代码版较低,而Mermaid近期升级了对C4模型的脚本规范,所以,我试图提升ChatGPT升级一下:

提示:这不是最新版的,请用Mermaid的最新版的C4 Diagrams脚本,再次输出

ChatGPT:

输出的结果,依然在我的渲染插件中无法展示。

所以,我尝试换了一种方法,来让其帮我继续做C4模型图,这也是我发现的一个大惊喜:

你给ChatGPT一个内容模版,它立马就会学会应用,而且效果惊人。

下面我们继续来看新方法的过程。

C4模型实验

C4Context视图

我输入的提示:下面,我要给你一个最新版本的Mermaid C4 Diagrams脚本案例,你要记住哦

ChatGPT:好的,尽管跟我分享吧。

我将从Mermaid官网上复制的C4Context代码输入给了ChatGPT:

C4Context
      title System Context diagram for Internet Banking System
      Enterprise_Boundary(b0, "BankBoundary0") {
        Person(customerA, "Banking Customer A", "A customer of the bank, with personal bank accounts.")
        Person(customerB, "Banking Customer B")
        Person_Ext(customerC, "Banking Customer C", "desc")

        Person(customerD, "Banking Customer D", "A customer of the bank, <br/> with personal bank accounts.")

        System(SystemAA, "Internet Banking System", "Allows customers to view information about their bank accounts, and make payments.")

        Enterprise_Boundary(b1, "BankBoundary") {

          SystemDb_Ext(SystemE, "Mainframe Banking System", "Stores all of the core banking information about customers, accounts, transactions, etc.")

          System_Boundary(b2, "BankBoundary2") {
            System(SystemA, "Banking System A")
            System(SystemB, "Banking System B", "A system of the bank, with personal bank accounts. next line.")
          }

          System_Ext(SystemC, "E-mail system", "The internal Microsoft Exchange e-mail system.")
          SystemDb(SystemD, "Banking System D Database", "A system of the bank, with personal bank accounts.")

          Boundary(b3, "BankBoundary3", "boundary") {
            SystemQueue(SystemF, "Banking System F Queue", "A system of the bank.")
            SystemQueue_Ext(SystemG, "Banking System G Queue", "A system of the bank, with personal bank accounts.")
          }
        }
      }

      BiRel(customerA, SystemAA, "Uses")
      BiRel(SystemAA, SystemE, "Uses")
      Rel(SystemAA, SystemC, "Sends e-mails", "SMTP")
      Rel(SystemC, customerA, "Sends e-mails to")

      UpdateElementStyle(customerA, $fontColor="red", $bgColor="grey", $borderColor="red")
      UpdateRelStyle(customerA, SystemAA, $textColor="blue", $lineColor="blue", $offsetX="5")
      UpdateRelStyle(SystemAA, SystemE, $textColor="blue", $lineColor="blue", $offsetY="-10")
      UpdateRelStyle(SystemAA, SystemC, $textColor="blue", $lineColor="blue", $offsetY="-40", $offsetX="-50")
      UpdateRelStyle(SystemC, customerA, $textColor="red", $lineColor="red", $offsetX="-50", $offsetY="20")

      UpdateLayoutConfig($c4ShapeInRow="3", $c4BoundaryInRow="1")

ChatGPT:

提示:请按照上面给的案例,输出我们修改后的账号系统的C4Context的Mermaid脚本,用markdown输出

ChatGPT输出:

C4Context
  title Account System Context Diagram

  Enterprise_Boundary(EB, "Account System Boundary") {
    System(AccountSystem, "Account System", "Allows users to manage their accounts, passwords, and login sessions.")
    User(User, "User", "A user of the account system.")

    System_Ext(MailSystem, "Email System", "The internal email system.")
    System_Ext(SMS, "SMS System", "The system that sends SMS messages for 2FA.")

    AccountSystem --> User
    AccountSystem --> MailSystem
    AccountSystem --> SMS
  }

  BiRel(User, AccountSystem, "Uses")
  Rel(AccountSystem, MailSystem, "Sends emails")
  Rel(AccountSystem, SMS, "Sends SMS messages for 2FA")

  UpdateElementStyle(User, $fontColor="red", $bgColor="grey", $borderColor="red")
  UpdateRelStyle(User, AccountSystem, $textColor="blue", $lineColor="blue", $offsetX="5")
  UpdateRelStyle(AccountSystem, MailSystem, $textColor="blue", $lineColor="blue", $offsetY="-10", $offsetX="-50")
  UpdateRelStyle(AccountSystem, SMS, $textColor="blue", $lineColor="blue", $offsetY="10", $offsetX="-50")

代码渲染后为:

实验成功,那接下来的其他的C4模型,就可以如法炮制啦。

C4Container视图

提供Mermaid官网的demo后,就可以提示:

请按照上面给的案例,输出我们我们要处理账号系统C4Container的Mermaid脚本,用markdown输出

ChatGPT输出代码为:

C4Container
title Container diagram for Account System

System_Ext(identity_provider, "Identity Provider", "An external identity provider, such as Auth0 or Okta", $tags="v1.0")
Person(user, User, "A user of the system, with a registered account", $tags="v1.0")

Container_Boundary(c1, "Account System") {
    Container_Ext(web_app, "Web Application", "Java, Spring MVC", "Delivers the static content and the account system SPA")
    Container(spa, "Single-Page App", "JavaScript, Angular", "Provides all the account system functionality to users via their web browser")
    ContainerDb(database, "Database", "SQL Database", "Stores user registration information, hashed auth credentials, access logs, etc.")
    ContainerDb_Ext(api, "API Application", "Java, Docker Container", "Provides account system functionality via API")

}

Rel(user, web_app, "Uses", "HTTPS")
UpdateRelStyle(user, web_app, $offsetY="60", $offsetX="90")
Rel(user, spa, "Uses", "HTTPS")
UpdateRelStyle(user, spa, $offsetY="-40")
Rel(web_app, spa, "Delivers")
UpdateRelStyle(web_app, spa, $offsetX="130")
Rel(spa, api, "Uses", "async, JSON/HTTPS")
Rel_Back(database, api, "Reads from and writes to", "sync, JDBC")

Rel(identity_provider, user, "Provides authentication for")
UpdateRelStyle(identity_provider, user, $offsetX="-45")
Rel(api, identity_provider, "Uses", "async, OAuth2/OpenID Connect")
UpdateRelStyle(api, identity_provider, $offsetY="-60")

脚本渲染后图片为:

C4Component视图

提供Mermaid官网的demo后,就可以提示:

请按照上面给的案例,输出我们我们要处理账号系统C4Component的Mermaid脚本,用markdown输出

ChatGPT输出代码渲染后图片为:

C4Dynamic 视图

提供Mermaid官网的demo后,就可以提示:

请按照上面给的案例,输出我们我们要处理账号系统C4Component的Mermaid脚本,用markdown输出

ChatGPT输出代码渲染后图片为:

C4Deployment视图

提供Mermaid官网的demo后,就可以提示:

请按照上面给的案例,输出我们我们要处理账号系统C4Deployment的Mermaid脚本,用markdown输出

ChatGPT输出代码渲染后图片为:

备注:在ChatGPT熟悉了2-3次过程后,只要输入模版,其就会自动给出结果啦,连提示都可以省略了,重复工作完全自动化啦。

复杂状态试图

在今天的实验中,最后尝试做了一个负责的系统状态转化图,具体如下:

提示:在账号系统中的EmailVerification,PhoneVerification,ThirdPartyBinding的状态发生变化时,Account中对应信息变化的 Mermaid 脚本

ChatGPT:

以下是在账号系统中的EmailVerification,PhoneVerification,ThirdPartyBinding的状态发生变化时,Account中对应信息变化的Mermaid脚本:

给出的代码如下:

stateDiagram-v2
  state "Account" as account {
    state "EmailVerified" as emailVerified
    state "PhoneVerified" as phoneVerified
    state "ThirdPartyBound" as thirdPartyBound

    [*] --> emailVerified : initialize
    [*] --> phoneVerified : initialize
    [*] --> thirdPartyBound : initialize

    emailVerified --> phoneVerified : Email verified
    emailVerified --> thirdPartyBound : Third-party bound
    phoneVerified --> thirdPartyBound : Third-party bound

    emailVerified --> emailVerified : Re-verify email
    phoneVerified --> phoneVerified : Re-verify phone
    thirdPartyBound --> thirdPartyBound : Unbind third-party

    emailVerified --> * : Email unverified
    phoneVerified --> * : Phone unverified
    thirdPartyBound --> * : No third-party
  }

这个脚本包含了四个节点,分别表示账户,电子邮件验证,电话验证和第三方绑定。每个节点有对应的状态,包括已验证,无效,已绑定和未绑定。节点之间的关系用箭头表示,例如账户有电子邮件验证,电话验证和第三方绑定。在状态发生变化时,节点的颜色也会相应地改变。

代码渲染图片:

继续尝试,输出以上变化的序列图,提示:

ChatGPT:

以下是在账号系统中的EmailVerification,PhoneVerification,ThirdPartyBinding的状态发生变化时,Account中对应信息变化的序列图的Mermaid脚本:

sequenceDiagram
    participant Account
    participant EmailVerification
    participant PhoneVerification
    participant ThirdPartyBinding

    Account ->> EmailVerification: VerifyEmail(email, code)
    activate EmailVerification
    EmailVerification -->> Account: EmailVerified(email)
    deactivate EmailVerification

    Account ->> PhoneVerification: VerifyPhone(phone, code)
    activate PhoneVerification
    PhoneVerification -->> Account: PhoneVerified(phone)
    deactivate PhoneVerification

    Account ->> ThirdPartyBinding: BindThirdParty(thirdPartyAccount)
    activate ThirdPartyBinding
    ThirdPartyBinding -->> Account: ThirdPartyBound(thirdPartyAccount)
    deactivate ThirdPartyBinding

    Account ->> ThirdPartyBinding: UnbindThirdParty(thirdPartyAccount)
    activate ThirdPartyBinding
    ThirdPartyBinding -->> Account: ThirdPartyUnbound(thirdPartyAccount)
    deactivate ThirdPartyBinding

上面的序列图描述了Account对象和三个相关对象(EmailVerification,PhoneVerification和ThirdPartyBinding)之间的交互,以处理EmailVerification,PhoneVerification和ThirdPartyBinding状态变化的情况。

在这个序列图中,Account对象向EmailVerification,PhoneVerification和ThirdPartyBinding发送请求,以更新与它们关联的信息。每个参与者在收到请求后执行其操作,并通过消息传递结果将结果返回给Account对象,以通知其信息已被更新。

渲染后的图片为:

写在最后

今天的实验,就这么多啦,希望对你有启发哦。

在日常的系统设计中,创造系统图表是一件不太容易,也非常繁琐的事情,而ChatGPT则可以很好的帮我们处理这些问题,让我们可以节省不少的时间和精力哦。

在实验的过程中,也可以发现ChatGPT存在这个各种可能性,只要我们能给出不错的提示,提供有效的上下文,他就可以快速的学习,并将此结果有效的利用在接下来的任务之中。

ChatGPT还有很多未知的能力,值得每一个人去探索,保持好奇心吧。

有关用ChatGPT,绘制一个账号系统的C4架构图的更多相关文章

  1. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  2. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  3. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  4. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  5. ruby - 为什么 SecureRandom.uuid 创建一个唯一的字符串? - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?

  6. ruby-on-rails - Rails - 从另一个模型中创建一个模型的实例 - 2

    我有一个正在构建的应用程序,我需要一个模型来创建另一个模型的实例。我希望每辆车都有4个轮胎。汽车模型classCar轮胎模型classTire但是,在make_tires内部有一个错误,如果我为Tire尝试它,则没有用于创建或新建的activerecord方法。当我检查轮胎时,它没有这些方法。我该如何补救?错误是这样的:未定义的方法'create'forActiveRecord::AttributeMethods::Serialization::Tire::Module我测试了两个环境:测试和开发,它们都因相同的错误而失败。 最佳答案

  7. ruby - 用 Ruby 编写一个简单的网络服务器 - 2

    我想在Ruby中创建一个用于开发目的的极其简单的Web服务器(不,不想使用现成的解决方案)。代码如下:#!/usr/bin/rubyrequire'socket'server=TCPServer.new('127.0.0.1',8080)whileconnection=server.acceptheaders=[]length=0whileline=connection.getsheaders想法是从命令行运行这个脚本,提供另一个脚本,它将在其标准输入上获取请求,并在其标准输出上返回完整的响应。到目前为止一切顺利,但事实证明这真的很脆弱,因为它在第二个请求上中断并出现错误:/usr/b

  8. ruby - 一个 YAML 对象可以引用另一个吗? - 2

    我想让一个yaml对象引用另一个,如下所示:intro:"Hello,dearuser."registration:$introThanksforregistering!new_message:$introYouhaveanewmessage!上面的语法只是它如何工作的一个例子(这也是它在thiscpanmodule中的工作方式。)我正在使用标准的ruby​​yaml解析器。这可能吗? 最佳答案 一些yaml对象确实引用了其他对象:irb>require'yaml'#=>trueirb>str="hello"#=>"hello"ir

  9. ruby - Rails 关联 - 同一个类的多个 has_one 关系 - 2

    我的问题的一个例子是体育游戏。一场体育比赛有两支球队,一支主队和一支客队。我的事件记录模型如下:classTeam"Team"has_one:away_team,:class_name=>"Team"end我希望能够通过游戏访问一个团队,例如:Game.find(1).home_team但我收到一个单元化常量错误:Game::team。谁能告诉我我做错了什么?谢谢, 最佳答案 如果Gamehas_one:team那么Rails假设您的teams表有一个game_id列。不过,您想要的是games表有一个team_id列,在这种情况下

  10. 亚特兰蒂斯的回声(中文版): chatGPT 的杰作 - 2

    英文版英文链接关注公众号在“亚特兰蒂斯的回声”中踏上一段难忘的冒险之旅,深入未知的海洋深处。足智多谋的考古学家AriaSeaborne偶然发现了一件古代神器,揭示了一张通往失落之城亚特兰蒂斯的隐藏地图。在她神秘的导师内森·兰登教授的指导和勇敢的冒险家亚历克斯·默瑟的帮助下,阿丽亚开始了一段危险的旅程,以揭开这座传说中城市的真相。他们的冒险之旅带领他们穿越险恶的大海、神秘的岛屿和充满陷阱和谜语的致命迷宫。随着Aria潜在的魔法能力的觉醒,她被睿智勇敢的QueenNeria的幻象所指引,她让她为即将到来的挑战做好准备。三人组揭开亚特兰蒂斯令人惊叹的隐藏文明,并了解到邪恶的巫师马拉卡勋爵试图利用其古

随机推荐