class SsidAggregateData 
  include Mongoid::Document

  field :ssid, type: Integer
  field :name, type: String
  field :ap, type: String
  field :ln, type: Integer
  field :hour, type: Array, default: []
  field :d, type: Date

  index({"ap" => 1, "ssid" => 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    
    #To Find TX_U, RX_U
    aggregate_ssid_data = col.collection.aggregate(
                                          {"$match" => {"info.NASID" => {"$in" => mac_ids},
                                                        "created_at" => {"$gte" => st.utc, "$lt" => (st + 1.hour).utc}}},
                                          {"$project" => {'ap' => "$info.NASID",
                                                          "ssids.SSID_UNIQ_ID" => 1,
                                                          "ssids.SSID" => 1,
                                                          "ssids.TX_BYTES_INT" => 1,
                                                          "ssids.RX_BYTES_INT" => 1}}, 
                                          {"$unwind" => "$ssids"},
                                          {"$group" => {"_id" => {"ap" => "$ap", "ssid" => "$ssids.SSID_UNIQ_ID"},
                                                        "tx_u" => {"$sum" => "$ssids.TX_BYTES_INT"},
                                                        "rx_u" => {"$sum" => "$ssids.RX_BYTES_INT"},
                                                        "name" => {"$first" => "$ssids.SSID"}}})

    aggregate_ssid_data.each do |v|
      self.collection.where({"ap" => v['_id']['ap'], "ssid" => v['_id']['ssid'].to_i, "ln" => ln_id, "d" => st.to_date})
                     .update( {"$push" => {"hour" => {'h' => st.hour, 'tx_u' => v["tx_u"], 'rx_u' => v["rx_u"]}},
                               "$set" => {"name" => v['name']}}, [:upsert] )

    #aggregate_clients_data = col.collection.aggregate(
    #                                        {"$match" => {"info.NASID" => {"$in" => mac_ids},
    #                                                      "created_at" => {"$gte" => st.utc, "$lt" => (st + 1.hour).utc}}},
    #                                        {"$project" => {'ap' => "$info.NASID",
    #                                                        "clients.SSID_UNIQ_ID" => 1,
    #                                                        "clients.MAC" => 1}}, 
    #                                        {"$unwind" => "$clients"},
    #                                        {"$group" => {"_id" => {"ap" => "$ap", "ssid" => "$clients.SSID_UNIQ_ID"},
    #                                                      "clis" => {"$addToSet" => "$clients.MAC"}}},
    #                                        {"$project" => {"count" => {"$size" => "$clis"}}})

    #aggregate_data = (aggregate_ssid_data + aggregate_clients_data).inject({}) { |h,e| 
    #                    h[e['_id']].blank? ? h.merge!({e.delete('_id') => e}) : (h[e['_id']].merge!('c' => e['count']);h) }

    #aggregate_data.each do |k,v|
    #  self.collection.where({"ap" => k['ap'], "ssid" => k['ssid'].to_i, "ln" => ln_id, "d" => st.to_date})
    #                 .update( {"$push" => {"hour" => {'h' => st.hour.to_i, 'tx_u' => v["tx_u"], 'rx_u' => v["rx_u"]}},
    #                           "$set" => {"name" => v['name']}}, [:upsert] )
    end
  end
end
