# Models

A model decides where the nodes of a [Mermaid diagram](graph.md) stand in space. The text stays the same: each
model answers another question about it, so the one to choose is the one whose question you are asking.

Mermaid is the most common way to write a diagram as text: a line such as `api --> db` says that the API is linked
to the database, and a `subgraph` puts a few nodes in a group. GitHub draws it, and so do many documentation
tools, so a team often has diagrams written in it already. Each model below says what it is good for, and shows
one example you can turn with the pointer.

| Model | Use it to show | It draws |
| --- | --- | --- |
| `planes` | Layers: which tier each thing belongs to | A stacked plane per group, blocks on a grid, drops between planes |
| `nodes` | Neighbourhoods: what is close to what | Spheres placed by a force layout in three dimensions |
| `city` | Hubs: the nodes with the most links | A district per group, a building per node, as tall as its number of edges |
| `tree` | Dependencies: what hangs on what | A cone tree: each node hangs its children on a circle below itself |
| `ring` | Coupling: how tightly the groups are tied to each other | Nodes in a circle on a table, every edge an arc over it |
| `stages` | Order: what comes after what, and where branches meet again | A flight of steps worked out from the edges, each node one step after the last node that leads to it |
| `lanes` | Hand-offs: who does each step, and where the work changes hands | A lane per group on one floor, each node as far along it as its step in the flow |
| `dominators` | Bottlenecks: the nodes no path can get around | A cone tree: each node hangs under the nearest node that every way to it goes through |
| `sankey` | Amounts: where a quantity goes, and how it splits | Columns in the order of the flow on one floor, each node a block as long as its amount, each link a band as wide as its own |
| `matrix` | Direction: how many links go from each group to each other one, and which way | A lane and a column per group on one floor, and where they meet a bar as tall as the number of links from one to the other |

A diagram that names no model gets `planes`. Every model is deterministic: the same text always gives the same
picture. Each example on this page is live: drag it to turn it, select a node to read its links.

## Choosing a model

