-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize multibuy database queries #469
base: next
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -128,15 +128,15 @@ def execute(self): | |||||
self.member = Member.objects.select_for_update().get(id=self.member.id) | ||||||
self.member.fulfill(transaction) | ||||||
|
||||||
# Collect all the sales of the order | ||||||
Sales = [] | ||||||
for item in self.items: | ||||||
# @HACK Since we want to use the old database layout, we need to | ||||||
# add a sale for every item and every instance of that item | ||||||
for i in range(item.count): | ||||||
s = Sale(member=self.member, product=item.product, room=self.room, price=item.product.price) | ||||||
s.save() | ||||||
Sales.append(s) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
# Save all the sales | ||||||
Sale.objects.bulk_create(Sales) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
# Bought (used above) is automatically calculated, so we don't need | ||||||
# to update it | ||||||
# We changed the user balance, so save that | ||||||
self.member.save() | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -24,7 +24,6 @@ | |||||||||||||||||||||||||
from django_select2 import forms as s2forms | ||||||||||||||||||||||||||
import urllib.parse | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
from stregsystem import parser | ||||||||||||||||||||||||||
from stregsystem.models import ( | ||||||||||||||||||||||||||
Member, | ||||||||||||||||||||||||||
|
@@ -140,15 +139,14 @@ def _multibuy_hint(now, member): | |||||||||||||||||||||||||
# get the sales with this timestamp | ||||||||||||||||||||||||||
recent_purchases = Sale.objects.filter(member=member, timestamp__gt=earliest_recent_purchase) | ||||||||||||||||||||||||||
number_of_recent_distinct_purchases = recent_purchases.values('timestamp').distinct().count() | ||||||||||||||||||||||||||
recent_unique_purchases = recent_purchases.values('product').distinct().annotate(total=Count('product')) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
# add hint for multibuy | ||||||||||||||||||||||||||
if number_of_recent_distinct_purchases > 1: | ||||||||||||||||||||||||||
sale_dict = {} | ||||||||||||||||||||||||||
for sale in recent_purchases: | ||||||||||||||||||||||||||
if not str(sale.product.id) in sale_dict: | ||||||||||||||||||||||||||
sale_dict[str(sale.product.id)] = 1 | ||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||
sale_dict[str(sale.product.id)] = sale_dict[str(sale.product.id)] + 1 | ||||||||||||||||||||||||||
for unique_sale in recent_unique_purchases: | ||||||||||||||||||||||||||
sale_dict[str(unique_sale['product'])] = unique_sale['total'] | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
sale_hints = ["<span class=\"username\">{}</span>".format(member.username)] | ||||||||||||||||||||||||||
if all(sale_count == 1 for sale_count in sale_dict.values()): | ||||||||||||||||||||||||||
return (False, None) | ||||||||||||||||||||||||||
|
@@ -715,14 +713,23 @@ def api_quicksale(request, room, member: Member, bought_ids): | |||||||||||||||||||||||||
|
||||||||||||||||||||||||||
def __append_bought_ids_to_product_list(products, bought_ids, time_now, room): | ||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||
for i in bought_ids: | ||||||||||||||||||||||||||
# Get the amount of unique items bought | ||||||||||||||||||||||||||
unique_product_dict = {} | ||||||||||||||||||||||||||
for unique_id in bought_ids: | ||||||||||||||||||||||||||
if str(unique_id) not in unique_product_dict: | ||||||||||||||||||||||||||
unique_product_dict[str(unique_id)] = 1 | ||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||
unique_product_dict[str(unique_id)] += 1 | ||||||||||||||||||||||||||
Comment on lines
+719
to
+722
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
what's the point in converting from an int object to string all the time? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can also optimize it further by just using dict comprehension: unique_product_dict = {i:bought_ids.count(i) for i in bought_ids} |
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
# Add the given amount of different products | ||||||||||||||||||||||||||
for i in unique_product_dict: | ||||||||||||||||||||||||||
product = Product.objects.get( | ||||||||||||||||||||||||||
Q(pk=i), | ||||||||||||||||||||||||||
Q(active=True), | ||||||||||||||||||||||||||
Q(deactivate_date__gte=time_now) | Q(deactivate_date__isnull=True), | ||||||||||||||||||||||||||
Q(rooms__id=room.id) | Q(rooms=None), | ||||||||||||||||||||||||||
Comment on lines
+725
to
730
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||
products.append(product) | ||||||||||||||||||||||||||
products.extend([product for x in range(unique_product_dict[str(i)])]) | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||
except Product.DoesNotExist: | ||||||||||||||||||||||||||
return "Invalid product id", 400, i | ||||||||||||||||||||||||||
return "OK", 200, None | ||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.