From d88b154d43216ce06be70b5c57e72f7f12cc95dd Mon Sep 17 00:00:00 2001 From: Gabriel Rosa Date: Wed, 30 Nov 2022 15:29:31 -0300 Subject: [PATCH] FIX: new id strategy for data assets --- docsfera.json | 6 +- src/modules/catalog/catalog.controller.ts | 46 +---- src/modules/catalog/catalog.service.ts | 231 +++++++--------------- 3 files changed, 78 insertions(+), 205 deletions(-) diff --git a/docsfera.json b/docsfera.json index e4ffcaa..542facf 100644 --- a/docsfera.json +++ b/docsfera.json @@ -2626,7 +2626,7 @@ }, "/catalog": { "get": { - "operationId": "CatalogController_catalogAll", + "operationId": "CatalogController_searchCatalog", "parameters": [], "responses": { "200": { @@ -3233,9 +3233,9 @@ } }, "info": { - "title": "Maestro - stg2", + "title": "Maestro - feat/view-table", "description": "Documentation for Maestro gateway", - "version": "0.0.0", + "version": "1.0.0", "contact": {} }, "tags": [], diff --git a/src/modules/catalog/catalog.controller.ts b/src/modules/catalog/catalog.controller.ts index ff6d047..a37be85 100644 --- a/src/modules/catalog/catalog.controller.ts +++ b/src/modules/catalog/catalog.controller.ts @@ -10,7 +10,6 @@ import { Post, Put, Query, - UnsupportedMediaTypeException, } from '@nestjs/common'; import { ApiTags } from '@nestjs/swagger'; import { @@ -52,12 +51,12 @@ export class CatalogController { PERMISSIONS_GROUPS.CATALOG.permissions.GET, PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER, ) - async catalogAll( + async searchCatalog( @User() user: RequestUser, @Query() query, ): Promise { const { user_id, customer_name, customer_id, username, permissions } = user; - this.logger.info(`/catalog - Catalog all data assets`, { + this.logger.info(`/catalog - searchCatalog`, { user_id, customer_name, }); @@ -77,13 +76,13 @@ export class CatalogController { is_data_manager, }); - const res = await this.catalogService.getAllOpenSearchDataAssets( + const res = await this.catalogService.searchDataAssets( query, metadata, customer_id, ); - return { ...res }; + return res; } @Get('tags') @@ -120,7 +119,6 @@ export class CatalogController { @Param('id') id, ) { const { username, user_id, customer_id, customer_name, permissions } = user; - const [data_asset_type, data_asset_id] = id.split('-'); this.logger.info(`/catalog - ON GET ONE DASHBOARD METABASE ROUTE`, { username, @@ -142,8 +140,7 @@ export class CatalogController { const { data_asset } = await this.catalogService.getOneDataAsset({ customer_id, - data_asset_id, - data_asset_type, + id, metadata, }); @@ -178,7 +175,6 @@ export class CatalogController { @Param('id') id, ) { const { username, user_id, customer_id, customer_name, permissions } = user; - const [data_asset_type, data_asset_id] = id.split('-'); this.logger.info(`/catalog - ON GET ONE DASHBOARD METABASE ROUTE`, { username, @@ -200,8 +196,7 @@ export class CatalogController { const { data_asset } = await this.catalogService.getOneDataAsset({ customer_id, - data_asset_id, - data_asset_type, + id, metadata, }); @@ -236,17 +231,8 @@ export class CatalogController { @Param('id') id, ): Promise { const { customer_name, customer_id, user_id, username } = user; - const data_asset_type = id.split('-')[0]; - const supported_data_assets = ['dataset']; - - if (!supported_data_assets.includes(data_asset_type)) { - throw new UnsupportedMediaTypeException( - `${data_asset_type} does not have a preview function for the time being.`, - ); - } - - this.logger.info(`/catalog - ON GET DATA DOCS ROUTE`, { + this.logger.info(`/catalog - columns-metadata`, { user_id, customer_name, }); @@ -276,15 +262,6 @@ export class CatalogController { @Param('id') id, ): Promise { const { customer_name, customer_id, user_id, username } = user; - const data_asset_type = id.split('-')[0]; - - const supported_data_assets = ['dataset']; - - if (!supported_data_assets.includes(data_asset_type)) { - throw new UnsupportedMediaTypeException( - `${data_asset_type} does not have a preview function for the time being.`, - ); - } this.logger.info(`/catalog - ON GET DATA DOCS ROUTE`, { user_id, @@ -315,15 +292,6 @@ export class CatalogController { @Param('id') id, ): Promise { const { customer_name, customer_id, user_id, username } = user; - const data_asset_type = id.split('-')[0]; - - const supported_data_assets = ['dataset']; - - if (!supported_data_assets.includes(data_asset_type)) { - throw new UnsupportedMediaTypeException( - `${data_asset_type} does not have a documentation function for the time being.`, - ); - } this.logger.info(`/catalog - ON GET DATA DOCS ROUTE`, { user_id, diff --git a/src/modules/catalog/catalog.service.ts b/src/modules/catalog/catalog.service.ts index 87c395f..ce9fb0f 100644 --- a/src/modules/catalog/catalog.service.ts +++ b/src/modules/catalog/catalog.service.ts @@ -144,34 +144,12 @@ class CatalogService implements OnModuleInit { return roles_ids; } - async getUsernamesAndRolesByCustomerId(customerId: string) { - const resultUsers = await this.userService.findAllUsersByCustomerId( - customerId, - ); - const resultRoles = await this.roleService.roleSearch( - {}, - { customer_id: customerId }, - ); - - const customer_users = resultUsers.users.map((user) => { - return { - id: user.id, - username: user.username, - }; - }); - - const customer_roles = resultRoles.roles.map((role) => { - return { - id: role.id, - name: role.name, - }; - }); - - return [customer_users, customer_roles]; - } - - async getAllOpenSearchDataAssets(query, metadata, customer_id) { - this.logger.info('CatalogService - getAllOpenSearchDashboard'); + async searchDataAssets( + query: Record, + metadata: Metadata, + customer_id: string, + ) { + this.logger.info('CatalogService - searchDataAssets'); const { search, page, size, sort_by, order, ...filters } = query; @@ -187,102 +165,39 @@ class CatalogService implements OnModuleInit { }, metadata, ), - ).catch((err) => { - throw new HttpException( - err.details, - err.code === 6 ? HttpStatus.CONFLICT : 404, - ); - }); + ); const result = JSON.parse(data_assets); - const [customer_users, customer_roles] = - await this.getUsernamesAndRolesByCustomerId(customer_id); + const response = await this.getAssetsUsersAndRoles( + result.data_assets, + customer_id, + ); - for (const asset of result.data_assets) { - const condition = - asset.owner && !asset.owner.includes('@') ? true : false; - const result = condition - ? await this.userService.findOneById(asset.owner).catch(() => null) - : null; - - const owner = result !== null ? result.user.username : null; - - const roles = []; - const users = []; - - for (const role of customer_roles) { - for (const role_id of asset.roles) { - if (role.id === role_id) roles.push(role); - } - } - - for (const user of customer_users) { - for (const user_id of asset.users) { - if (user.id === user_id) users.push(user); - } - } - - Object.assign(asset, { - ...asset, - owner, - roles, - users, - }); - } - - return { data_assets: result.data_assets, total }; + return { data_assets: response, total }; } - async getOneDataAsset({ - data_asset_id, - data_asset_type, - customer_id, - metadata, + async getOneDataAsset(data: { + id: string; + customer_id: string; + metadata: Metadata; }) { + const { customer_id, id, metadata } = data; const { data_asset } = await lastValueFrom( this.catalogReadService.GetOneDataAsset( - { id: data_asset_id, type: data_asset_type }, + { id, type: undefined }, metadata, ), ); - const opensearchTable = JSON.parse(data_asset); + let asset = JSON.parse(data_asset); + asset = { + ...asset, + p_roles: asset.roles, + p_users: asset.users, + }; + asset = await this.getAssetsUsersAndRoles([asset], customer_id); - const result = opensearchTable.owner - ? await this.userService - .findOneById(opensearchTable.owner) - .catch(() => null) - : null; - - const owner = result !== null ? result.user.username : null; - - const [customer_users, customer_roles] = - await this.getUsernamesAndRolesByCustomerId(customer_id); - - const users = []; - const roles = []; - - for (const role of customer_roles) { - for (const role_id of opensearchTable.roles) { - if (role.id === role_id) roles.push(role); - } - } - - for (const user of customer_users) { - for (const user_id of opensearchTable.users) { - if (user.id === user_id) users.push(user); - } - } - - Object.assign(opensearchTable, { - p_roles: opensearchTable.roles, - p_users: opensearchTable.users, - roles, - users, - owner, - }); - - return { data_asset: opensearchTable }; + return { data_asset: asset[0] }; } async updateOneDataAsset({ data_asset_id, customer_id, body, metadata }) { @@ -292,66 +207,30 @@ class CatalogService implements OnModuleInit { metadata, ), ); + let asset = JSON.parse(data_asset); + asset = { + ...asset, + p_roles: asset.roles, + p_users: asset.users, + }; + asset = await this.getAssetsUsersAndRoles([asset], customer_id); - const opensearchTable = JSON.parse(data_asset); - - const result = opensearchTable.owner - ? await this.userService - .findOneById(opensearchTable.owner) - .catch(() => null) - : null; - - const owner = result !== null ? result.user.username : null; - - const [customer_users, customer_roles] = - await this.getUsernamesAndRolesByCustomerId(customer_id); - - const users = []; - const roles = []; - - for (const role of customer_roles) { - for (const role_id of opensearchTable.roles) { - if (role.id === role_id) roles.push(role); - } - } - - for (const user of customer_users) { - for (const user_id of opensearchTable.users) { - if (user.id === user_id) users.push(user); - } - } - - Object.assign(opensearchTable, { - p_roles: opensearchTable.roles, - p_users: opensearchTable.users, - roles, - users, - owner, - }); - - return { data_asset: opensearchTable }; + return { data_asset: asset[0] }; } - async getDataDocs(data_asset_id: string, metadata: Metadata) { - const [data_asset_type, id] = data_asset_id.split('-'); - + async getDataDocs(id: string, metadata: Metadata) { const { documentation } = await lastValueFrom( - this.catalogReadService.GetDatasetDoc( - { id, type: data_asset_type }, - metadata, - ), + this.catalogReadService.GetDatasetDoc({ id, type: undefined }, metadata), ); console.log(documentation); const docs = JSON.parse(documentation); return docs; } - async getDatasetPreview(data_asset_id: string, metadata: Metadata) { - const [data_asset_type, id] = data_asset_id.split('-'); - + async getDatasetPreview(id: string, metadata: Metadata) { const { preview } = await lastValueFrom( this.catalogReadService.GetDatasetPreview( - { id, type: data_asset_type }, + { id, type: undefined }, metadata, ), ); @@ -359,12 +238,10 @@ class CatalogService implements OnModuleInit { return result; } - async getDatasetColumnsMetadata(data_asset_id: string, metadata: Metadata) { - const [data_asset_type, id] = data_asset_id.split('-'); - + async getDatasetColumnsMetadata(id: string, metadata: Metadata) { const { columns_metadata } = await lastValueFrom( this.catalogReadService.GetDatasetColumnsMetadata( - { id, type: data_asset_type }, + { id, type: undefined }, metadata, ), ); @@ -398,6 +275,34 @@ class CatalogService implements OnModuleInit { return response; } + async getAssetsUsersAndRoles(data_assets: Array, customer_id: string) { + const { users: customer_users } = + await this.userService.findAllUsersByCustomerId(customer_id); + const { roles: customer_roles } = await this.roleService.roleSearch( + {}, + { customer_id }, + ); + return data_assets.map((data_asset) => { + const owner = customer_users.find((u) => u.id === data_asset.owner); + + const roles = []; + const users = []; + for (const role_id of data_asset.roles) { + const role = customer_roles.find((r) => r.id === role_id); + if (role) roles.push({ id: role.id, name: role.name }); + } + for (const user_id of data_asset.users) { + const user = customer_users.find((r) => r.id === user_id); + if (user) users.push({ id: user.id, username: user.username }); + } + return { + ...data_asset, + roles, + users, + owner, + } as typeof data_asset; + }); + } } export { CatalogService };