Skip to content

Commit beb74d1

Browse files
authored
Merge pull request #322 from Seluj78/feature/minmax-search
Added minmax search route + added common tags in search route
2 parents b7a3f6c + 45bd54d commit beb74d1

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

backend/PyMatcha/routes/api/search.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import datetime
2+
import json
23

34
from flask import Blueprint
45
from flask import request
56
from flask_jwt_extended import current_user
67
from flask_jwt_extended import jwt_required
8+
from PyMatcha import redis
79
from PyMatcha.utils.decorators import validate_params
810
from PyMatcha.utils.errors import BadRequestError
911
from PyMatcha.utils.match_score import _get_common_tags
1012
from PyMatcha.utils.match_score import _get_distance
1113
from PyMatcha.utils.match_score import _get_gender_query
1214
from PyMatcha.utils.success import SuccessOutput
15+
from PyMatcha.utils.tasks import calc_search_min_max
1316

1417
search_bp = Blueprint("search", __name__)
1518

@@ -71,16 +74,29 @@ def search():
7174
if max_distance != -1:
7275
continue
7376
else:
77+
distance = -1
7478
if max_distance != -1:
7579
raise BadRequestError("user needs to sets his location first")
7680

7781
user_tags = [t.name for t in user.get_tags()]
7882
common_tags = _get_common_tags(tags, user_tags)
7983
if not common_tags:
84+
common_tags = []
8085
if tags:
8186
continue
8287

8388
user_dict = user.to_dict()
84-
user_dict.update({"distance": distance, "common_tags": common_tags})
89+
user_dict["distance"] = distance
90+
user_dict["common_tags"] = common_tags
8591
returned_list.append(user_dict)
8692
return SuccessOutput("search_results", returned_list)
93+
94+
95+
@search_bp.route("/search/values", methods=["GET"])
96+
def get_min_maxes_values():
97+
try:
98+
minmax = json.loads(redis.get("search_minmax"))
99+
except TypeError:
100+
calc_search_min_max()
101+
minmax = json.loads(redis.get("search_minmax"))
102+
return SuccessOutput("search_minmax", minmax)

backend/PyMatcha/utils/tasks.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import datetime
2+
import json
23
import logging
34
from math import ceil
45

@@ -22,6 +23,9 @@ def setup_periodic_tasks(sender, **kwargs):
2223
sender.add_periodic_task(60, update_offline_users.s(), name="Update online users every minute")
2324
sender.add_periodic_task(3600, update_heat_scores.s(), name="Update heat scores every hour")
2425
sender.add_periodic_task(60, update_user_recommendations.s(), name="Update user recommendations every minute")
26+
sender.add_periodic_task(
27+
600, calc_search_min_max.s(), name="Update Minimum and Maximum scores and ages for search every 10 minutes"
28+
)
2529

2630

2731
@celery.task
@@ -111,3 +115,28 @@ def update_user_recommendations():
111115
@celery.task
112116
def set_user_superlikes(user_id, amount=5):
113117
redis.set(f"superlikes:{user_id}", amount)
118+
119+
120+
@celery.task
121+
def calc_search_min_max():
122+
min_score = 9999
123+
max_score = 0
124+
min_age = 100
125+
max_age = 0
126+
for user in User.select_all():
127+
if user.heat_score > max_score:
128+
max_score = user.heat_score
129+
if user.heat_score < min_score:
130+
min_score = user.heat_score
131+
132+
today = datetime.datetime.utcnow()
133+
134+
age = today.year - user.birthdate.year - ((today.month, today.day) < (user.birthdate.month, user.birthdate.day))
135+
136+
if age > max_age:
137+
max_age = age
138+
if age < min_age:
139+
min_age = age
140+
minmax = {"min_score": min_score, "max_score": max_score, "min_age": min_age, "max_age": max_age}
141+
redis.set("search_minmax", json.dumps(minmax))
142+
return "Successfully updated min and max ages and scores"

0 commit comments

Comments
 (0)