Changeset 1746

Show
Ignore:
Timestamp:
08/19/08 13:32:39 (3 months ago)
Author:
dbryson
Message:

updating Bee interface

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • hive/trunk/data_webapp/app/controllers/bee_controller.rb

    r1709 r1746  
    11# Talks exclusively to the Worker Bees 
    22class BeeController < ApplicationController 
    3   #before_filter :valid_key? 
     3  include WorkerBeeAuthorization 
     4  before_filter :verify_signed_request 
    45   
    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 
    209  end 
    2110   
    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"] 
    2820  end 
    29    
    30   def client 
    31     encoded = params[:data] 
    32     decoded = ActiveSupport::JSON.decode(encoded) 
    33     c = Client.new_from_hash(decoded) 
    34     if c.save 
    35       render :text => "ok", :status => 201 
    36     else 
    37       render :text => "error", :status => 500 
    38     end 
    39   end 
    40    
    41   def fingerprint 
    42     encoded = params[:data] 
    43     decoded = ActiveSupport::JSON.decode(encoded) 
    44     cid = decoded["cid"] 
    45     c = Client.find_by_cid(cid) 
    46     if c 
    47       decoded["client_id"] = c.id 
    48       decoded.delete("cid") 
    49       c_time = decoded.delete("time_at") 
    50       f = Fingerprint.new_from_hash(decoded) 
    51       if f.save 
    52         c.update_attributes({:compromise => string_to_microseconds(c_time),:status => "suspicious"}) 
    53         render :text => "ok", :status => 201 
    54       else 
    55         render :text => "error", :status => 500 
    56       end 
    57     else 
    58       render :text => "no client", :status => 500 
    59     end 
    60   end 
    61    
    62   def history 
    63     encoded = params[:data] 
    64     decoded = ActiveSupport::JSON.decode(encoded) 
    65     cid = decoded["cid"] 
    66     c = Client.find_by_cid(cid) 
    67     if c 
    68       urls = decoded["urls"] 
    69       if urls 
    70         urls.each do |history| 
    71           history["client_id"] = c.id 
    72         end 
    73         HistoryUrl.create(urls) 
    74         render :text => "ok", :status => 201 
    75       else 
    76         render :text => "error", :status => 500 
    77       end  
    78     else 
    79       render :text => "no client", :status => 500 
    80     end 
    81   end 
    82    
    8321   
    8422  private 
    8523   
    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 
    9729    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! 
    9836  end 
    9937   
  • hive/trunk/data_webapp/app/models/history_url.rb

    r1602 r1746  
    1414    HistoryUrl.new(obj_hash) 
    1515  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 
    1645end