Merge branch '2291-new-keepd-read-blocks' (fixes #2291)
[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   before_filter :ensure_current_user_is_admin, only: :sudo
5
6   def welcome
7     if current_user
8       params[:action] = 'home'
9       home
10     end
11   end
12
13   def activity
14     @breadcrumb_page_name = nil
15     @users = User.all
16     @user_activity = {}
17     @activity = {
18       logins: {},
19       jobs: {},
20       pipeline_instances: {}
21     }
22     @total_activity = {}
23     @spans = [['This week', Time.now.beginning_of_week, Time.now],
24               ['Last week',
25                Time.now.beginning_of_week.advance(weeks:-1),
26                Time.now.beginning_of_week],
27               ['This month', Time.now.beginning_of_month, Time.now],
28               ['Last month',
29                1.month.ago.beginning_of_month,
30                Time.now.beginning_of_month]]
31     @spans.each do |span, threshold_start, threshold_end|
32       @activity[:logins][span] = Log.
33         filter([[:event_type, '=', 'login'],
34                 [:object_kind, '=', 'arvados#user'],
35                 [:created_at, '>=', threshold_start],
36                 [:created_at, '<', threshold_end]])
37       @activity[:jobs][span] = Job.
38         filter([[:created_at, '>=', threshold_start],
39                 [:created_at, '<', threshold_end]])
40       @activity[:pipeline_instances][span] = PipelineInstance.
41         filter([[:created_at, '>=', threshold_start],
42                 [:created_at, '<', threshold_end]])
43       @activity.each do |type, act|
44         records = act[span]
45         @users.each do |u|
46           @user_activity[u.uuid] ||= {}
47           @user_activity[u.uuid][span + ' ' + type.to_s] ||= 0
48         end
49         records.each do |record|
50           @user_activity[record.modified_by_user_uuid] ||= {}
51           @user_activity[record.modified_by_user_uuid][span + ' ' + type.to_s] ||= 0
52           @user_activity[record.modified_by_user_uuid][span + ' ' + type.to_s] += 1
53           @total_activity[span + ' ' + type.to_s] ||= 0
54           @total_activity[span + ' ' + type.to_s] += 1
55         end
56       end
57     end
58     @users = @users.sort_by do |a|
59       [-@user_activity[a.uuid].values.inject(:+), a.full_name]
60     end
61     # Prepend a "Total" pseudo-user to the sorted list
62     @user_activity[nil] = @total_activity
63     @users = [OpenStruct.new(uuid: nil)] + @users
64   end
65
66   def show_pane_list
67     if current_user.andand.is_admin
68       super | %w(Admin)
69     else
70       super
71     end
72   end
73
74   def index_pane_list
75     if current_user.andand.is_admin
76       super | %w(Activity)
77     else
78       super
79     end
80   end
81
82   def sudo
83     resp = $arvados_api_client.api(ApiClientAuthorization, '', {
84                                      api_client_authorization: {
85                                        owner_uuid: @object.uuid
86                                      }
87                                    })
88     redirect_to root_url(api_token: resp[:api_token])
89   end
90
91   def home
92     @showallalerts = false
93     @my_ssh_keys = AuthorizedKey.where(authorized_user_uuid: current_user.uuid)
94     # @my_vm_perms = Link.where(tail_uuid: current_user.uuid, head_kind: 'arvados#virtual_machine', link_class: 'permission', name: 'can_login')
95     # @my_repo_perms = Link.where(tail_uuid: current_user.uuid, head_kind: 'arvados#repository', link_class: 'permission', name: 'can_write')
96
97     @my_tag_links = {}
98
99     @my_jobs = Job.
100       limit(10).
101       order('created_at desc').
102       where(created_by: current_user.uuid)
103
104     @my_collections = Collection.
105       limit(10).
106       order('created_at desc').
107       where(created_by: current_user.uuid)
108
109     Link.limit(1000).where(head_uuid: @my_collections.collect(&:uuid),
110                            link_class: 'tag').each do |link|
111       (@my_tag_links[link.head_uuid] ||= []) << link
112     end
113
114     @my_pipelines = PipelineInstance.
115       limit(10).
116       order('created_at desc').
117       where(created_by: current_user.uuid)
118
119
120     # A Tutorial is a Link which has link_class "resources" and name
121     # "wants", and is owned by the Tutorials Group (i.e., named
122     # "Arvados Tutorials" and owned by the system user).
123     @tutorial_group = Group.where(owner_uuid: User.system.uuid,
124                                   name: 'Arvados Tutorials').first
125     if @tutorial_group
126       @tutorial_links = Link.where(tail_uuid: @tutorial_group.uuid,
127                                    link_class: 'resources',
128                                    name: 'wants')
129     else
130       @tutorial_links = []
131     end
132     @tutorial_complete = {
133       'Run a job' => @my_last_job
134     }
135     respond_to do |f|
136       f.js { render template: 'users/home.js' }
137       f.html { render template: 'users/home' }
138     end
139   end
140 end