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

[Question] about nodes #306

Open
sitereliabilityengineer opened this issue Sep 30, 2020 · 14 comments
Open

[Question] about nodes #306

sitereliabilityengineer opened this issue Sep 30, 2020 · 14 comments

Comments

@sitereliabilityengineer

Hello everyone, first of all, apologies if here it's not the principal way to ask my doubts.

I want to dynamically create diagrams. For example, if you have a server that's not healthy, you want to show it with a cross over it or maybe just red around the node.

For example:

`from diagrams.onprem.network import Nginx
from diagrams.onprem.database import PostgreSQL

with Diagram("Advanced Web Service with On-Premise", show=False):
ingress = Nginx("ingress")

with Cluster("Database HA"):
master = PostgreSQL("users")
master - PostgreSQL("slave") <-- And this server is not healthy`

So "master - PostgreSQL("slave")" is not healthy and I want to show it as non-healthy in the diagram. Which should be the best way to do it ?

@steschuser
Copy link

Hello,

nice idea.
The easiest way would be to show the connect as broken, I think
That is, change the color from green to red between the systems.

import random
status = random.choice(["red","green"])
master - Edge(labe="connection", color=status) - slave

Or you could try to set the node_attr of that node
node_attr = = {
"fontsize": "45",
"bgcolor": "red"
}
slave - PostgreSQL("slave", node_attr=node_attr)
https://diagrams.mingrammer.com/docs/guides/diagram
https://www.graphviz.org/doc/info/attrs.html

@sitereliabilityengineer
Copy link
Author

Hello,

nice idea.
The easiest way would be to show the connect as broken, I think
That is, change the color from green to red between the systems.

import random
status = random.choice(["red","green"])
master - Edge(labe="connection", color=status) - slave

Or you could try to set the node_attr of that node
node_attr = = {
"fontsize": "45",
"bgcolor": "red"
}
slave - PostgreSQL("slave", node_attr=node_attr)
https://diagrams.mingrammer.com/docs/guides/diagram
https://www.graphviz.org/doc/info/attrs.html

Thanks for the help.

I liked the second option, but it didn't changed the bg color at all:

`
import json

node_attr = {
"fontsize": "45",
"bgcolor": "red"
}

node_attr_json = json.dumps(node_attr)

with Diagram("Advanced Web Service with On-Premise", show=False):
ingress = Nginx("ingress")

with Cluster("Database HA"):
    master = PostgreSQL("users")
    master - PostgreSQL("slave", node_attr=node_attr_json)

`
If I don't convert the json to string, when I add the node_attr=node_attr gives an Exception.

After doing the json.dumps no exception is given, but the bg color is the same as default (cluster)

If this is not possible, maybe I should use your first option.

Thanks.

@sitereliabilityengineer
Copy link
Author

Hello @steschuser

Any idea why node_attr doesn't work for me ?

Thanks.

@steschuser
Copy link

Hello again,
Im afraid not. I havent actually played around with it yet.

My suggestion would be to check out the genrated .dot file #182
and check with Graphviz https://github.com/graphp/graphviz

Its on my agenda to take a look into this but time is limited...

@sitereliabilityengineer
Copy link
Author

Thank you @steschuser !

@redNixon
Copy link

redNixon commented Oct 3, 2020

@sitereliabilityengineer

The first issue is that setting bgcolor doesn't seem to be the attribute you are really after the link steschuser posted goes into all the different ways to define a color attribute and when they will matter(honestly I still don't understand its logic fully). However how exactly to pass in attributes any place besides Diagram() is at best unclear if not confusing to be point of it being called "bugged" in my mind. Here is a modified version of your code and the resultant diagram:

from diagrams import Cluster, Diagram
from diagrams.onprem.database import PostgreSQL
from diagrams.onprem.network import Nginx

offline_node_attrs = {"fillcolor": "red", "style": "filled"}

with Diagram("Advanced Web Service with On-Premise", show=False) as diag:
    ingress = Nginx("ingress")

    with Cluster("Database HA"):
        master = PostgreSQL("users")

        master - PostgreSQL("slave", **offline_node_attrs)
diag

Screenshot from 2020-10-03 06-14-15

Besides changing what attributes are being set, notice how the dictionary has to be passed in:

master - PostgreSQL("slave", **offline_node_attrs)

A more standardized name for these attribute variables in the __init__ function of Node, Edge and Cluster would be "kwargs". To make it clearer about why the dictionary containing attributes needs to be unpacked during the initialization call, consider that this line works the same as the one above and produces the same result:

