mirror of
https://github.com/dadosfera/maestro.git
synced 2026-11-12 22:29:09 +00:00
491 lines
13 KiB
TypeScript
491 lines
13 KiB
TypeScript
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';
|
|
|
|
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,
|
|
) {
|
|
this.logger = dadosferaLogger.logger;
|
|
}
|
|
|
|
onModuleInit() {
|
|
this.catalogReadService =
|
|
this.grpcClient.getService<ReadService.CatalogReadServices>(
|
|
ProtoServices.CatalogReadServices,
|
|
);
|
|
this.catalogWriteService =
|
|
this.grpcClient.getService<WriteService.CatalogWriteServices>(
|
|
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) => {
|
|
console.log(err.details);
|
|
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) => {
|
|
console.log(err.details);
|
|
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);
|
|
|
|
const roles_ids = result.user.roles.map((role) => role.id);
|
|
|
|
console.log(roles_ids);
|
|
|
|
return roles_ids;
|
|
}
|
|
|
|
async getAllOpenSearchDataAssets(
|
|
data: Messages.GetAllDataAssetsRequest,
|
|
metadata,
|
|
) {
|
|
this.logger.info('CatalogService - getAllOpenSearchDashboard');
|
|
|
|
const response = await lastValueFrom(
|
|
this.catalogReadService.GetAllDataAssets(data, metadata),
|
|
)
|
|
.then((res) => {
|
|
this.logger.info('Done');
|
|
return JSON.parse(res.data_assets);
|
|
})
|
|
.catch((err) => {
|
|
this.logger.error(err.message);
|
|
throw new Error(err);
|
|
});
|
|
|
|
for (const asset of response.tables) {
|
|
const result =
|
|
asset.owner && !asset.owner.includes('@')
|
|
? await this.userService.findOneById(asset.owner)
|
|
: null;
|
|
|
|
const owner = result !== null ? result.user.username : null;
|
|
|
|
Object.assign(asset, { ...asset, owner });
|
|
}
|
|
|
|
for (const asset of response.dashboards) {
|
|
const result =
|
|
asset.owner && !asset.owner.includes('@')
|
|
? await this.userService.findOneById(asset.owner)
|
|
: null;
|
|
|
|
const owner = result !== null ? result.user.username : null;
|
|
console.log(asset.owner, result.user.username, owner);
|
|
|
|
Object.assign(asset, { ...asset, owner });
|
|
}
|
|
|
|
return { data_assets: response };
|
|
}
|
|
|
|
async getOneDashboardMetabase(id, body, metadata) {
|
|
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.user_id
|
|
? await this.userService.findOneById(opensearchTable.owner)
|
|
: null;
|
|
|
|
const owner = result !== null ? result.user.username : null;
|
|
|
|
return {
|
|
...data,
|
|
tags: opensearchTable.tags,
|
|
description: opensearchTable.description || null,
|
|
owner,
|
|
};
|
|
}
|
|
|
|
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) {
|
|
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.user_id
|
|
? await this.userService.findOneById(opensearchTable.owner)
|
|
: null;
|
|
|
|
const owner = result !== null ? result.user.username : null;
|
|
|
|
return [
|
|
{
|
|
...data[0],
|
|
tags: opensearchTable.tags,
|
|
table_description: opensearchTable.description,
|
|
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 };
|