Guest User

Untitled

a guest
Nov 11th, 2013
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. class LowIpAlertController < ApplicationController
  2.  
  3. def all_stats
  4. @all_stats = AutonomousSystem.all(:conditions => ['low_ip_alert_check = ?', true]).inject([]) do |list, autonomous_system|
  5. stats = LowIpAlertStat.new(autonomous_system)
  6. stats.send(:calculate)
  7. list << stats
  8. end
  9.  
  10. csv_data = CSV.generate do |csv|
  11. csv << ["autonomous_system_name", "cidr", "percentage"]
  12. @all_stats.flat_map {|stat| stat.percentages_grouped_by_block.map {|key,value| [stat.autonomous_system.name,key,value] } }.each {|row| csv << row }
  13. end
  14. respond_to do |format|
  15. format.json {render :json => csv_data.to_json}
  16. format.csv { send_data (csv_data,
  17. :type => 'text/csv; charset=utf-8; header=present',
  18. :disposition => "attachment",
  19. :filename => "lowIpAlert.csv")}
  20. end
  21. end
  22.  
  23. end
  24. desired output:
  25.  
  26. autonomous_system_name cider percentage
  27. DFW 166.78.42.0/23 100
  28. DFW 166.78.33.0/24 100
  29. DFW 209.61.129.0/24 100.0
  30. DFW 166.78.36.0/22 99.21875
  31.  
  32. input :
  33. @all_stats =
  34. [#<LowIpAlertStat:0x7f5af0527cc0
  35. @autonomous_system=
  36. #<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>,
  37. @percentages_grouped_by_block=
  38. {"166.78.42.0/23"=>100.0,
  39. "166.78.33.0/24"=>100.0,
  40. "209.61.129.0/24"=>100.0,
  41. "166.78.36.0/22"=>99.21875,
  42. "166.78.32.0/24"=>100.0,
  43. "209.61.131.0/24"=>100.0,
  44. "209.61.128.0/24"=>100.0,
  45. "50.56.228.0/24"=>100.0},>]
  46. =>nil
  47.  
  48. controller:
Advertisement
Add Comment
Please, Sign In to add comment