Skip to content

Commit

Permalink
Analysis (#35)
Browse files Browse the repository at this point in the history
* show all activities

* Fix user recording.

* Infer meetings.

* Added tests for session inference.

* Fixed rspec tests.

* Updated UI.

* Updated UI and added some basic numbers for a meeting.
  • Loading branch information
an-ju authored and adnanhemani committed Apr 10, 2019
1 parent 64fdea3 commit 4c764c1
Show file tree
Hide file tree
Showing 18 changed files with 212 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.2
2.3.8
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ gem "omniauth-google-oauth2"
gem 'omniauth-oauth2', '~> 1.3.1'

gem 'rspec-rails', '~> 3.6.1', group: [:test, :development]
gem 'rb-readline'

group :test do
gem 'factory_bot_rails'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ GEM
rb-fsevent (0.10.3)
rb-inotify (0.10.0)
ffi (~> 1.0)
rb-readline (0.5.5)
rdoc (4.3.0)
ref (2.0.0)
representable (3.0.4)
Expand Down Expand Up @@ -431,6 +432,7 @@ DEPENDENCIES
rack_session_access
rails (~> 4.2.10)
rails_12factor
rb-readline
responders (~> 2.0)
rest-client
rspec-rails (~> 3.6.1)
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/activities_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class ActivitiesController < ApplicationController
layout false
before_action :set_activity, only: [:show, :destroy]

# GET /activities
Expand All @@ -12,6 +13,10 @@ def index
def show
end

def meetings
@meetings = Activity.infer_meetings
end

# POST /activities.json
def create
@activity = Activity.new(activity_params)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def rescue_steps(message)
def record_activity
activity_param = {
activity_type: "#{params[:controller]}\##{params[:action]}",
user_id: current_user.nil? ? nil : current_user[:id],
username: current_user.nil? ? nil : current_user['username']
}

if params[:action].eql? 'project'
Expand Down
20 changes: 20 additions & 0 deletions app/helpers/activities_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
module ActivitiesHelper
def meeting_start(meetings)
meetings.first.created_at
end

def meeting_length(meetings)
((meetings.last.created_at - meetings.first.created_at) / 60.0).round(2)
end

def num_stories_updated(meetings)
meetings.select { |act| act.activity_type.eql? 'dashboard#update' }
.map(&:story_id).uniq.length
end

def num_votes(meetings)
meetings.select { |act| act.activity_type.eql? 'dashboard#vote' }.length
end

def num_participants(meetings)
meetings.map(&:username).uniq.length
end
end
18 changes: 17 additions & 1 deletion app/models/activity.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
class Activity < ActiveRecord::Base
belongs_to :user
belongs_to :vote, required: false

def self.infer_meetings
prev_time = Activity.first.created_at
meetings = []
current_meeting = []
Activity.all.each do |activity|
if (activity.created_at - prev_time) > 60 * 60
meetings.push(current_meeting.clone)
current_meeting = [activity]
else
current_meeting.push(activity)
end
prev_time = activity.created_at
end
meetings.push(current_meeting.clone)
meetings
end
end
24 changes: 24 additions & 0 deletions app/views/activities/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<table>
<thead>
<tr>
<th>Project ID</th>
<th>Story ID</th>
<th>User</th>
<th>Activity Type</th>
<th>Activity Data</th>
<th>Timestamp</th>
</tr>
</thead>
<tbody>
<%- @activities.each do |activity| %>
<tr>
<td><%= activity.project_id %></td>
<td><%= activity.story_id %></td>
<td><%= activity.username %></td>
<td><%= activity.activity_type %></td>
<td><%= activity.activity_data %></td>
<td><%= activity.created_at %></td>
</tr>
<% end %>
</tbody>
</table>
94 changes: 94 additions & 0 deletions app/views/activities/meetings.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</head>

<table class="table">
<thead>
<tr>
<th>Meeting Number</th>
<th>Started At</th>
<th>Length of the meeting</th>
<th>Participants</th>
<th>Story updated</th>
<th>Votes</th>
<th>See more</th>
</tr>
</thead>
<tbody>
<%- @meetings.each_with_index do |meeting, ind| %>
<tr>
<td>
Meeting <%= ind %>
</td>
<td>
<%= meeting_start(meeting) %>
</td>
<td>
<%= meeting_length(meeting) %> minutes
</td>
<td>
<%= num_participants(meeting) %> participants
</td>
<td>
<%= num_stories_updated(meeting) %> stories updated
</td>
<td>
<%= num_votes(meeting) %> votes
</td>
<td>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#meeting-<%= ind %>">
Activities
</button>
</td>
</tr>
<% end %>
</tbody>

</table>

<%- @meetings.each_with_index do |meeting, ind| %>
<!-- Modal -->
<div class="modal fade" id="meeting-<%= ind %>" tabindex="-1" role="dialog" aria-labelledby="label_<%= ind %>" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="label_<%= ind %>">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<table>
<thead>
<tr>
<th>Project ID</th>
<th>Story ID</th>
<th>User</th>
<th>Activity Type</th>
<th>Timestamp</th>
</tr>
</thead>
<tbody>
<%- meeting.each do |activity| %>
<tr>
<td><%= activity.project_id %></td>
<td><%= activity.story_id %></td>
<td><%= activity.username %></td>
<td><%= activity.activity_type %></td>
<td><%= activity.created_at %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<% end %>

6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,9 @@
get 'discussion/:story_id', action: :discussion, as: :discussion
end

resources :activities, only: [:index, :show, :create, :destroy]
resources :activities, only: [:index] do
collection do
get 'meetings'
end
end
end
Binary file removed db/development
Binary file not shown.
5 changes: 5 additions & 0 deletions db/migrate/20190409034843_add_username_to_activity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddUsernameToActivity < ActiveRecord::Migration
def change
add_column :activities, :username, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20180801063748) do
ActiveRecord::Schema.define(version: 20190409034843) do

