Skip to content

Instantly share code, notes, and snippets.

@deathweaselx86
Last active September 6, 2017 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deathweaselx86/8b273115d7a66760f613782645b3343d to your computer and use it in GitHub Desktop.
Save deathweaselx86/8b273115d7a66760f613782645b3343d to your computer and use it in GitHub Desktop.
Simple script to determine what's going on with an ad or business
#!/usr/bin/env ruby
require 'optparse'
require_relative 'config/environment'
env = Rails.env
AD_ATTRS = [:id, :start_date, :end_date, :business_id, :slug, :processed, :processed_count, :processed_error,
:need_qc_at, :aasm_state, :created_at, :updated_at]
BUSINESS_ATTRS = [:id, :name, :aasm_state, :has_run_ad, :need_qc_at, :skipped_at, :deleted_at, :created_at, :updated_at]
QUEUE_ENTRY_ATTRS = [:id, :user, :created_at, :updated_at, :queue_id, :last_user_id]
WORK_SESSION_ATTRS = [:id, :user, :created_at, :updated_at, :queue_id]
WORK_ITEM_ATTRS = [:id, :action, :change_log, :created_at, :updated_at]
def get_ad_data(id)
begin
ad = Ad.with_deleted.find id
rescue
raise "Ad #{id} not found."
end
format_ad_info(ad)
format_messages(ad)
format_queue_entry(ad)
format_work_sessions(ad)
end
def get_business_data(id)
begin
business = Business.with_deleted.find id
rescue
raise "Business #{id} not found."
end
format_business_info(business)
format_extra_stuff(business)
format_messages(business)
format_queue_entry(business)
format_work_sessions(business)
end
def format_info(attrs, obj)
attrs.each {|attr| puts "\t#{attr}: #{obj.send(attr).inspect}" }
end
def format_ad_info(ad)
puts "Ad:"
format_info(AD_ATTRS, ad)
end
def format_business_info(business)
puts "Business:"
format_info(BUSINESS_ATTRS, business)
end
def format_extra_stuff(business)
contact_info_attrs = ContactInfo.column_names
contact_info_attrs.delete("publisher_id")
current_ads = business.ads.where("start_date >= #{Date.today} and end_date <= #{Date.today}")
puts "Is business complete (FLAF-able, definition as of 5/12/2016)?: #{business.is_complete?}"
puts "Has a current ad? #{current_ads.length > 0}"
puts "Business contact info:"
format_info(contact_info_attrs, business.contact_info)
end
def format_messages(item)
puts "\tmessages:\n\t#{item.messages.split("\n").join("\n\t")}" if item.messages.present?
end
def format_queue_entry(item)
puts "Queue Entry: "
if entry = item.queue_entry
format_info(QUEUE_ENTRY_ATTRS, entry)
else
puts "\tNo queue entry."
end
puts "\n"
end
def format_work_sessions(item)
puts "Work Sessions: "
if work_sessions = item.work_sessions
item.work_sessions.each do |session|
format_work_session(session)
puts "\n"
end
else
puts "\tNo work sessions. Contractors did not operate on this object. Admins/Support may have operated on it.\n"
end
end
def format_work_session(work_session)
format_info(WORK_SESSION_ATTRS, work_session)
if work_items = work_session.work_items && work_items.present?
puts "\n\tWork Items:"
work_items.each do |item|
format_work_item(item)
puts "\n"
end
else
puts "\n\tNo work items. Did the contractor operate on this object for this session?"
end
end
def format_work_item(work_item)
WORK_ITEM_ATTRS.each {|attr| puts "\t\t#{attr}: #{work_item.send(attr)}" }
end
options = OpenStruct.new
options.ad = nil
options.business = nil
OptionParser.new do |opts|
opts.banner = "Usage: forensics.rb [options]"
opts.separator ""
opts.separator "Specific options (these operations are mutually exclusive, favoring the ad)"
opts.separator "You MUST run this on Assembly Line!"
opts.on("-a", "--ad_id N", "Ad id to get forensics data on") do |ad|
options.ad = ad
end
opts.on("-b", "--business_id N", "Business id to get forensics data on") do |biz|
options.business = biz
end
end.parse(ARGV)
if options.ad.present?
get_ad_data(options.ad)
elsif options.business.present?
get_business_data(options.business)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment