class CreateApProfileInRedis < ActiveRecord::Migration[6.0]
  def self.up
    RouterInventory.all.includes(:location_network, :location_network => [:alerts]).each do |ri|
      $redis.hset "AP:#{ri.mac_id}", "AP", ri.name
      $redis.hset "AP:#{ri.mac_id}", "ID", ri.id
      $redis.hset "AP:#{ri.mac_id}", "Tags", ri.tags.map(&:name).join(',') unless ri.tags.blank?
      $redis.hset "AP:#{ri.mac_id}", "HW", ri.hardware_part.try(:name)
      $redis.hset "AP:#{ri.mac_id}", "Network", ri.location_network.try(:network_name)
      $redis.hset "AP:#{ri.mac_id}", "HB", ri.find_last_hb["hb"]
      $redis.hset "AP:#{ri.mac_id}", "alert", '1' unless ri.location_network.try(:alerts).blank?
    end
  end

  def self.down
    RouterInventory.all.includes(:location_network).each do |ri|
      $redis.hdel "AP:#{x.mac_id}",["Network","alert","AP","ID","Tags","HW","HB"]
    end
  end
end
