Wellfire Interactive // Expertise for established Django SaaS applications

This Old Pony: This is how you analyze your Django Monolith (for fun and profit)

Today we’re going to get into tools and tactics for analyzing your Django monolith.

So where do we start?

We’ll start back in high school.

Because I still remember the words of my high school math teacher when presenting a problem and querying the class on how to solve it.

Mr. Brunner: “What should we do first?”
Student: “Extract the coefficients… ?”
Mr. Brunner: “That’s right, draw a picture!”

It didn’t matter what anyone said or if anyone said anything at all. His response was always the same: “That’s right, draw a picture!”

A simple picture can untangle concepts

All these years later and I’m starting to think he was on to something…
 

Graphing model relationships

Step one is building a graphical representation of your models. This will include their fields and most importantly it will document relationships.

This turns out to be blindingly easy thanks to django-extensions. Follow the instructions to install[0] in your project if you have not already, then install the necessary dependencies for graphing.

This should be sufficient:

pip install pygraphviz

Now we’re ready. To start off, let’s build a PNG image:

./manage.py graph\_models -a -g -o models\_visualized.png

The result should look roughly like this (but bigger and without obfuscated and fuzzy text!)[1]:

Your project's models and relationships graphed

A little explanation of what we produced is in order.

First a _graph _in the mathematical sense was generated to represent the model relationships (more on this shortly). With the -a option we selected all applications; with the -g option we requested that models be grouped by application; and the -o option takes an output file name.

And for a bit more clarity, the main app is shown below highlighted in pink.

Image

 

Understanding the graph

Okay, so the graph is neat, but what matters is what it tells us.

By examining how models are grouped and where there are relationships, as well as the direction of those relationships and where there _aren’t _relationships, you can start to see where there are natural clusters and cleavages.

That’s what we get from the visualization.

There’s no special magic, no step-by-step procedures to follow. The relationships are either latent or… they’re not.

In either case, it often helps to put some firmer analysis on paper.
 

Understanding interdependencies with network analysis

So far we’ve built the PNG version. That’s quite helpful and in some cases will be enough to get you headed in the right direction.

In other cases you’ll need to go beyond this and pull some data out of the visualization.

Luckily, we can also build a Graphviz[2] compatible file for viewing in Graphviz.

./manage.py graph\_models -a \> project\_models.dot

Except instead of using Graphviz, we’ll use networx, a Python library for building and working with graph data.

Fire up a Python console (ideally using IPython or a Jupyter notebook) and get start:

\>\>\> from networkx import read\_dot \>\>\> project = read\_dot('project\_models.dot')

Now project is an instance of a networx Graph.

You can explore the size of it by checking the length of either the instance itself or it’s edges. _The former will tell you how many nodes (models) there are, the latter how many _edges (relationships).

\>\>\> project.edges()[1] ('myapp\_models\_CustomModel', 'auth\_models\_User')

That result is an example showing the relationship from the CustomModel to the User model. It’s a _directed relationship _in that the CustomModel depends on the User model here.

If it’s not apparent that this is great, it is great! You can create tables of relationships from app to app, see what one app depends on, which apps depend on another, and so on. Which is all amazingly helpful when getting more detailed clusters and cleavages, but also identifying _low risk _apps for getting started.

As for building these tables from the graph, that much is left as an exercise to the reader :)
 

Tools for non-model dependencies

So far we’ve purposefully limited the scope to model relationships. Django apps contain _a lot _more than just models, so how do you get beyond this?

There are other tools out there for this kind of analysis.

Snakefood[3] will build graphs based on an AST and has proven reasonably helpful in code analysis.

pydepgraph[4] is another however I haven’t [yet!] had an opportunity to try it. I’ll report back when I do.

And there are definitely other tools out there that I haven’t come across. If you know of any others I’d love to hear from you!

Belatedly yours,
Ben

[0] Installing django-extensions: https://django-extensions.readthedocs.io/en/latest/installation_instructions.html
[1] This is an example from an open source client project; it has been shrunken for email and to keep the focus on the high level.
[2] Graphviz is a program for viewing graphs http://graphviz.org/
[3] Snakefood http://furius.ca/snakefood/
[4] pydepgraph for dependency analysis https://github.com/stefano-maggiolo/pydepgraph
 

Learn from more articles like this how to make the most out of your existing Django site.