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

Node as cluster #438

Open
wants to merge 24 commits into
base: master
Choose a base branch
from

Conversation

bkmeneguello
Copy link

@bkmeneguello bkmeneguello commented Jan 19, 2021

This PR contains and supersedes the #407
I'm collaborating with the OP @dan-ash but he seems to be having personal issues to keep this PR up to date.

from diagrams import Cluster, Diagram
from diagrams.aws.compute import ECS
from diagrams.aws.database import RDS, Aurora
from diagrams.aws.network import Route53, VPC

with Diagram("Simple Web Service with DB Cluster", show=True, filename="mysql"):
    dns = Route53("dns")
    web = ECS("service")

    with VPC('VPC'):
        # using cluster with an icon
        with Cluster("DB ClusterA", icon=ECS):
            db_master1 = RDS("main")
            db_master1 - [RDS("replica1"), RDS("replica2")]
        # using the node
        with Aurora("DB ClusterA") as db2:
            db_master2 = RDS("main")
            db_master2 - [RDS("replica1"), RDS("replica2")]

        dns >> web >> db_master1
        # link to/from cluster
        dns >> web >> db2

diagram

@mingrammer mingrammer added kind/feature New feature or request proposal Suggestion proposed status/need-to-review Need to review comp/cluster Issue of cluster component labels Jan 27, 2021
@dan-ash
Copy link
Contributor

dan-ash commented Jan 28, 2021

@bkmeneguello I suggest to close this in favour of #439 what do you think? did you got a change to go over it?

@bkmeneguello
Copy link
Author

@dan-ash Sorry, but I think this PR is more mature and featureful than #439. Maybe I could simplify the commits in more logical ones but this could remove your changes.

@seema1711
Copy link

I tried this, but I got the error.

    with VPC('VPC'):
AttributeError: __enter__

Can anyone help?

@seema1711
Copy link

seema1711 commented Feb 20, 2021

I tried this, but I got the error.

    with VPC('VPC'):
AttributeError: __enter__

Can anyone help?

Update:
I tried implementing this:

class VPC(Cluster):
    # fmt: off
    _default_graph_attrs = {
        "shape": "box",
        "style": "dashed",
        "labeljust": "l",
        "pencolor": "#8c8c8c",
        "fontname": "sans-serif",
        "fontsize": "12",
    }
    # fmt: on

class ECS(Cluster):
     # fmt: off
    _default_graph_attrs = {
        "shape": "box",
        "style": "dashed",
        "labeljust": "l",
        "pencolor": "#8c8c8c",
        "fontname": "sans-serif",
        "fontsize": "12",
    }
    # fmt: on

class Aurora(Cluster):
     # fmt: off
    _default_graph_attrs = {
        "shape": "box",
        "style": "dashed",
        "labeljust": "l",
        "pencolor": "#8c8c8c",
        "fontname": "sans-serif",
        "fontsize": "12",
    }
    # fmt: on

but it's not still displaying the cluster branding. Any solution?

One more doubt:
Line Spacing is not working i.e. \n. And what if we want to display two lines on one edge, one on top, and one on bottom. What to do in this case?
Edit: @clayms can you help?

@gabriel-tessier
Copy link
Collaborator

gabriel-tessier commented Feb 22, 2021

