lib/perus/server/app.rb in perus-0.1.11 vs lib/perus/server/app.rb in perus-0.1.12

- old
+ new

@@ -100,11 +100,16 @@ end redirect "#{url_prefix}admin/configs/#{params['config_id']}" end + get '/admin/stats' do + @stats = Stats.new + erb :stats + end + #---------------------- # API #---------------------- # csv for graphs shown on system page get '/systems/:id/values' do @@ -140,34 +145,37 @@ csv end # receive data from a client post '/systems/:id/ping' do - timestamp = Time.now.to_i + Server.ping_queue.post do + timestamp = Time.now.to_i - # update the system with its last known ip and update time - system = System.with_pk!(params['id']) - system.last_updated = timestamp + # update the system with its last known ip and update time + system = System.with_pk!(params['id']) + system.last_updated = timestamp - if request.ip == '127.0.0.1' && ENV['RACK_ENV'] == 'production' - system.ip = request.env['HTTP_X_FORWARDED_FOR'] - else - system.ip = request.ip - end + if request.ip == '127.0.0.1' && ENV['RACK_ENV'] == 'production' + system.ip = request.env['HTTP_X_FORWARDED_FOR'] + else + system.ip = request.ip + end - # errors is either nil or a hash of the format - module: [err, ...] - system.save_metric_errors(params, timestamp) + # errors is either nil or a hash of the format - module: [err, ...] + system.save_metric_errors(params, timestamp) - # add each new value, a later process cleans up old values - system.save_values(params, timestamp) + # add each new value, a later process cleans up old values + system.save_values(params, timestamp) - # save action return values and prevent them from running again - system.save_actions(params, timestamp) + # save action return values and prevent them from running again + system.save_actions(params, timestamp) - # ip, last updated, uploads and metrics are now updated. these are - # stored on the system. - system.save + # ip, last updated, uploads and metrics are now updated. these are + # stored on the system. + system.save + end + content_type :json {success: true}.to_json end # system config @@ -218,13 +226,36 @@ end redirect "#{url_prefix}groups/#{params['id']}/systems" end + # delete completed actions in a group + delete '/groups/:id/systems/actions' do + group = Group.with_pk!(params['id']) + group.systems.each do |system| + system.actions.each do |action| + next if action.timestamp.nil? + action.destroy + end + end + + redirect "#{url_prefix}groups/#{params['id']}/systems" + end + # create an action for all systems post '/systems/actions' do System.each do |system| Action.add(system.id, params) + end + + redirect "#{url_prefix}systems" + end + + # delete all completed actions + delete '/systems/actions' do + Action.each do |action| + next if action.timestamp.nil? + action.destroy end redirect "#{url_prefix}systems" end