easierbycode
2/12/2013 - 11:19 PM

dummy_trips.rb

fake_trips = []
hour = 60 * 60  # 3600

calc_mi =->duration,mph {(duration.to_f / hour) * mph}

25.times do
  mph = 25
  duration = rand(1..5) * 60

  fake_trips.push({ duration:duration*1000, miles:calc_mi.(duration, mph), highest_speed:rand(25..35) })
end

65.times do
  mph = 45
  duration = rand(5..15) * 60

  fake_trips.push({ duration:duration*1000, miles:calc_mi.(duration, mph), highest_speed:rand(45..75) })
end

10.times do
  mph = 65
  duration = rand(15..60) * 60

  fake_trips.push({ duration:duration*1000, miles:calc_mi.(duration, mph), highest_speed:rand(45..75) })
end

start_imei = 4530000000
trip_index = 0

# create 20 devices with 5 trips each
20.times do |n|
  device = Device.create(imei: (start_imei+n).to_s)

  5.times do
    trip = HistoricalTrip.new(fake_trips[trip_index])

    trip.device_id            = device.id
    trip.start_at             = rand(3.months.ago.to_i..1.day.ago.to_i)
    trip.num_hard_brakes      = rand(4..12)
    trip.num_hard_accel       = rand(3..10)
    trip.num_cornering_events = rand(4..12)
    trip.num_rpm_events       = rand(3..10)
    trip.idle_mins            = rand(0..20)
    trip.num_speed_events     = rand(0..7)

    trip.save!

    trip_index += 1

  end
end