def create_owners(fake, count=100): current_owner_count = Owner.objects().count() if current_owner_count >= count: print("There are currently {:,} owners. Skipping create.") return [] count = count - current_owner_count datetime_start = datetime(year=2000, month=1, day=1) datetime_end = datetime(year=datetime.now().year, month=1, day=1) owners = [] print("Building owners") for _ in range(0, count): owner = Owner() owner.name = fake.name() owner.created = fake.date_time_between_dates( datetime_start=datetime_start, datetime_end=datetime_end, tzinfo=None) owners.append(owner) print("Saving owners") Owner.objects().insert(owners, load_bulk=True) return list(Owner.objects())
def find_owner_by_name(name) -> Owner: t0 = datetime.datetime.now() owner = Owner.objects(name=name).first() dt = datetime.datetime.now() - t0 print("Owner found in {} ms".format(dt.total_seconds() * 1000)) return owner
def create_owners(fake, count=100): current_owner_count = Owner.objects().count() if current_owner_count >= count: print("There are currently {:,} owners. Skipping create.") return [] count = count - current_owner_count datetime_start = datetime(year=2000, month=1, day=1) datetime_end = datetime(year=datetime.now().year, month=1, day=1) owners = [] print("Building owners") for _ in range(0, count): owner = Owner() owner.name = fake.name() owner.created = fake.date_time_between_dates(datetime_start=datetime_start, datetime_end=datetime_end, tzinfo=None) owners.append(owner) print("Saving owners") Owner.objects().insert(owners, load_bulk=True) return list(Owner.objects())
def main(): mongo_setup.init() print("Computing stats, this WILL take awhile...", flush=True) cars = list(Car.objects()) print("There are {:,} cars.".format(len(cars))) owners = list(Owner.objects()) print("There are {:,} owners.".format(len(owners))) owned_cars = sum((len(o.car_ids) for o in owners)) print("Each owner owns an average of {:.2f} cars.".format(owned_cars / len(owners))) service_histories = sum((len(c.service_history) for c in cars)) print("There are {:,} service histories.".format(service_histories)) print("Each car has an average of {:.2f} service records.".format(service_histories / len(cars)))
def main(): mongo_setup.init() print("Computing stats, this WILL take awhile...", flush=True) cars = list(Car.objects()) print("There are {:,} cars.".format(len(cars))) owners = list(Owner.objects()) print("There are {:,} owners.".format(len(owners))) owned_cars = sum((len(o.car_ids) for o in owners)) print("Each owner owns an average of {:.2f} cars.".format(owned_cars / len(owners))) service_histories = sum((len(c.service_history) for c in cars)) print("There are {:,} service histories.".format(service_histories)) print("Each car has an average of {:.2f} service records.".format( service_histories / len(cars)))
def create_owners(fake, count=100): datetime_start = datetime(year=2000, month=1, day=1) datetime_end = datetime(year=datetime.now().year, month=1, day=1) owners = [] print("Building owners") for _ in range(0, count): owner = Owner() owner.name = fake.name() owner.created = fake.date_time_between_dates(datetime_start=datetime_start, datetime_end=datetime_end, tzinfo=None) owners.append(owner) print("Saving owners") Owner.objects().insert(owners, load_bulk=True) return list(Owner.objects())
def timed(msg, func): t0 = datetime.now() func() dt = datetime.now() - t0 print("{} Time: {:,.3f} ms".format(msg, dt.total_seconds() * 1000.0), flush=True) mongo_setup.init() print("Time to ask some questions") timed( 'How many owners?', lambda: Owner.objects().filter().count() ) timed( 'How many cars?', lambda: Owner.objects().filter().count() ) timed( 'Find the 10,000th owner by name?', lambda: Owner.objects().order_by('name')[10000:10001][0] ) owner = Owner.objects().order_by('name')[10000:10001][0] def find_cars_by_owner(owner_id):
def add_owner(owner_id, car_id): res = Owner.objects(id=owner_id).update_one(add_to_set__car_ids=car_id) if res != 1: raise Exception('No owner with id {}'.format(owner_id))
def find_owner_by_id(owner_id) -> Owner: owner = Owner.objects(id=owner_id).first() return owner
def record_visit(customer): Owner.objects(name=customer).update_one(inc__number_of_visits=1)
def create_owner(name: str) -> Owner: owner = Owner(name=name) owner.save() return owner
def timed(msg, func): t0 = datetime.now() func() dt = datetime.now() - t0 print("{} Time: {:,.3f} ms".format(msg, dt.total_seconds() * 1000.0), flush=True) mongo_setup.init() print("Time to ask some questions") timed('How many owners?', lambda: Owner.objects().filter().count()) timed('How many cars?', lambda: Owner.objects().filter().count()) timed('Find the 10,000th owner by name?', lambda: Owner.objects().order_by('name')[10000:10001][0]) owner = Owner.objects().order_by('name')[10000:10001][0] def find_cars_by_owner(owner_id): the_owner = Owner.objects(id=owner_id).first() cars = Car.objects().filter(id__in=the_owner.car_ids) return list(cars) timed('How many cars are owned by the 10,000th owner?',
def find_cars_by_owner(owner_id): the_owner = Owner.objects(id=owner_id).first() cars = Car.objects().filter(id__in=the_owner.car_ids) return list(cars)
def add_owner(owner_id, car_id): res = Owner.objects(id=owner_id).update_one(add_to_set__car_ids=car_id) if res != 1: raise Exception("No owner with id {}".format(owner_id))
def clear_db(): Car.drop_collection() Owner.drop_collection()
def find_owners_by_car(car_id): print(car_id) owners = Owner.objects(car_ids=car_id) return list(owners)