-
Notifications
You must be signed in to change notification settings - Fork 1
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
fix: query_simplified_user_actions and add test cases #15
base: master
Are you sure you want to change the base?
Conversation
Some comments to this PR:
|
c9aafc8
to
19d843d
Compare
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.
Some changes TBD
tests/test_query.py
Outdated
([ | ||
{'user': 1, 'action': 'upvote'}, | ||
], { | ||
0: Action.upvote.name, |
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.
Don't use a dict, and don't use .name, it should be an object!
tests/test_query.py
Outdated
{'dtid': '1234', 'username': 'username'}, | ||
{'id': 1, 'dtid': '1234', 'username': 'username', 'country': 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.
Formatting shouldn't be changed here
tests/test_query.py
Outdated
|
||
for action in user_action: | ||
if action['user'] is None: | ||
user = await user_generator() |
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.
Just create this statically, like 3 users and that's it
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.
If we create the users statically, we still need to somehow map the action['user']
value to the corresponding user (user1
, user2
...). Is it not too verbose?
user1 = await user_generator()
user2 = await user_generator()
user3 = await user_generator()
user4 = await user_generator()
if action['user'] == 1:
user = user1
elif action['user'] == 2:
user = user2
elif action['user'] == 3:
user = uset3
elif action['user'] == 4:
user = user4
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.
Check that user_action_generator just needs a dict with an 'id' key, therefore you just need to call it like
await user_action_generator(user={'id': action['user_id']}, ...)
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.
I was actually looking for that, but I got confused with the function signature (def user_action_generator(db_conn):
). I missed that all the arguments extra arguments are passed down to the generate_user_action
function via *
... #$@&%*!
69f6a14
to
89114e4
Compare
mosbot/query.py
Outdated
async with ensure_connection(conn) as conn: | ||
result = [] | ||
async for user_action in await conn.execute(query): | ||
result.append(dict(user_action)) | ||
return result | ||
return result |
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.
No newline at the end of the file...
155946f
to
14364e5
Compare
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.
I have some doubts, but overall looks good
await user_action_generator(user={'id': action['user_id']}, playback=playback, action=action['action']) | ||
|
||
simplified_user_action = await query_simplified_user_actions(playback_id=playback['id'], conn=db_conn) | ||
actual_result = {i: user_action['action'] for i, user_action in enumerate(simplified_user_action)} |
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.
I don't understand why compare a dict instead of the list of the result direclty. Why are you transforming the result to check it?
left join user_action ua on p.id = ua.playback_id | ||
left join track t on p.track_id = t.id | ||
left join "user" u on ua.user_id = u.id | ||
where p.id = {playback_id} |
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.
Have you checked a safer way to execute this query? you are literally rendering the parameter directly, instead of escaping it or using it as a parameter. I don't know if aiopg the library we are using can do it but please have a look
https://chartio.com/resources/tutorials/how-to-execute-raw-sql-in-sqlalchemy/
f79f1b9
to
17545ee
Compare
So going back through this, I have found that the performance is a bit terrible, using explain analyze:
|
Main change
With this fix, the function
query_simplified_user_actions
will now return all the simplified actions related to a specific playback (given the playback ID). In this case, simplified means that only the lastupvote
ordownvote
action of each user will be returned, together with theskip
, if any at all. If there are no actions, an empty list will returned.Other changes
Close #2