`master - PostgreSQL("slave", fillcolor="red", style="filled")

@sitereliabilityengineer
Copy link
Author

Hi @redNixon !! That's awesome, thank you so much.

I also have issues with the nodes position within a clúster. I have approx 50 Tomcat nodes in a cluster and when I visualized them, they are all positioned in the same column, so the diagram looks very strange for me.

There's anyway to specify the number of columns in a cluster or change the position of the nodes?

Sorry to ask you all this, but I couldn't find much info.

Thank you again.

@redNixon
Copy link

redNixon commented Oct 3, 2020

Can you post the code? The answer is yes but what is making it "weird" can often changes and have a simple fix that can be found most often to make graphviz start flowing as expected instead of trying to position everything manually. When using graphviz indirectly(ie via diagrams) edges, line breaks and transparent clusters are some methods to force some behavior without getting into the graphviz logic though.

I will say that while diagrams is an amazing library to let you quickly code out a diagram, if you start creating real large or have real complex interconnected graphs then utilizing the graphviz library directly will probably be easier(50 nodes is not large btw) to use.

@sitereliabilityengineer
Copy link
Author

Can you post the code? The answer is yes but what is making it "weird" can often changes and have a simple fix that can be found most often to make graphviz start flowing as expected instead of trying to position everything manually. When using graphviz indirectly(ie via diagrams) edges, line breaks and transparent clusters are some methods to force some behavior without getting into the graphviz logic though.

I will say that while diagrams is an amazing library to let you quickly code out a diagram, if you start creating real large or have real complex interconnected graphs then utilizing the graphviz library directly will probably be easier(50 nodes is not large btw) to use.

Hey @redNixon

Sure, my code is not complex.
`
from diagrams import Cluster, Diagram
from diagrams.aws.network import ELB
from diagrams.aws.network import Route53
from diagrams.onprem.network import Tomcat

graph_attr = {
"fontsize": "25",
"bgcolor": "red"
}

with Diagram("DC1 DIAGRAM", show=False):
dns = Route53("dns")
lb = ELB("lb")

with Cluster("Application servers"):
    app_group = [
            Tomcat("server-app1"),
            Tomcat("server-app2"),
            Tomcat("server-app3"),
            ......
            Tomcat("server-app50")
            ]

    with Cluster("IN_MAINTENANCE", graph_attr=graph_attr):
        app_maintenance = [Tomcat("server-app14)]

with Cluster("DB Cluster"):
    db_master = RDS("userdb")
    db_master - [RDS("userdb ro")]

memcached = ElastiCache("memcached")

dns >> lb >> app_group
app_group >> db_master
app_group >> memcached

`

So basically, app_group appear all in the same column, with a few nodes it's ok, but when you have 50 nodes in the same column, it's not great.

Thank you!

@steschuser
Copy link

This looks like the problem in #276

@sitereliabilityengineer
Copy link
Author

sitereliabilityengineer commented Oct 6, 2020

Hello @steschuser and @redNixon

Thanks a lot for your help!!

@sitereliabilityengineer
Copy link
Author

Tried adding size & ratio, but I can't see any difference.

@clayms
Copy link

clayms commented Nov 4, 2020

I also have issues with the nodes position within a clúster. I have approx 50 Tomcat nodes in a cluster and when I visualized them, they are all positioned in the same column, so the diagram looks very strange for me.

There's anyway to specify the number of columns in a cluster or change the position of the nodes?

@sitereliabilityengineer

See if you can adapt the following to fit your needs:

from diagrams import Diagram, Cluster, Edge, Node

graph_attr = {
    "layout":"neato",
    }

clus_attr = {
    "bgcolor":"transparent",
    "pencolor":"blue",
    "penwidth":"4.0"
    }

with Diagram("Clusters", show=False, graph_attr=graph_attr) as diag:

    with Cluster("A"):
        A_UpLf = Node("", shape="plaintext", pin="true", pos="0,4")
        A_LwRt = Node("", shape="plaintext", pin="true", pos="4,0")
        A1 = Node("A1", shape="circle", labelloc="c", pin="true", pos="1,0")
        A2 = Node("A2", shape="triangle",    labelloc="c", pin="true", pos="3,3.5")
    with Cluster("B"):
        B_UpLf = Node("", shape="plaintext", pin="true", pos="6,4")
        B_LwRt = Node("", shape="plaintext", pin="true", pos="10,-0.5")
        B1 = Node("B1", shape="star", labelloc="c", pin="true", pos="9,4")
        B2 = Node("B2", shape="box3d",    labelloc="c", pin="true", pos="7,1.5")
    with Cluster("C", graph_attr=clus_attr):
        C_UpLf = Node("", shape="plaintext", pin="true", pos="2,3")
        C_LwRt = Node("", shape="plaintext", pin="true", pos="8,-2")
        C1 = Node("C1", shape="folder", labelloc="c", pin="true", pos="6,2.5")
        C2 = Node("C2", shape="rarrow", labelloc="c", pin="true", pos="2.5,-1")

diag

image

@doluk
Copy link

doluk commented Oct 22, 2021

the code above is currently not working as expected

debian 10

$ pip freeze
diagrams==0.20.0
graphviz==0.16

$ dot -V
dot - graphviz version 2.40.1 (20161225.0304)

clusters

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants