Skip to content

Commit

Permalink
Merge pull request #899 from basedosdados/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
AldemirLucas authored Aug 14, 2024
2 parents 77bcdcc + a67ec44 commit d8bdf8d
Show file tree
Hide file tree
Showing 13 changed files with 231 additions and 146 deletions.
13 changes: 8 additions & 5 deletions next/components/molecules/ColumnsTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ export default function ColumnsTable({
if(result?.error) return

let cloudTables = result?.cloudTables?.edges[0]?.node
const downloadInfo = HasDownloadPermitted(result?.uncompressedFileSize)

triggerGAEvent("download_da_tabela",`{
gcp: ${cloudTables?.gcpProjectId+"."+cloudTables?.gcpDatasetId+"."+cloudTables?.gcpTableId},
Expand All @@ -268,7 +269,8 @@ export default function ColumnsTable({
table: ${resource?._id},
columnDownload: true
}`)
window.open(`https://storage.googleapis.com/basedosdados-public/one-click-download/${cloudTables?.gcpDatasetId}/${cloudTables?.gcpTableId}/${cloudTables?.gcpTableId}.csv.gz`)

window.open(`/api/tables/downloadTable?p=${btoa(cloudTables?.gcpDatasetId)}&q=${btoa(cloudTables?.gcpTableId)}&d=${btoa(downloadInfo.downloadPermitted)}&s=${btoa(downloadInfo.downloadWarning)}`)
}

return (
Expand Down Expand Up @@ -305,15 +307,13 @@ export default function ColumnsTable({

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>
{value === null ?
Expand All @@ -325,7 +325,10 @@ export default function ColumnsTable({
<Text
as="a"
target="_blank"
href={downloadUrl}
href={value?.table?.isClosed || !downloadInfo.downloadPermitted
? `${process.env.NEXT_PUBLIC_BASE_URL_FRONTEND}/dataset/${value?.table?.dataset?._id}?table=${value?.table?._id}`
: `/api/tables/downloadTable?p=${btoa(gcpDatasetID)}&q=${btoa(gcpTableId)}&d=${btoa(downloadInfo.downloadPermitted)}&s=${btoa(downloadInfo.downloadWarning)}`
}
display="flex"
onClick={() => {
if(!downloadInfo.downloadPermitted) return
Expand Down
78 changes: 50 additions & 28 deletions next/components/molecules/DataInformationQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ export function CodeHighlight({ language, children }) {
export default function DataInformationQuery({ resource }) {
const [tabAccessIndex, setTabAccessIndex] = useState(0)
const [tabIndex, setTabIndex] = useState(0)
const [downloadNotAllowed, setDownloadNotAllowed] = useState(false)
const [downloadPermitted, setDownloadPermitted] = useState(false)
const [downloadWarning, setDownloadWarning] = useState("")
const [checkedColumns, setCheckedColumns] = useState([])
const [numberColumns, setNumberColumns] = useState(0)
const [columnsTranslationPro, setColumnsTranslationPro] = useState([])
Expand All @@ -185,7 +186,6 @@ export default function DataInformationQuery({ resource }) {
const [gcpProjectID, setGcpProjectID] = useState("")
const [gcpDatasetID, setGcpDatasetID] = useState("")
const [gcpTableId, setGcpTableId] = useState("")
const [downloadUrl, setDownloadUrl] = useState("")

const isUserPro = () => {
let user
Expand All @@ -200,21 +200,27 @@ export default function DataInformationQuery({ resource }) {
}, [resource.dataset])

useEffect(() => {
if (resource?.numberRows === 0) setDownloadNotAllowed(false)
if (resource?.numberRows) resource?.numberRows > 200000 ? setDownloadNotAllowed(false) : setDownloadNotAllowed(true)

if (resource?.uncompressedFileSize) {
const limit100MB = 100 * 1024 * 1024;
const limit1GB = 1 * 1024 * 1024 * 1024;

if (resource?.uncompressedFileSize < limit100MB) {
setDownloadPermitted(true)
setDownloadWarning("free")
} else if (resource?.uncompressedFileSize < limit1GB) {
setDownloadPermitted(isUserPro())
setDownloadWarning("100mbBetween1gb")
} else {
setDownloadWarning("biggest1gb")
}
}

if (resource?.cloudTables?.[0]) {
setGcpProjectID(resource.cloudTables[0]?.gcpProjectId || "")
setGcpDatasetID(resource.cloudTables[0]?.gcpDatasetId || "")
setGcpTableId(resource.cloudTables[0]?.gcpTableId || "")
}

if (gcpDatasetID) {
if (gcpTableId) {
setDownloadUrl(`https://storage.googleapis.com/basedosdados-public/one-click-download/${gcpDatasetID}/${gcpTableId}/${gcpTableId}.csv.gz`)
}
}
}, [resource.numberRows, resource.cloudTables])
}, [resource.uncompressedFileSize, resource.cloudTables])

useEffect(() => {
if(resource._id === undefined) return
Expand Down Expand Up @@ -516,8 +522,7 @@ export default function DataInformationQuery({ resource }) {
marginTop="16px"
padding={0}
>
{downloadNotAllowed ?
isUserPro() ? "" :
{isUserPro() === false && downloadWarning === "free" &&
<AlertDiscalimerBox type="info">
Estes dados estão disponíveis porque diversas pessoas colaboram para a sua manutenção. <Text as="br" display={{base: "none", lg: "flex"}}/>
Antes de baixar os dados, apoie você também com uma doação financeira ou
Expand All @@ -532,41 +537,58 @@ export default function DataInformationQuery({ resource }) {
saiba como contribuir com seu tempo.
</Text>
</AlertDiscalimerBox>
:
}
{isUserPro() === false && downloadWarning === "100mbBetween1gb" &&
<AlertDiscalimerBox
type="warning"
>
O download de tabelas com tamanho entre 100 MB e 1 GB está disponível apenas para
<Text
marginLeft="4px"
as="a"
target="_blank"
href="https://basedosdados.github.io/mais/colab_data/"
color="#0068C5"
_hover={{color: "#0057A4"}}
>assinantes dos nossos planos pagos
</Text>. No entanto, você pode acessar a tabela gratuitamente utilizando SQL, Python, R ou Stata. Considere atualizar para um plano pago para fazer o download.
</AlertDiscalimerBox>
}
{downloadWarning === "biggest1gb" &&
<AlertDiscalimerBox
type="error"
text={`O tamanho da tabela ultrapassou o limite permitido para download, de 200.000 linhas. Você pode acessar os dados em SQL, Python e R.`}
text={`O tamanho da tabela ultrapassou o limite permitido para download, de 1 GB. Você pode acessar os dados em SQL, Python, R e Stata.`}
/>
}

<Box
as="a"
href={downloadUrl}
onClick={() => { triggerGAEvent("download_da_tabela",`
{
href={`/api/tables/downloadTable?p=${btoa(gcpDatasetID)}&q=${btoa(gcpTableId)}&d=${btoa(downloadPermitted)}&s=${btoa(downloadWarning)}`}
target="_blank"
onClick={() => {
triggerGAEvent("download_da_tabela",`{
gcp: ${gcpProjectID+"."+gcpDatasetID+"."+gcpTableId},
tamanho: ${formatBytes(resource.uncompressedFileSize) || ""},
dataset: ${resource?.dataset?._id},
table: ${resource?._id},
}`
) }}
target="_blank"
}`)
}}
display="flex"
alignItems="center"
height="40px"
width="fit-content"
borderRadius="8px"
backgroundColor={downloadNotAllowed ? "#2B8C4D" : "#ACAEB1"}
backgroundColor={downloadPermitted ? "#2B8C4D" : "#ACAEB1"}
padding="8px 16px"
cursor={downloadNotAllowed ? "pointer" : "default"}
cursor={downloadPermitted ? "pointer" : "default"}
color="#FFF"
fill="#FFF"
fontFamily="Roboto"
fontWeight="500"
fontSize="14px"
gap="8px"
lineHeight="20px"
pointerEvents={downloadNotAllowed ? "default" : "none"}
pointerEvents={downloadPermitted ? "default" : "none"}
_hover={{
backgroundColor: "#22703E"
}}
Expand All @@ -575,7 +597,7 @@ export default function DataInformationQuery({ resource }) {
width="24px"
height="24px"
/>
Download da tabela {downloadNotAllowed && `(${formatBytes(resource.uncompressedFileSize)})`}
Download da tabela {downloadPermitted && `(${formatBytes(resource.uncompressedFileSize)})`}
</Box>
</Stack>
</VStack>
Expand Down Expand Up @@ -852,8 +874,8 @@ bd.read_sql(query = query, billing_project_id = billing_id)`}
isLoaded={!isLoadingCode}
>
<CodeHighlight language="r">{`
# Defina o seu projeto no Google Cloud
set_billing_id("<YOUR_PROJECT_ID>")
# Defina o seu projeto no Google Cloud
set_billing_id("<YOUR_PROJECT_ID>")
# Para carregar o dado direto no R
query <- "
Expand Down
20 changes: 10 additions & 10 deletions next/components/organisms/BdmTablePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,16 +196,16 @@ export default function BdmTablePage({ id }) {
let formats
{yearFrequency ?
formats = {
"second":`Atualização a cada ${frequency} por segundo`,
"minute":`Atualização a cada ${frequency} por minuto`,
"hour":`Atualização a cada ${frequency} por hora`,
"day":`Atualização a cada ${frequency} diária`,
"week":`Atualização a cada ${frequency} semanal`,
"month":`Atualização a cada ${frequency} mensal`,
"bimester":`Atualização a cada ${frequency} bimestral`,
"quarter":`Atualização a cada ${frequency} trimestral`,
"semester":`Atualização a cada ${frequency} semestral`,
"year":`Atualização a cada ${frequency} anual`,
"second":`Atualização a cada ${frequency} segundos`,
"minute":`Atualização a cada ${frequency} minutos`,
"hour":`Atualização a cada ${frequency} horas`,
"day":`Atualização a cada ${frequency} dias`,
"week":`Atualização a cada ${frequency} semanas`,
"month":`Atualização a cada ${frequency} meses`,
"bimester":`Atualização a cada ${frequency} bimestres`,
"quarter":`Atualização a cada ${frequency} trimestres`,
"semester":`Atualização a cada ${frequency} semestres`,
"year":`Atualização a cada ${frequency} anos`,
}
:
formats = {
Expand Down
22 changes: 1 addition & 21 deletions next/components/organisms/PaymentSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ import {
import Button from "../atoms/RoundedButton";
import styles from "../../styles/paymentSystem.module.css";

import {
getPrices,
} from "../../pages/api/stripe";

const stripePromise = loadStripe(process.env.NEXT_PUBLIC_KEY_STRIPE)

const PaymentForm = ({ onSucess, onErro, clientSecret}) => {
Expand Down Expand Up @@ -112,24 +108,8 @@ export default function PaymentSystem({ userData, plan, onSucess, onErro }) {
}
}

async function customerCreat(plan) {
const prices = await getPrices()

const findPlan = (slug, slots) => {
const foundPlan = prices.find(p => {
return p.node.productSlug === slug && p.node.productSlots === slots
})

return foundPlan ? foundPlan.node : null
}

const idPlan = findPlan(plan.slug, plan.slots)?._id

customerCreatPost(idPlan)
}

useEffect(() => {
customerCreat(plan)
customerCreatPost(plan.id)
}, [])

const SkeletonBox = ({ type, ...props }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,43 @@ import axios from "axios";

const API_URL= `${process.env.NEXT_PUBLIC_API_URL}/api/v1/graphql`

export default async function getPrices() {
async function getPlans() {
try {
const res = await axios({
url: API_URL,
method: "POST",
data: {
query: `
query {
allStripePrice {
allStripePrice (active: true) {
edges {
node {
_id
amount
productName
productSlug
productSlots
interval
isActive
}
}
}
}
`
}
})
const data = res?.data?.data?.allStripePrice?.edges
const data = res.data
return data
} catch (error) {
console.error(error)
return "err"
}
}

export default async function handler(req, res) {
const result = await getPlans()

if(result.errors) return res.status(500).json({error: result.errors, success: false})
if(result === "err") return res.status(500).json({error: "err", success: false})

res.status(200).json({data: result?.data?.allStripePrice?.edges, success: true})
}
5 changes: 0 additions & 5 deletions next/pages/api/stripe/index.js

This file was deleted.

34 changes: 31 additions & 3 deletions next/pages/api/tables/downloadTable.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
import axios from "axios";

async function downloadTable(datasetID, tableId, res) {
const API_URL= `${process.env.NEXT_PUBLIC_API_URL}/api/v1/graphql`

async function validateToken(token) {
try {
const res = await axios({
url: API_URL,
method: "POST",
data: { query: ` mutation { verifyToken ( token: "${token}" ) { payload } }`
}
})
const data = res.data?.data?.verifyToken?.payload
return data
} catch (error) {
console.error(error)
return "err"
}
}

async function downloadTable(url, datasetID, tableId, token, res) {
let payloadToken
if(url !== "free" && token !== null) payloadToken = await validateToken(token)

try {
const fileUrl = `https://storage.googleapis.com/basedosdados-public/one-click-download/${datasetID}/${tableId}/${tableId}.csv.gz`
const fileUrl = url === "free"
? `${process.env.URL_DOWNLOAD_TABLE}/${datasetID}/${tableId}/${tableId}.csv.gz`
: payloadToken?.pro_subscription_status === "active" ? `${process.env.URL_DOWNLOAD_PRIVATE_TABLE}/${datasetID}/${tableId}/${tableId}.csv.gz` : ""

const response = await axios({
url: fileUrl,
method: 'GET',
Expand All @@ -20,5 +44,9 @@ async function downloadTable(datasetID, tableId, res) {
}

export default async function handler(req, res) {
return downloadTable(atob(req.query.p), atob(req.query.q), res)
const token = req.cookies.token || null

if(atob(req.query.d) === "false") return res.status(500).json({error: "Você não tem permissão para fazer esse download"})

return downloadTable(atob(req.query.s), atob(req.query.p), atob(req.query.q), token, res)
}
24 changes: 12 additions & 12 deletions next/pages/api/user/validateToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import axios from "axios";
const API_URL= `${process.env.NEXT_PUBLIC_API_URL}/api/v1/graphql`

async function validateToken(token) {
const res = await axios({
url: API_URL,
method: "POST",
data: {
query: `
mutation {
verifyToken ( token: "${token}" ) {
payload,
}
}`
}
})
try {
const res = await axios({
url: API_URL,
method: "POST",
data: {
query: `
mutation {
verifyToken ( token: "${token}" ) {
payload,
}
}`
}
})
const data = res.data
return data
} catch (error) {
Expand Down
Loading

0 comments on commit d8bdf8d

Please sign in to comment.