From fae8b8adde3d4dc888dcb2279a20f14c43e5fd09 Mon Sep 17 00:00:00 2001 From: Ian Hirschfeld Date: Tue, 8 Mar 2016 21:23:05 -0800 Subject: [PATCH 1/2] [WIP] Starts adding room stats. * Numbers of played tracks. * MVDJ. * Total played hours. --- app/models/comment.rb | 6 +++++- app/models/message.rb | 15 +++++++++++++++ app/views/messages/show.html.erb | 6 +++--- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/app/models/comment.rb b/app/models/comment.rb index d44c5ed..d0b0dc3 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -23,6 +23,10 @@ def blurred_album_art_url ix_image_url(album_art_url, { blur: 200 }) end + def duration + (self.responses['duration'] / 1000).ceil + end + def palette_url ix_image_url(album_art_url, { palette: 'css', colors: 4, class: 'album', zoom: 100 }) end @@ -34,7 +38,7 @@ def reset! def start_playing(should_push) now = Time.now.utc update_attributes(now_playing: true, aired_at: now) - next_track_at = now + (self.responses['duration'] / 1000).ceil + next_track_at = now + duration NextUpJob.set(wait_until: next_track_at).perform_later(self.message.id, self.id) PusherService.now_playing_track(self) if should_push end diff --git a/app/models/message.rb b/app/models/message.rb index e733500..3e30b41 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -2,7 +2,22 @@ class Message < ActiveRecord::Base belongs_to :user has_many :comments + def mvdj + grouped_track_count = self.comments.group(:user_id).count + sorted_group = grouped_track_count.sort_by { |user_id, count| -count } + User.find_by(id: sorted_group.first) + end + + def played_tracks + self.comments.where.not(aired_at: nil) + end + def queue_count self.comments.enqueued.count end + + def total_played_hours + seconds = played_tracks.reduce(0) { |sum, track| sum + track.duration } + (seconds.to_f / 60.0 / 60.0).round(2) + end end diff --git a/app/views/messages/show.html.erb b/app/views/messages/show.html.erb index f36bff3..7093bd0 100644 --- a/app/views/messages/show.html.erb +++ b/app/views/messages/show.html.erb @@ -149,19 +149,19 @@ - + - + - +
Songs played9,001Songs played<%= @message.played_tracks.count %>
Most users in roomA lot
Hours of music queued upSo manyHours of music queued up<%= @message.total_played_hours %>
Longest streakLike, days
MVDJBaldwinMVDJ<%= @message.mvdj.full_name %>
From 355ee634a71e041100b8c769d3a17e39b646495b Mon Sep 17 00:00:00 2001 From: Ian Hirschfeld Date: Tue, 8 Mar 2016 22:14:42 -0800 Subject: [PATCH 2/2] Adds most users count to room stats. --- app/controllers/messages_controller.rb | 35 ++++++++++++++----- app/views/messages/show.html.erb | 13 +++++-- config/routes.rb | 1 + ...053318_add_most_users_count_to_messages.rb | 5 +++ db/schema.rb | 18 +++++----- 5 files changed, 51 insertions(+), 21 deletions(-) create mode 100644 db/migrate/20160309053318_add_most_users_count_to_messages.rb diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index b7e94f1..4755c32 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -1,7 +1,11 @@ class MessagesController < ApplicationController - def backfill - @message = Message.find(params[:id]) + before_action :set_message, only: [ + :backfill, + :next, + :update_user_count + ] + def backfill url = "https://api-v2.soundcloud.com/explore/Popular+Music?tag=out-of-experiment&limit=100&client_id=#{ENV['SOUNDCLOUD_ID']}" resp = Net::HTTP.get_response(URI.parse(url)) buffer = resp.body @@ -20,15 +24,18 @@ def backfill end end + def create + @message = Message.create message_params + redirect_to @message + end + def index @messages = Message.all.order('lower(title) ASC') end def next - if @message = Message.find_by(id: params[:id]) - if @comment = @message.comments.playing.first - NextUpJob.perform_now(@message.id, @comment.id) - end + if @comment = @message.comments.playing.first + NextUpJob.perform_now(@message.id, @comment.id) end end @@ -60,9 +67,13 @@ def show end end - def create - @message = Message.create message_params - redirect_to @message + def update_user_count + user_count = params[:user_count].presence || 0 + user_count = user_count.to_i + if user_count > @message.most_users_count + @message.update_attributes(most_users_count: user_count) + end + render json: {user_count: @message.most_users_count}, status: :ok end private @@ -70,4 +81,10 @@ def create def message_params params.require(:message).permit(:title) end + + def set_message + unless @message = Message.find_by(id: params[:id]) + render nothing: true, status: :bad_request + end + end end diff --git a/app/views/messages/show.html.erb b/app/views/messages/show.html.erb index 7093bd0..66ebd64 100644 --- a/app/views/messages/show.html.erb +++ b/app/views/messages/show.html.erb @@ -152,7 +152,7 @@ Songs played<%= @message.played_tracks.count %> - Most users in roomA lot + Most users in room<%= @message.most_users_count %> Hours of music queued up<%= @message.total_played_hours %> @@ -365,6 +365,16 @@ $('.usersInRoom').append(''); }); $('.js-membersOnline').text(presenceChannel.members.count); + $.ajax({ + url: "/messages/<%= @message.id %>/update_user_count", + method: 'post', + data: { + user_count: presenceChannel.members.count + }, + success: function(data) { + $('.js-most-users-count').text(data.user_count); + } + }); }); // Listen for users entering the room @@ -498,5 +508,4 @@ $('#new_comment').bind('ajax:complete', function(event,xhr,status){ $('#comment_content').val(''); }); - diff --git a/config/routes.rb b/config/routes.rb index df84cd9..a6ccb26 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,6 +7,7 @@ get :backfill get :next get :reload_all + post :update_user_count end end resources :pusher, only: [] do diff --git a/db/migrate/20160309053318_add_most_users_count_to_messages.rb b/db/migrate/20160309053318_add_most_users_count_to_messages.rb new file mode 100644 index 0000000..0a4cc0e --- /dev/null +++ b/db/migrate/20160309053318_add_most_users_count_to_messages.rb @@ -0,0 +1,5 @@ +class AddMostUsersCountToMessages < ActiveRecord::Migration[5.0] + def change + add_column :messages, :most_users_count, :integer, default: 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index 4e7d441..ed773e9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160220202815) do +ActiveRecord::Schema.define(version: 20160309053318) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -20,9 +20,9 @@ t.integer "message_id" t.integer "user_id" t.text "content" - t.datetime "created_at" - t.datetime "updated_at" t.jsonb "responses", default: {}, null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.boolean "now_playing", default: false, null: false t.datetime "aired_at" t.index ["message_id"], name: "index_comments_on_message_id", using: :btree @@ -33,21 +33,19 @@ t.integer "user_id" t.string "title" t.text "content" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.integer "most_users_count", default: 0 t.index ["user_id"], name: "index_messages_on_user_id", using: :btree end create_table "users", force: :cascade do |t| t.string "name" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.jsonb "soundcloud_token" t.jsonb "soundcloud_response" t.boolean "boss", default: false end - add_foreign_key "comments", "messages" - add_foreign_key "comments", "users" - add_foreign_key "messages", "users" end