create_table "activities", force: :cascade do |t|
t.integer "user_id"
Expand All @@ -22,6 +22,7 @@
t.text "activity_data"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "username"
end

add_index "activities", ["user_id"], name: "index_activities_on_user_id"
Expand Down
Binary file removed db/test
Binary file not shown.
16 changes: 8 additions & 8 deletions spec/controllers/dashboard_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def valid_session

it 'should create an activity' do
Activity.expects(:create)
.with({user_id: user.id, activity_type: 'dashboard#index'})
.with({username: user.username, activity_type: 'dashboard#index'})
get :index, {}, valid_session
end
end
Expand All @@ -42,7 +42,7 @@ def valid_session
it 'should create an acvitiy' do
Activity.expects(:create)
.with({
user_id: user.id,
username: user.username,
activity_type: 'dashboard#project',
project_id: params[:id]
})
Expand Down Expand Up @@ -79,7 +79,7 @@ def valid_session
it 'should create an activity' do
Activity.expects(:create)
.with({
user_id: user.id,
username: user.username,
activity_type: 'dashboard#vote',
story_id: '123',
activity_data: {
Expand Down Expand Up @@ -120,7 +120,7 @@ def valid_session
it 'should create an activity' do
Activity.expects(:create)
.with({
user_id: user.id,
username: user.username,
activity_type: 'dashboard#reset',
story_id: '123',
activity_data: {story_id: '123', user: decoded_user(params['user'])}.to_json
Expand Down Expand Up @@ -149,7 +149,7 @@ def valid_session
it 'should create an activity' do
Activity.expects(:create)
.with({
user_id: user.id,
username: user.username,
activity_type: 'dashboard#detail',
story_id: '123',
activity_data: {
Expand Down Expand Up @@ -181,7 +181,7 @@ def valid_session
it 'should create an activity' do
Activity.expects(:create)
.with({
user_id: user.id,
username: user.username,
activity_type: 'dashboard#reveal',
story_id: '123',
activity_data: {story_id: '123'}.to_json
Expand Down Expand Up @@ -230,7 +230,7 @@ def valid_session
it 'should create an activity' do
Activity.expects(:create)
.with({
user_id: user.id,
username: user.username,
activity_type: 'dashboard#update',
story_id: '1',
activity_data: fake_return.to_json
Expand Down Expand Up @@ -259,7 +259,7 @@ def valid_session
it 'should create an activity' do
Activity.expects(:create)
.with({
user_id: user.id,
username: user.username,
activity_type: 'dashboard#select',
story_id: '123',
activity_data: {story_id: '123', username: 'username'}.to_json
Expand Down
3 changes: 3 additions & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@
vote { Forgery::Basic.number(at_least: 1, at_most: 5) }
end

factory :activity do
end

end
23 changes: 22 additions & 1 deletion spec/models/activity_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
require 'rails_helper'
require 'spec_helper'

RSpec.describe Activity, type: :model do

describe 'self.infer_meetings' do
before :each do
t = Time.now
FactoryBot.create(:activity, created_at: t)
FactoryBot.create(:activity, created_at: t+1.minute)
FactoryBot.create(:activity, created_at: t+1.hour+2.minute)
FactoryBot.create(:activity, created_at: t+1.day)
FactoryBot.create(:activity, created_at: t+1.day+1.minute)
end
it 'groups activities together' do
expect(Activity.infer_meetings.length).to eql(3)
end

it 'divides activities correctly' do
meetings = Activity.infer_meetings
expect(meetings[0].length).to eql(2)
expect(meetings[1].length).to eql(1)
expect(meetings[2].length).to eql(2)
end
end
end
12 changes: 2 additions & 10 deletions spec/routing/activities_routing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,8 @@
expect(:get => "/activities").to route_to("activities#index")
end

it "routes to #show" do
expect(:get => "/activities/1").to route_to("activities#show", :id => "1")
end

it "routes to #create" do
expect(:post => "/activities").to route_to("activities#create")
end

it "routes to #destroy" do
expect(:delete => "/activities/1").to route_to("activities#destroy", :id => "1")
it "routes to #meetings" do
expect(get: '/activities/meetings').to route_to('activities#meetings')
end

end
Expand Down

0 comments on commit 4c764c1

Please sign in to comment.