-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Discovery] Add bulk get dashboard wallet users to Discovery Api [C-3532
] (#6984) Co-authored-by: Nikki Kang <kangaroo233@gmail.com>
- Loading branch information
Showing
5 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
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
59 changes: 59 additions & 0 deletions
59
packages/discovery-provider/src/api/v1/dashboard_wallet_users.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,59 @@ | ||
from flask_restx import Namespace, Resource, fields, marshal_with, reqparse | ||
|
||
from src.api.v1.helpers import ( | ||
DescriptiveArgument, | ||
format_dashboard_wallet_user, | ||
make_response, | ||
success_response, | ||
) | ||
from src.api.v1.models.dashboard_wallet_user import dashboard_wallet_user | ||
from src.queries.get_dashboard_wallet_users import get_bulk_dashboard_wallet_users | ||
from src.utils.redis_cache import cache | ||
from src.utils.redis_metrics import record_metrics | ||
|
||
ns = Namespace( | ||
"dashboard_wallet_users", | ||
description="Protocol dashboard wallet users related operations", | ||
) | ||
|
||
get_dashboard_wallet_users_parser = reqparse.RequestParser( | ||
argument_class=DescriptiveArgument | ||
) | ||
get_dashboard_wallet_users_parser.add_argument( | ||
"wallets", | ||
required=True, | ||
action="split", | ||
description="The wallets for which to fetch connected Audius user profiles.", | ||
) | ||
|
||
get_dashboard_wallet_users_response = make_response( | ||
"dashboard_wallet_users_response", | ||
ns, | ||
fields.List(fields.Nested(dashboard_wallet_user)), | ||
) | ||
|
||
|
||
@ns.route("") | ||
class BulkDashboardWalletUsers(Resource): | ||
@record_metrics | ||
@ns.doc( | ||
id="Bulk get dashboard wallet users", | ||
description="Gets Audius user profiles connected to given dashboard wallet addresses", | ||
responses={ | ||
200: "Success", | ||
400: "Bad request", | ||
404: "No such dashboard wallets", | ||
500: "Server error", | ||
}, | ||
) | ||
@ns.expect(get_dashboard_wallet_users_parser) | ||
@marshal_with(get_dashboard_wallet_users_response) | ||
@cache(ttl_sec=5) | ||
def get(self): | ||
args = get_dashboard_wallet_users_parser.parse_args() | ||
wallets_arg = args.get("wallets") | ||
dashboard_wallet_users = get_bulk_dashboard_wallet_users(wallets_arg) | ||
dashboard_wallet_users = list( | ||
map(format_dashboard_wallet_user, dashboard_wallet_users) | ||
) | ||
return success_response(dashboard_wallet_users) |
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
13 changes: 13 additions & 0 deletions
13
packages/discovery-provider/src/api/v1/models/dashboard_wallet_user.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,13 @@ | ||
from flask_restx import fields | ||
|
||
from src.api.v1.models.users import user_model | ||
|
||
from .common import ns | ||
|
||
dashboard_wallet_user = ns.model( | ||
"dashboard_wallet_user", | ||
{ | ||
"wallet": fields.String(required=True), | ||
"user": fields.Nested(user_model, required=True), | ||
}, | ||
) |
35 changes: 35 additions & 0 deletions
35
packages/discovery-provider/src/queries/get_dashboard_wallet_users.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,35 @@ | ||
import logging | ||
from typing import Dict, List | ||
|
||
from sqlalchemy import func | ||
|
||
from src.models.dashboard_wallet_user.dashboard_wallet_user import DashboardWalletUser | ||
from src.models.users.user import User | ||
from src.utils import db_session | ||
from src.utils.helpers import model_to_dictionary | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def get_bulk_dashboard_wallet_users(wallets: List[str]) -> List[Dict]: | ||
""" | ||
Returns dashboard wallet users matching array of wallets | ||
Args: | ||
wallets: List of wallet addresses | ||
Returns: | ||
List of dashboard wallet users | ||
""" | ||
lc_wallets = [wallet.lower() for wallet in wallets] | ||
db = db_session.get_db_read_replica() | ||
with db.scoped_session() as session: | ||
rows = ( | ||
session.query(DashboardWalletUser.wallet, User) | ||
.join(User, User.user_id == DashboardWalletUser.user_id) | ||
.filter(func.lower(DashboardWalletUser.wallet).in_(lc_wallets)) | ||
.filter(DashboardWalletUser.is_delete == False) | ||
.all() | ||
) | ||
res = [{"wallet": row[0], "user": model_to_dictionary(row[1])} for row in rows] | ||
return res |