[80745] branches/gsoc11-statistics/stats-server/app

derek at macports.org derek at macports.org
Sat Jul 16 12:19:17 PDT 2011


Revision: 80745
          http://trac.macports.org/changeset/80745
Author:   derek at macports.org
Date:     2011-07-16 12:19:16 -0700 (Sat, 16 Jul 2011)
Log 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

Modified Paths:
--------------
    branches/gsoc11-statistics/stats-server/app/controllers/submissions_controller.rb
    branches/gsoc11-statistics/stats-server/app/models/submission.rb

Modified: branches/gsoc11-statistics/stats-server/app/controllers/submissions_controller.rb
===================================================================
--- branches/gsoc11-statistics/stats-server/app/controllers/submissions_controller.rb	2011-07-16 19:08:22 UTC (rev 80744)
+++ branches/gsoc11-statistics/stats-server/app/controllers/submissions_controller.rb	2011-07-16 19:19:16 UTC (rev 80745)
@@ -19,98 +19,12 @@
     end
   end
 
-  # Populate an OsStatistics row based on submitted JSON data
-  def add_os_data(user, os)
-    logger.debug "In add_os_data"
-    
-    if os.nil?
-      return
-    end
-    
-    macports_version = os['macports_version']
-    osx_version      = os['osx_version']
-    os_arch          = os['os_arch']
-    os_platform      = os['os_platform']
-    build_arch       = os['build_arch']
-    gcc_version      = os['gcc_version']
-    xcode_version    = os['xcode_version']
-   
-    # Try to find an existing entry
-    os_stats = OsStatistic.find_by_user_id(user.id)
-     
-    if os_stats.nil?
-      # No entry for this user - create a new one
-      os_stats = OsStatistic.new()
-    end
-    
-    os_stats[:user_id]          = user.id
-    os_stats[:macports_version] = macports_version
-    os_stats[:osx_version]      = osx_version
-    os_stats[:os_arch]          = os_arch
-    os_stats[:os_platform]      = os_platform
-    os_stats[:build_arch]       = build_arch
-    os_stats[:gcc_version]      = gcc_version
-    os_stats[:xcode_version]    = xcode_version
-    
-    if not os_stats.save
-      logger.debug "Unable to save os_stats"
-      logger.debug "Error message: #{os_stats.errors.full_messages}"
-    end
-  end
-
-  def add_port(user_id, macports_port, installed_port, month, year)
-    logger.debug {"Adding installed port #{installed_port['name']}"}
-        
-    portEntry = InstalledPort.new(:user_id => user_id,
-                             :port_id => macports_port.id,
-                             :version => installed_port['version'],
-                             :variants => installed_port['variants'],
-                             :month => month,
-                             :year => year)
-                        
-    if not portEntry.save
-     logger.debug "Unable to save port #{port['name']}"
-     logger.debug "Error message: #{portEntry.errors.full_messages}"
-   end
- end
-
-  def add_installed_ports(uuid, installed_ports)
-    logger.debug "In add_installed_ports"
-    
-    if installed_ports.nil?
-      return
-    end
-    
-    current_time = Time.now
-    month = current_time.month
-    year = current_time.year
-    
-    installed_ports.each do |installed_port|
-      # Find the reported port in the MacPorts repository
-      macports_port = Port.find_by_name(installed_port['name'])
-
-      if macports_port.nil?
-        logger.debug {"Skipping unknown port #{installed_port['name']} - Not in MacPorts repository"}
-        next
-      end
-      
-      # Update installed port information
-      add_port(uuid, macports_port, installed_port, month, year)
-    end
-  end
-
   # POST /submissions
   def create
     @submission = Submission.new(params[:submission])
-    json = ActiveSupport::JSON.decode(@submission.data) 
-    os = json['os']
-    active_ports = json['active_ports']
     
-    user = User.find_or_create_by_uuid(json['id'])
-    
-    add_os_data(user, os)
-    add_installed_ports(json['id'], active_ports)
-
+    @submission.save_data
+  
     respond_to do |format|
       format.html { redirect_to(@submission, :notice => 'Submission was successfully created.') }
     end

Modified: branches/gsoc11-statistics/stats-server/app/models/submission.rb
===================================================================
--- branches/gsoc11-statistics/stats-server/app/models/submission.rb	2011-07-16 19:08:22 UTC (rev 80744)
+++ branches/gsoc11-statistics/stats-server/app/models/submission.rb	2011-07-16 19:19:16 UTC (rev 80745)
@@ -1,2 +1,94 @@
 class Submission < ActiveRecord::Base
+  
+  # Populate an OsStatistics row based on submitted JSON data
+  def add_os_data(user, os)
+    logger.debug "In add_os_data"
+    
+    if os.nil?
+      return
+    end
+    
+    macports_version = os['macports_version']
+    osx_version      = os['osx_version']
+    os_arch          = os['os_arch']
+    os_platform      = os['os_platform']
+    build_arch       = os['build_arch']
+    gcc_version      = os['gcc_version']
+    xcode_version    = os['xcode_version']
+   
+    # Try to find an existing entry
+    os_stats = OsStatistic.find_by_user_id(user.id)
+     
+    if os_stats.nil?
+      # No entry for this user - create a new one
+      os_stats = OsStatistic.new()
+    end
+    
+    os_stats[:user_id]          = user.id
+    os_stats[:macports_version] = macports_version
+    os_stats[:osx_version]      = osx_version
+    os_stats[:os_arch]          = os_arch
+    os_stats[:os_platform]      = os_platform
+    os_stats[:build_arch]       = build_arch
+    os_stats[:gcc_version]      = gcc_version
+    os_stats[:xcode_version]    = xcode_version
+    
+    if not os_stats.save
+      logger.debug "Unable to save os_stats"
+      logger.debug "Error message: #{os_stats.errors.full_messages}"
+    end
+  end
+
+  def add_port(user, macports_port, installed_port, month, year)
+    logger.debug {"Adding installed port #{installed_port['name']}"}
+        
+    portEntry = InstalledPort.new(:user_id => user.id,
+                             :port_id => macports_port.id,
+                             :version => installed_port['version'],
+                             :variants => installed_port['variants'],
+                             :month => month,
+                             :year => year)
+                        
+    if not portEntry.save
+     logger.debug "Unable to save port #{port['name']}"
+     logger.debug "Error message: #{portEntry.errors.full_messages}"
+   end
+ end
+
+  def add_installed_ports(uuid, installed_ports)
+    logger.debug "In add_installed_ports"
+    
+    if installed_ports.nil?
+      return
+    end
+    
+    current_time = Time.now
+    month = current_time.month
+    year = current_time.year
+    
+    installed_ports.each do |installed_port|
+      # Find the reported port in the MacPorts repository
+      macports_port = Port.find_by_name(installed_port['name'])
+
+      if macports_port.nil?
+        logger.debug {"Skipping unknown port #{installed_port['name']} - Not in MacPorts repository"}
+        next
+      end
+      
+      # Update installed port information
+      add_port(uuid, macports_port, installed_port, month, year)
+    end
+  end
+  
+  def save_data
+    json = ActiveSupport::JSON.decode(data) 
+    os = json['os']
+    active_ports = json['active_ports']
+    #inactive_ports = json['inactive_ports']
+    
+    user = User.find_or_create_by_uuid(json['id'])
+    
+    add_os_data(user, os)
+    add_installed_ports(user, active_ports)
+  end
 end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macports-changes/attachments/20110716/b0c466ad/attachment.html>


More information about the macports-changes mailing list