Skip to content

Commit 780871c

Browse files
author
Vidas P
committed
Live update message counts on the diagram
1 parent 5c99d9c commit 780871c

File tree

5 files changed

+54
-6
lines changed

5 files changed

+54
-6
lines changed

app/assets/javascripts/diagram.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,33 @@ window.updateDiagram = function(options){
2626
badge.css({
2727
left: tl.x - (badge.outerWidth() * (2/3)),
2828
top: tl.y - (badge.outerHeight() * (1/3)),
29-
'background-color': badge.find('.badge').css('background-color')}).show();
29+
'background-color': badge.find('.badge').css('background-color')});
3030
});
3131
});
3232
};
3333

3434
window.setupDiagram = function() {
35+
var setBadge = function(agent_id, count) {
36+
var selector = `#b${agent_id}`;
37+
if (count > 0) {
38+
$(selector).show();
39+
$(`${selector} span.count`).text(count);
40+
} else {
41+
$(selector).hide();
42+
}
43+
};
44+
var fetchStatus = function() {
45+
$.getJSON('/agents', function(json) {
46+
for(const agent_status of json) {
47+
let agent_id = agent_status.id;
48+
let messages_count = agent_status.messages_count;
49+
setBadge(agent_id, messages_count);
50+
}
51+
});
52+
setTimeout(fetchStatus, 2000);
53+
};
54+
fetchStatus();
55+
3556
let updateVisibility = (show) => {
3657
const $toggle = $('#show-diagram-toggle');
3758
const $diagram = $('.overview-diagram');

app/controllers/agents_controller.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ def index
1515
end
1616
end
1717

18+
def status
19+
agents = current_user.agents
20+
21+
statuses = agents.pluck(:id, :messages_count).map do |props|
22+
{
23+
id: props[0],
24+
messages_count: props[1]
25+
}
26+
end
27+
28+
render json: statuses
29+
end
30+
1831
def toggle_visibility
1932
if show_only_enabled_agents?
2033
mark_all_agents_viewable

app/helpers/dot_helper.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,17 @@ def decorate_svg(xml, agents, workflow)
200200
node['data-agent-id'] = agent_id
201201

202202
count = agent.messages_count
203-
next unless count && count > 0
204203

205204
overlay << Nokogiri::XML::Node.new('a', doc) { |badge|
206205
badge['id'] = id = 'b%d' % agent_id
207206
badge['class'] = 'badge'
208207
badge['href'] = agent_path(agent, params: { tab: 'messages', workflow_id: workflow })
209208
badge['title'] = "#{count} messages created"
210-
badge.content = count.to_s
209+
210+
badge << Nokogiri::XML::Node.new('span', doc) { |lbl|
211+
lbl.content = count.to_s
212+
lbl['class'] = 'count'
213+
}
211214

212215
node['data-badge-id'] = id
213216

@@ -216,11 +219,11 @@ def decorate_svg(xml, agents, workflow)
216219
label['class'] = [
217220
'badge',
218221
if agent.unavailable?
219-
'badge badge-warning'
222+
'badge-warning'
220223
elsif agent.working?
221-
'badge badge-success'
224+
'badge-success'
222225
else
223-
'badge badge-danger'
226+
'badge-danger'
224227
end
225228
].join(' ')
226229
label['style'] = 'display: none';

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
end
1010

1111
collection do
12+
get :status
1213
put :toggle_visibility
1314
get :type_details
1415
get :message_descriptions

spec/controllers/agents_controller_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ def valid_attributes(options = {})
2626
end
2727
end
2828

29+
describe 'GET status' do
30+
it 'returns message counts for users agents' do
31+
sign_in users(:bob)
32+
get :status
33+
JSON.parse(response.body).each do |status|
34+
expect(status['messages_count']).to eq Agent.find(status['id']).messages_count
35+
end
36+
end
37+
end
38+
2939
describe 'POST handle_details_post' do
3040
it 'passes control to handle_details_post on the agent' do
3141
sign_in users(:bob)

0 commit comments

Comments
 (0)