Guest User

Untitled

a guest
Nov 11th, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. 1- UI-austonomous_system.html.erb
  2. <small>
  3. <%=link_to "Download CSV Data", :format => :csv, :controller => "low_ip_alert", :action => "all_stats", :method => :get%>
  4. </small>
  5.  
  6.  
  7. 2- route.rb
  8.  
  9. map.resources :low_ip_alert,
  10. :only => [],
  11. :member => {
  12. :all_stats => :get,
  13.  
  14. }
  15.  
  16. 3- Contoller:
  17.  
  18. class LowIpAlertController < ApplicationController
  19.  
  20. def all_stats
  21. @all_stats = AutonomousSystem.all(:conditions => ['low_ip_alert_check = ?', true]).inject([]) do |list, autonomous_system|
  22. stats = LowIpAlertStat.new(autonomous_system)
  23. stats.send(:calculate)
  24. list << stats
  25. end
  26.  
  27. csv_data = CSV.generate do |csv|
  28. csv << ["autonomous_system_name", "cidr", "percentage"]
  29. @all_stats.flat_map {|stat| stat.percentages_grouped_by_block.map {|key,value| [stat.autonomous_system.name,key,value] } }.each {|row| csv << row }
  30. end
  31. respond_to do |format|
  32. format.json {render :json => csv_data.to_json}
  33. format.csv { send_data (csv_data,
  34. :type => 'text/csv; charset=utf-8; header=present',
  35. :disposition => "attachment",
  36. :filename => "lowIpAlert.csv")}
  37. end
  38. end
  39.  
  40. end
  41.  
  42. 4-input:
  43.  
  44. pp @all_stats
  45. [#<LowIpAlertStat:0x7f5af0527cc0
  46. @autonomous_system=
  47. #<AutonomousSystem id: 11, name: "DFW", number: 33070, description: "DFW", created_at: nil, updated_at: "2013-09-04 19:30:06", owner_number: "DCO1", private: false, autonomous_system_id: nil, network_environment_id: 55, low_ip_alert_check: true, low_ip_alert_threshold: 70>,
  48. @percentages_grouped_by_block=
  49. {"166.78.42.0/23"=>100.0,
  50. "166.78.33.0/24"=>100.0,
  51. "209.61.129.0/24"=>100.0,
  52. "166.78.36.0/22"=>99.21875,
  53. "166.78.32.0/24"=>100.0,
  54. "209.61.131.0/24"=>100.0,
  55. "209.61.128.0/24"=>100.0,
  56. "50.56.228.0/24"=>100.0},>]
  57. =>nil
  58.  
  59. 5- error in the UI:
  60. No route matches {:action=>"all_stats", :method=>"get", :format=>"csv", :controller=>"low_ip_alert"}
Advertisement
Add Comment
Please, Sign In to add comment