[84042] branches/gsoc11-statistics/stats-server/app/models
derek at macports.org
derek at macports.org
Thu Sep 15 10:37:05 PDT 2011
Revision: 84042
http://trac.macports.org/changeset/84042
Author: derek at macports.org
Date: 2011-09-15 10:37:04 -0700 (Thu, 15 Sep 2011)
Log 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
Modified Paths:
--------------
branches/gsoc11-statistics/stats-server/app/models/installed_port.rb
branches/gsoc11-statistics/stats-server/app/models/os_statistic.rb
branches/gsoc11-statistics/stats-server/app/models/submission.rb
Modified: branches/gsoc11-statistics/stats-server/app/models/installed_port.rb
===================================================================
--- branches/gsoc11-statistics/stats-server/app/models/installed_port.rb 2011-09-15 17:01:42 UTC (rev 84041)
+++ branches/gsoc11-statistics/stats-server/app/models/installed_port.rb 2011-09-15 17:37:04 UTC (rev 84042)
@@ -3,4 +3,53 @@
has_one :user
validates_presence_of :user_id, :port_id, :version
+
+ # Populate InstalledPort rows based on submitted JSON data
+ def self.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
+ InstalledPort.add_port(uuid, macports_port, installed_port, month, year)
+ end
+ end
+
+ def self.add_port(user, macports_port, installed_port, month, year)
+ logger.debug {"Adding installed port #{installed_port['name']}"}
+
+ # Update any ports found for this user if they have already been submitted this month
+ port_entrys = user.installed_ports.where(:created_at => (Date.today.at_beginning_of_month)..(Date.today.at_end_of_month))
+ port_entry = port_entrys.find_by_port_id(macports_port.id)
+
+ # No existing entry was found - create a new one
+ if port_entry.nil?
+ port_entry = InstalledPort.new
+ end
+
+ port_entry[:user_id] = user.id
+ port_entry[:port_id] = macports_port.id
+ port_entry[:version] = installed_port['version']
+ port_entry[:variants] = installed_port['variants']
+
+ if not port_entry.save
+ logger.debug "Unable to save port #{installed_port['name']}"
+ logger.debug "Reason: #{port_entry.errors.full_messages}"
+ end
+ end
end
Modified: branches/gsoc11-statistics/stats-server/app/models/os_statistic.rb
===================================================================
--- branches/gsoc11-statistics/stats-server/app/models/os_statistic.rb 2011-09-15 17:01:42 UTC (rev 84041)
+++ branches/gsoc11-statistics/stats-server/app/models/os_statistic.rb 2011-09-15 17:37:04 UTC (rev 84042)
@@ -19,4 +19,43 @@
validates :gcc_version, :presence => true
validates :xcode_version, :presence => true
+ # Populate an OsStatistics row with data
+ def self.add_os_data(user, os)
+ logger.debug "In OsStatistic.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 = user.os_statistic
+
+ 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
+
end
Modified: branches/gsoc11-statistics/stats-server/app/models/submission.rb
===================================================================
--- branches/gsoc11-statistics/stats-server/app/models/submission.rb 2011-09-15 17:01:42 UTC (rev 84041)
+++ branches/gsoc11-statistics/stats-server/app/models/submission.rb 2011-09-15 17:37:04 UTC (rev 84042)
@@ -1,92 +1,5 @@
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 = user.os_statistic
-
- 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']}"}
-
- # Update any ports found for this user if they have already been submitted this month
- port_entrys = user.installed_ports.where(:created_at => (Date.today.at_beginning_of_month)..(Date.today.at_end_of_month))
- port_entry = port_entrys.find_by_port_id(macports_port.id)
-
- # No existing entry was found - create a new one
- if port_entry.nil?
- port_entry = InstalledPort.new
- end
-
- port_entry[:user_id] = user.id
- port_entry[:port_id] = macports_port.id
- port_entry[:version] = installed_port['version']
- port_entry[:variants] = installed_port['variants']
-
- if not port_entry.save
- logger.debug "Unable to save port #{installed_port['name']}"
- logger.debug "Reason: #{port_entry.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']
@@ -95,7 +8,8 @@
user = User.find_or_create_by_uuid(json['id'])
- add_os_data(user, os)
- add_installed_ports(user, active_ports)
+ OsStatistic.add_os_data(user, os)
+ InstalledPort.add_installed_ports(user, active_ports)
end
+
end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macports-changes/attachments/20110915/a9414bb7/attachment-0001.html>
More information about the macports-changes
mailing list