ActiveAdmin.register_page "Monthly Stats" do
  menu :parent => 'Reports'


  content :title => "Monthly Stats" do
    h2 "Distinct Users Playing in Sessions"
    table_for MusicSession.select([:month, :count]).find_by_sql("select date_trunc('month', msuh.created_at)::date as month, count(distinct(user_id)) from music_sessions_user_history msuh group by month  order by month desc;") do
      column "Month", Proc.new { |row| row.month.strftime('%B %Y') }
      column "Users", :count
    end

    h2 "Music Sessions"
    table_for MusicSession.select([:month, :count]).find_by_sql("select date_trunc('month', ms.created_at)::date as month, count(id) from music_sessions ms where started_at is not null group by month  order by month desc;") do
      column "Month", Proc.new { |row| row.month.strftime('%B %Y') }
      column "Sessions", :count
    end

    h2 "Distinct Users Who Played with a JamTrack"
    table_for MusicSession.select([:month, :count]).find_by_sql("select date_trunc('month', jts.created_at)::date as month, count(distinct(user_id)) from jam_track_sessions jts group by month  order by month desc;") do
      column "Month", Proc.new { |row| row.month.strftime('%B %Y') }
      column "Users", :count
    end

    h2 "Music Sessions with JamTracks Played"
    table_for MusicSession.select([:month, :count]).find_by_sql("select date_trunc('month', jts.created_at)::date as month, count(distinct(music_session_id)) from jam_track_sessions jts where session_type = 'session' group by month  order by month desc;") do
      column "Month", Proc.new { |row| row.month.strftime('%B %Y') }
      column "Sessions", :count
    end

    h2 "JamTrack Web Player Sessions"
    table_for MusicSession.select([:month, :count]).find_by_sql("select date_trunc('month', jts.created_at)::date as month, count(id) from jam_track_sessions jts where session_type = 'browser' group by month  order by month desc;") do
      column "Month", Proc.new { |row| row.month.strftime('%B %Y') }
      column "Sessions", :count
    end

  end

end