Changeset 1746
- Timestamp:
- 08/19/08 13:32:39 (3 months ago)
- Files:
-
- hive/trunk/data_webapp/app/controllers/bee_controller.rb (modified) (1 diff)
- hive/trunk/data_webapp/app/models/history_url.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
hive/trunk/data_webapp/app/controllers/bee_controller.rb
r1709 r1746 1 1 # Talks exclusively to the Worker Bees 2 2 class BeeController < ApplicationController 3 #before_filter :valid_key? 3 include WorkerBeeAuthorization 4 before_filter :verify_signed_request 4 5 5 6 def add_drone_job 7 # JSON from Bee 8 result = "failed" 9 begin 10 job = ActiveSupport::JSON.decode(params[:data]) 11 if job.is_a? Hash 12 job.symbolize_keys! 13 DroneJob.insert(job) 14 result = "success" 15 end 16 rescue ParseError 17 logger.error("Data not in JSON format!") 18 end 19 render :json => {:message => result} 6 def latest 7 data = HistoryUrl.fetch_latest(params[:id]) 8 render :json => data 20 9 end 21 10 22 23 # ALL CODE BELOW IS NO LONGER USED ON THE DRONE APP 24 # Collect and record a ping from a Bee 25 def ping 26 Heartbeat.create_or_update_ping(authorization_key) 27 render :text => "ok",:status => 200 11 def work 12 raw_data = request.raw_post 13 begin 14 urls = ActiveSupport::JSON.decode(d) 15 work = build_hash_for_drone(urls) 16 DroneJob.insert(work) 17 rescue 18 end 19 render :json => ["ok"] 28 20 end 29 30 def client31 encoded = params[:data]32 decoded = ActiveSupport::JSON.decode(encoded)33 c = Client.new_from_hash(decoded)34 if c.save35 render :text => "ok", :status => 20136 else37 render :text => "error", :status => 50038 end39 end40 41 def fingerprint42 encoded = params[:data]43 decoded = ActiveSupport::JSON.decode(encoded)44 cid = decoded["cid"]45 c = Client.find_by_cid(cid)46 if c47 decoded["client_id"] = c.id48 decoded.delete("cid")49 c_time = decoded.delete("time_at")50 f = Fingerprint.new_from_hash(decoded)51 if f.save52 c.update_attributes({:compromise => string_to_microseconds(c_time),:status => "suspicious"})53 render :text => "ok", :status => 20154 else55 render :text => "error", :status => 50056 end57 else58 render :text => "no client", :status => 50059 end60 end61 62 def history63 encoded = params[:data]64 decoded = ActiveSupport::JSON.decode(encoded)65 cid = decoded["cid"]66 c = Client.find_by_cid(cid)67 if c68 urls = decoded["urls"]69 if urls70 urls.each do |history|71 history["client_id"] = c.id72 end73 HistoryUrl.create(urls)74 render :text => "ok", :status => 20175 else76 render :text => "error", :status => 50077 end78 else79 render :text => "no client", :status => 50080 end81 end82 83 21 84 22 private 85 23 86 def authorization_key 87 request.env['HTTP_AUTHORIZATION'] 88 end 89 90 # Verify the access key on a request. 91 # If unauthorized, send 403 - Forbidden 92 # Otherwise, continue with the request 93 def valid_key? 94 key = authorization_key 95 unless User.find_by_access_key(key) 96 render :text => "error", :status => 403 24 def build_hash_for_drone(urls) 25 queue = {} 26 urls.each do |url| 27 # Default priority for now 28 queue[url] = 3 97 29 end 30 31 job_hash = { :source => { :feed_type => 'web',:name => 'community_hive' }, 32 :notify_source => 0, 33 :queue_urls => queue 34 } 35 job_hash.symbolize_keys! 98 36 end 99 37 hive/trunk/data_webapp/app/models/history_url.rb
r1602 r1746 14 14 HistoryUrl.new(obj_hash) 15 15 end 16 17 # Used by the WorkerBee to fetch the latest history urls 18 def self.fetch_latest(last_time_value=nil) 19 if last_time_value 20 p = last_time_value.split("-") 21 timestamp = "#{p[0]}.#{p[1]}".to_f 22 else 23 # start from now 24 t = Time.now 25 timestamp = string_to_microseconds(t.to_s) 26 end 27 urls = find(:all,:conditions => ["time_at > ?",timestamp], :order => "time_at DESC") 28 adjust_format(urls) 29 end 30 31 # Convert to string 32 def f_time_at 33 v = read_attribute(:time_at) 34 v.to_s 35 end 36 37 # Adjust the outgoing format 38 def self.adjust_format(data) 39 result = [] 40 data.each do |h| 41 result << [{:url => h.url,:status => h.status,:time_at => h.f_time_at}] 42 end 43 result 44 end 16 45 end
