From 68ad0f2e890278c4d8e85a8e37bbdd43df5947c7 Mon Sep 17 00:00:00 2001 From: MingShyanWei Date: Thu, 16 Mar 2017 11:43:43 -0500 Subject: [PATCH] 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 --- app/helpers/dot_helper.rb | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/app/helpers/dot_helper.rb b/app/helpers/dot_helper.rb index c6600506..406ed5e2 100644 --- a/app/helpers/dot_helper.rb +++ b/app/helpers/dot_helper.rb @@ -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