Skip to content

Commit

Permalink
feat: dictionary download in table column
Browse files Browse the repository at this point in the history
  • Loading branch information
AldemirLucas committed Aug 9, 2024
1 parent 8c2aa78 commit d9d0ccb
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 41 deletions.
173 changes: 132 additions & 41 deletions next/components/molecules/ColumnsTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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))
Expand Down Expand Up @@ -213,28 +226,56 @@ export default function ColumnsTable({
return 0
}

function TranslationTable({ value }) {
if(value === null) return (
<Text>
Não precisa de tradução
</Text>
)
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 (
<Box>
<Text
as="a"
target="_blank"
href={`${process.env.NEXT_PUBLIC_BASE_URL_FRONTEND}/dataset/${value?.table?.dataset?._id}?table=${value?.table?._id}`}
as="button"
onClick={() => downloadTable()}
display="flex"
flexDirection="row"
alignItems="center"
Expand All @@ -246,39 +287,86 @@ export default function ColumnsTable({
fill:"#0057A4"
}}
>
Acessar tabela que faz a tradução desta coluna
<RedirectIcon width="14px" height="14px"/>
Baixar tabela que faz a tradução desta coluna
<DownloadIcon width="18px" height="18px"/>
</Text>
<Text>{value?.table?.dataset?.name} - {value?.table?.name}</Text>
<Text>Dicionário</Text>
</Box>
)
}

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 (
<Box>
<Text
as="a"
target="_blank"
href={downloadUrl}
display="flex"
flexDirection="row"
alignItems="center"
gap="4px"
color="#0068C5"
fill="#0068C5"
_hover={{
color:"#0057A4",
fill:"#0057A4"
}}
>
Baixar tabela que faz a tradução desta coluna
<DownloadIcon width="18px" height="18px"/>
</Text>
<Text>{datasetName} - {tableName}</Text>
{value === null ?
<Text display={dictionary === true ? "none" : "" }>
Não precisa de tradução
</Text>
:
<Box>
<Text
as="a"
target="_blank"
href={downloadUrl}
display="flex"
onClick={() => {
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
<RedirectIcon width="14px" height="14px"/>
</>
:
<>
Baixar tabela que faz a tradução desta coluna
<DownloadIcon width="18px" height="18px"/>
</>
}
</Text>
<Text>{datasetName} - {tableName}</Text>
</Box>
}

{dictionary === true &&
<DictionaryDownload/>
}
</Box>
)
}
Expand Down Expand Up @@ -540,7 +628,10 @@ export default function ColumnsTable({

<TableValue>
{template === "download" ?
<TranslationTable value={elm?.node?.directoryPrimaryKey}/>
<TranslationTable
value={elm?.node?.directoryPrimaryKey}
dictionary={elm?.node?.coveredByDictionary}
/>
:
<TranslationColumnException value={elm}/>
}
Expand Down
24 changes: 24 additions & 0 deletions next/pages/api/tables/downloadTable.js
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions next/pages/api/tables/getColumnsBdmTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default async function getColumnsBdmTable(id) {
_id
name
isClosed
uncompressedFileSize
cloudTables{
edges{
node{
Expand Down
61 changes: 61 additions & 0 deletions next/pages/api/tables/getDictionaryTable.js
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit d9d0ccb

Please sign in to comment.