require 'fileutils'
require 'usagewatch'

class Admin::DashboardsController < OrbitBackendController

  open_for_visitor

  layout "basic_back_end"
  
  def index
    check_backend_openness
    @module_app_contents, @module_app_contents_total = get_module_app_count('bulletin', 'page_context', 'web_link')
    @recent_updated = get_recently_updated('bulletin', 'page_context', 'web_link')
    @most_visited = get_most_visited('bulletin', 'page_context','page')
  end

  def reload_all_content
    @module_app_contents, @module_app_contents_total = get_module_app_count('bulletin', 'page_context', 'web_link')
    respond_to do |format|
      format.js { render 'reload', locals: {div_id: 'all_content'} }
    end
  end

  def reload_most_visited
    @most_visited = get_most_visited('bulletin', 'page_context','page')
    respond_to do |format|
      format.js { render 'reload', locals: {div_id: 'most_visited'} }
    end
  end

  def reload_recent_update
    @recent_updated = get_recently_updated('bulletin', 'page_context', 'web_link')
    respond_to do |format|
      format.js { render 'reload', locals: {div_id: 'recent_update'} }
    end
  end

  def get_cpu_usage
    @usw = Usagewatch
    render :js => @usw.uw_cpuused.to_s
  end

  def get_mem_usage
    @usw = Usagewatch
    render :js => @usw.uw_memused.to_s
  end

  def get_disk_usage
    @usw = Usagewatch
    render :json => @usw.uw_diskused_perc.to_s
  end

  def get_month_traffic
    result = []
    (0..31).each do |i|
      the_day = i.day.ago
      visits = Impression.where( created_at: {'$gte' => the_day.beginning_of_day, '$lte' => the_day.end_of_day}).count
      result.push({the_day.strftime("%b-%d")=>visits})
    end
    render :js => result.to_json
  end

  protected

  def get_module_app_count(*args)
  	a = {}
  	total = 0
  	args.each do |module_app|
  		module_app_class = module_app.classify.constantize
  		count = module_app_class.count
  		a.merge!(module_app => count)
  		total += count
  	end
    [Kaminari.paginate_array(a.sort {|a,b| b[1]<=>a[1]}).page(params[:page]).per(5), total]
  end

  def get_recently_updated(*args)
  	a = {}
  	args.each do |module_app|
  		module_app_class = module_app.classify.constantize
  		objects = module_app_class.order_by(:updated_at, :desc).limit(20)
  		objects.each do |object|
  			a.merge!(object => object.updated_at) unless (object.archived rescue nil)
  		end
  	end
  	sorted_objects = a.sort {|a,b| b[1]<=>a[1]}
  	sorted_objects[0..19]
    Kaminari.paginate_array(sorted_objects).page(params[:page]).per(5)
  end

  def get_most_visited(*args)
    a = {}
    args.each do |module_app|
      module_app_class = module_app.classify.constantize
      objects = module_app_class.order_by(:view_count, :desc).limit(20)
      objects.each do |object|
        a.merge!(object => object.view_count) if object.view_count > 0 && (!object.archived rescue true)
      end
    end
    sorted_objects = a.sort {|a,b| b[1]<=>a[1]}
    sorted_objects[0..19]
    Kaminari.paginate_array(sorted_objects).page(params[:page]).per(5)
  end
  
end
