|
| 1 | +import nosql.mongo_setup as mongo_setup |
| 2 | +import services.car_service as car_service |
| 3 | +from nosql.car import Car |
| 4 | +from nosql.engine import Engine |
| 5 | +from nosql.owner import Owner |
| 6 | + |
| 7 | +from datetime import datetime |
| 8 | +import random |
| 9 | +from faker import Faker |
| 10 | + |
| 11 | +from nosql.service_record import ServiceRecord |
| 12 | + |
| 13 | + |
| 14 | +def main(): |
| 15 | + # large data DB example |
| 16 | + car_count = 250_000 |
| 17 | + owner_count = 100_000 |
| 18 | + |
| 19 | + # simple DB example |
| 20 | + # car_count = 200 |
| 21 | + # owner_count = 100 |
| 22 | + |
| 23 | + mongo_setup.init() |
| 24 | + clear_db() |
| 25 | + |
| 26 | + t0 = datetime.now() |
| 27 | + |
| 28 | + fake = create_faker_and_seed() |
| 29 | + owners = create_owners(fake, count=owner_count) |
| 30 | + print("Created {:,.0f} owners".format(len(owners))) |
| 31 | + cars = create_cars(count=car_count) |
| 32 | + print("Created {:,.0f} cars".format(len(cars))) |
| 33 | + if cars and owners: |
| 34 | + add_cars_to_owners(owners, cars) |
| 35 | + create_service_records(cars, fake) |
| 36 | + |
| 37 | + dt = datetime.now() - t0 |
| 38 | + print("Done in {} sec".format(dt.total_seconds())) |
| 39 | + |
| 40 | + |
| 41 | +models = [ |
| 42 | + 'Ferrari 488 GTB', |
| 43 | + 'Ferrari 360 modena', |
| 44 | + 'F430', |
| 45 | + '599 GTB Fiorano', |
| 46 | + '458 Italia', |
| 47 | + 'LaFerrari', |
| 48 | + 'Testarossa', |
| 49 | + 'F12 Berlinetta', |
| 50 | + '308 GTB/GTS', |
| 51 | + 'F355', |
| 52 | + 'California', |
| 53 | + '575M Maranello', |
| 54 | + 'F50', |
| 55 | + 'F40', |
| 56 | + 'Enzo Ferrari', |
| 57 | +] |
| 58 | + |
| 59 | +service_operations = [ |
| 60 | + ('Oil change', 200), |
| 61 | + ('New tires', 1000), |
| 62 | + ('New engine', 15000), |
| 63 | + ('Body repair', 4000), |
| 64 | + ('New seat', 5000), |
| 65 | + ('Tune up', 1500), |
| 66 | + ('Air filter', 100), |
| 67 | + ('Flat tire', 200), |
| 68 | +] |
| 69 | + |
| 70 | + |
| 71 | +def create_faker_and_seed(): |
| 72 | + fake = Faker() |
| 73 | + fake.seed(42) |
| 74 | + random.seed(42) |
| 75 | + return fake |
| 76 | + |
| 77 | + |
| 78 | +def clear_db(): |
| 79 | + Car.drop_collection() |
| 80 | + Owner.drop_collection() |
| 81 | + |
| 82 | + |
| 83 | +def create_owners(fake, count=100): |
| 84 | + current_owner_count = Owner.objects().count() |
| 85 | + if current_owner_count >= count: |
| 86 | + print("There are currently {:,} owners. Skipping create.") |
| 87 | + return [] |
| 88 | + |
| 89 | + count = count - current_owner_count |
| 90 | + |
| 91 | + datetime_start = datetime(year=2000, month=1, day=1) |
| 92 | + datetime_end = datetime(year=datetime.now().year, month=1, day=1) |
| 93 | + |
| 94 | + owners = [] |
| 95 | + print("Building owners") |
| 96 | + for _ in range(0, count): |
| 97 | + owner = Owner() |
| 98 | + owner.name = fake.name() |
| 99 | + owner.created = fake.date_time_between_dates(datetime_start=datetime_start, |
| 100 | + datetime_end=datetime_end, |
| 101 | + tzinfo=None) |
| 102 | + owners.append(owner) |
| 103 | + |
| 104 | + print("Saving owners") |
| 105 | + Owner.objects().insert(owners, load_bulk=True) |
| 106 | + |
| 107 | + return list(Owner.objects()) |
| 108 | + |
| 109 | + |
| 110 | +def create_cars(count=200): |
| 111 | + current_car_count = Car.objects().count() |
| 112 | + if current_car_count >= count: |
| 113 | + print("There are currently {:,} cars. Skipping create.") |
| 114 | + return [] |
| 115 | + |
| 116 | + count = count - current_car_count |
| 117 | + |
| 118 | + hp_factor = 660 |
| 119 | + mpg_factor = 21 |
| 120 | + liters_factor = 4 |
| 121 | + |
| 122 | + cars = [] |
| 123 | + print("Building cars...") |
| 124 | + for _ in range(0, count): |
| 125 | + model = random.choice(models) |
| 126 | + make = 'Ferrari' |
| 127 | + year = random.randint(1985, datetime.now().year) |
| 128 | + mileage = random.randint(0, 150000) |
| 129 | + |
| 130 | + mpg = int((mpg_factor + mpg_factor * random.random() / 4) * 10) / 10.0 |
| 131 | + horsepower = int(hp_factor + hp_factor * random.random() / 2) |
| 132 | + liters = int((liters_factor + liters_factor * random.random() / 2) * 100) / 100.0 |
| 133 | + |
| 134 | + engine = Engine(horsepower=horsepower, liters=liters, mpg=mpg) |
| 135 | + car = Car(model=model, make=make, year=year, engine=engine, mileage=mileage) |
| 136 | + cars.append(car) |
| 137 | + |
| 138 | + print("Saving cars...") |
| 139 | + Car.objects().insert(cars) |
| 140 | + |
| 141 | + return list(Car.objects()) |
| 142 | + |
| 143 | + |
| 144 | +def add_cars_to_owners(owners: list, cars: list): |
| 145 | + for o in owners: |
| 146 | + counter = random.randint(0, 5) |
| 147 | + for _ in range(0, counter): |
| 148 | + car = random.choice(cars) |
| 149 | + car_service.add_owner(o.id, car.id) |
| 150 | + |
| 151 | + |
| 152 | +def create_service_records(cars, fake): |
| 153 | + datetime_start = datetime(year=2000, month=1, day=1) |
| 154 | + datetime_end = datetime(year=datetime.now().year, month=1, day=1) |
| 155 | + |
| 156 | + for car in cars: |
| 157 | + counter = random.randint(0, 10) |
| 158 | + is_positive = random.randint(0, 1) == 1 |
| 159 | + for _ in range(0, counter): |
| 160 | + s = random.choice(service_operations) |
| 161 | + sr = ServiceRecord() |
| 162 | + sr.description = s[0] |
| 163 | + sr.date = fake.date_time_between_dates(datetime_start=datetime_start, |
| 164 | + datetime_end=datetime_end, |
| 165 | + tzinfo=None) |
| 166 | + sr.price = int(s[1] + (random.random() - .5) * s[1] / 4) |
| 167 | + if is_positive: |
| 168 | + sr.customer_rating = random.randint(4, 5) |
| 169 | + else: |
| 170 | + sr.customer_rating = random.randint(1, 3) |
| 171 | + car.service_history.append(sr) |
| 172 | + car.save() |
| 173 | + |
| 174 | + |
| 175 | +if __name__ == '__main__': |
| 176 | + main() |
0 commit comments