| Where the diagram is | Write |
| --- | --- |
| The live editor | The **Model** menu. It is there when the text is Mermaid: a story has its own floor |
| A link or an [iframe](embedding.md#1-iframe) | `?model=city` before the `#` of the address |
| A block of [Markdown](embedding.md#2-markdown-plugin) | The word after `tritone`, as in the examples of this page |
| An [element](embedding.md#4-custom-element) | `<tritone-diagram model="city">`, or `data-model="city"` on an element `run()` reads |
| The [API](embedding.md#5-api) | `mount(element, text, { model: 'city' })`, or `initialize({ model: 'city' })` for a whole page |

## planes

Use it when the groups of the diagram are **layers**: an edge, an application, a data tier. Each group is a plane,
stacked in the order written, so a link that leaves its layer is a drop between two planes and stands out.

Good for:

- The tiers of a web application, shown to someone who joins the team: the edge, the application, the data
- The zones of a network in a security review: public, private, restricted
- A data platform, from where the data comes in to where it is read

```tritone planes
flowchart TB
  subgraph edge[Edge]
    cdn[CDN]
    gateway[API gateway]
  end
  subgraph app[Application]
    web[Web app]
    api[Orders API]
    worker[Worker]
  end
  subgraph data[Data]
    db[(PostgreSQL)]
    queue[(Job queue)]
  end

  cdn --> web
  web --> gateway
  gateway --> api
  api --> db
  api --> queue
  queue --> worker
  worker --> db
```

- The first group written is on top. Nodes with no group share a plane above all the others.

## nodes

Use it when there are no layers to speak of and the question is what **belongs together**. Nodes that are linked
pull each other close, so the clusters of a system show up on their own.

Good for:

- A system of many small services, to see which of them form a family
- Who works with whom, drawn from who reviews whose work
- The topics of a knowledge base, and the links between them

```tritone nodes
flowchart LR
  web[Web] --> api[API]
  mobile[Mobile] --> api
  api --> auth[Auth]
  api --> orders[Orders]
  api --> search[Search]
  auth --> users[(Users)]
  orders --> db[(Orders DB)]
  orders --> billing[Billing]
  billing --> ledger[(Ledger)]
  search --> index[(Index)]
```

- There is no floor: a node moved by hand moves in the plane of the screen.
- It is made for tens of nodes. The layout compares every node with every other, so hundreds are slow.

## city

Use it to find the **hubs**. A node is a building as tall as the number of its links, so the busiest ones are the
skyline, and a district is a group.

Good for:

- Choosing what to watch most closely: the tallest buildings are what most things are linked to
- The pages of a website that most other pages link to
- The teams or people that most requests go through

```tritone city
flowchart LR
  subgraph clients[Clients]
    web[Web]
    mobile[Mobile]
    partner[Partner API]
  end
  subgraph core[Core]
    gateway[Gateway]
    orders[Orders]
    catalog[Catalog]
  end
  subgraph data[Data]
    db[(Database)]
    cache[(Cache)]
  end

  web --> gateway
  mobile --> gateway
  partner --> gateway
  gateway --> orders
  gateway --> catalog
  orders --> db
  catalog --> db
  catalog --> cache
```

- A tall building can hide the ones behind it: turn the scene. Labels always stay on top.
- A hub has many links, which is not the same as being on every path: [`dominators`](#dominators) shows that.

## tree

Use it for what **depends on** what: the packages of an application, a call tree, the services behind a request.
Each node hangs what it needs on a circle below itself.

Good for:

- The packages of an application, before one of them is upgraded
- The services a request needs, from the first one down
- An organisation chart: each person hangs their team below themselves

```tritone tree
flowchart TD
  app[App] --> ui[UI kit]
  app --> client[API client]
  app --> state[State]
  ui --> icons[Icons]
  ui --> theme[Theme]
  client --> http[HTTP]
  client --> auth[Auth]
  state --> storage[Storage]
  auth --> http
```

- The roots are the nodes no edge points at. The tree is the one found going breadth first from the roots; the
  other edges, like the one from `Auth` to `HTTP` here, are drawn across it.
- A cycle with no root gets its first node as one.

## ring

Use it to see **coupling**. Every node is on one circle, next to the others of its group, and every link is an arc
over the table: links inside a group are short, and two groups that depend on each other are joined by a bundle of
long ones.

Good for:

- How tightly the modules of a code base are tied, before splitting it
- How much the teams of a company depend on each other
- Checking that a new boundary holds: few long arcs cross it

```tritone ring
flowchart LR
  subgraph checkout[Checkout]
    cart[Cart]
    payment[Payment]
  end
  subgraph catalog[Catalog]
    products[Products]
    search[Search]
  end
  subgraph accounts[Accounts]
    users[Users]
    sessions[Sessions]
  end

  cart --> payment
  cart --> products
  cart --> sessions
  payment --> users
  search --> products
  sessions --> users
```

## stages

Use it for the **order** things happen in, and for where branches **meet again**. The steps are worked out from the
edges alone, with no group to write: a node stands one step below the last of the nodes that point at it, so every
edge runs downhill.

Good for:

- A build or a release pipeline: what runs after what, and what can run at the same time
- A launch plan: which tasks wait for which, and what can start today
- A course: what to learn before what

```tritone stages
flowchart TD
  app[App] --> ui[UI kit]
  app --> client[API client]
  app --> state[State]
  ui --> icons[Icons]
  ui --> theme[Theme]
  client --> http[HTTP]
  client --> auth[Auth]
  state --> storage[Storage]
  auth --> http
```

- This is the text of the [`tree`](#tree) example. There `HTTP` hangs under `API client`, the first node that
  reaches it. Here it stands after `Auth`, the last one, and the edge from `API client` skips a step to get there.
- In `planes` the writer says which layer a node is in. Here the edges say it, and the groups are only the colours
  of the blocks.
- Nodes that lead back to each other, round a cycle, have no order: they share a step.
- The edges of this example point at what a package needs, so what is built first is on the last step.

## lanes

Use it for a process several people or systems take turns in: **who does each step**, and where the work
**changes hands**. A group is a lane, and a node is as far along its lane as its step in the flow, the same step
[`stages`](#stages) gives it.

Good for:

- A business process across teams: a refund, a hire, an expense
- The response to an incident: who does what, and when the work changes hands
- Finding the round trips: work that comes back to someone who already had it

```tritone lanes
flowchart LR
  subgraph customer[Customer]
    ask[Ask to return]
    photo[Send a photo]
    ship[Ship parcel]
  end
  subgraph support[Support]
    check[Check order]
    approve[Approve]
  end
  subgraph warehouse[Warehouse]
    inspect[Inspect item]
  end
  subgraph accounts[Accounts]
    refund[Refund]
  end

  ask --> check
  check -->|needs a photo| photo
  check -->|photo attached| approve
  photo --> approve
  approve -->|label| ship
  ship --> inspect
  inspect --> refund
```

- A link along a lane is work that stays with its owner. A link across the lanes is a hand-off: here the return
  goes back to the customer twice before the warehouse sees it.
- The first group written is the lane at the back. Nodes with no group get a lane of their own, last.
- Two nodes of one lane at the same step stand side by side across it.

## dominators

Use it to find the **bottlenecks**: what every way to a node goes through, starting from the entries. It is a cone
tree like [`tree`](#tree), but a node hangs under the nearest node no path to it can get around, so everything
under a node is what would be cut off without it.

Good for:

- Finding single points of failure: what everything below a node would lose without it
- Preparing for an outage: what can no longer be reached when one service stops
- An approval chain: the one person every request has to pass

```tritone dominators
flowchart TD
  web[Web] --> gateway[Gateway]
  gateway --> orders[Orders]
  gateway --> catalog[Catalog]
  orders --> db[(Database)]
  orders --> queue[(Queue)]
  queue --> worker[Worker]
  worker --> db
  catalog --> db
  catalog --> cache[(Cache)]
```

- `Cache` hangs under `Catalog`, the only way to it. `Database` hangs straight under `Gateway`, which it has no
  edge from: it is reached through `Orders`, `Worker` or `Catalog`, so none of them is on every way to it. In
  `tree` it hangs under `Orders`, the first node that reaches it.
- The entries are the nodes no edge points at, like the roots of `tree`. A cycle with no entry gets its first node
  as one.
- With several entries, what more than one of them reaches hangs under none of them: it stands beside them, at
  the top.
- The links are the edges as written, so they do not always follow the tree. A bigger ball has more hanging
  straight under it.
- It is made for tens of nodes: the layout searches the graph once for every node.

## sankey

Use it for **where an amount goes**: a budget, the visitors of a funnel, the energy a process spends. The nodes
stand in columns in the order of the flow, each one a block as long as its amount, and each link is a band on the
floor as wide as its own. It is made for Mermaid's [`sankey-beta`](graph.md#sankey-beta--sankey) text, where every
row is a link and what it carries.

Good for:

- A budget: where the money goes, and how it splits
- A funnel: how many visitors sign up, try the product, and pay
- Costs by team or by service, or the hours a project spent

```tritone sankey
sankey-beta

Revenue,Operations,7000
Revenue,Set aside,3000
Operations,People,4000
Operations,Infrastructure,2000
Operations,Marketing,1000

%% @Operations: What it costs to run the product. The rest is set aside.
%% @Infrastructure: Hosting, the database and the CDN.
```

- Select a node to read its amount and its shares. `Infrastructure` is 2,000: 20% of `Revenue`, where everything
  comes from, and 28.6% of `Operations`, the node its link leaves. With more than one node to come from, the first
  share is of the total.
- The amount of a node is the larger of what comes in and what goes out.
- A column is the step [`stages`](#stages) gives a node. Along its column a node stands across from where its
  amount comes from, so the bands cross as little as they can; nodes fed from the same place keep the order written.
- A band that skips a column passes under the blocks that stand there. Nodes round a cycle share a column, and the
  band between two of them is a loop.
- A flowchart has no amounts, so every link counts 1: a block is as long as the number of links through it, and a
  band takes the colour of the group it leaves.
- The other models read `sankey-beta` text too. They draw its links as lines, and the amounts are in the panel.

## matrix

Use it to **count** the links from one group to another, and to see **which way** they go. Each group is a lane,
with its nodes at the head of it, and each group is also a column across the lanes, named by the tile in front.
Where a lane meets a column stands a bar as tall as the number of links from the group of the lane to the group of
the column.

Good for:

- Counting the dependencies between teams or modules, and seeing which way they go
- Finding two groups that depend on each other both ways, the hardest pair to split
- Which departments send work to which

```tritone matrix
flowchart LR
  subgraph checkout[Checkout]
    cart[Cart]
    payment[Payment]
  end
  subgraph catalog[Catalog]
    products[Products]
    search[Search]
  end
  subgraph accounts[Accounts]
    users[Users]
    sessions[Sessions]
  end

  cart --> payment
  cart --> products
  cart --> sessions
  payment --> users
  search --> products
  sessions --> users
```

- This is the text of the [`ring`](#ring) example. There the arcs between `Checkout` and `Accounts` say the two are
  tied. Here the bar where the lane of `Checkout` meets the column of `Accounts` says 2, and the lane of `Accounts`
  has no bar in the column of `Checkout`: the links all go one way.
- A bar has the colour of the group its links reach. The bar where a lane meets its own column counts the links
  inside the group, so a lane with no other bar depends on nothing but itself.
- Select a bar to light the links it counts and to read them in the panel, each with a way to its two nodes.
- The first group written is the lane at the back and the first column. Nodes with no group get a lane and a
  column of their own, last.
- The number on a bar is exact. Its height compares it with the other bars: where there are more links than fit,
  all the bars are scaled down together.
- A column is as wide as the longest name of a group, so that the names in front do not run into each other: long
  names make a wide floor.

## A model of your own

A model is a function from a graph to a scene: [how to write one](graph.md#writing-a-model), and
[how to register it](plugins.md#models). It is then chosen by its name, like the ten above.