@bkmeneguello
I run all the examples I found in the source and figure out 2 issues:
First one is with the color, somehow the color order is shifted (I didn't checked the code yet so sorry for the lack of details).

Basically when you do this:

from diagrams import Diagram
from diagrams.onprem.network import Nginx
from diagrams.onprem.container import Docker

with Diagram(name="", direction="TB", show=False, filename="aws"):
    with Docker("Docker") as container:
      with Docker("Docker2 "):
        with Docker("Docker3 "):
          with Docker("Docker4 "):
            with Docker("Docker5 "):
                Nginx("Nginx")

    container

it render that:
aws

Where the first color is green.

If I run the same nested code with actual version I have this:

from diagrams import Diagram
from diagrams.onprem.network import Nginx
from diagrams.onprem.container import Docker

with Diagram(name="", direction="TB", show=False, filename="aws"):
    with Docker("Docker") as container:
      with Docker("Docker2 "):
        with Docker("Docker3 "):
          with Docker("Docker4 "):
            with Docker("Docker5 "):
                Nginx("Nginx")

    container

it render this one:
color
Which start with blue.

The second problem which is more serious even if there's no tests or example is the support for "graph_attr" in cluster.

from diagrams import Diagram, Cluster
from diagrams.onprem.network import Nginx
# from diagrams.onprem.container import Docker

with Diagram(name="", direction="TB", show=False, filename="aws"):
   with Cluster("AWS", graph_attr={"pencolor": "#60193C", "bgcolor": "#E587B5"}) as container: # overwrite attributes for the default cluster
       Nginx("Nginx")

   container
   

on master it render this:
pink_nginx

on your PR branch it generate an error:


Traceback (most recent call last):
  File "/usr/src/diagrams/example.py", line 9, in <module>
    container
  File "/usr/src/diagrams/diagrams/__init__.py", line 217, in __exit__
    super().__exit__(*args)
  File "/usr/src/diagrams/diagrams/__init__.py", line 71, in __exit__
    self.dot.subgraph(subgraph.dot)
  File "/usr/local/lib/python3.9/site-packages/graphviz/dot.py", line 244, in subgraph
    lines = ['\t' + line for line in graph.__iter__(subgraph=True)]
  File "/usr/local/lib/python3.9/site-packages/graphviz/dot.py", line 244, in <listcomp>
    lines = ['\t' + line for line in graph.__iter__(subgraph=True)]
  File "/usr/local/lib/python3.9/site-packages/graphviz/dot.py", line 110, in __iter__
    yield self._attr % (kw, self._attr_list(None, attrs))
  File "/usr/local/lib/python3.9/site-packages/graphviz/lang.py", line 136, in attr_list
    content = a_list(label, kwargs, attributes)
  File "/usr/local/lib/python3.9/site-packages/graphviz/lang.py", line 109, in a_list
    items = ['%s=%s' % (quote(k), quote(v))
  File "/usr/local/lib/python3.9/site-packages/graphviz/lang.py", line 109, in <listcomp>
    items = ['%s=%s' % (quote(k), quote(v))
  File "/usr/local/lib/python3.9/site-packages/graphviz/lang.py", line 72, in quote
    if is_html_string(identifier) and not isinstance(identifier, NoHtml):
TypeError: expected string or bytes-like object

@seema1711
Copy link

from diagrams import Diagram
from diagrams.onprem.network import Nginx
from diagrams.onprem.container import Docker

with Diagram(name="", direction="TB", show=False, filename="aws"):
    with Docker("Docker") as container:
      with Docker("Docker2 "):
        with Docker("Docker3 "):
          with Docker("Docker4 "):
            with Docker("Docker5 "):
                Nginx("Nginx")

    container

this code is not working on my machine, it throws an error:

    with Docker("Docker") as container:
AttributeError: __enter__

@gabriel-tessier
Copy link
Collaborator

@seema1711
Can you please stop posting on the issues, if you have problem or question you can use stackoverflow on how to use diagrams.
To make it clear this code is not working with your diagrams version, if you want to use the new feature consider forking the PR branch, if you don't understand what I'm talking about, just wait that this feature go in the new release.

Your problem is that you use the actual library version which don't have the code of the PR and it's normal that this error raise.
You are only making noise by posting that it's not working and make us waste time.

Copy link
Collaborator

@gabriel-tessier gabriel-tessier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here some fix for the problems I found and commented here: #438 (comment)

I didn't find others issue so far.

One last remarks is the examples directory maybe better to move the code in the website.

self.dot.graph_attr["rankdir"] = self._direction

# Set cluster depth for distinguishing the background color
self.depth = self._parent.depth + 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here need to be
self.depth = self._parent.depth + 1 if self._parent.depth else 1
To respect the color order.
Check the example in the comment: #438 (comment)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gabriel-tessier Thank you always for your volunteering. I'll contact you soon.

self.depth = self._parent.depth + 1
coloridx = self.depth % len(self.__bgcolors)
self.dot.graph_attr["bgcolor"] = self.__bgcolors[coloridx]

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to set the attributs at the end after all the default apply so you can overwrite in your diagrams.

    for params, attributs in self._attrs.items():
        if params == 'graph_attr':
            for k, v in attributs.items():
                self.dot.graph_attr[k] = v

self.dot.graph_attr[k] = v
for k, v in self._attrs.items():
self.dot.graph_attr[k] = v

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This 2 lines need to be removed: ("The second problem" listed in the comment #438 (comment))
for k, v in self._attrs.items():
self.dot.graph_attr[k] = v

And moved after all the attributes are set also the value of k ad v are not correct chek the code line 378.

@DonDebonair
Copy link

Any progress on this one? Would love to have this functionality!

@nvictor nvictor mentioned this pull request Apr 6, 2022
shagr4th pushed a commit to shagr4th/diagrams that referenced this pull request Jun 29, 2022
@mingrammer
Copy link
Owner

I'm here. Sorry for the late reply. I'll review this PR ASAP. It could be really good improvements. Thank you :)

@mingrammer
Copy link
Owner

@bkmeneguello Please resolve the conflicts.

@jobinjosem1
Copy link

@bkmeneguello Do you have time to resolve the conflicts? We really would like to have this feature.

@bkmeneguello
Copy link
Author

Sorry, two years ago I had the time, now I'm not involved anymore with this project.

@lazzurs
Copy link
Contributor

lazzurs commented Dec 14, 2022

I am willing to take this branch and resolve the conflicts if no one minds? I will open this as a new PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp/cluster Issue of cluster component kind/feature New feature or request proposal Suggestion proposed status/need-to-review Need to review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants