Changeset 97 for hw

Show
Ignore:
Timestamp:
11/09/07 00:09:16 (5 years ago)
Author:
cs186
Message:

Only show top 10 closest authors.

Location:
hw/hw4/library/app
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • hw/hw4/library/app/controllers/librarymaps_controller.rb

    r87 r97  
    1616 
    1717    # Retrieve the list of libraries for the Select (Drop Down) Box 
    18     # "@" preceeding a variable name indicates an instance variable. 
     18    # "@" preceding a variable name indicates an instance variable. 
    1919    # Instance variables can be passed to views, whereas 
    20     # local variables (variable names without preceeding "@") cannot 
     20    # local variables (variable names without preceding "@") cannot 
    2121    # be passed to views. 
    2222    @alllibraries = Array.new  
     
    5757                                  :info_window => libraryname)) 
    5858 
    59     # Get all the authors. 
     59    # The following code fetches all the authors, geocodes their birthplaces, 
     60    # computes distance to the library, sorts by distance, and plots the 
     61    # top 15 closest.  This chunk is not going to scale well as we  
     62    # increase the number of authors! 
     63 
     64    # Suck all the authors out of the database into a Ruby array, and 
     65    # loop through them. 
    6066    @authors = Array.new 
    6167    results = Author.find(:all) 
     
    7076      if geoloc.status == Geocoding::GEO_SUCCESS 
    7177        authlatlon = geoloc[0].latlon 
    72         # Distances recalculated from a relativley stable  set of libraries. 
     78        # Distances recalculated from a relatively stable  set of libraries. 
    7379        # This is a good candidate for caching. 
    7480        dist = distance_between(authlatlon,liblatlon).floor 
    75         @map.overlay_init(GMarker.new(geoloc[0].latlon, 
    76                                       :title => result.name,  
    77                                       :info_window => "#{result.name} says:<br> I am #{dist} miles away<br> from #{@mylibrary.lname}")) 
    78         @authors << [result.name,result.bplace,dist] 
     81        @authors << [result.name,result.bplace,dist,authlatlon] 
    7982      end 
    8083    end 
     
    8285    # Sort authors in place by dist.  "sort!" means in place. 
    8386    @authors.sort! {|x,y| x[2] <=> y[2] } 
     87    # truncate the array to just the top 10 
     88    @authors = @authors[0..9] 
    8489 
     90    @authors.each do |a| 
     91        @map.overlay_init(GMarker.new(a[3], 
     92                                      :title => a[0], 
     93                                      :info_window => "#{a[0]} says:<br> I am #{a[2]} miles away<br> from #{@mylibrary.lname}")) 
     94    end 
    8595    # Measure speed of server-side operation. 
    86     # Typical times w/  RPC: 3.5 seconds 
    87     # Typical times w/o RPC: 0.3 seconds 
     96    # Typical times w/  RPC on 17 authors: 3.5 seconds 
     97    # Typical times w/o RPC on 17 authors: 0.3 seconds 
    8898    # Rhombus server fluctuations will cause this value to vary. 
    8999    endtime = Time.now 
  • hw/hw4/library/app/views/librarymaps/index.rhtml

    r87 r97  
    2828<% end %> 
    2929</table> 
     30<i>Top <%= @authors.length %> of <%= Author.count %> shown.</i> 
    3031</td> 
    3132</tr>