Skip to content

Instantly share code, notes, and snippets.

@gsmendoza
Created March 1, 2011 03:08
Show Gist options
  • Save gsmendoza/848533 to your computer and use it in GitHub Desktop.
Save gsmendoza/848533 to your computer and use it in GitHub Desktop.
Rakefile
# require 'rake'
require 'bundler'
Bundler::GemHelper.install_tasks
dir = File.dirname(File.expand_path(__FILE__))
# $LOAD_PATH.unshift dir + '/lib/fcg-service-servers'
require 'lib/fcg-service-servers/version'
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new do |t|
t.rspec_opts = ["-c", "-f progress", "-r ./spec/spec_helper.rb"]
t.pattern = 'spec/**/*_spec.rb'
end
RSpec::Core::RakeTask.new(:rcov) do |spec|
spec.rcov_opts = %q[--exclude "spec"]
spec.pattern = 'spec/**/*_spec.rb'
spec.rcov = true
end
task :spec => :check_dependencies
task :default => :spec
require 'rake/rdoctask'
Rake::RDocTask.new do |rdoc|
version = File.exist?('VERSION') ? File.read('VERSION') : ""
rdoc.rdoc_dir = 'rdoc'
rdoc.title = "fcg-service-servers #{version}"
rdoc.rdoc_files.include('README*')
rdoc.rdoc_files.include('lib/**/*.rb')
end
namespace :seed do
desc "Seed redis with site data"
task :sites do
require "lib/fcg-service-servers"
Seed::create_sites
end
desc "Populate Redis with US geographic data"
task :us_geodata do
require "lib/fcg-service-servers"
Seed::create_geodata
end
end
namespace :activities do
desc "Summarize data for reports"
task :summarize do
require "lib/fcg-service-servers"
require "lib/rake_helper"
activities = Activity.find(:all, :conditions => ["was_summarized = ?", "false"])
LOGGER.info activities.length.to_s + " activity found on #{1.minute.ago.gmtime.strftime('%Y-%m-%dT%H:%MZ')}"
unless activities.nil?
i = 0
activities.each do |activity|
i += 1
# Save object type of activity: event, city, event type,...
if save_summaries(activity, TypeSummary, ["object_type = ?", activity.object_type], ["object_type"])
LOGGER.info "Save type summary of #{i}th activity successfully"
else
LOGGER.info "Error save type summary of #{i}th activity:"
LOGGER.info activity
end
# Save object id summary.
if activity.object_id.nil?
conditions = ["object_id is null"]
else
conditions = ["object_id = ?", activity.object_id]
end
if save_summaries(activity, ObjectSummary, conditions, ["object_type", "object_id"])
LOGGER.info "Save object summary of #{i}th activity successfully"
else
LOGGER.info "Error save object summary of #{i}th activity:"
LOGGER.info activity
end
conditions = nil
# Save summary about city that the activity is in.
if save_summaries(activity, CitySummary, ["city = ?", activity.city], ["city"])
LOGGER.info "Save city summary of #{i}th activity successfully"
else
LOGGER.info "Error save city summary of #{i}th activity:"
LOGGER.info activity
end
# Save user's objects summary.
user_id = nil
if activity.object_id.nil?
conditions = ["user_id is null"]
else
case activity.object_type
when "event":
user_id = Event.find(activity.object_id).user_id
end
unless user_id.nil?
conditions = ["user_id = ?", user_id]
end
end
unless conditions.nil?
if save_summaries(activity, UserObjectSummary, conditions, ["user_id"], [user_id])
LOGGER.info "Save object summary of #{i}th activity successfully"
else
LOGGER.info "Error save object summary of #{i}th activity:"
LOGGER.info activity
end
end
activity.was_summarized = true
activity.save
end
end
end
task :submit_to_bigquery do
require "lib/fcg-service-servers"
require "lib/rake_helper"
bucket_name = Google.bucket_name
table_name = Google.table_name
response = Google.query("select user_id from [#{bucket_name}/#{table_name}];")
error = false
unless response.index("Not found").nil?
response = Google.create_table(table_name, Google.get_fields)
if response.index("bigquery#table").nil?
LOGGER.info "Error on create bigquery #{table_name} table."
error = true
end
end
# 1 minute ago to ensure that no un-submitted activities are deleted.
time = 1.minute.ago
activities = Activity.find(:all, :conditions => {:created_at.lt => time})
data = ""
unless activities.empty? || error
activities.each do |activity|
data += activity.to_csv + "\n"
end
objectname = table_name + "_" + Time.now.to_i.to_s
response = Google.upload_object_to_storage(objectname, data) unless error
if response != ""
LOGGER.inf "Error upload data to google storage."
error = true
end
response = Google.import_object(table_name, objectname) unless error
if response.index("bigquery#import_id").nil?
LOGGER.info "Error on import data to biquery table."
error = true
end
Activity.destroy_all(:conditions => {"created_at".lt => time}) unless error
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment