Skip to content

Conversation

@codedsun
Copy link
Contributor

Fixes #6653

#6653 (comment)

Short description of what this resolves:

Count query of sold tickets. Solution: Calculate only placed orders + initializing orders which were created under order expiry time

Checklist

  • I have read the Contribution & Best practices Guide and my PR follows them.
  • My branch is up-to-date with the Upstream development branch.
  • The unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)
  • All the functions created/modified in this PR contain relevant docstrings.

@auto-label auto-label bot added the fix label Dec 23, 2019
@codecov
Copy link

codecov bot commented Dec 23, 2019

Codecov Report

Merging #6693 into development will increase coverage by 0.06%.
The diff coverage is 97.22%.

Impacted file tree graph

@@              Coverage Diff               @@
##           development   #6693      +/-   ##
==============================================
+ Coverage        65.44%   65.5%   +0.06%     
==============================================
  Files              300     300              
  Lines            15297   15324      +27     
==============================================
+ Hits             10011   10038      +27     
  Misses            5286    5286
Impacted Files Coverage Δ
app/models/order.py 90% <100%> (ø) ⬆️
app/factories/attendee.py 100% <100%> (ø) ⬆️
tests/all/integration/api/helpers/test_order.py 98.41% <100%> (+0.79%) ⬆️
app/api/attendees.py 40% <85.71%> (+2.22%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f27e08d...bee05bf. Read the comment docs.

order_expiry_time = get_settings()['order_expiry_time']
if get_count(db.session.query(TicketHolder.id)
.filter(TicketHolder.ticket_id == int(data['ticket']),
TicketHolder.state == 'placed', TicketHolder.deleted_at.is_(None),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

placed or completed

Copy link
Contributor Author

@codedsun codedsun Dec 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iamareebjamal This is done wrong as TicketHolder has a state which represents its address and not a placed state.It represents a state of address. I have to change this to order and then check for order status

@codedsun
Copy link
Contributor Author

codedsun commented Dec 23, 2019

@iamareebjamal What command should I run locally to get the hound violations here on my local?

@iamareebjamal
Copy link
Member

Please add tests as well

@iamareebjamal
Copy link
Member

What command should I run locally to get the hound violations here only?

Not sure

@codedsun
Copy link
Contributor Author

I am away from keyboard for today, will update the PR as soon as I get back in the evening

.filter(TicketHolder.ticket_id == int(data['ticket']),
TicketHolder.state == 'placed', TicketHolder.deleted_at.is_(None),
or_(TicketHolder.state ==' initializing', TicketHolder.created_at <
datetime.datetime.utcnow()+datetime.timedelta(minutes=order_expiry_time))))\
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why check the order expiry time condition? If the order is not created under it, Order is labelled expired. It is not required IMO.
Simply counting the Placed & Completed orders in the before the condition is enough.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to count the currently reserved tickets. Or else more tickets than max tickets will be bought. See the discussion in the issue

@kushthedude
Copy link
Member

@iamareebjamal Why not to count the placed orders and tickets in here

total = db.session.query(func.sum(OrderTicket.quantity.label('sum'))).join(Order.order_tickets).filter(

. here we can event correct the sales statistics all over the web app. As of now we are not showing the sales data and ticket count of Offline Tickets in the dashboard?

@iamareebjamal
Copy link
Member

We are showing it, but separately. And yes, this is where it will be placed finally. This PR is work in progress

@codedsun
Copy link
Contributor Author

@iamareebjamal For the test, shall I write functions with the multiple status of orders? or seperate.

Also while creating attendee I can't change the order status, so shall I run the only create the order or shall I create the attendee.?

@iamareebjamal
Copy link
Member

For the test, shall I write functions with the multiple status of orders? or seperate.

What?

Also while creating attendee I can't change the order status, so shall I run the only create the order or shall I create the attendee.?

You need to create both

@codedsun
Copy link
Contributor Author

codedsun commented Dec 27, 2019

@iamareebjamal - While creating both, 2 orders and 1 attendee is created as in attendee factory an order factory reference is also their.

What?

For the query to test, I am creating a single function with multiple attendees and I am running this query to check that count is valid or not.

@codedsun
Copy link
Contributor Author

@iamareebjamal - Guide me

@iamareebjamal
Copy link
Member

While creating both, 2 orders and 1 attendee is created as in attendee factory an order factory reference is also their.

Split it and use base

https://github.com/fossasia/open-event-server/blob/development/app/factories/session.py

@iamareebjamal
Copy link
Member

iamareebjamal commented Dec 27, 2019

For the query to test, I am creating a single function with multiple attendees and I am running this query to check that count is valid or not.

Create 2 attendees with no order ID, 6 with completed order, 1 with placed, 4 with initializing/initialized and not expired (created_at within order expiry time), 3 with initializing but expired created_at, 5 order status == expired

The count should be 11

@codedsun
Copy link
Contributor Author

While creating both, 2 orders and 1 attendee is created as in attendee factory an order factory reference is also their.

Split it and use base

https://github.com/fossasia/open-event-server/blob/development/app/factories/session.py

Shall this be done in another PR? as it requires changes everywhere it's used

@iamareebjamal
Copy link
Member

It requires changes in just one file. attendee factory

@codedsun
Copy link
Contributor Author

Ok got that

@codedsun
Copy link
Contributor Author

@iamareebjamal Review.

Order.status == 'completed',
and_(Order.status == 'initializing',
Order.created_at + datetime.timedelta(
minutes=30 + order_expiry_time) > datetime.datetime.utcnow()))).count()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, initializing = order expiry time
initialized = 30 minutes + order expiry time

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a initialized state too🙄

@codedsun
Copy link
Contributor Author

@iamareebjamal What i see here is something else.

status = fields.Str(
validate=validate.OneOf(
choices=["initializing", "pending", "cancelled", "completed", "placed", "expired"]
))

@iamareebjamal
Copy link
Member

That's wrong. There is no pending state in production DB

@kushthedude
Copy link
Member

That's wrong. There is no pending state in production DB

Then what is the status getting assigned to pending orders or orders with online payment due?

@codedsun codedsun force-pushed the sold-query branch 2 times, most recently from cc4d153 to f2587d9 Compare December 28, 2019 10:08
@codedsun
Copy link
Contributor Author

@iamareebjamal Check, I have created also 2 attendees with the same initialized state in the test and checked for them also. The test is passing 👍

iamareebjamal
iamareebjamal previously approved these changes Dec 28, 2019
@iamareebjamal
Copy link
Member

Then what is the status getting assigned to pending orders or orders with online payment due

Initialized AFAIK

@iamareebjamal
Copy link
Member

Waiting for verification of a few things. Will merge then

@codedsun
Copy link
Contributor Author

@iamareebjamal Sure, thanks a lot. It was fun solving this. I am taking other issues now.

@kushthedude
Copy link
Member

kushthedude commented Dec 28, 2019 via email

@iamareebjamal
Copy link
Member

iamareebjamal commented Dec 28, 2019

Alright, please change initialized to pending @codedsun

I just confirmed.

Don't know how but there are 257 orders in initialized state in production

@codedsun
Copy link
Contributor Author

@iamareebjamal Check now!

@iamareebjamal iamareebjamal changed the title fix:count query of sold tickets fix: count query of sold tickets Dec 28, 2019
@iamareebjamal iamareebjamal merged commit 7e94006 into fossasia:development Dec 28, 2019
transaction_id=None,
paid_via=None,
is_billing_enabled=False,
created_at=datetime.datetime.now(datetime.timezone.utc),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the problem. created_at is static - set to the starting time of the server #6703

@kushthedude @codedsun

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making the PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Server showing 409: tickets sold out when they are not

4 participants