Fix up start/end dates and display them in a more friendly way.
[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',
23                Time.now.beginning_of_week.advance(weeks:-1),
24                Time.now.beginning_of_week],
25               ['This month', Time.now.beginning_of_month, Time.now],
26               ['Last month',
27                1.month.ago.beginning_of_month,
28                Time.now.beginning_of_month]]
29     @spans.each do |span, threshold_start, threshold_end|
30       @activity[:logins][span] = Log.
31         filter([[:event_type, '=', 'login'],
32                 [:object_kind, '=', 'arvados#user'],
33                 [:created_at, '>=', threshold_start],
34                 [:created_at, '<', threshold_end]])
35       @activity[:jobs][span] = Job.
36         filter([[:created_at, '>=', threshold_start],
37                 [:created_at, '<', threshold_end]])
38       @activity[:pipeline_instances][span] = PipelineInstance.
39         filter([[:created_at, '>=', threshold_start],
40                 [:created_at, '<', threshold_end]])
41       @activity.each do |type, act|
42         records = act[span]
43         @users.each do |u|
44           @user_activity[u.uuid] ||= {}
45           @user_activity[u.uuid][span + ' ' + type.to_s] ||= 0
46         end
47         records.each do |record|
48           @user_activity[record.modified_by_user_uuid] ||= {}
49           @user_activity[record.modified_by_user_uuid][span + ' ' + type.to_s] ||= 0
50           @user_activity[record.modified_by_user_uuid][span + ' ' + type.to_s] += 1
51         end
52       end
53     end
54     @users = @users.sort_by do |a|
55       [-@user_activity[a.uuid].values.inject(:+), a.full_name]
56     end
57   end
58
59   def home
60     @showallalerts = false
61     @my_ssh_keys = AuthorizedKey.where(authorized_user_uuid: current_user.uuid)
62     # @my_vm_perms = Link.where(tail_uuid: current_user.uuid, head_kind: 'arvados#virtual_machine', link_class: 'permission', name: 'can_login')
63     # @my_repo_perms = Link.where(tail_uuid: current_user.uuid, head_kind: 'arvados#repository', link_class: 'permission', name: 'can_write')
64
65     @my_tag_links = {}
66
67     @my_jobs = Job.
68       limit(10).
69       order('created_at desc').
70       where(created_by: current_user.uuid)
71
72     @my_collections = Collection.
73       limit(10).
74       order('created_at desc').
75       where(created_by: current_user.uuid)
76
77     Link.limit(1000).where(head_uuid: @my_collections.collect(&:uuid),
78                            link_class: 'tag').each do |link|
79       (@my_tag_links[link.head_uuid] ||= []) << link
80     end
81
82     @my_pipelines = PipelineInstance.
83       limit(10).
84       order('created_at desc').
85       where(created_by: current_user.uuid)
86
87
88     # A Tutorial is a Link which has link_class "resources" and name
89     # "wants", and is owned by the Tutorials Group (i.e., named
90     # "Arvados Tutorials" and owned by the system user).
91     @tutorial_group = Group.where(owner_uuid: User.system.uuid,
92                                   name: 'Arvados Tutorials').first
93     if @tutorial_group
94       @tutorial_links = Link.where(tail_uuid: @tutorial_group.uuid,
95                                    link_class: 'resources',
96                                    name: 'wants')
97     else
98       @tutorial_links = []
99     end
100     @tutorial_complete = {
101       'Run a job' => @my_last_job
102     }
103     respond_to do |f|
104       f.js { render template: 'users/home.js' }
105       f.html { render template: 'users/home' }
106     end
107   end
108 end