class UplinkAggregateData
  include Mongoid::Document

  field :mac, type: String
  field :name, type: String
  field :utype, type: String
  field :uid, type: String
  field :ln, type: Integer
  field :hr, type: Array, default: [] #=> [{'h' => 23, 'tx_r' => 0, 'rx_r' => 0, 'tx_u' => 0, 'rx_u' => 0}....]
  field :d, type: Date

  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" => {"info.NASID" => 1,
                                                                          "uplink" => {"$ifNull" => [ "$uplink", "$lan" ]}}},
                                                          {"$unwind" => "$uplink"},
                                                          {"$project" => {'ap' => "$info.NASID",
                                                                          "tx_u" => "$uplink.TX_BYTES_INT",
                                                                          "tx_r" => "$uplink.TX_RATE",
                                                                          "rx_u" => "$uplink.RX_BYTES_INT",
                                                                          "rx_r" => "$uplink.RX_RATE",
                                                                          "utype" => "$uplink.TYPE",
                                                                          "uid" => "$uplink.UID"}},
                                                           {"$group" => {"_id" => {"ap" => "$ap", "uid" => "$uid", "utype" => "$utype"},
                                                                        "tx_u" => {"$sum" => "$tx_u"},
                                                                        "tx_r" => {"$avg" => "$tx_r"},
                                                                        "rx_u" => {"$sum" => "$rx_u"},
                                                                        "rx_r" => {"$avg" => "$rx_r"}}},
                                                          {"$project" => {"_id" => 1,
                                                                          "tx_u" => 1,
                                                                          "tx_r" => 1,
                                                                          "rx_u" => 1,
                                                                          "rx_r" => 1}})
    aggregate_data.each do |d|
      self.collection.where({"mac" => d['_id']['ap'], "uid" => d['_id']['uid'], "utype" => d['_id']['utype'], "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"]}}}, [:upsert] )
    end
  end
end
