From d9d0ccbbb321df946f4be9ce2b58342a0d2c9c8d Mon Sep 17 00:00:00 2001 From: aldemirLucas Date: Fri, 9 Aug 2024 13:28:56 -0300 Subject: [PATCH] feat: dictionary download in table column --- next/components/molecules/ColumnsTable.js | 173 +++++++++++++++----- next/pages/api/tables/downloadTable.js | 24 +++ next/pages/api/tables/getColumnsBdmTable.js | 1 + next/pages/api/tables/getDictionaryTable.js | 61 +++++++ 4 files changed, 218 insertions(+), 41 deletions(-) create mode 100644 next/pages/api/tables/downloadTable.js create mode 100644 next/pages/api/tables/getDictionaryTable.js diff --git a/next/components/molecules/ColumnsTable.js b/next/components/molecules/ColumnsTable.js index af776eb3..a5e93a4d 100644 --- a/next/components/molecules/ColumnsTable.js +++ b/next/components/molecules/ColumnsTable.js @@ -13,10 +13,13 @@ import { Skeleton, } from '@chakra-ui/react'; import { useState, useEffect } from 'react'; +import { useRouter } from 'next/router'; import FuzzySearch from 'fuzzy-search'; import Latex from 'react-latex-next'; +import cookies from 'js-cookie'; import { ControlledInputSimple } from '../atoms/ControlledInput'; import Checkbox from '../atoms/Checkbox'; +import { triggerGAEvent, formatBytes } from '../../utils'; import { getColumnsBdmTable @@ -94,6 +97,8 @@ export default function ColumnsTable({ template, columnsPro }) { + const router = useRouter() + const { query } = router const [resource, setResource] = useState({}) const [columns, setColumns] = useState({}) const [isError, setIsError] = useState(false) @@ -102,6 +107,14 @@ export default function ColumnsTable({ const isChecked = (columnSlug) => checkedColumns.includes(columnSlug) + const isUserPro = () => { + let user + if(cookies.get("userBD")) user = JSON.parse(cookies.get("userBD")) + + if(user?.proSubscriptionStatus === "active") return true + return false + } + const handleCheckboxChange = (columnSlug) => { if (isChecked(columnSlug)) { onChangeCheckedColumns(checkedColumns.filter(slug => slug !== columnSlug)) @@ -213,28 +226,56 @@ export default function ColumnsTable({ return 0 } - function TranslationTable({ value }) { - if(value === null) return ( - - Não precisa de tradução - - ) - const cloudValues = value?.table?.cloudTables?.edges?.[0]?.node - const gcpDatasetID = cloudValues?.gcpDatasetId || "" - const gcpTableId = cloudValues?.gcpTableId || "" + function HasDownloadPermitted(value) { + let downloadPermitted = false + let downloadWarning = "" - if(gcpDatasetID === "br_bd_diretorios_data_tempo") return "Não precisa de tradução" - if(gcpDatasetID === "br_bd_diretorios_brasil") { - if(gcpTableId === "empresa" || gcpTableId === "cep") return "Não precisa de tradução" + if (value) { + const limit100MB = 100 * 1024 * 1024; + const limit1GB = 1 * 1024 * 1024 * 1024; + + if (value < limit100MB) { + downloadPermitted = true + downloadWarning = "free" + } else if (value < limit1GB) { + downloadPermitted = isUserPro() + downloadWarning = "100mbBetween1gb" + } else { + downloadPermitted = false + downloadWarning = "biggest1gb" + } } - if(value?.name === "ddd") return "Não precisa de tradução" - if(value?.table?.isClosed) return ( + return { + downloadPermitted : downloadPermitted, + downloadWarning : downloadWarning + } + } + + function DictionaryDownload() { + async function downloadTable() { + const result = await fetch(`/api/tables/getDictionaryTable?p=${btoa(query.dataset)}`, {method: "GET"}) + .then(res => res.json()) + + if(result?.error) return + + let cloudTables = result?.cloudTables?.edges[0]?.node + + triggerGAEvent("download_da_tabela",`{ + gcp: ${cloudTables?.gcpProjectId+"."+cloudTables?.gcpDatasetId+"."+cloudTables?.gcpTableId}, + tamanho: ${formatBytes(result.uncompressedFileSize) || ""}, + dataset: ${query.dataset}, + table: ${resource?._id}, + columnDownload: true + }`) + window.open(`https://storage.googleapis.com/basedosdados-public/one-click-download/${cloudTables?.gcpDatasetId}/${cloudTables?.gcpTableId}/${cloudTables?.gcpTableId}.csv.gz`) + } + + return ( downloadTable()} display="flex" flexDirection="row" alignItems="center" @@ -246,39 +287,86 @@ export default function ColumnsTable({ fill:"#0057A4" }} > - Acessar tabela que faz a tradução desta coluna - + Baixar tabela que faz a tradução desta coluna + - {value?.table?.dataset?.name} - {value?.table?.name} + Dicionário ) + } - const downloadUrl = `https://storage.googleapis.com/basedosdados-public/one-click-download/${gcpDatasetID}/${gcpTableId}/${gcpTableId}.csv.gz` + function TranslationTable({ value, dictionary }) { + const downloadInfo = HasDownloadPermitted(value?.table?.uncompressedFileSize) + const cloudValues = value?.table?.cloudTables?.edges?.[0]?.node + + const gcpProjectID = cloudValues?.gcpProjectId || "" + const gcpDatasetID = cloudValues?.gcpDatasetId || "" + const gcpTableId = cloudValues?.gcpTableId || "" const datasetName = value?.table?.dataset?.name || "" const tableName = value?.table?.name || "" + if(gcpDatasetID === "br_bd_diretorios_data_tempo") return "Não precisa de tradução" + if(gcpDatasetID === "br_bd_diretorios_brasil") { + if(gcpTableId === "empresa" || gcpTableId === "cep") return "Não precisa de tradução" + } + if(value?.name === "ddd") return "Não precisa de tradução" + + const downloadUrl = `https://storage.googleapis.com/basedosdados-public/one-click-download/${gcpDatasetID}/${gcpTableId}/${gcpTableId}.csv.gz` + return ( - - Baixar tabela que faz a tradução desta coluna - - - {datasetName} - {tableName} + {value === null ? + + Não precisa de tradução + + : + + { + if(!downloadInfo.downloadPermitted) return + triggerGAEvent("download_da_tabela",`{ + gcp: ${gcpProjectID+"."+gcpDatasetID+"."+gcpTableId}, + tamanho: ${formatBytes(value?.table?.uncompressedFileSize) || ""}, + dataset: ${value?.table?.dataset?._id}, + table: ${value?.table?._id}, + columnDownload: true + }`) + }} + flexDirection="row" + alignItems="center" + gap="4px" + color="#0068C5" + fill="#0068C5" + _hover={{ + color:"#0057A4", + fill:"#0057A4" + }} + > + {value?.table?.isClosed || !downloadInfo.downloadPermitted + ? + <> + Acessar tabela que faz a tradução desta coluna + + + : + <> + Baixar tabela que faz a tradução desta coluna + + + } + + {datasetName} - {tableName} + + } + + {dictionary === true && + + } ) } @@ -540,7 +628,10 @@ export default function ColumnsTable({ {template === "download" ? - + : } diff --git a/next/pages/api/tables/downloadTable.js b/next/pages/api/tables/downloadTable.js new file mode 100644 index 00000000..d2997eb9 --- /dev/null +++ b/next/pages/api/tables/downloadTable.js @@ -0,0 +1,24 @@ +import axios from "axios"; + +async function downloadTable(datasetID, tableId, res) { + try { + const fileUrl = `https://storage.googleapis.com/basedosdados-public/one-click-download/${datasetID}/${tableId}/${tableId}.csv.gz` + const response = await axios({ + url: fileUrl, + method: 'GET', + responseType: 'stream' + }) + + res.setHeader('Content-Disposition', `attachment; filename=${datasetID}_${tableId}.csv.gz`) + res.setHeader('Content-Type', 'application/gzip') + + response.data.pipe(res) + } catch (error) { + console.error(error) + res.status(500).json({ message: 'Error downloading the file' }) + } +} + +export default async function handler(req, res) { + return downloadTable(atob(req.query.p), atob(req.query.q), res) +} \ No newline at end of file diff --git a/next/pages/api/tables/getColumnsBdmTable.js b/next/pages/api/tables/getColumnsBdmTable.js index 584e9149..0190be73 100644 --- a/next/pages/api/tables/getColumnsBdmTable.js +++ b/next/pages/api/tables/getColumnsBdmTable.js @@ -28,6 +28,7 @@ export default async function getColumnsBdmTable(id) { _id name isClosed + uncompressedFileSize cloudTables{ edges{ node{ diff --git a/next/pages/api/tables/getDictionaryTable.js b/next/pages/api/tables/getDictionaryTable.js new file mode 100644 index 00000000..4391851a --- /dev/null +++ b/next/pages/api/tables/getDictionaryTable.js @@ -0,0 +1,61 @@ +import axios from "axios"; + +const API_URL= `${process.env.NEXT_PUBLIC_API_URL}/api/v1/graphql` + +async function getDictionaryTable(id, slug) { + try { + const res = await axios({ + url: API_URL, + method: "POST", + data: { + query: ` + query { + allDataset (id: "${id}") { + edges { + node { + tables (slug: "${slug}") { + edges { + node { + _id + slug + uncompressedFileSize + cloudTables{ + edges{ + node{ + gcpTableId + gcpDatasetId + gcpProjectId + } + } + } + } + } + } + } + } + } + } + ` + } + }) + const data = res?.data?.data?.allDataset?.edges[0]?.node?.tables?.edges[0]?.node + return data + } catch (error) { + console.error(error) + } +} + +export default async function handler(req, res) { + const param = atob(req.query.p) + let result = await getDictionaryTable(param, "dicionario") + + if (result === undefined) { + result = await getDictionaryTable(param, "dictionary") + } + + if (result === undefined) { + res.status(500).json({ error: "Nenhum resultado encontrado" }) + } else { + res.status(200).json(result) + } +}