# Created by this very Rails plugin in order to report stats more efficiently. # # See the rake task in this plugin for the details. class MintCache < ActiveRecord::Base # Rebuilds cache table based on visits. # # Ignores pages with fewer than 10 visits. # # Can take a while to run. Try rebuild_efficient instead. def self.rebuild seen = {} Mint.popular_pages.each do |hit_summary| # Remove domain from front of resource generic_resource = hit_summary[:resource].gsub(%r%(https?://[^\/]+)%, '') next if seen[generic_resource] next if hit_summary[:resource_title].blank? if mc = MintCache.find_or_create_by_resource(generic_resource) # Titles are often "Page Name | Site Name", so remove the site name mc.resource_title = hit_summary[:resource_title].gsub(/\|.*$/, '') mc.visit_count = Mint.visits_to_resource(generic_resource) mc.earliest_visit = Mint.earliest_visit_to_resource(generic_resource) (mc.visit_count < 10) ? mc.destroy : mc.save seen[generic_resource] = true end end end # Rebuild cache counts from the resources already listed in the mint_caches table. # # New resources must be entered into the table in another way, such as running MintCache.rebuild def self.rebuild_efficient seen = {} MintCache.find(:all).each do |mint_cache| # Remove domain from front of resource generic_resource = mint_cache.resource.gsub(%r%(https?://[^\/]+)%, '') next if seen[generic_resource] next if mint_cache.resource_title.blank? if mc = MintCache.find_or_create_by_resource(generic_resource) # Titles are often "Page Name | Site Name", so remove the site name mc.resource_title = mint_cache.resource_title.gsub(/\|.*$/, '') mc.visit_count = Mint.visits_to_resource(generic_resource) mc.earliest_visit = Mint.earliest_visit_to_resource(generic_resource) (mc.visit_count < 10) ? mc.destroy : mc.save seen[generic_resource] = true end end end # Returns an array of the 10 most popular pages in descending popularity, with the following fields: # # resource # resource_title # visit_count # earliest_visit # # Arguments will be sent to the standard ActiveRecord find method. def self.popular_pages(options={}) defaults = { :limit => 10, :order => 'visit_count DESC' } options = defaults.merge(options) find :all, options end # Returns stats for a single resource. # # resource # resource_title # visit_count # earliest_visit # # The resource url is stored without a domain in the mint_caches table # /page/action/yada/yada def self.visits_to_resource(url) find_by_resource(url) rescue nil end end