-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
f07e199
commit ac2a331
Showing
14 changed files
with
241 additions
and
24 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
discovery-provider/alembic/versions/0d2067242dd5_change_reaction_column_names.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
"""Change reaction column names | ||
Revision ID: 0d2067242dd5 | ||
Revises: f11f9e83b28b | ||
Create Date: 2022-05-09 22:03:16.838837 | ||
""" | ||
import inspect | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "0d2067242dd5" | ||
down_revision = "f11f9e83b28b" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def column_exists(table_name, column_name): | ||
bind = op.get_context().bind | ||
insp = sa.inspect(bind) | ||
columns = insp.get_columns(table_name) | ||
return any(c["name"] == column_name for c in columns) | ||
|
||
|
||
def upgrade(): | ||
# Handle lack of idempotency for this migration | ||
if column_exists("reactions", "entity_id"): | ||
op.alter_column("reactions", "entity_id", new_column_name="reacted_to") | ||
if column_exists("reactions", "entity_type"): | ||
op.alter_column("reactions", "entity_type", new_column_name="reaction_type") | ||
if column_exists("reactions", "reaction"): | ||
op.alter_column("reactions", "reaction", new_column_name="reaction_value") | ||
op.create_index( | ||
op.f("ix_reactions_reacted_to_reaction_type"), | ||
"reactions", | ||
["reacted_to", "reaction_type"], | ||
unique=False, | ||
info={"if_not_exists": True}, | ||
) | ||
|
||
|
||
def downgrade(): | ||
if column_exists("reactions", "reacted_to"): | ||
op.alter_column("reactions", "reacted_to", new_column_name="entity_id") | ||
if column_exists("reactions", "reaction_type"): | ||
op.alter_column("reactions", "reaction_type", new_column_name="entity_type") | ||
if column_exists("reactions", "reaction_value"): | ||
op.alter_column("reactions", "reaction_value", new_column_name="reaction") | ||
op.drop_index( | ||
op.f("ix_reactions_reacted_to_reaction_type"), | ||
table_name="reaction", | ||
info={"if_exists": True}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from flask_restx import fields | ||
|
||
from .common import ns | ||
|
||
reaction = ns.model( | ||
"reaction", | ||
{ | ||
"reaction_value": fields.String(required=True), | ||
"reaction_type": fields.String(required=True), | ||
"sender_user_id": fields.String(required=True), | ||
"reacted_to": fields.String(required=True), | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from flask_restx import Namespace, Resource, fields, marshal_with, reqparse | ||
from src.api.v1.helpers import ( | ||
DescriptiveArgument, | ||
extend_reaction, | ||
make_response, | ||
success_response, | ||
) | ||
from src.api.v1.models.reactions import reaction | ||
from src.queries.reactions import get_reactions | ||
from src.utils.db_session import get_db_read_replica | ||
from src.utils.redis_cache import cache | ||
from src.utils.redis_metrics import record_metrics | ||
|
||
ns = Namespace("reactions", description="Reaction related operations") | ||
|
||
get_reactions_parser = reqparse.RequestParser(argument_class=DescriptiveArgument) | ||
get_reactions_parser.add_argument( | ||
"type", required=False, description="The type of reactions for which to query." | ||
) | ||
get_reactions_parser.add_argument( | ||
"tx_signatures", | ||
required=True, | ||
action="split", | ||
description="The `reacted_to` transaction id(s) of the reactions in question.", | ||
) | ||
|
||
get_reactions_response = make_response( | ||
"reactions", ns, fields.List(fields.Nested(reaction)) | ||
) | ||
|
||
|
||
@ns.route("") | ||
class BulkReactions(Resource): | ||
@record_metrics | ||
@ns.doc( | ||
id="Bulk get Reactions", | ||
description="Gets reactions by transaction_id and type", | ||
responses={200: "Success", 400: "Bad request", 500: "Server error"}, | ||
) | ||
@ns.expect(get_reactions_parser) | ||
@marshal_with(get_reactions_response) | ||
@cache(ttl_sec=5) | ||
def get(self): | ||
args = get_reactions_parser.parse_args() | ||
tx_ids, type = args.get("tx_signatures"), args.get("type") | ||
db = get_db_read_replica() | ||
with db.scoped_session() as session: | ||
reactions = get_reactions(session, tx_ids, type) | ||
reactions = list(map(extend_reaction, reactions)) | ||
return success_response(reactions) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from typing import List, Optional, Tuple, TypedDict | ||
|
||
from sqlalchemy.orm.session import Session | ||
from src.models.models import User | ||
from src.models.reaction import Reaction | ||
|
||
|
||
class ReactionResponse(TypedDict): | ||
reaction_value: int | ||
reaction_type: str | ||
reacted_to: str | ||
sender_user_id: int | ||
|
||
|
||
def get_reactions( | ||
session: Session, transaction_ids: List[str], type: Optional[str] | ||
) -> List[ReactionResponse]: | ||
filters = [Reaction.reacted_to.in_(transaction_ids), User.is_current == True] | ||
if type: | ||
filters.append(Reaction.reaction_type == type) | ||
|
||
results: List[Tuple[Reaction, int]] = ( | ||
session.query(Reaction, User.user_id) | ||
.join(User, User.wallet == Reaction.sender_wallet) | ||
.filter( | ||
*filters, | ||
) | ||
.all() | ||
) | ||
|
||
return [ | ||
{ | ||
"reaction_value": r.reaction_value, | ||
"reaction_type": r.reaction_type, | ||
"reacted_to": r.reacted_to, | ||
"sender_user_id": user_id, | ||
} | ||
for (r, user_id) in results | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
identity-service/sequelize/migrations/20220509223231-change_reaction_columns.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict' | ||
|
||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.renameColumn('Reactions', 'entityId', 'reactedTo') | ||
.then(() => queryInterface.renameColumn('Reactions', 'entityType', 'reactionType')) | ||
.then(() => queryInterface.renameColumn('Reactions', 'reaction', 'reactionValue')) | ||
}, | ||
|
||
down: (queryInterface, Sequelize) => { | ||
return queryInterface.renameColumn('Reactions', 'reactedTo', 'entityId') | ||
.then(() => queryInterface.renameColumn('Reactions', 'reactionType', 'entityType')) | ||
.then(() => queryInterface.renameColumn('Reactions', 'reactionValue', 'reaction')) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.