Skip to content

Instantly share code, notes, and snippets.

@TrevorS
Created November 5, 2012 22:14
Show Gist options
  • Save TrevorS/4020746 to your computer and use it in GitHub Desktop.
Save TrevorS/4020746 to your computer and use it in GitHub Desktop.
Inject random IDRs into database.
namespace :db do
desc "Injects a variable amount of fabricated Internal Data Records into the database."
task :load_fake_idrs, [:amount] => :environment do |t, args|
# convert the first argument into the number of records to create
num_records_to_make = args.amount.to_i
# set to true if we should print each record
print_records = false
# set to true if we should load each record
load_records = false
puts "generating #{num_records_to_make} fake internal data records."
# arrays of valid data for certain fields
@tons = [ 1, 2, 3 ]
@error_statuses = [ "00", "CN", "CS", "EC", "FA", "HO", "IC", "ID", "LR", "ND", "NC", "RE", "SA", "SD", "UT", "ZD", "ZE", "ZI", "ZU" ]
@call_directions = [ "MO", "MT", "CF" ]
@technologies = [ "CDMA", "GSM", "UMTS", "SMS" ]
@switch_names = [ "NT01", "ZT02", "ER01", "ZT01", "NK01" ]
@switch_ids = [ "80", "90", "20", "60", "10" ]
@service_features = [ "14", "18", "19", "20", "21", "24", "28", "29", "2A", "2B", "9", "30", "31", "40", "41", "42", "43", "44", "50",
"51", "60", "61", "70", "71", "72", "80", "81", "82", "83", "90", "91", "92", "93", "94", "99", "9A", "9B", "FF" ]
# an array to hold the generated fake records
records = []
# create the fake records and add them to the array
num_records_to_make.times do
records << new_fake_idr
puts records.last.inspect if print_records
end
# insert the records into the database
InternalRecord.import records if load_records
end
# create a fake internal data record
def new_fake_idr
record = InternalRecord.new
record.error_status = @error_statuses.sample
record.creation_date = Time.now
record.direction = @call_directions.sample
record.technology = @technologies.sample
record.calling_ton = @tons.sample
record.calling_npi = 1
record.calling_mdn = clean_fake_number
record.called_ton = @tons.sample
record.called_npi = 1
record.called_mdn = clean_fake_number
record.eid = fixed_length_random(15)
record.imsi_min = fixed_length_random(15)
record.billed_mdn = clean_fake_number
record.celltower = @switch_ids.sample + fixed_length_random(10)
record.call_end_reason = 1
record.caller_id = clean_fake_number
record.caller_id_block = false
record.timestamp_end = Time.now
record.timestamp_connect = Time.now
record.timestamp_ring = Time.now
record.description = Faker::Lorem.characters(25)
record.dst_country_numeric = fixed_length_random(2)
record.dst_country_alpha = "USA"
record.dst_dialed = clean_fake_number
record.duration_ring = generate_duration
record.duration_connect = generate_duration
record.sequence_no = fixed_length_random(20)
record.home_pmn = fixed_length_random(5)
record.home_sid = fixed_length_random(5)
record.served_pmn = fixed_length_random(5)
record.served_sid = fixed_length_random(5)
record.service_feature = @service_features.sample
record.switch_file = "switch-file-name.dat"
record.timezone_incr_done = true
record.trunk_group_incoming = fixed_length_random(10)
record.trunk_group_outgoing = fixed_length_random(10)
record.timezone_offset = -7
record.mscid = fixed_length_random(15)
record.lac = fixed_length_random(5)
record.switch_name = @switch_names.sample
record
end
# clean up faker's phone number generator
def clean_fake_number
Faker::PhoneNumber.phone_number.gsub("-", "").gsub("(", "").gsub(")", "").gsub(".", "")[0..9]
end
# generate a fixed length random number
def fixed_length_random(length)
rand(length).to_s.center(length, rand(length).to_s)
end
# generate a random duration
def generate_duration
rand(0..250)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment