Skip to content

Commit

Permalink
Fix View diagram : Too many agent to display (#1664) (#1935)
Browse files Browse the repository at this point in the history
* Fix View diagram : Too many agent to display (#1664)
Making a GET request is simple, but GET URLs are limited to 2K characters.
The Google Chart API supports HTTP POST for chart requests up to 16K long. (*1)

(*1) https://developers.google.com/chart/image/docs/post_requests

* Fix View diagram : Too many agent to display (#1664)

Use  Faraday to POST api. 
Use when case to check response.status
  • Loading branch information
MingShyanWei authored and knu committed Mar 16, 2017
1 parent 37a2aaa commit 68ad0f2
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions app/helpers/dot_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,23 @@ def render_agents_diagram(agents, layout: nil)
} rescue false)
decorate_svg(svg, agents).html_safe
else
uriquery = URI.encode_www_form(cht: 'gv', chl: agents_dot(agents))
#Get query maximum length should be under 2048 bytes with including "chart?" of google chart request url
if uriquery.length > 2042
"Too many agent to display, please check unused agents"
# Google chart request url
faraday = Faraday.new { |builder|
builder.request :url_encoded
builder.adapter Faraday.default_adapter
}
response = faraday.post('https://chart.googleapis.com/chart', { cht: 'gv', chl: agents_dot(agents) })

case response.status
when 200
# Display Base64-Encoded images
tag('img', src: 'data:image/jpg;base64,'+Base64.encode64(response.body))
when 400
"The diagram can't be displayed because it has too many nodes. Max allowed is 80."
when 413
"The diagram can't be displayed because it is too large."
else
tag('img', src: URI('https://chart.googleapis.com/chart').tap { |uri|
uri.query = uriquery
})
"Unknow error. Response code is #{response.status}."
end
end
end
Expand Down

0 comments on commit 68ad0f2

Please sign in to comment.