Changeset 84042


Ignore:
Timestamp:
Sep 15, 2011, 5:37:04 PM (6 years ago)
Author:
derek@…
Message:

Moved data saving methods from Submission to the models that they actually operate on.

  • Moved add_installed_ports, add_port to InstalledPort
  • Moved add_os_data to OsStatistic
Location:
branches/gsoc11-statistics/stats-server/app/models
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/gsoc11-statistics/stats-server/app/models/installed_port.rb

    r80843 r84042  
    44 
    55  validates_presence_of :user_id, :port_id, :version
     6 
     7  # Populate InstalledPort rows based on submitted JSON data
     8  def self.add_installed_ports(uuid, installed_ports)
     9    logger.debug "In add_installed_ports"
     10   
     11    if installed_ports.nil?
     12      return
     13    end
     14   
     15    current_time = Time.now
     16    month = current_time.month
     17    year = current_time.year
     18   
     19    installed_ports.each do |installed_port|
     20      # Find the reported port in the MacPorts repository
     21      macports_port = Port.find_by_name(installed_port['name'])
     22
     23      if macports_port.nil?
     24        logger.debug {"Skipping unknown port #{installed_port['name']} - Not in MacPorts repository"}
     25        next
     26      end
     27     
     28      # Update installed port information
     29      InstalledPort.add_port(uuid, macports_port, installed_port, month, year)
     30    end
     31  end
     32 
     33  def self.add_port(user, macports_port, installed_port, month, year)
     34    logger.debug {"Adding installed port #{installed_port['name']}"}
     35   
     36    # Update any ports found for this user if they have already been submitted this month 
     37    port_entrys = user.installed_ports.where(:created_at => (Date.today.at_beginning_of_month)..(Date.today.at_end_of_month))
     38    port_entry = port_entrys.find_by_port_id(macports_port.id)
     39
     40    # No existing entry was found - create a new one               
     41    if port_entry.nil?
     42      port_entry = InstalledPort.new
     43    end
     44   
     45    port_entry[:user_id]  = user.id
     46    port_entry[:port_id]  = macports_port.id
     47    port_entry[:version]  = installed_port['version']
     48    port_entry[:variants] = installed_port['variants']
     49                           
     50    if not port_entry.save
     51     logger.debug "Unable to save port #{installed_port['name']}"
     52     logger.debug "Reason: #{port_entry.errors.full_messages}"
     53   end
     54 end
    655end
  • branches/gsoc11-statistics/stats-server/app/models/os_statistic.rb

    r80568 r84042  
    2020  validates :xcode_version,     :presence => true
    2121
     22  # Populate an OsStatistics row with data
     23  def self.add_os_data(user, os)
     24    logger.debug "In OsStatistic.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 = user.os_statistic
     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
    2261end
  • branches/gsoc11-statistics/stats-server/app/models/submission.rb

    r80844 r84042  
    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"
    62   
    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 = user.os_statistic
    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     # Update any ports found for this user if they have already been submitted this month 
    46     port_entrys = user.installed_ports.where(:created_at => (Date.today.at_beginning_of_month)..(Date.today.at_end_of_month))
    47     port_entry = port_entrys.find_by_port_id(macports_port.id)
    48 
    49     # No existing entry was found - create a new one               
    50     if port_entry.nil?
    51       port_entry = InstalledPort.new
    52     end
    53    
    54     port_entry[:user_id]  = user.id
    55     port_entry[:port_id]  = macports_port.id
    56     port_entry[:version]  = installed_port['version']
    57     port_entry[:variants] = installed_port['variants']
    58                            
    59     if not port_entry.save
    60      logger.debug "Unable to save port #{installed_port['name']}"
    61      logger.debug "Reason: #{port_entry.errors.full_messages}"
    62    end
    63  end
    64 
    65   def add_installed_ports(uuid, installed_ports)
    66     logger.debug "In add_installed_ports"
    67    
    68     if installed_ports.nil?
    69       return
    70     end
    71    
    72     current_time = Time.now
    73     month = current_time.month
    74     year = current_time.year
    75    
    76     installed_ports.each do |installed_port|
    77       # Find the reported port in the MacPorts repository
    78       macports_port = Port.find_by_name(installed_port['name'])
    79 
    80       if macports_port.nil?
    81         logger.debug {"Skipping unknown port #{installed_port['name']} - Not in MacPorts repository"}
    82         next
    83       end
    84      
    85       # Update installed port information
    86       add_port(uuid, macports_port, installed_port, month, year)
    87     end
    88   end
    89  
    903  def save_data
    914    json = ActiveSupport::JSON.decode(data)
     
    969    user = User.find_or_create_by_uuid(json['id'])
    9710   
    98     add_os_data(user, os)
    99     add_installed_ports(user, active_ports)
     11    OsStatistic.add_os_data(user, os)
     12    InstalledPort.add_installed_ports(user, active_ports)
    10013  end
     14 
    10115end
Note: See TracChangeset for help on using the changeset viewer.