Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1- UI-austonomous_system.html.erb
- <small>
- <%=link_to "Download CSV Data", :format => :csv, :controller => "low_ip_alert", :action => "all_stats", :method => :get%>
- </small>
- 2- route.rb
- map.resources :low_ip_alert,
- :only => [],
- :member => {
- :all_stats => :get,
- }
- 3- Contoller:
- class LowIpAlertController < ApplicationController
- def all_stats
- @all_stats = AutonomousSystem.all(:conditions => ['low_ip_alert_check = ?', true]).inject([]) do |list, autonomous_system|
- stats = LowIpAlertStat.new(autonomous_system)
- stats.send(:calculate)
- list << stats
- end
- csv_data = CSV.generate do |csv|
- csv << ["autonomous_system_name", "cidr", "percentage"]
- @all_stats.flat_map {|stat| stat.percentages_grouped_by_block.map {|key,value| [stat.autonomous_system.name,key,value] } }.each {|row| csv << row }
- end
- respond_to do |format|
- format.json {render :json => csv_data.to_json}
- format.csv { send_data (csv_data,
- :type => 'text/csv; charset=utf-8; header=present',
- :disposition => "attachment",
- :filename => "lowIpAlert.csv")}
- end
- end
- end
- 4-input:
- pp @all_stats
- [#<LowIpAlertStat:0x7f5af0527cc0
- @autonomous_system=
- #<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>,
- @percentages_grouped_by_block=
- {"166.78.42.0/23"=>100.0,
- "166.78.33.0/24"=>100.0,
- "209.61.129.0/24"=>100.0,
- "166.78.36.0/22"=>99.21875,
- "166.78.32.0/24"=>100.0,
- "209.61.131.0/24"=>100.0,
- "209.61.128.0/24"=>100.0,
- "50.56.228.0/24"=>100.0},>]
- =>nil
- 5- error in the UI:
- No route matches {:action=>"all_stats", :method=>"get", :format=>"csv", :controller=>"low_ip_alert"}
Advertisement
Add Comment
Please, Sign In to add comment