Added the HN Stats written by Uthman Apatira.
[mwamko.git] / app / controllers / admin_controller.rb
blob8fefa45bf30006dbae186c7a4d493c4e52f1476d
1 class AdminController < ApplicationController
2   before_filter :only_for_admin
3   before_filter :set_page_title
4   before_filter :set_mode 
6   
7   def index
8     redirect_to :action => "global_table"
9   end
11   
12   def global_table
13     @order_list = (@params['order_list']||'').split('/').collect {|i| i.to_sym}
14     @order_directions = (@params['order_directions']||'').split('/')
16     # Default Order
17     if @order_list.empty?
18       @order_list = [:is_admin, :veid]
19       @order_directions = ['ASC', 'DESC']
20     end
22     @notices ||= []
23     
24     @user_columns = [:name, :institution, :is_admin, :labels]
25     @ve_columns = [:status, :veid, :ip, :label]
27     virtual_environments = VirtualEnvironment.find(:all)
28     users = User.find(:all)
30     # Building the @rows array
31     @rows = []
32     virtual_environments.each do |ve|
33       user = User.find_user_by_id(users, ve.uid)
34       
35       @rows << {}
36       @user_columns.each do |c| 
37         @rows.last[c] = case c
38                         when :institution
39                           # user[:email] =~ SAFE_EMAIL
40                           # $3
41                         when :labels
42                         else
43                           user.method(c).call
44                         end
45       end
47       @ve_columns.each do |c| 
48         @rows.last[c] = case c
49                         when :label
50                         else
51                           ve.method(c).call
52                         end
53       end
55     end
58     # Sorting the @rows array
60     def comparison(first, second) 
61       human2digit = {'ASC' => 1, 'DESC' => -1}
63       @order_list.each_with_index do |o, i|
64         human_direction = @order_directions[i]
66         formated_first, formated_second = * case o
67                                             when :is_admin
68                                               m = {true=>1, false=>0}
69                                               [ # booleans
70                                                m[first[o]], 
71                                                m[second[o]]
72                                               ]
73                                             when :veid
74                                               [ # integers
75                                                first[o], 
76                                                second[o]
77                                               ]
78                                             else
79                                               [ 
80                                                first[o].to_s, 
81                                                second[o].to_s
82                                               ]
83                                             end
85         # This will be 1, -1, or 0 
86         cmp = (formated_first <=> formated_second)
88         if cmp != 0
89           return cmp * human2digit[human_direction]
90         end
91       end
93       return 0 # Elements are equal
94     end
96     @rows.sort!{ |f,s| comparison(f,s) }
99   end
101   def action   
103     flash[:notices] ||= []
104     has_selection = false
105     
106     params.each_pair do |key, val|
107       if key =~ /checkbox_for_([a-z]+)_([0-9a-z_\-.]+)/ and val == "on"
108         has_selection = true
109         case $1 
110         when 'veid'
111           ve = VirtualEnvironment.find_by_veid($2.to_i)
112         when 'name'
113           name =  User.find_by_name($2)
114         end 
115           
116         
117         session[:workers] ||= {}
118         if 'destroy'== params[:selected_action]
119           if ve.status == 'stopped'
120             session[:workers][ve.veid] = 
121               Nucleus.new.destroy(ve.veid), 'destroying'
122           else
123             flash[:notices] << "Error Destroy: VE #{ve.veid} is not stopped"
124           end
125         elsif 'stop' == params[:selected_action]
126           if ve.status == 'started'
127             session[:workers][ve.veid] = Nucleus.new.stop(ve.veid), 'stopping'
128           else
129             flash[:notices] << "Error Stop: VE #{ve.veid} is not started"
130           end        
131         elsif 'start' == params[:selected_action]
132           if ve.status == 'stopped'
133             session[:workers][ve.veid] = Nucleus.new.start(ve.veid), 'starting'
134           else
135             flash[:notices] << "Error Start: VE #{ve.veid} is not stopped"
136           end
137         elsif 'disable' == params[:selected_action]
138           if name.is_disabled
139             flash[:notices] << 
140               "Error Disable: #{name.name} is already disabled."
141           else 
142             name.is_disabled = true
143             
144             name.save!
145           end
146         elsif 'enable' == params[:selected_action]
147           if !name.is_disabled
148             flash[:notices] << "Error Enable: #{name.name} is already enabled."
149           else
150             name.is_disabled = false
151             name.save!
152             #code here to stop all virtual machines by the user
153           end
154         else
155           render (:inline => "<%=simple_message('Action was not selected.')%>",
156                   :layout => 'application')          
157           return false
158         end 
159       end    
160     end
163     if !has_selection
164       render (:inline => "<%=simple_message('No VEs were selected.')%>",
165               :layout => 'application')
166       return false
167     else 
168       redirect_to :controller => 'admin', :action => params[:come_back_to]
169       return false  
170     end
171   end
174   def account_management
175     @names = User.find_all.collect{|u| u.name}.sort
176   end
179   def hn_statistics
180     @body = `cat /home/alevchuk/sstats/aggregatedsstats.txt`
181   end
184   private 
185   
186   def set_page_title
187     @page_title1 = "Mwamko Administration"
188     @page_title2 = params[:action].gsub('_', ' ').titleize
189   end
191   def set_mode
192     #set mwamko to normal mode
193     session[:mwamko_mode] = ['normal', 'gadget'][0]
194   end