Add users/activity page.
[arvados.git] / apps / workbench / app / controllers / users_controller.rb
1 class UsersController < ApplicationController
2   skip_before_filter :find_object_by_uuid, :only => [:welcome, :activity]
3   skip_around_filter :thread_with_mandatory_api_token, :only => :welcome
4
5   def welcome
6     if current_user
7       params[:action] = 'home'
8       home
9     end
10   end
11
12   def activity
13     @breadcrumb_page_name = nil
14     @users = User.all
15     @user_activity = {}
16     @activity = {
17       logins: {},
18       jobs: {},
19       pipeline_instances: {}
20     }
21     @spans = [['This week', Time.now.beginning_of_week, Time.now],
22               ['Last week', 1.week.ago.beginning_of_week, nil],
23               ['This month', Time.now.beginning_of_month, Time.now],
24               ['Last month', 1.month.ago.beginning_of_month, nil]].
25       collect do |span|
26       span[2] ||= span[1].advance(months: 1) if span[0].match /month/
27       span[2] ||= span[1].advance(weeks: 1) if span[0].match /week/
28       span
29     end
30     @spans.each do |span, threshold_start, threshold_end|
31       @activity[:logins][span] = Log.
32         filter([[:event_type, '=', 'login'],
33                 [:object_kind, '=', 'arvados#user'],
34                 [:created_at, '>=', threshold_start],
35                 [:created_at, '<', threshold_end]])
36       @activity[:jobs][span] = Job.
37         filter([[:created_at, '>=', threshold_start],
38                 [:created_at, '<', threshold_end]])
39       @activity[:pipeline_instances][span] = PipelineInstance.
40         filter([[:created_at, '>=', threshold_start],
41                 [:created_at, '<', threshold_end]])
42       @activity.each do |type, act|
43         records = act[span]
44         @users.each do |u|
45           @user_activity[u.uuid] ||= {}
46           @user_activity[u.uuid][span + ' ' + type.to_s] ||= 0
47         end
48         records.each do |record|
49           @user_activity[record.modified_by_user_uuid] ||= {}
50           @user_activity[record.modified_by_user_uuid][span + ' ' + type.to_s] ||= 0
51           @user_activity[record.modified_by_user_uuid][span + ' ' + type.to_s] += 1
52         end
53       end
54     end
55     @users = @users.sort_by do |a|
56       [-@user_activity[a.uuid].values.inject(:+), a.full_name]
57     end
58   end
59
60   def home
61     @showallalerts = false
62     @my_ssh_keys = AuthorizedKey.where(authorized_user_uuid: current_user.uuid)
63     # @my_vm_perms = Link.where(tail_uuid: current_user.uuid, head_kind: 'arvados#virtual_machine', link_class: 'permission', name: 'can_login')
64     # @my_repo_perms = Link.where(tail_uuid: current_user.uuid, head_kind: 'arvados#repository', link_class: 'permission', name: 'can_write')
65
66     @my_tag_links = {}
67
68     @my_jobs = Job.
69       limit(10).
70       order('created_at desc').
71       where(created_by: current_user.uuid)
72
73     @my_collections = Collection.
74       limit(10).
75       order('created_at desc').
76       where(created_by: current_user.uuid)
77
78     Link.limit(1000).where(head_uuid: @my_collections.collect(&:uuid),
79                            link_class: 'tag').each do |link|
80       (@my_tag_links[link.head_uuid] ||= []) << link
81     end
82
83     @my_pipelines = PipelineInstance.
84       limit(10).
85       order('created_at desc').
86       where(created_by: current_user.uuid)
87
88
89     # A Tutorial is a Link which has link_class "resources" and name
90     # "wants", and is owned by the Tutorials Group (i.e., named
91     # "Arvados Tutorials" and owned by the system user).
92     @tutorial_group = Group.where(owner_uuid: User.system.uuid,
93                                   name: 'Arvados Tutorials').first
94     if @tutorial_group
95       @tutorial_links = Link.where(tail_uuid: @tutorial_group.uuid,
96                                    link_class: 'resources',
97                                    name: 'wants')
98     else
99       @tutorial_links = []
100     end
101     @tutorial_complete = {
102       'Run a job' => @my_last_job
103     }
104     respond_to do |f|
105       f.js { render template: 'users/home.js' }
106       f.html { render template: 'users/home' }
107     end
108   end
109 end