-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rb
More file actions
172 lines (144 loc) · 3.94 KB
/
Copy pathmain.rb
File metadata and controls
172 lines (144 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
require "sinatra"
require "trello"
require "omniauth"
require "omniauth-trello"
require "trello"
require "open3"
require "erb"
require "securerandom"
set :port, ENV.fetch("PORT", "4567")
set :session_secret, ENV.fetch("SESSION_SECRET", SecureRandom.hex(64))
enable :sessions
use OmniAuth::Builder do
provider(
:trello,
ENV.fetch("TRELLO_KEY"),
ENV.fetch("TRELLO_TOKEN"),
app_name: "trello-zettelkasten",
scope: "read,account",
expiration: "1day",
)
end
get "/" do
if session.has_key?(:token)
begin
client = Trello::Client.new(
consumer_key: ENV.fetch("TRELLO_KEY"),
consumer_secret: ENV.fetch("TRELLO_TOKEN"),
oauth_token: session.fetch(:token),
)
person = client.find(:members, "me")
erb :main, locals: {
signed_in: true,
person: person,
boards: person.boards,
}
rescue
erb :main, locals: {
signed_in: false,
}
end
else
erb :main, locals: {
signed_in: false,
}
end
end
get "/visualise" do
redirect to("/auth/trello") unless session.has_key?(:token)
client = Trello::Client.new(
consumer_key: ENV.fetch("TRELLO_KEY"),
consumer_secret: ENV.fetch("TRELLO_TOKEN"),
oauth_token: session.fetch(:token),
)
board_ids = params.fetch("board", {}).keys
filter_label_ids = Set.new(params.fetch("label", {}).keys)
boards = board_ids.map do |board_id|
client.find(:boards, board_id)
end
cards = boards.flat_map do |board|
board.cards
end
labels = Set.new(cards.flat_map do |card|
card.labels
end)
filtered_cards = cards.select do |card|
filter_label_ids.empty? || (filter_label_ids & Set.new(card.labels.map {|l| l.id})).any?
end
erb :visualise, locals: {
board_ids: board_ids,
board_names: boards.map {|b| b.name}.join(","),
graphs: draw_graphs(cards, filtered_cards),
labels: labels,
query: request.query_string,
}
end
get "/auth/trello/callback" do
session[:token] = request.env.dig(
"omniauth.auth",
"credentials",
"token"
)
redirect to("/")
end
CARD_PATTERN = /https:\/\/trello[.]com\/c\/[A-Za-z0-9]+/
GraphNode = Struct.new(:id, :label, :href, :desc, :tags)
GraphEdge = Struct.new(:from, :to)
def get_card_url_prefix(url)
url.match(CARD_PATTERN)[0]
end
def draw_graphs(cards, filtered_cards)
all_nodes = cards.map do |card|
GraphNode.new(
get_card_url_prefix(card.url),
card.name,
card.url,
card.desc,
card.labels.map{ |l| l.name },
)
end
filtered_nodes = filtered_cards.map do |card|
GraphNode.new(
get_card_url_prefix(card.url),
card.name,
card.url,
card.desc,
card.labels.map{ |l| l.name },
)
end
filtered_edges = filtered_nodes.flat_map do |node|
node.desc.scan(CARD_PATTERN).map do |match|
GraphEdge.new(node.id, match)
end
end
nodes_to_render = all_nodes.filter do |node|
filtered_edges.any? do |edge|
node.id == edge.from || node.id == edge.to
end
end
template = <<~'ERB'
digraph g {
<% nodes_to_render.each do |node| %>
"<%= node.id %>" [
label = <
<table border="0" cellborder="1" cellspacing="0" cellpadding="4">
<tr><td href="<%= node.href %>"><font color="#1d70b8"><u><%= node.label %></u></font></td></tr>
<% node.tags.each do |tag| %>
<tr><td><%= tag %></td></tr>
<% end %>
</table>
>
shape = none
fontname = "Arial"
]
<% end %>
<% filtered_edges.each do |edge| %>
"<%= edge.from %>" -> "<%= edge.to %>"
<% end %>
}
ERB
graphviz_input = ERB.new(template).result(binding)
connected_components_output, status = Open3.capture2("ccomps", "-x", stdin_data: graphviz_input)
graphviz_output, status = Open3.capture2("dot", "-Tsvg", stdin_data: connected_components_output)
graphviz_output.split(/<\?xml version="1\.0" encoding="UTF-8" standalone="no"\?>/).reject {|x| x.empty?}.sort_by {|x| x.length}
end