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

* 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
This commit is contained in:
MingShyanWei 2017-03-16 11:43:43 -05:00 committed by Akinori MUSHA
parent 37a2aaaf65
commit 68ad0f2e89

View file

@ -8,14 +8,23 @@ module DotHelper
} 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