From 8b8fc318fed85e5b04aaf3b05edd07203639416c Mon Sep 17 00:00:00 2001 From: Isaac Date: Wed, 6 Jul 2022 00:21:21 +0000 Subject: [PATCH 1/5] wip --- discovery-provider/src/tasks/audius_data.py | 3 +-- discovery-provider/src/tasks/index.py | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/discovery-provider/src/tasks/audius_data.py b/discovery-provider/src/tasks/audius_data.py index bf246cde827..d20460a9e5a 100644 --- a/discovery-provider/src/tasks/audius_data.py +++ b/discovery-provider/src/tasks/audius_data.py @@ -23,6 +23,7 @@ def audius_data_state_update( ipfs_metadata, # prefix unused args with underscore to prevent pylint _blacklisted_cids, ) -> Tuple[int, Dict[str, Set[(int)]]]: + logger.info("asdf audius_data_state_update") num_total_changes = 0 changed_entity_ids: Dict[str, Set[(int)]] = {} @@ -104,9 +105,7 @@ def audius_data_state_update( "playlist" ] = playlist_record playlist_events_lookup[playlist_id]["events"].append(event_type) - playlist_ids.add(playlist_id) - processed_entries += 1 num_total_changes += processed_entries diff --git a/discovery-provider/src/tasks/index.py b/discovery-provider/src/tasks/index.py index 69a4556aefc..8c5511afbb1 100644 --- a/discovery-provider/src/tasks/index.py +++ b/discovery-provider/src/tasks/index.py @@ -503,6 +503,7 @@ def process_state_changes( tx_type_to_grouped_lists_map, block, ): + logger.info("asdf process_state_changes") block_number, block_hash, block_timestamp = itemgetter( "number", "hash", "timestamp" )(block) From 7224a95d0e074eb8c0dfdaf05f16187341cab03b Mon Sep 17 00:00:00 2001 From: Isaac Date: Thu, 7 Jul 2022 17:34:29 +0000 Subject: [PATCH 2/5] Add delete playlist to audius data libs + indexing --- discovery-provider/src/tasks/audius_data.py | 1 - discovery-provider/src/tasks/index.py | 1 - 2 files changed, 2 deletions(-) diff --git a/discovery-provider/src/tasks/audius_data.py b/discovery-provider/src/tasks/audius_data.py index d20460a9e5a..8a0830d5f05 100644 --- a/discovery-provider/src/tasks/audius_data.py +++ b/discovery-provider/src/tasks/audius_data.py @@ -23,7 +23,6 @@ def audius_data_state_update( ipfs_metadata, # prefix unused args with underscore to prevent pylint _blacklisted_cids, ) -> Tuple[int, Dict[str, Set[(int)]]]: - logger.info("asdf audius_data_state_update") num_total_changes = 0 changed_entity_ids: Dict[str, Set[(int)]] = {} diff --git a/discovery-provider/src/tasks/index.py b/discovery-provider/src/tasks/index.py index 8c5511afbb1..69a4556aefc 100644 --- a/discovery-provider/src/tasks/index.py +++ b/discovery-provider/src/tasks/index.py @@ -503,7 +503,6 @@ def process_state_changes( tx_type_to_grouped_lists_map, block, ): - logger.info("asdf process_state_changes") block_number, block_hash, block_timestamp = itemgetter( "number", "hash", "timestamp" )(block) From d0da0257b714c7cadc47cf044c46cc44bcfe3edd Mon Sep 17 00:00:00 2001 From: Isaac Date: Sun, 10 Jul 2022 20:50:13 +0000 Subject: [PATCH 3/5] update playlist --- discovery-provider/src/tasks/audius_data.py | 7 +- libs/src/api/entityManager.ts | 78 +++++++++++++++++++++ 2 files changed, 82 insertions(+), 3 deletions(-) diff --git a/discovery-provider/src/tasks/audius_data.py b/discovery-provider/src/tasks/audius_data.py index 8a0830d5f05..43cd99a7538 100644 --- a/discovery-provider/src/tasks/audius_data.py +++ b/discovery-provider/src/tasks/audius_data.py @@ -56,7 +56,7 @@ def audius_data_state_update( else None ) logger.info( - f"index.py | AudiusData state update: {user_id}, entity_id={entity_id}, entity_type={entity_type}, action={action}, metadata_cid={metadata_cid}, metadata={metadata} signer={signer}" + f"asdf index.py | AudiusData state update: {user_id}, entity_id={entity_id}, entity_type={entity_type}, action={action}, metadata_cid={metadata_cid}, metadata={metadata} signer={signer}" ) # Handle playlist creation @@ -77,8 +77,9 @@ def audius_data_state_update( txhash, ) - if action == "Create": - playlist_id = entity_id + if action == "Create" or action == "Update": + logger.info("asdf update") + playlist_record = parse_playlist_create_data_event( update_task, entry, diff --git a/libs/src/api/entityManager.ts b/libs/src/api/entityManager.ts index dcfb6c24937..92605501877 100644 --- a/libs/src/api/entityManager.ts +++ b/libs/src/api/entityManager.ts @@ -137,6 +137,84 @@ export class EntityManager extends Base { } return respValues } + /** + * Update a playlist using updated data contracts flow + **/ + async updatePlaylist({ + playlistId, + playlistName, + trackIds, + description, + isAlbum, + isPrivate, + coverArt, + logger = console + }: { + playlistId: number + playlistName: string + trackIds: number[] + description: string + isAlbum: boolean + isPrivate: boolean + coverArt: any + logger: any + }): Promise<{ blockHash: any; blockNumber: any; playlistId: number }> { + let respValues = { + blockHash: null, + blockNumber: null, + playlistId: 0 + } + try { + const userId: number = parseInt(this.userStateManager.getCurrentUserId()) + const action = 'Update' + const entityType = 'Playlist' + this.REQUIRES(Services.CREATOR_NODE) + // if (playlistName){ + + // } + let dirCID; + if (coverArt) { + const updatedPlaylistImage = await this.creatorNode.uploadImage( + coverArt, + true // square + ) + dirCID = updatedPlaylistImage.dirCID + } + + const playlist = (await this.discoveryProvider.getPlaylists(1, 0, [playlistId]))[0] + + const metadata = { + action, // why include action here? + entityType, + playlist_id: playlistId, + playlist_contents: trackIds || playlist.playlist_contents, + playlist_name: playlistName || playlist.playlist_name, + playlist_image_sizes_multihash: dirCID || playlist.playlist_image_sizes_multihash, + description: description || playlist.description, + is_album: isAlbum || playlist.is_album, + is_private: isPrivate || playlist.is_private + } + const { metadataMultihash } = await this.creatorNode.uploadPlaylistMetadata(metadata) + + const resp = await this.manageEntity({ + userId, + entityType, + entityId: playlistId, + action, + metadataMultihash + }) + logger.info(`UpdatePlaylistData - ${JSON.stringify(resp)}`) + const txReceipt = resp.txReceipt + respValues = { + blockHash: txReceipt.blockHash, + blockNumber: txReceipt.blockNumber, + playlistId: entityId + } + } catch (e) { + logger.error(`Data update playlist: err ${e}`) + } + return respValues + } /** * Manage an entity with the updated data contract flow From ef1f2157e2175fc2685ed61568782f468d13a5ff Mon Sep 17 00:00:00 2001 From: Isaac Date: Mon, 11 Jul 2022 16:14:08 +0000 Subject: [PATCH 4/5] clean --- discovery-provider/src/tasks/audius_data.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/discovery-provider/src/tasks/audius_data.py b/discovery-provider/src/tasks/audius_data.py index 43cd99a7538..fb3b9b81b49 100644 --- a/discovery-provider/src/tasks/audius_data.py +++ b/discovery-provider/src/tasks/audius_data.py @@ -56,7 +56,7 @@ def audius_data_state_update( else None ) logger.info( - f"asdf index.py | AudiusData state update: {user_id}, entity_id={entity_id}, entity_type={entity_type}, action={action}, metadata_cid={metadata_cid}, metadata={metadata} signer={signer}" + f"index.py | AudiusData state update: {user_id}, entity_id={entity_id}, entity_type={entity_type}, action={action}, metadata_cid={metadata_cid}, metadata={metadata} signer={signer}" ) # Handle playlist creation @@ -78,8 +78,6 @@ def audius_data_state_update( ) if action == "Create" or action == "Update": - logger.info("asdf update") - playlist_record = parse_playlist_create_data_event( update_task, entry, From 6ef577695a9ba3cb27aff24e758d67a85a71eb2a Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Mon, 11 Jul 2022 20:47:33 +0000 Subject: [PATCH 5/5] add types --- discovery-provider/src/tasks/audius_data.py | 17 ++++- libs/src/api/entityManager.ts | 74 +++++++++------------ 2 files changed, 47 insertions(+), 44 deletions(-) diff --git a/discovery-provider/src/tasks/audius_data.py b/discovery-provider/src/tasks/audius_data.py index fb3b9b81b49..e3608edbb52 100644 --- a/discovery-provider/src/tasks/audius_data.py +++ b/discovery-provider/src/tasks/audius_data.py @@ -1,5 +1,6 @@ import logging from datetime import datetime +from enum import Enum from typing import Any, Dict, Set, Tuple from sqlalchemy.orm.session import Session, make_transient @@ -12,6 +13,16 @@ logger = logging.getLogger(__name__) +class Action(Enum): + CREATE = "Create" + UPDATE = "Update" + DELETE = "Delete" + + +class EntityType(Enum): + PLAYLIST = "Playlist" + + def audius_data_state_update( self, update_task: DatabaseTask, @@ -60,7 +71,7 @@ def audius_data_state_update( ) # Handle playlist creation - if entity_type == "Playlist": + if entity_type == EntityType.PLAYLIST: playlist_id = entity_id # look up or populate existing record if playlist_id in playlist_events_lookup: @@ -77,7 +88,7 @@ def audius_data_state_update( txhash, ) - if action == "Create" or action == "Update": + if action == Action.CREATE or Action.UPDATE: playlist_record = parse_playlist_create_data_event( update_task, entry, @@ -88,7 +99,7 @@ def audius_data_state_update( session, ) - elif action == "Delete": + elif Action.DELETE: existing_playlist_record.is_delete = True playlist_record = existing_playlist_record diff --git a/libs/src/api/entityManager.ts b/libs/src/api/entityManager.ts index 92605501877..1b310c32e84 100644 --- a/libs/src/api/entityManager.ts +++ b/libs/src/api/entityManager.ts @@ -1,5 +1,15 @@ import { Base, BaseConstructorArgs, Services } from './base' +export enum Action { + CREATE = 'Create', + UPDATE = 'Update', + DELETE = 'Delete' +} + +export enum EntityType { + PLAYLIST = 'Playlist' +} + /* API surface for updated data contract interactions. Provides simplified entity management in a generic fashion @@ -50,18 +60,13 @@ export class EntityManager extends Base { description: string isAlbum: boolean isPrivate: boolean - coverArt: any - logger: any - }): Promise<{ blockHash: any; blockNumber: any; playlistId: number }> { - let respValues = { - blockHash: null, - blockNumber: null, - playlistId: 0 - } + coverArt: string + logger: Console + }): Promise<{ blockHash: string; blockNumber: number; playlistId: number }> { try { const ownerId: number = parseInt(this.userStateManager.getCurrentUserId()) - const action = 'Create' - const entityType = 'Playlist' + const createAction = Action.CREATE + const entityType = EntityType.PLAYLIST const entityId = await this.getValidPlaylistId() this.REQUIRES(Services.CREATOR_NODE) const updatedPlaylistImage = await this.creatorNode.uploadImage( @@ -70,7 +75,7 @@ export class EntityManager extends Base { ) const dirCID = updatedPlaylistImage.dirCID const metadata = { - action, + createAction, entity_type: entityType, playlist_id: entityId, playlist_contents: trackIds, @@ -86,20 +91,20 @@ export class EntityManager extends Base { userId: ownerId, entityType, entityId, - action, + createAction, metadataMultihash }) logger.info(`CreatePlaylistData - ${JSON.stringify(resp)}`) const txReceipt = resp.txReceipt - respValues = { + return { blockHash: txReceipt.blockHash, blockNumber: txReceipt.blockNumber, playlistId: entityId } } catch (e) { logger.error(`Data create playlist: err ${e}`) + throw e } - return respValues } /** @@ -114,10 +119,6 @@ export class EntityManager extends Base { userId: number logger: any }): Promise<{ blockHash: any; blockNumber: any }> { - let respValues = { - blockHash: null, - blockNumber: null - } try { const resp = await this.manageEntity({ userId, @@ -128,14 +129,14 @@ export class EntityManager extends Base { }) logger.info(`DeletePlaylistData - ${JSON.stringify(resp)}`) const txReceipt = resp.txReceipt - respValues = { + return { blockHash: txReceipt.blockHash, blockNumber: txReceipt.blockNumber } } catch (e) { logger.error(`Data delete playlist: err ${e}`) + throw e } - return respValues } /** * Update a playlist using updated data contracts flow @@ -156,22 +157,14 @@ export class EntityManager extends Base { description: string isAlbum: boolean isPrivate: boolean - coverArt: any - logger: any - }): Promise<{ blockHash: any; blockNumber: any; playlistId: number }> { - let respValues = { - blockHash: null, - blockNumber: null, - playlistId: 0 - } + coverArt: string + logger: Console + }): Promise<{ blockHash: string; blockNumber: number; playlistId: number }> { try { const userId: number = parseInt(this.userStateManager.getCurrentUserId()) - const action = 'Update' + const updateAction = Action.UPDATE const entityType = 'Playlist' this.REQUIRES(Services.CREATOR_NODE) - // if (playlistName){ - - // } let dirCID; if (coverArt) { const updatedPlaylistImage = await this.creatorNode.uploadImage( @@ -184,7 +177,7 @@ export class EntityManager extends Base { const playlist = (await this.discoveryProvider.getPlaylists(1, 0, [playlistId]))[0] const metadata = { - action, // why include action here? + action: updateAction, // why include action here? entityType, playlist_id: playlistId, playlist_contents: trackIds || playlist.playlist_contents, @@ -200,20 +193,19 @@ export class EntityManager extends Base { userId, entityType, entityId: playlistId, - action, + action: updateAction, metadataMultihash }) - logger.info(`UpdatePlaylistData - ${JSON.stringify(resp)}`) const txReceipt = resp.txReceipt - respValues = { + return { blockHash: txReceipt.blockHash, blockNumber: txReceipt.blockNumber, - playlistId: entityId + playlistId } } catch (e) { logger.error(`Data update playlist: err ${e}`) + throw e } - return respValues } /** @@ -228,11 +220,11 @@ export class EntityManager extends Base { metadataMultihash }: { userId: number - entityType: string + entityType: EntityType entityId: number - action: string + action: Action metadataMultihash: string - }): Promise<{ txReceipt: {}; error: any }> { + }): Promise<{ txReceipt: {blockHash: string, blockNumber: number}; error: any }> { let error: string = '' let resp: any try {