Skip to content

Commit 9aad6e6

Browse files
committed
feat: Support Accept: text/vnd.graphviz for /projects.
1 parent e34c50d commit 9aad6e6

5 files changed

Lines changed: 65 additions & 2 deletions

File tree

config/config.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
# General application configuration
88
import Config
99

10+
config :mime, :types, %{
11+
"text/vnd.graphviz" => ["gv"]
12+
}
13+
1014
config :zout,
1115
ecto_repos: [Zout.Repo]
1216

lib/zout_web.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ defmodule ZoutWeb do
2020

2121
def controller do
2222
quote do
23-
use Phoenix.Controller, formats: [html: "View", json: "View"]
23+
use Phoenix.Controller, formats: [html: "View", json: "View", gv: "View"]
2424

2525
import Plug.Conn
2626
use Gettext, backend: ZoutWeb.Gettext

lib/zout_web/controllers/project_controller.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ defmodule ZoutWeb.ProjectController do
4747
|> Enum.group_by(fn dep -> dep.from_id end, fn dep -> dep.to_id end)
4848

4949
[projects_and_pings: projects_and_pings, dependencies: dependencies]
50+
51+
"gv" ->
52+
projects_and_pings = Data.list_projects_and_status()
53+
54+
[projects_and_pings: projects_and_pings]
5055
end
5156

5257
render(conn, :index, params)

lib/zout_web/router.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule ZoutWeb.Router do
33
import Phoenix.LiveDashboard.Router
44

55
pipeline :browser do
6-
plug :accepts, ["html", "json"]
6+
plug :accepts, ["html", "json", "gv"]
77
plug :fetch_session
88
plug :fetch_live_flash
99
plug :put_layout, html: {ZoutWeb.LayoutView, :app}

lib/zout_web/views/project_view.ex

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,60 @@ defmodule ZoutWeb.ProjectView do
103103
defp json_stop_ping(%{stop: nil}), do: nil
104104
defp json_stop_ping(%{stop: s}), do: NaiveDateTime.to_iso8601(s)
105105

106+
def render_dotx_graph(projects_and_pings) do
107+
nodes =
108+
projects_and_pings
109+
|> Enum.map(fn %{project: project, ping: ping} ->
110+
%Dotx.Node{
111+
id: [project.slug],
112+
attrs: %{
113+
"href" => ~p"/projects/#{project.id}",
114+
"label" => %Dotx.HTML{
115+
html: """
116+
<table border="0">
117+
<tr><td><b>#{project.name}</b></td></tr>
118+
<tr><td><font point-size="8">#{render_status(ping, false)}</font></td></tr>
119+
</table>
120+
"""
121+
},
122+
"fontcolor" => text_colour(ping),
123+
"fillcolor" => status_colour(ping)
124+
}
125+
}
126+
end)
127+
128+
edges =
129+
projects_and_pings
130+
|> Enum.map(fn %{project: project} -> project end)
131+
# Load the dependencies field
132+
|> Enum.map(fn project -> Data.get_project!(project.id) end)
133+
|> Enum.flat_map(fn project ->
134+
project.dependencies
135+
|> Enum.map(fn dependency ->
136+
%Dotx.Edge{
137+
from: %Dotx.Node{id: [project.slug], attrs: %{}},
138+
to: %Dotx.Node{id: [dependency.slug], attrs: %{}},
139+
attrs: %{},
140+
bidir: false
141+
}
142+
end)
143+
end)
144+
145+
%Dotx.Graph{
146+
strict: false,
147+
type: :digraph,
148+
id: "ZOUT",
149+
attrs: %{},
150+
graphs_attrs: %{},
151+
nodes_attrs: %{
152+
"shape" => "box",
153+
"style" => "filled"
154+
},
155+
edges_attrs: %{},
156+
children: Enum.concat(nodes, edges)
157+
}
158+
end
159+
106160
def can?(conn, action, params \\ nil) do
107161
user = Guardian.Plug.current_resource(conn)
108162
Bodyguard.permit?(Zout.Data.Policy, action, user, params)

0 commit comments

Comments
 (0)