lib/ardtweeno/restapi.rb in ardtweeno-0.3.1 vs lib/ardtweeno/restapi.rb in ardtweeno-0.4.0

- old
+ new

@@ -8,12 +8,12 @@ ##### Require statements require 'rubygems' require 'sinatra/base' require 'ardtweeno' require 'logger' -require 'rufus/scheduler' + class RESTAPI < Sinatra::Base ##### Sinatra Variables enable :static, :sessions, :logging set :root, File.join(File.dirname(__FILE__) + '/../../') @@ -56,38 +56,20 @@ } set :options, {:test=>true, :log=>Logger.new(STDOUT), :level=>Logger::DEBUG, :confdata=>@confdata} end - # Rufus-scheduler object - set :scheduler, Rufus::Scheduler.start_new - # Setup the system for use Ardtweeno.setup(settings.options) @@theDispatcher = Ardtweeno::Dispatcher.instance # Posts Array set :posts, @@theDispatcher.getPosts # Posts URI set :newsURI, @@theDispatcher.getPostsURI - -######################################################################################################### - - settings.scheduler.every '60m' do - - begin - settings.log.debug "Running scheduled data flush" - @@theDispatcher.flush() - - rescue Ardtweeno::DBError => e - settings.log.warn "ERROR: #{e.message}" - end - - end - ######################################################################################################### get '/' do @@ -107,14 +89,47 @@ erb :home, :locals => {:postdata => theposts.reverse} end get '/status' do - erb :status + begin + diskusage = @@theDispatcher.diskUsage + + rescue Exception => e + throw :halt, [ 500, "500 Internal Server Error" ] + end + + erb :status, :locals => {:diskusage=>diskusage} end + get '/topology' do + settings.log.debug params.inspect + + begin + theResponse = @@theDispatcher.constructTopology(params) + + rescue Exception => e + throw :halt, [ 500, "500 Internal Server Error" ] + end + + erb :topology, :locals => {:theTopology=>theResponse} + end + + + get '/graph/v1/punchcard/:node' do |node| + begin + theData, theDays, theRange= @@theDispatcher.constructPunchcard(params) + + rescue Exception => e + throw :halt, [ 500, "500 Internal Server Error" ] + end + + erb :punchcard, :locals => {:node=>params[:node], :ourGraphData=>theData, :ourGraphDays=>theDays, :ourGraphRange=>theRange} + end + + get "/#{settings.newsURI}/create/post" do running = @@theDispatcher.running? erb :createpost, :locals => {:running => running} end @@ -227,14 +242,39 @@ begin @@theDispatcher.addWatch(params) rescue Exception => e throw :halt, [ 400, "400 Bad Request" ] end + end + + + get '/api/v1/watch/:node' do |node| + settings.log.debug params.inspect + throw :halt, [ 404, "404 Page Not Found" ] unless @@theDispatcher.authenticate?(params[:key]) + settings.log.debug "Check if a node is being watched" + begin + @@theDispatcher.watched?(params).to_json + rescue Exception => e + throw :halt, [ 400, "400 Bad Request" ] + end end + get '/api/v1/watch' do + settings.log.debug params.inspect + throw :halt, [ 404, "404 Page Not Found" ] unless @@theDispatcher.authenticate?(params[:key]) + settings.log.debug "Check if a node is being watched" + + begin + @@theDispatcher.watchList.to_json + rescue Exception => e + raise e + #throw :halt, [ 400, "400 Bad Request" ] + end + end + ######################################################################################################### get '/api/v1/system/config' do throw :halt, [ 404, "404 Page Not Found" ] unless @@theDispatcher.authenticate?(params[:key]) settings.log.debug "The system config hook has been called, querying the Ardtweeno gateway to retrieve config" @@ -290,24 +330,35 @@ "The host is rebooting, this will take a moment..." end get '/api/v1/system/status' do - # Considering making this api target public to avoid having to store API keys in the highcarts.js - # graphs.. - #throw :halt, [ 404, "404 Page Not Found" ] unless @@theDispatcher.authenticate?(params[:key]) settings.log.debug "The system status hook has been called, reading the host configuration" begin - return @@theDispatcher.status?().to_json + return @@theDispatcher.status?.to_json rescue Exception => e - raise e - #throw :halt, [ 500, "500 Internal Server Error" ] + throw :halt, [ 500, "500 Internal Server Error" ] end end + + get '/api/v1/system/status/list' do + settings.log.debug "The system status list hook has been called, returning the last 15 mins of status data" + + begin + return {:buffer=>@@theDispatcher.statuslist}.to_json + + rescue Exception => e + throw :halt, [ 500, "500 Internal Server Error"] + end + end + ######################################################################################################### + + + # End of RESTAPI Class end