迭代Rails对象数组以创建键/值对

时间:2016-05-04 19:21:08

标签: ruby-on-rails ruby activerecord

我有一个rails对象network_hosts,它返回我的应用程序的所有网络主机。现在,我想创建一个显示host.os_vendor细分的Morris.js donut chart(Linux,Windows,Apple等)。

那么,迭代对象,提取操作系统类型,创建type count并将其返回到data属性的最佳方法是什么,Morris.js依赖该属性来创建圆环图?

以下是network_hosts的输出示例:

[#<NetworkHost id: 80, location_id: 14, mac_address: "70:56:81:C1:7C:FD", ip_address: "10.10.10.122", hostname: "Air1", created_at: "2016-04-14 19:30:20", updated_at: "2016-04-22 16:11:33", deleted_at: nil, os_vendor: "Unknown", nickname: nil, vendor: "Unknown", os: "Unknown", ignore_date: nil, ignore_flag: false>,
 #<NetworkHost id: 10, location_id: 14, mac_address: "C8:E0:EB:3D:2B:B7", ip_address: "10.10.10.129", hostname: "iMac2", created_at: "2014-11-25 23:20:41", updated_at: "2016-04-22 16:11:33", deleted_at: nil, os_vendor: "Unknown", nickname: nil, vendor: "Unknown", os: "Unknown", ignore_date: nil, ignore_flag: false>,
 #<NetworkHost id: 29, location_id: 14, mac_address: "18:B4:30:15:BC:63", ip_address: "10.10.10.130", hostname: "02AA01AC4414076R", created_at: "2015-02-26 02:24:32", updated_at: "2016-04-22 16:11:33", deleted_at: nil, os_vendor: "Unknown", nickname: nil, vendor: "Nest Labs", os: "Unknown", ignore_date: nil, ignore_flag: false>,
 #<NetworkHost id: 30, location_id: 14, mac_address: "00:04:20:F0:6E:9F", ip_address: "10.10.10.135", hostname: "HarmonyHub", created_at: "2015-02-26 02:24:32", updated_at: "2016-04-22 16:11:33", deleted_at: nil, os_vendor: "Linux", nickname: nil, vendor: "Slim Devices", os: "Linux 2.6.17 - 2.6.36", ignore_date: nil, ignore_flag: false>,
 #<NetworkHost id: 2, location_id: 14, mac_address: "DC:9F:DB:E6:CB:A9", ip_address: "10.10.10.100", hostname: "office", created_at: "2014-11-25 23:20:41", updated_at: "2016-05-03 18:30:36", deleted_at: nil, os_vendor: "Unknown", nickname: "Who 1", vendor: "Ubiquiti Networks", os: "Unknown", ignore_date: "2016-05-30 00:09:00", ignore_flag: false>,
 #<NetworkHost id: 27, location_id: 14, mac_address: "74:54:7D:02:53:8C", ip_address: "10.10.10.101", hostname: "Hostname Unknown", created_at: "2015-02-26 02:24:32", updated_at: "2016-05-03 18:30:36", deleted_at: nil, os_vendor: "Linksys", nickname: nil, vendor: "Unknown", os: "Linksys WET54G wireless bridge", ignore_date: nil, ignore_flag: false>,
 #<NetworkHost id: 76, location_id: 14, mac_address: "D0:63:B4:00:5B:40", ip_address: "10.10.10.106", hostname: "OpenELEC", created_at: "2016-02-19 01:03:24", updated_at: "2016-05-02 17:08:28", deleted_at: nil, os_vendor: "Unknown", nickname: "OpenElec", vendor: "Unknown", os: "Unknown", ignore_date: "2016-06-01 17:07:50", ignore_flag: false>,
 #<NetworkHost id: 11, location_id: 14, mac_address: "84:34:97:1C:42:50", ip_address: "10.10.10.137", hostname: "HP1A4DC4", created_at: "2014-11-25 23:20:41", updated_at: "2016-04-22 16:11:33", deleted_at: nil, os_vendor: "HP", nickname: nil, vendor: "Unknown", os: "HP printer: LaserJet CM1312, Photosmart C510a, or ...", ignore_date: nil, ignore_flag: false>,
 #<NetworkHost id: 14, location_id: 14, mac_address: "E8:8D:28:57:AE:22", ip_address: "10.10.10.144", hostname: "time-capsule", created_at: "2014-11-25 23:20:42", updated_at: "2016-04-22 16:11:33", deleted_at: nil, os_vendor: "Apple", nickname: nil, vendor: "Unknown", os: "Apple AirPort Extreme WAP or Time Capsule NAS devi...", ignore_date: nil, ignore_flag: false>,
 #<NetworkHost id: 75, location_id: 14, mac_address: "00:14:BF:E0:A0:05", ip_address: "10.10.10.2", hostname: "Hostname Unknown", created_at: "2016-02-18 22:48:01", updated_at: "2016-04-22 16:11:33", deleted_at: nil, os_vendor: "Linux", nickname: "test", vendor: "Cisco-Linksys", os: "DD-WRT v23 - v24-sp2 (Linux 2.4.20 - 2.4.37)", ignore_date: nil, ignore_flag: false>,
 #<NetworkHost id: 4, location_id: 14, mac_address: "DC:9F:DB:E6:CF:C1", ip_address: "10.10.10.104", hostname: "main", created_at: "2014-11-25 23:20:41", updated_at: "2016-05-03 18:30:36", deleted_at: nil, os_vendor: "Unknown", nickname: nil, vendor: "Ubiquiti Networks", os: "Unknown", ignore_date: nil, ignore_flag: false>,
 #<NetworkHost id: 28, location_id: 14, mac_address: "18:B4:30:15:BB:76", ip_address: "10.10.10.105", hostname: "02AA01AC451400G4", created_at: "2015-02-26 02:24:32", updated_at: "2016-05-03 18:30:36", deleted_at: nil, os_vendor: "Unknown", nickname: nil, vendor: "Nest Labs", os: "Unknown", ignore_date: nil, ignore_flag: false>,
 #<NetworkHost id: 8, location_id: 14, mac_address: "B8:78:2E:05:E7:F0", ip_address: "10.10.10.118", hostname: "Apple-TV-3", created_at: "2014-11-25 23:20:41", updated_at: "2016-04-22 16:11:33", deleted_at: nil, os_vendor: "Apple", nickname: nil, vendor: "Unknown", os: "Apple iOS 4.4.2 - 5.0.1 (Darwin 11.0.0)", ignore_date: nil, ignore_flag: false>,
 #<NetworkHost id: 24, location_id: 14, mac_address: "74:31:70:0B:56:75", ip_address: "10.10.10.113", hostname: "Hostname Unknown", created_at: "2015-01-29 19:05:44", updated_at: "2016-04-24 14:20:44", deleted_at: nil, os_vendor: "Unknown", nickname: nil, vendor: "Arcadyan Technology", os: "Unknown", ignore_date: nil, ignore_flag: false>,
 #<NetworkHost id: 82, location_id: 14, mac_address: "d2:2a:aa:3e:1e:5a:2d", ip_address: "10.10.10.222", hostname: "Hostname Unknown", created_at: "2016-04-18 17:45:30", updated_at: "2016-04-24 14:20:44", deleted_at: nil, os_vendor: "Unknown", nickname: nil, vendor: "Unknown", os: "Unknown", ignore_date: nil, ignore_flag: false>,
 #<NetworkHost id: 81, location_id: 14, mac_address: "10:9d:07:1b:e4:a7:a8", ip_address: "10.10.10.199", hostname: "Hostname Unknown", created_at: "2016-04-18 16:21:07", updated_at: "2016-04-28 20:49:47", deleted_at: nil, os_vendor: "Linux", nickname: "", vendor: "Unknown", os: "Linux 2.6.32 - 2.6.38", ignore_date: nil, ignore_flag: false>,
 #<NetworkHost id: 1, location_id: 14, mac_address: "4C:E6:76:3F:2F:77", ip_address: "10.10.10.1", hostname: "DD-WRT", created_at: "2014-11-25 23:20:41", updated_at: "2016-05-02 13:25:07", deleted_at: nil, os_vendor: "Linux", nickname: "Router", vendor: "Buffalo", os: "DD-WRT v24-sp2 (Linux 2.6.24)", ignore_date: "2016-05-30 00:56:45", ignore_flag: false>,
 #<NetworkHost id: 41, location_id: 14, mac_address: "54:9F:13:2A:29:16", ip_address: "10.10.10.133", hostname: "TravisFrsiPhone", created_at: "2015-03-05 18:00:18", updated_at: "2016-04-22 16:11:33", deleted_at: nil, os_vendor: "Unknown", nickname: nil, vendor: "Unknown", os: "Unknown", ignore_date: nil, ignore_flag: false>]

2 个答案:

答案 0 :(得分:4)

如果这是ActiveRecord::Relation,那么您可以使用数据库对您进行分组和计数。

network_hosts.group(:os_vendor).count

# returns a hash
{ "linux" => 5, "unkown" => 35, "mac" => 10, etc... }

您链接的库似乎需要一个包含2个键labelvalue的数组,因此您可以map对结果将数据转换为您想要的形状

network_hosts
  .group(:os_vendor)
  .count
  .map { |os_vendor, count| { "label" => os_vendor, "value" => count } }

# returns an array of hashes
[ { "label" => "linux", "value" => 5 } ... ]

如果数组只是一个普通的Ruby数组,那么你需要使用Ruby而不是数据库进行分组+计数

network_hosts
   .group_by(&:os_vendor)
   .map { |os_vendor, group| { "label" => os_vendor, "value" => group.size } }

答案 1 :(得分:3)

我会做类似的事情:

type_count = network_hosts.reduce(Hash.new(0)) do |hash, host| 
  hash.update(host. os_vendor => hash[host. os_vendor].next)
end
data = type_count.map { |k, v| { label: k, value: v } }

哪会回来:

[
  {
    :label => "Unknown",
    :value => 10
  },
  {
    :label => "Linux",
    :value => 4
  },
  {
    :label => "Linksys",
    :value => 1
  },
  {
    :label => "HP",
    :value => 1
  },
  {
    :label => "Apple",
    :value => 2
  }
]