import DadosferaLogger from '@dadosfera/dadosfera-logs/dist'; import { ReadService } from '@dadosfera/protospack-v2/dist/lib/Catalog'; import { ProtoServices } from '@dadosfera/protospack-v2/dist/lib/Catalog'; import { Messages } from '@dadosfera/protospack-v2/dist/lib/Catalog'; import { WriteService } from '@dadosfera/protospack-v2/dist/lib/Catalog'; import { HttpException, HttpStatus, Inject, OnModuleInit, } from '@nestjs/common'; import { ClientGrpc } from '@nestjs/microservices'; import axios from 'axios'; import { lastValueFrom } from 'rxjs'; import { CatalogClientConfiguration } from './catalog-client'; import { UsersService } from '../users/users.service'; import { RolesService } from '../roles/roles.service'; class CatalogService implements OnModuleInit { catalogReadService: ReadService.CatalogReadServices; catalogWriteService: WriteService.CatalogWriteServices; logger: any; constructor( @Inject(DadosferaLogger) dadosferaLogger: DadosferaLogger, @Inject(CatalogClientConfiguration.name) private readonly grpcClient: ClientGrpc, private readonly userService: UsersService, private readonly roleService: RolesService, ) { this.logger = dadosferaLogger.logger; } onModuleInit() { this.catalogReadService = this.grpcClient.getService( ProtoServices.CatalogReadServices, ); this.catalogWriteService = this.grpcClient.getService( ProtoServices.CatalogWriteServices, ); } _getNimbusUrl(body) { const customer = body.info.customer.toLowerCase(); if (process.env.ENV === 'prd') { return `https://nimbus-${customer}.dadosfera.ai`; } return `https://nimbus-${customer}.${process.env.ENV.replace( 'local', 'stg', )}.dadosfera.ai`; } async managePermissions(data: Messages.ManagePermissionRequest, metadata) { this.logger.info('CatalogService - Manage Data assets permissions'); return lastValueFrom( this.catalogWriteService.ManagePermission(data, metadata), ).catch((err) => { throw new HttpException( err.details, err.code === 6 ? HttpStatus.CONFLICT : 404, ); }); } async revokePermissions(data: Messages.RevokePermissionRequest, metadata) { this.logger.info('CatalogService - Manage Data assets permissions'); return lastValueFrom( this.catalogWriteService.RevokePermission(data, metadata), ).catch((err) => { throw new HttpException( err.details, err.code === 6 ? HttpStatus.CONFLICT : 404, ); }); } async catalogAll(body) { const nimbusUrl = this._getNimbusUrl(body); const { data } = await axios.get(`${nimbusUrl}/api/catalog/all/`); return data; } async dataAppsAll(body) { const nimbusUrl = this._getNimbusUrl(body); const { data } = await axios.get(`${nimbusUrl}/api/catalog/data_apps/`); return data; } async dataAppsOne(id, body) { const nimbusUrl = this._getNimbusUrl(body); const { data } = await axios.get( `${nimbusUrl}/api/catalog/data_apps/${id}/`, ); return data; } async getAllDashboardMetabase(body) { const nimbusUrl = this._getNimbusUrl(body); const { data } = await axios.get( `${nimbusUrl}/api/catalog/dashboard-metabase/`, ); return data; } async getUserRolesIds(userId: string) { const result = await this.userService.findOneById(userId).catch(() => null); const roles_ids = result.user.roles.map((role) => role.id); 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( data: Messages.GetAllDataAssetsRequest, metadata, customer_id, ) { this.logger.info('CatalogService - getAllOpenSearchDashboard'); const response = await lastValueFrom( this.catalogReadService.GetAllDataAssets(data, metadata), ).catch((err) => { throw new HttpException( err.details, err.code === 6 ? HttpStatus.CONFLICT : 404, ); }); const data_assets = JSON.parse(response.data_assets); const [customer_users, customer_roles] = await this.getUsernamesAndRolesByCustomerId(customer_id); for (const asset of data_assets.tables) { 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, }); } for (const asset of data_assets.dashboards) { 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; Object.assign(asset, { ...asset, owner }); } return { data_assets }; } async getOneDashboardMetabase(id, body, metadata, customer_id) { const nimbusUrl = this._getNimbusUrl(body); const { data } = await axios.get( `${nimbusUrl}/api/catalog/dashboard-metabase/${id}`, ); const { data_asset } = await lastValueFrom( this.catalogReadService.GetOneDataAsset( { id, type: 'dashboard' }, metadata, ), ); 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(data.dashboard, { tags: opensearchTable.tags, description: opensearchTable.description || null, p_roles: opensearchTable.roles, p_users: opensearchTable.users, roles, users, owner, }); return data; } async getAllTableMetadata(body) { const nimbusUrl = this._getNimbusUrl(body); const { data } = await axios.get( `${nimbusUrl}/api/catalog/table-metadata/`, ); return data; } async getOneTableMetadata(body, params, metadata, customer_id) { const nimbusUrl = this._getNimbusUrl(body); const { data } = await axios.get( `${nimbusUrl}/api/catalog/table-metadata/`, { params: params }, ); if (data?.length === 1) { const { data_asset } = await lastValueFrom( this.catalogReadService.GetOneDataAsset( { id: data[0].id, type: 'dataset' }, metadata, ), ); 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); } } return [ { ...data[0], tags: opensearchTable.tags, table_description: opensearchTable.description, p_roles: opensearchTable.roles, p_users: opensearchTable.users, users, roles, owner, }, ]; } return data; } async deleteOneTableMetadata(id, body) { const nimbusUrl = this._getNimbusUrl(body); const { data } = await axios.delete( `${nimbusUrl}/api/catalog/table-metadata/${id}`, ); return data; } async getOneColumnMetadata(body, params) { const nimbusUrl = this._getNimbusUrl(body); const { data } = await axios.get( `${nimbusUrl}/api/catalog/column-metadata/`, { params: params }, ); return data; } async deleteOneColumnMetadata(id, body) { const nimbusUrl = this._getNimbusUrl(body); const { data } = await axios.delete( `${nimbusUrl}/api/catalog/column-metadata/${id}/`, ); return data; } async getOneDataPreview(body, params) { const nimbusUrl = this._getNimbusUrl(body); const { data } = await axios.get(`${nimbusUrl}/api/catalog/data-preview/`, { params: params, }); return data; } async getDataStatus(body) { const nimbusUrl = this._getNimbusUrl(body); const { data } = await axios.get(`${nimbusUrl}/api/catalog/data-status/`); return data; } async createDataStatus(body) { const nimbusUrl = this._getNimbusUrl(body); const { data } = await axios.post( `${nimbusUrl}/api/catalog/data-status/`, body, ); return data; } async getDataDescription(body) { const nimbusUrl = this._getNimbusUrl(body); const { data } = await axios.get( `${nimbusUrl}/api/catalog/data-description/`, ); return data; } async createDataDescription(body, metadata) { const response = await lastValueFrom( this.catalogWriteService.UpdateDataAsset( { id: `dataset-${body.table_id}`, description: body.description, tags: undefined, }, metadata, ), ); // { // description: 'descrição nova'; // id: 5; // table_id: 4; // } // const nimbusUrl = this._getNimbusUrl(body); // const { data } = await axios.post( // `${nimbusUrl}/api/catalog/data-description/`, // body, // ); // {"id":5,"description":"descrição nova","updated_at":"2022-09-28T14:35:15.618750Z","table_id":4} return { id: 0, description: body.description, updated_at: new Date().toISOString(), table_id: body.table_id, }; } async getDataDocs(body) { const nimbusUrl = this._getNimbusUrl(body); const { data } = await axios.get(`${nimbusUrl}/api/catalog/data-docs/`); return data; } async createDataDocs(body) { const nimbusUrl = this._getNimbusUrl(body); const { data } = await axios.post( `${nimbusUrl}/api/catalog/data-docs/`, body, ); return data; } async getDataRating(body) { const nimbusUrl = this._getNimbusUrl(body); const { data } = await axios.get(`${nimbusUrl}/api/catalog/data-rating/`); return data; } async createDataRating(body) { const nimbusUrl = this._getNimbusUrl(body); const { data } = await axios.post( `${nimbusUrl}/api/catalog/data-rating/`, body, ); return data; } async getSummaryRating(id, body) { const nimbusUrl = this._getNimbusUrl(body); const { data } = await axios.get( `${nimbusUrl}/api/catalog/summary-rating/${id}`, ); return data; } async getDataComment(body) { const nimbusUrl = this._getNimbusUrl(body); const { data } = await axios.get(`${nimbusUrl}/api/catalog/data-comment/`); return data; } async createDataComment(body) { const nimbusUrl = this._getNimbusUrl(body); const { data } = await axios.post( `${nimbusUrl}/api/catalog/data-comment/`, body, ); return data; } async getDataReview(body, params) { const nimbusUrl = this._getNimbusUrl(body); const { data } = await axios.get(`${nimbusUrl}/api/catalog/data-review/`, { params: params, }); return data; } async createTags(data: Messages.TagTableRequest, metadata) { this.logger.info('CatalogService - createTags'); const response = await lastValueFrom( this.catalogWriteService.TagTable(data, metadata), ) .then((res) => { this.logger.info('Done'); return res; }) .catch((err) => { this.logger.error(err.message); throw new Error(err); }); return { data_assets: response }; } async findAllTags(data, metadata) { this.logger.info('CatalogService - findAllCustomerTags'); const response = await lastValueFrom( this.catalogReadService.GetCustomerTags(data, metadata), ) .then((res) => { this.logger.info('Done'); return res; }) .catch((err) => { this.logger.error(err.message); throw new Error(err); }); return response; } async findAllTableTags(data, metadata) { this.logger.info('CatalogService - findAllTableTags'); const response = await lastValueFrom( this.catalogReadService.GetTableTags(data, metadata), ) .then((res) => { this.logger.info('Done'); return res; }) .catch((err) => { this.logger.error(err.message); throw new Error(err); }); return response; } async deleteTags(data, metadata) { this.logger.info('CatalogService - deleteTags'); const response = await lastValueFrom( this.catalogWriteService.UntagTable(data, metadata), ) .then((res) => { this.logger.info('Done'); return res; }) .catch((err) => { this.logger.error(err.message); throw new Error(err); }); return response; } async createTableTags(body) { const nimbusUrl = this._getNimbusUrl(body); const { data } = await axios.post( `${nimbusUrl}/api/catalog/table-tags/`, body, ); return data; } async deleteTableTags(id, body) { const nimbusUrl = this._getNimbusUrl(body); const { data } = await axios.delete( `${nimbusUrl}/api/catalog/table-tags/${id}`, ); return data; } async getTableRules(body) { const nimbusUrl = this._getNimbusUrl(body); const { data } = await axios.get( `${nimbusUrl}/api/catalog/table-rules/`, body, ); return data; } async deleteTableRules(id, body) { const nimbusUrl = this._getNimbusUrl(body); const { data } = await axios.delete( `${nimbusUrl}/api/catalog/table-rules/${id}`, ); return data; } } export { CatalogService };