New Ticket     Tickets     Wiki     Browse Source     Timeline     Roadmap     Ticket Reports     Search

Changeset 80745


Ignore:
Timestamp:
07/16/11 12:19:16 (4 years ago)
Author:
derek@…
Message:

Moved submission saving code from controller to model

  • Moved add_os_data, add_port, add_installed_ports from controller to model
  • Added save_data method to model
  • Controller now calls save_data
Location:
branches/gsoc11-statistics/stats-server/app
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/gsoc11-statistics/stats-server/app/controllers/submissions_controller.rb

    r80731 r80745  
    2020  end 
    2121 
    22   # Populate an OsStatistics row based on submitted JSON data 
    23   def add_os_data(user, os) 
    24     logger.debug "In add_os_data" 
    25      
    26     if os.nil? 
    27       return 
    28     end 
    29      
    30     macports_version = os['macports_version'] 
    31     osx_version      = os['osx_version'] 
    32     os_arch          = os['os_arch'] 
    33     os_platform      = os['os_platform'] 
    34     build_arch       = os['build_arch'] 
    35     gcc_version      = os['gcc_version'] 
    36     xcode_version    = os['xcode_version'] 
    37     
    38     # Try to find an existing entry 
    39     os_stats = OsStatistic.find_by_user_id(user.id) 
    40       
    41     if os_stats.nil? 
    42       # No entry for this user - create a new one 
    43       os_stats = OsStatistic.new() 
    44     end 
    45      
    46     os_stats[:user_id]          = user.id 
    47     os_stats[:macports_version] = macports_version 
    48     os_stats[:osx_version]      = osx_version 
    49     os_stats[:os_arch]          = os_arch 
    50     os_stats[:os_platform]      = os_platform 
    51     os_stats[:build_arch]       = build_arch 
    52     os_stats[:gcc_version]      = gcc_version 
    53     os_stats[:xcode_version]    = xcode_version 
    54      
    55     if not os_stats.save 
    56       logger.debug "Unable to save os_stats" 
    57       logger.debug "Error message: #{os_stats.errors.full_messages}" 
    58     end 
    59   end 
    60  
    61   def add_port(user_id, macports_port, installed_port, month, year) 
    62     logger.debug {"Adding installed port #{installed_port['name']}"} 
    63          
    64     portEntry = InstalledPort.new(:user_id => user_id, 
    65                              :port_id => macports_port.id, 
    66                              :version => installed_port['version'], 
    67                              :variants => installed_port['variants'], 
    68                              :month => month, 
    69                              :year => year) 
    70                          
    71     if not portEntry.save 
    72      logger.debug "Unable to save port #{port['name']}" 
    73      logger.debug "Error message: #{portEntry.errors.full_messages}" 
    74    end 
    75  end 
    76  
    77   def add_installed_ports(uuid, installed_ports) 
    78     logger.debug "In add_installed_ports" 
    79      
    80     if installed_ports.nil? 
    81       return 
    82     end 
    83      
    84     current_time = Time.now 
    85     month = current_time.month 
    86     year = current_time.year 
    87      
    88     installed_ports.each do |installed_port| 
    89       # Find the reported port in the MacPorts repository 
    90       macports_port = Port.find_by_name(installed_port['name']) 
    91  
    92       if macports_port.nil? 
    93         logger.debug {"Skipping unknown port #{installed_port['name']} - Not in MacPorts repository"} 
    94         next 
    95       end 
    96        
    97       # Update installed port information 
    98       add_port(uuid, macports_port, installed_port, month, year) 
    99     end 
    100   end 
    101  
    10222  # POST /submissions 
    10323  def create 
    10424    @submission = Submission.new(params[:submission]) 
    105     json = ActiveSupport::JSON.decode(@submission.data)  
    106     os = json['os'] 
    107     active_ports = json['active_ports'] 
    10825     
    109     user = User.find_or_create_by_uuid(json['id']) 
    110      
    111     add_os_data(user, os) 
    112     add_installed_ports(json['id'], active_ports) 
    113  
     26    @submission.save_data 
     27   
    11428    respond_to do |format| 
    11529      format.html { redirect_to(@submission, :notice => 'Submission was successfully created.') } 
  • branches/gsoc11-statistics/stats-server/app/models/submission.rb

    r80511 r80745  
    11class Submission < ActiveRecord::Base 
     2   
     3  # Populate an OsStatistics row based on submitted JSON data 
     4  def add_os_data(user, os) 
     5    logger.debug "In add_os_data" 
     6     
     7    if os.nil? 
     8      return 
     9    end 
     10     
     11    macports_version = os['macports_version'] 
     12    osx_version      = os['osx_version'] 
     13    os_arch          = os['os_arch'] 
     14    os_platform      = os['os_platform'] 
     15    build_arch       = os['build_arch'] 
     16    gcc_version      = os['gcc_version'] 
     17    xcode_version    = os['xcode_version'] 
     18    
     19    # Try to find an existing entry 
     20    os_stats = OsStatistic.find_by_user_id(user.id) 
     21      
     22    if os_stats.nil? 
     23      # No entry for this user - create a new one 
     24      os_stats = OsStatistic.new() 
     25    end 
     26     
     27    os_stats[:user_id]          = user.id 
     28    os_stats[:macports_version] = macports_version 
     29    os_stats[:osx_version]      = osx_version 
     30    os_stats[:os_arch]          = os_arch 
     31    os_stats[:os_platform]      = os_platform 
     32    os_stats[:build_arch]       = build_arch 
     33    os_stats[:gcc_version]      = gcc_version 
     34    os_stats[:xcode_version]    = xcode_version 
     35     
     36    if not os_stats.save 
     37      logger.debug "Unable to save os_stats" 
     38      logger.debug "Error message: #{os_stats.errors.full_messages}" 
     39    end 
     40  end 
     41 
     42  def add_port(user, macports_port, installed_port, month, year) 
     43    logger.debug {"Adding installed port #{installed_port['name']}"} 
     44         
     45    portEntry = InstalledPort.new(:user_id => user.id, 
     46                             :port_id => macports_port.id, 
     47                             :version => installed_port['version'], 
     48                             :variants => installed_port['variants'], 
     49                             :month => month, 
     50                             :year => year) 
     51                         
     52    if not portEntry.save 
     53     logger.debug "Unable to save port #{port['name']}" 
     54     logger.debug "Error message: #{portEntry.errors.full_messages}" 
     55   end 
     56 end 
     57 
     58  def add_installed_ports(uuid, installed_ports) 
     59    logger.debug "In add_installed_ports" 
     60     
     61    if installed_ports.nil? 
     62      return 
     63    end 
     64     
     65    current_time = Time.now 
     66    month = current_time.month 
     67    year = current_time.year 
     68     
     69    installed_ports.each do |installed_port| 
     70      # Find the reported port in the MacPorts repository 
     71      macports_port = Port.find_by_name(installed_port['name']) 
     72 
     73      if macports_port.nil? 
     74        logger.debug {"Skipping unknown port #{installed_port['name']} - Not in MacPorts repository"} 
     75        next 
     76      end 
     77       
     78      # Update installed port information 
     79      add_port(uuid, macports_port, installed_port, month, year) 
     80    end 
     81  end 
     82   
     83  def save_data 
     84    json = ActiveSupport::JSON.decode(data)  
     85    os = json['os'] 
     86    active_ports = json['active_ports'] 
     87    #inactive_ports = json['inactive_ports'] 
     88     
     89    user = User.find_or_create_by_uuid(json['id']) 
     90     
     91    add_os_data(user, os) 
     92    add_installed_ports(user, active_ports) 
     93  end 
    294end 
Note: See TracChangeset for help on using the changeset viewer.