Skip to content
This repository has been archived by the owner on Dec 15, 2024. It is now read-only.

Commit

Permalink
feat: speed up search; speed up location hints
Browse files Browse the repository at this point in the history
  • Loading branch information
dantetemplar committed Nov 23, 2024
1 parent d3559ba commit fe166b6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
5 changes: 5 additions & 0 deletions backend/src/modules/events/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ async def read_one(self, id: str) -> Event | None:
async def read_all(self) -> list[Event] | None:
return await Event.all().to_list()

async def read_all_locations(self) -> list:
s = await Event.get_motor_collection().aggregate([{"$project": {"location": 1}}]).to_list()
s = [i["location"] for i in s]
return s

async def create_many(self, events: list[Event]) -> bool:
res = await Event.insert_many(events)
if not res.acknowledged:
Expand Down
34 changes: 25 additions & 9 deletions backend/src/modules/events/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,32 @@ async def get_all_filters_locations() -> list[LocationsFilterVariants]:
Get all locations.
"""
# From all 'location' fields of events, get unique values
# TODO: Write Mongo request
countries: dict[str, dict[str, RegionsFilterVariants]] = {}
for event in await events_repository.read_all():
for location in event.location:
if location.country not in countries:
countries[location.country] = {}
if location.region not in countries[location.country]:
countries[location.country][location.region] = RegionsFilterVariants(region=location.region, cities=[])
if location.city not in countries[location.country][location.region].cities:
countries[location.country][location.region].cities.append(location.city)
unique_locs = set()
_ = await events_repository.read_all_locations()
for locations in _:
for loc in locations:
unique_locs.add((loc.get("country"), loc.get("region"), loc.get("city")))
unique_locs = sorted(unique_locs, key=lambda x: (x[0], x[1] or "", x[2] or ""))

for country, region, city in unique_locs:
if region is None and city in (
"городской округ",
"деревня",
"железнодорожной станции",
"поселок",
"поселок городского типа",
"село",
):
continue

if country not in countries:
countries[country] = {}
if region not in countries[country]:
countries[country][region] = RegionsFilterVariants(region=region, cities=[])
if city not in countries[country][region].cities:
countries[country][region].cities.append(city)

return [
LocationsFilterVariants(
country=country,
Expand Down
10 changes: 10 additions & 0 deletions backend/src/storages/mongo/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,14 @@ class Settings:
default_language="russian",
name="text_index",
),
IndexModel([("start_date", pymongo.ASCENDING)]),
IndexModel([("end_date", pymongo.ASCENDING)]),
IndexModel([("location.country", pymongo.ASCENDING)]),
IndexModel([("location.region", pymongo.ASCENDING)]),
IndexModel([("location.city", pymongo.ASCENDING)]),
IndexModel([("sport", pymongo.ASCENDING)]),
IndexModel([("discipline", pymongo.ASCENDING)]),
IndexModel([("gender", pymongo.HASHED)]),
IndexModel([("age_min", pymongo.ASCENDING)]),
IndexModel([("age_max", pymongo.ASCENDING)]),
]

0 comments on commit fe166b6

Please sign in to comment.