Skip to content

Commit

Permalink
Refactor mermaid (#923)
Browse files Browse the repository at this point in the history
* Rename subgraph to numbers and escape the actual name

* Add escape string and clean up printing of mermaid

* Added epa diagram for a set of applications
  • Loading branch information
ashwinsajiv authored Jul 3, 2020
1 parent eae73f0 commit db11277
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 11 deletions.
41 changes: 36 additions & 5 deletions pkg/mermaid/endpointanalysisdiagram/endpointanalysisdiagram.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,54 @@ func GenerateEndpointAnalysisDiagram(m *sysl.Module) (string, error) {
return generateEndpointAnalysisDiagramHelper(m, &[]externalLink{}, true)
}

//Similar to the above, but accepts a slice of application names
//and returns a diagram only including applications specified
func GenerateMultipleAppEndpointAnalysisDiagram(m *sysl.Module, appNames []string) (string, error) {
return generateMultipleAppEndpointAnalysisDiagramHelper(m, appNames, &[]externalLink{}, true)
}

//generateEndpointAnalysisDiagram is a helper which has additional arguments which need not be entered by the user
func generateEndpointAnalysisDiagramHelper(m *sysl.Module,
externalLinks *[]externalLink, theStart bool) (string, error) {
var result string
if theStart {
result = mermaid.GeneratedHeader + "graph TD\n"
}
count := 1
for appName, app := range m.Apps {
result += fmt.Sprintf(" subgraph %s\n", appName)
result += fmt.Sprintf(" subgraph %d[\"%s\"]\n", count, appName)
for epName, endPoint := range app.Endpoints {
statements := endPoint.Stmt
result += printEndpointAnalysisStatements(m, statements, mermaid.CleanString(epName), externalLinks)
}
result += " end\n"
count++
}
for _, eLink := range *externalLinks {
result += fmt.Sprintf(" %s --> %s\n", eLink.statement, eLink.endPoint)
}
return result, nil
}

func generateMultipleAppEndpointAnalysisDiagramHelper(m *sysl.Module, appNames []string,
externalLinks *[]externalLink, theStart bool) (string, error) {
var result string
if theStart {
result = mermaid.GeneratedHeader + "graph TD\n"
}
count := 1
for _, appName := range appNames {
result += fmt.Sprintf(" subgraph %d[\"%s\"]\n", count, appName)
endPoints := m.Apps[appName].Endpoints
for epName, endPoint := range endPoints {
statements := endPoint.Stmt
result += printEndpointAnalysisStatements(m, statements, mermaid.CleanString(epName), externalLinks)
}
result += " end\n"
count++
}
for _, eLink := range *externalLinks {
result += fmt.Sprintf(" %s-->%s\n", eLink.statement, eLink.endPoint)
result += fmt.Sprintf(" %s --> %s\n", eLink.statement, eLink.endPoint)
}
return result, nil
}
Expand All @@ -47,7 +78,7 @@ func printEndpointAnalysisStatements(m *sysl.Module, statements []*sysl.Statemen
switch c := statement.Stmt.(type) {
case *sysl.Statement_Call:
appEndPoint := fmt.Sprintf("%s-%s", mermaid.CleanString(c.Call.Target.Part[0]), mermaid.CleanString(c.Call.Endpoint))
result += fmt.Sprintf(" %s-->%s\n", endPoint, appEndPoint)
result += fmt.Sprintf(" %s --> %s\n", endPoint, appEndPoint)
pair := externalLink{appEndPoint, mermaid.CleanString(c.Call.Endpoint)}
if !externalLinksContain(*externalLinks, pair) {
*externalLinks = append(*externalLinks, pair)
Expand All @@ -63,9 +94,9 @@ func printEndpointAnalysisStatements(m *sysl.Module, statements []*sysl.Statemen
case *sysl.Statement_Foreach:
result += printEndpointAnalysisStatements(m, c.Foreach.Stmt, endPoint, externalLinks)
case *sysl.Statement_Action:
result += fmt.Sprintf(" %s-->%s\n", endPoint, mermaid.CleanString(c.Action.Action))
result += fmt.Sprintf(" %s --> %s\n", endPoint, mermaid.CleanString(c.Action.Action))
case *sysl.Statement_Ret:
result += fmt.Sprintf(" %s-->%s\n", endPoint, mermaid.CleanString(c.Ret.Payload))
result += fmt.Sprintf(" %s --> %s\n", endPoint, mermaid.CleanString(c.Ret.Payload))
default:
result += ""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,16 @@ func TestGenerateMermaidIntegrationDiagram3(t *testing.T) {
assert.NotNil(t, r)
assert.NoError(t, err)
}

func TestGenerateMermaidIntegrationDiagram4(t *testing.T) {
m, err := parse.NewParser().Parse("demo/simple/best-ever-sysl-example.sysl",
syslutil.NewChrootFs(afero.NewOsFs(), mermaid.ProjectDir))
if err != nil {
t.Error(err)
}
apps := []string{"TheWorld", "DragonEater"}
r, err := GenerateMultipleAppEndpointAnalysisDiagram(m, apps)
assert.NotNil(t, m)
assert.NotNil(t, r)
assert.NoError(t, err)
}
8 changes: 7 additions & 1 deletion pkg/mermaid/integrationdiagram/integrationdiagram.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package integrationdiagram
import (
"errors"
"fmt"
"strings"

"github.com/anz-bank/sysl/pkg/mermaid"
"github.com/anz-bank/sysl/pkg/sysl"
Expand Down Expand Up @@ -88,7 +89,8 @@ func printIntegrationDiagramStatements(m *sysl.Module, statements []*sysl.Statem
pair := integrationPair{appName, nextApp}
if !integrationPairsContain(*integrationPairs, pair) {
*integrationPairs = append(*integrationPairs, pair)
result += fmt.Sprintf(" %s --> %s\n", appName, nextApp)
result += fmt.Sprintf(" %s[\"%s\"] --> %s[\"%s\"]\n",
cleanAppName(appName), appName, cleanAppName(nextApp), nextApp)
out, err := generateIntegrationDiagramHelper(m, nextApp, integrationPairs, false)
if err != nil {
panic("Error in generating integration diagram; check if app name is correct")
Expand Down Expand Up @@ -133,3 +135,7 @@ func integrationPairsContain(i []integrationPair, ip integrationPair) bool {
}
return false
}

func cleanAppName(s string) string {
return strings.ReplaceAll(s, " ", "_")
}
19 changes: 14 additions & 5 deletions pkg/mermaid/sequencediagram/sequencediagram.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"regexp"
"strings"

"github.com/anz-bank/sysl/pkg/mermaid"
"github.com/anz-bank/sysl/pkg/sysl"
Expand Down Expand Up @@ -35,7 +36,7 @@ func generateSequenceDiagramHelper(m *sysl.Module, appName string, epName string
if err := isValidAppNameAndEndpoint(m, appName, epName); err != nil {
return "", err
}
result += fmt.Sprintf(" %s->>%s: %s\n", previousApp, appName, epName)
result += fmt.Sprintf(" %s ->> %s: %s\n", previousApp, cleanAppName(appName), epName)
}
statements := m.Apps[appName].Endpoints[epName].GetStmt()
result += printSequenceDiagramStatements(m, statements, appName, previousApp, indent, sequencePairs, theStart)
Expand Down Expand Up @@ -125,20 +126,24 @@ func isValidAppNameAndEndpoint(m *sysl.Module, appName string, epName string) er

//callStatement is a printer to print a call statement
func callStatement(appName string, epName string, nextApp string, indent int) string {
return fmt.Sprintf("%s%s->>+%s: %s\n", addIndent(indent), appName, nextApp, epName)
return fmt.Sprintf("%s%s ->>+ %s: %s\n", addIndent(indent),
cleanAppName(appName), cleanAppName(nextApp), epName)
}

//retStatement is a printer to print a return statement
func retStatement(appName string, epName string, previousApp string, indent int, theStart bool) string {
if theStart {
return fmt.Sprintf("%s%s-->>%s: %s\n", addIndent(indent), appName, previousApp, epName)
return fmt.Sprintf("%s%s -->> %s: %s\n", addIndent(indent),
cleanAppName(appName), cleanAppName(previousApp), epName)
}
return fmt.Sprintf("%s%s-->>-%s: %s\n", addIndent(indent), appName, previousApp, epName)
return fmt.Sprintf("%s%s -->>- %s: %s\n", addIndent(indent),
cleanAppName(appName), cleanAppName(previousApp), epName)
}

//actionStatement is a printer to print an action statement
func actionStatement(appName string, action string, indent int) string {
return fmt.Sprintf("%s%s->>%s: %s\n", addIndent(indent), appName, appName, action)
return fmt.Sprintf("%s%s ->> %s: %s\n", addIndent(indent),
cleanAppName(appName), cleanAppName(appName), action)
}

//addIndent adds indents based on the input
Expand All @@ -159,3 +164,7 @@ func sequencePairsContain(s []sequencePair, sp sequencePair) bool {
}
return false
}

func cleanAppName(s string) string {
return strings.ReplaceAll(s, "-", "_")
}

0 comments on commit db11277

Please sign in to comment.