{"id":11363,"date":"2017-02-20T12:09:30","date_gmt":"2017-02-20T12:09:30","guid":{"rendered":"https:\/\/sep.live.e3cms.net\/blog\/graphviz-tool-arent-using\/"},"modified":"2017-02-20T12:09:30","modified_gmt":"2017-02-20T12:09:30","slug":"graphviz-tool-arent-using","status":"publish","type":"post","link":"https:\/\/hexboxdev.wpenginepowered.com\/blog\/graphviz-tool-arent-using\/","title":{"rendered":"GraphViz &#8211; A Tool You Aren&#8217;t Using, but Should"},"content":{"rendered":"<p><!--kg-card-begin: markdown--><\/p>\n<p>Recently, we had a book club where we read the <a href=\"https:\/\/www.amazon.com\/Mikado-Method-Ola-Ellnestam\/dp\/1617291218\">Mikado Method<\/a>, a process of handling large scale rearchitecting via refactors on the Master Branch without adversely impacting new features. One of the techniques that make up the method is visualizing the refactoring plan as a digraph of dependent steps. But as much as I love sketch notes, fountain pens, and sharpies, I can make a right mess of a piece of paper when I can\u2019t fit all the nodes on a graph where I want.<\/p>\n<p>Enter <a href=\"https:\/\/graphviz.org\/\">GraphViz<\/a>. It\u2019s a suite of tools for laying out diagrams. In particular, I learned just enough of the <a href=\"https:\/\/www.graphviz.org\/pdf\/dotguide.pdf\">dot language<\/a> to track my work. Here\u2019s an example Mikado Graph where we are replacing a legacy data store with a new database engine.<\/p>\n<p>digraph G { subgraph finishedTasks { node[color=&#8221;gray&#8221;, fontcolor=&#8221;gray&#8221;]; &#8220;Service Class&#8221; &#8220;Move User Queries\\nto Service&#8221; &#8220;Move Project Queries\\nto Service&#8221; &#8220;Consolidate\\nBehind Interface&#8221; } subgraph currentTask { node[color=&#8221;orange&#8221;, style=filled]; &#8220;Migrate\\nData&#8221; } goal [peripheries=2, label=&#8221;Replace\\nDB Engine&#8221;] ; goal -&gt; &#8220;Migrate\\nData&#8221; -&gt; &#8220;Consolidate\\nBehind Interface&#8221; goal -&gt; &#8220;Rewrite\\nquery methods&#8221; -&gt; &#8220;Consolidate\\nBehind Interface&#8221; &#8220;Consolidate\\nBehind Interface&#8221; -&gt; &#8220;Move User Queries\\nto Service&#8221; &#8220;Consolidate\\nBehind Interface&#8221; -&gt; &#8220;Move Project Queries\\nto Service&#8221; &#8220;Move User Queries\\nto Service&#8221; -&gt; &#8220;Service Class&#8221; &#8220;Move Project Queries\\nto Service&#8221; -&gt; &#8220;Service Class&#8221; }<\/p>\n<p>And here\u2019s what it looks like. The Double Line represents the ideal goal state. The arrows show work that\u2019s dependent on other work. The orange is the task currently being worked on, and the gray nodes are tasks we have completed. While this example is from a real use case, the actual graphs tend to be larger and emerge over time as we uncover more dependencies. So leaving a trail like this is super useful for an effort that spans several days or weeks with multiple commits a day. And I can slack the image so the team knows how things are going.<br \/>\n<img decoding=\"async\" src=\"https:\/\/hexboxdev.wpenginepowered.com\/wp-content\/uploads\/2025\/12\/2017-02-mikado.png\" alt=\"\" \/><\/p>\n<h2 id=\"howtodot\">How to DOT<\/h2>\n<ul>\n<li><strong>digraph<\/strong> \u2013 This allows us to use connections with arrows<\/li>\n<li><strong>node definition<\/strong> \u2013 A node is either a bare word, or a string. Nodes use \u201cautomatic semicolon insertion\u201d just like javascript. You can put several statements on a line if you use a semicolon between them.<\/li>\n<li><strong>styling nodes<\/strong> \u2013 You can style nodes by using a <em>node<\/em> command to modify all the nodes in a graph or by adding attributes to a node. I used attributes on the goal node.<\/li>\n<li><strong>subgraph<\/strong> \u2013 This allows me to style all the nodes at once. I just need to mention that a node exists in the subgraph and have the connection in the main graph below.<\/li>\n<li><strong>node connections<\/strong> \u2013 I\u2019m using a digraph, so the nodes need to be connected with an arrow <em>-&gt;<\/em>. I can chain several connections together. I can mention nodes repeatedly.<\/li>\n<\/ul>\n<h2 id=\"theeditcycle\">The edit cycle<\/h2>\n<ol>\n<li>Write the file: <em><strong>mikado.dot<\/strong><\/em><\/li>\n<li>Run dot: <em><strong>dot -Tpng -omikado.png mikado.dot<\/strong><\/em><\/li>\n<li>Open the file: <em><strong>open \u2014background mikado.png<\/strong><\/em><\/li>\n<\/ol>\n<p><em><strong>open<\/strong><\/em> is a macOS specific program. Windows folks can use<\/p>\n<div class=\"codecolorer-container text railscasts code-inline\" style=\"overflow: auto;\">\n<div class=\"text codecolorer\">start<\/div>\n<\/div>\n<p>. For linux, try ***open*** or check with your distro or tell someone on the internet it can\u2019t be done and they will correct you loudly.<\/p>\n<h2 id=\"howtofaster\">How to <em>faster<\/em><\/h2>\n<p>First, I wrote a shell script to combine two steps. Write the script and open the picture. I\u2019ll call it openDot.sh.<\/p>\n<div class=\"codecolorer-container text railscasts code-multiline\" style=\"overflow: auto;\">\n<div class=\"text codecolorer\">\n<p>#! \/bin\/sh<\/p>\n<p>INFILE=$1<br \/>\nFILEBASE=<code>basename $1 .dot<\/code><br \/>\nDIR=<code>dirname $1<\/code><br \/>\nOUTFILE=&#8221;$DIR\/$FILEBASE.png&#8221;<br \/>\ndot -Tpng -o$OUTFILE $INFILE<br \/>\nopen &#8211;background $OUTFILE<\/p>\n<\/div>\n<\/div>\n<p>Now I can run <em><strong>.\/openDot.sh mikado.dot<\/strong><\/em> and the png opens in preview!<\/p>\n<p>But that\u2019s still too much work. I need to use <em><strong>fswatch<\/strong><\/em> to pump out the updated file and xargs to update. (get you a bash subsystem. git-bash if nothing else) .<\/p>\n<div class=\"codecolorer-container text railscasts code-multiline\" style=\"overflow: auto;\">\n<div class=\"text codecolorer\">fswatch mikado.dot | xargs .\/openDot.sh<\/div>\n<\/div>\n<p>Seriously. Learn xargs. <em>Soooo Goood<\/em><\/p>\n<h2 id=\"greatbutnowwhat\">Great, but now what?<\/h2>\n<p>You can write graphs by hand, or write them with scripts!<\/p>\n<ol>\n<li>Sketch out relations to discuss.<\/li>\n<li><a href=\"https:\/\/graphviz.org\/Gallery\/directed\/fsm.html\">Document a state machine<\/a>.<\/li>\n<li><a href=\"https:\/\/github.com\/voormedia\/rails-erd\">Generate database tables used by your rails app<\/a>.<\/li>\n<li>Auto generate dependency graphs.<\/li>\n<li>Mind maps.<\/li>\n<li>Anything!<\/li>\n<\/ol>\n<p><!--kg-card-end: markdown--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently, we had a book club where we read the Mikado Method, a process of handling large scale rearchitecting via refactors on the Master Branch without adversely impacting new features. One of the techniques that make up the method is visualizing the refactoring plan as a digraph of dependent steps. But as much as I [&hellip;]<\/p>\n","protected":false},"author":18,"featured_media":31211,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"content-type":"","associated_team_member":0,"associated_user_id":0,"footnotes":""},"categories":[281,259],"tags":[297],"service":[],"class_list":["post-11363","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-practices","category-sep-tips","tag-tools"],"_links":{"self":[{"href":"https:\/\/hexboxdev.wpenginepowered.com\/wp-json\/wp\/v2\/posts\/11363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hexboxdev.wpenginepowered.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hexboxdev.wpenginepowered.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hexboxdev.wpenginepowered.com\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/hexboxdev.wpenginepowered.com\/wp-json\/wp\/v2\/comments?post=11363"}],"version-history":[{"count":0,"href":"https:\/\/hexboxdev.wpenginepowered.com\/wp-json\/wp\/v2\/posts\/11363\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/hexboxdev.wpenginepowered.com\/wp-json\/wp\/v2\/media\/31211"}],"wp:attachment":[{"href":"https:\/\/hexboxdev.wpenginepowered.com\/wp-json\/wp\/v2\/media?parent=11363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hexboxdev.wpenginepowered.com\/wp-json\/wp\/v2\/categories?post=11363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hexboxdev.wpenginepowered.com\/wp-json\/wp\/v2\/tags?post=11363"},{"taxonomy":"service","embeddable":true,"href":"https:\/\/hexboxdev.wpenginepowered.com\/wp-json\/wp\/v2\/service?post=11363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}