diff --git a/CHANGELOG.md b/CHANGELOG.md index 501fe91a0..d5382d946 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,8 @@ You can also check the [release page](https://github.com/visualize-admin/visuali ## Unreleased - Performance - - (min & max)Inclusive values stored in `sh:or` are now also retrieved + - Improved the performance of data download + - (min|max)Inclusive values stored in `sh:or` are now also retrieved - Style - Map now outlines shapes on hover, instead of changing their colors - Maintenance diff --git a/app/.env.development b/app/.env.development index 5355b3a90..41f14b6b8 100644 --- a/app/.env.development +++ b/app/.env.development @@ -3,3 +3,4 @@ ENDPOINT=sparql+https://lindas.admin.ch/query SPARQL_GEO_ENDPOINT=https://geo.ld.admin.ch/query GRAPHQL_ENDPOINT=/api/graphql WHITELISTED_DATA_SOURCES=["Prod", "Int", "Test"] +SENTRY_IGNORE_API_RESOLUTION_ERROR=1 \ No newline at end of file diff --git a/app/browser/dataset-browse.tsx b/app/browser/dataset-browse.tsx index ded0dee85..a3573bfe7 100644 --- a/app/browser/dataset-browse.tsx +++ b/app/browser/dataset-browse.tsx @@ -959,7 +959,7 @@ export const DatasetResult = ({ )) : null} - {creator ? ( + {creator && creator.label ? ( { @@ -343,13 +344,29 @@ const DownloadMenuItem = ({ dimensionParsers, }); - return fetch("/api/download", { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ columnKeys, data, fileFormat }), - }).then((res) => - res.blob().then((blob) => saveAs(blob, `${fileName}.${fileFormat}`)) - ); + const workbook = new Workbook(); + const worksheet = workbook.addWorksheet("data"); + worksheet.columns = columnKeys.map((d) => ({ + header: d, + key: d, + })); + worksheet.addRows(data); + + switch (fileFormat) { + case "csv": + const csv = await workbook.csv.writeBuffer(); + saveAs(new Blob([csv], { type: "text/csv" }), `${fileName}.csv`); + break; + case "xlsx": + const xlsx = await workbook.xlsx.writeBuffer(); + saveAs( + new Blob([xlsx], { + type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + }), + `${fileName}.xlsx` + ); + break; + } }, [fileFormat, fileName, locale] ); @@ -363,8 +380,8 @@ const DownloadMenuItem = ({ dispatch({ isDownloading: true }); try { - const componentsResult: OperationResult = - await urqlClient + const [componentsResult, observationsResult] = await Promise.all([ + urqlClient .query( ComponentsDocument, { @@ -375,9 +392,8 @@ const DownloadMenuItem = ({ componentIris: undefined, } ) - .toPromise(); - const observationsResult: OperationResult = - await urqlClient + .toPromise(), + urqlClient .query< DataCubeObservationsQuery, DataCubeObservationsQueryVariables @@ -389,7 +405,8 @@ const DownloadMenuItem = ({ componentIris: undefined, filters, }) - .toPromise(); + .toPromise(), + ]); if (componentsResult.data && observationsResult.data) { await download(componentsResult.data, observationsResult.data); diff --git a/app/pages/api/download.ts b/app/pages/api/download.ts deleted file mode 100644 index 869e22a79..000000000 --- a/app/pages/api/download.ts +++ /dev/null @@ -1,61 +0,0 @@ -import { Workbook } from "exceljs"; -import { NextApiRequest, NextApiResponse } from "next"; - -import { FileFormat } from "../../components/data-download"; -import { Observation } from "../../domain/data"; - -export default async function Download( - req: Omit & { - body: { - columnKeys: string[]; - data: Observation[]; - fileFormat: FileFormat; - }; - }, - res: NextApiResponse -) { - const { method } = req; - - switch (method) { - case "POST": - try { - const { columnKeys, data, fileFormat } = req.body; - const workbook = new Workbook(); - const worksheet = workbook.addWorksheet("data"); - worksheet.columns = columnKeys.map((d) => ({ - header: d, - key: d, - })); - worksheet.addRows(data); - - switch (fileFormat) { - case "csv": - await workbook.csv.write(res, { sheetId: worksheet.id }); - break; - case "xlsx": - await workbook.xlsx.write(res); - break; - } - - res.status(200); - res.end(); - } catch (e) { - console.error(e); - res.status(500).json({ message: "Something went wrong!" }); - } - - break; - default: - res.setHeader("Allow", ["POST"]); - res.status(405).end(`Method ${method} Not Allowed`); - } -} - -export const config = { - api: { - bodyParser: { - sizeLimit: "1024mb", - }, - reponseLimit: false, - }, -};