Skip to content

Commit

Permalink
Merge pull request #1231 from visualize-admin/perf/move-data-download…
Browse files Browse the repository at this point in the history
…-to-api-route

perf: Improve data download performance
  • Loading branch information
bprusinowski authored Oct 24, 2023
2 parents ef546f6 + d73f211 commit fff1c84
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 78 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/.env.development
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/browser/dataset-browse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,7 @@ export const DatasetResult = ({
</Link>
))
: null}
{creator ? (
{creator && creator.label ? (
<Link
key={creator.iri}
href={`/browse/organization/${encodeURIComponent(creator.iri)}`}
Expand Down
47 changes: 32 additions & 15 deletions app/components/data-download.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Typography,
} from "@mui/material";
import { ascending } from "d3";
import { Workbook } from "exceljs";
import { saveAs } from "file-saver";
import keyBy from "lodash/keyBy";
import HoverMenu from "material-ui-popup-state/HoverMenu";
Expand All @@ -25,7 +26,7 @@ import {
useContext,
useState,
} from "react";
import { OperationResult, useClient } from "urql";
import { useClient } from "urql";

import { getSortedColumns } from "@/browse/datatable";
import Flex from "@/components/flex";
Expand Down Expand Up @@ -325,7 +326,7 @@ const DownloadMenuItem = ({
const urqlClient = useClient();
const [state, dispatch] = useDataDownloadState();
const download = useCallback(
(
async (
componentsData: ComponentsQuery,
observationsData: DataCubeObservationsQuery
) => {
Expand All @@ -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]
);
Expand All @@ -363,8 +380,8 @@ const DownloadMenuItem = ({
dispatch({ isDownloading: true });

try {
const componentsResult: OperationResult<ComponentsQuery> =
await urqlClient
const [componentsResult, observationsResult] = await Promise.all([
urqlClient
.query<ComponentsQuery, ComponentsQueryVariables>(
ComponentsDocument,
{
Expand All @@ -375,9 +392,8 @@ const DownloadMenuItem = ({
componentIris: undefined,
}
)
.toPromise();
const observationsResult: OperationResult<DataCubeObservationsQuery> =
await urqlClient
.toPromise(),
urqlClient
.query<
DataCubeObservationsQuery,
DataCubeObservationsQueryVariables
Expand All @@ -389,7 +405,8 @@ const DownloadMenuItem = ({
componentIris: undefined,
filters,
})
.toPromise();
.toPromise(),
]);

if (componentsResult.data && observationsResult.data) {
await download(componentsResult.data, observationsResult.data);
Expand Down
61 changes: 0 additions & 61 deletions app/pages/api/download.ts

This file was deleted.

1 comment on commit fff1c84

@vercel
Copy link

@vercel vercel bot commented on fff1c84 Oct 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

visualization-tool – ./

visualization-tool-git-main-ixt1.vercel.app
visualization-tool-ixt1.vercel.app
visualization-tool-alpha.vercel.app

Please sign in to comment.