class ClientAggregateData 
  include Mongoid::Document

  field :mac, type: String
  field :ap, type: String
  field :ln, type: Integer
  field :device, type: Integer
  field :hostname, type: String
  field :band, type: Integer
  field :hr, type: Array, default: [] #=> [{'h' => 23, 'tx_r' => 0, 'rx_r' => 0, 'tx_u' => 0, 'rx_u' => 0}....]
  field :ssid, type: Array, default: [] #=> [{'id' => 1, 'n' => "SSID name"}....]
  field :d, type: Date
  field :ip, type: String
  field :l_time, type: Date

  index({"mac" => 1, "ap" => 1, "ln" => 1, "d" => 1}, { background: true })
  index({"ap" => 1, "ln" => 1, "d" => 1}, { background: true })
  index({"mac" => 1, "ln" => 1, "d" => 1}, { background: true })
  index({"ln" => 1, "d" => 1}, { background: true })
 
  def self.save_hourly_data st, mac_ids, ln_id, hourly=true
    col = hourly ? MonitoringDataCapped : MonitoringChild    
     aggregate_data = col.collection.aggregate(
                                    {"$match" => {"info.NASID" => {"$in" => mac_ids},
                                                  "created_at" => {"$gte" => st.utc, "$lt" => (st + 1.hour).utc}}},
                                    {"$project" => {'ap' => "$info.NASID",
                                                    'tm' => "$created_at",
                                                    "clients.MAC" => 1,
                                                    "clients.IPADDR" => 1,
                                                    "clients.TX_RATE" => 1,
                                                    "clients.TX_BYTES_INT" => 1,
                                                    "clients.RX_RATE" => 1,
                                                    "clients.RX_BYTES_INT" => 1,
                                                    "clients.SSID_UNIQ_ID" => 1,
                                                    "clients.SSID" => 1,
                                                    "clients.DEVICE_TYPE" => 1,
                                                    "clients.HOSTNAME" => 1,
                                                    "clients.BAND" => 1,
                                                    "clients.BD" => 1}},
                                    {"$unwind" => "$clients"},
                                    {"$project" => {"clients.MAC" => 1,
                                                    "clients.IPADDR" => 1,
                                                    "clients.TX_RATE" => 1,
                                                    "clients.TX_BYTES_INT" => 1,
                                                    "clients.RX_RATE" => 1,
                                                    "clients.RX_BYTES_INT" => 1,
                                                    "clients.SSID_UNIQ_ID" => 1,
                                                    "clients.SSID" => 1,
                                                    "clients.DEVICE_TYPE" => 1,
                                                    "clients.HOSTNAME" => 1,
                                                    "band" => {"$ifNull" => ["$clients.BAND", "$clients.BD"] },
                                                    "ap" => "$ap",
                                                    "tm" => "$tm"}},
                                    {"$group" => {"_id" => {"ap" => "$ap", "cli" => "$clients.MAC"},
                                                  "tx_u" => {"$sum" => "$clients.TX_BYTES_INT"},
                                                  "tx_r" => {"$avg" => "$clients.TX_RATE"},
                                                  "rx_u" => {"$sum" => "$clients.RX_BYTES_INT"},
                                                  "rx_r" => {"$avg" => "$clients.RX_RATE"},
                                                  "device" => {"$last" => "$clients.DEVICE_TYPE"},
                                                  "hostname" => {"$last" => "$clients.HOSTNAME"},
                                                  "band" => {"$last" => "$band"},
                                                  "ip" => {"$last" => "$clients.IPADDR"},
                                                  "tm" => {"$last" => "$tm"},
                                                  'SSID' => {"$addToSet" => {"id" => "$clients.SSID_UNIQ_ID", "n" => "$clients.SSID"}}}})
    aggregate_data.each do |d|
      self.collection.where({"mac" => d['_id']['cli'], "ap" => d['_id']['ap'], "ln" => ln_id, "d" => st.to_date})
                     .update( {"$push" => {"hr" => {'h' => st.hour.to_i,
                                                    'tx_r' => d["tx_r"],
                                                    'tx_u' => d["tx_u"],
                                                    'rx_r' => d["rx_r"],
                                                    'rx_u' => d["rx_u"]}},
                               "$addToSet" => {"ssid" => {"$each" => d['SSID']}},
                               "$set" => {"device" => (d['device'] || 0), "hostname" => (d['hostname']), "band" => (d['band'] || 0),"ip" => (d['ip']), "l_time" => (d['tm'])}}, [:upsert] )
    end
  end

  def self.find_recent mac, ln
    self.where(:mac => mac, :ln => ln).desc(:d).first
  end

end
