# Several other statistical methods are in production_log/analyzer. module Enumerable # Find the middle element of the array, or the average of the two middle ones # if there are an odd number of elements. def median sorted_array = self.sort return nil if self.empty? middle_index = self.size / 2 return self.size % 2 == 1 ? self[middle_index] : self[middle_index-1..middle_index].average end end ## # Add a record to the mint_analyze table. # def add_record(klass, records) # Summary times = records.values.flatten klass.send :create, { :resource => 'All', :visit_count => times.size, :average => times.average, :standard_deviation => times.standard_deviation, :min => times.min, :max => times.max } # Detail of each record records.sort_by { |k,v| v.size}.reverse_each do |req, times| next if req.nil? # Skip resources with no name klass.send :create, { :resource => req, :visit_count => times.size, :average => times.average, :standard_deviation => times.standard_deviation, :min => times.min, :max => times.max } end end namespace :mint do desc "Read access logs and save Typo RSS/XML hits to mint_visit table" task :parse_logs => :environment do Mint.parse_log_file end desc "Create a table for caching visit totals." task :create_cache_table => :environment do puts "Creating mint_caches table..." ActiveRecord::Base.connection.execute " CREATE TABLE mint_caches ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, resource VARCHAR(255) NOT NULL, resource_title VARCHAR(255) NOT NULL, visit_count INT(10) NOT NULL, earliest_visit DATETIME ) " puts "Success!" end desc "Recount visits to popular resources and save in cache table. Uses existing resources listed in MintCache table, so you need to get them in there via some other means." task :rebuild => :environment do MintCache.rebuild_efficient end desc "Create a table for holding the output of pl_analyze syslogger analysis" task :create_analyze_table => :environment do puts "Creating mint_analyze table..." ActiveRecord::Base.connection.execute " CREATE TABLE mint_analyze ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, resource VARCHAR(255) NOT NULL, visit_count INT(10) NOT NULL, average FLOAT UNSIGNED NOT NULL, standard_deviation FLOAT UNSIGNED NOT NULL, min FLOAT UNSIGNED NOT NULL, max FLOAT UNSIGNED NOT NULL, type VARCHAR(255) NOT NULL, created_at DATETIME ) " puts "Success!" end desc "Analyze Syslogger log and save to database" task :analyze => :environment do require 'production_log/analyzer' a = Analyzer.new ENV['PRODUCTION_LOG'] puts "Processing..." a.process puts "Recording..." add_record(MintAnalyzeRequestTime, a.request_times) add_record(MintAnalyzeDBTime, a.db_times) add_record(MintAnalyzeRenderTime, a.render_times) puts "Done" end end