From c3107e11131691e315413f128ce430c549c2f860 Mon Sep 17 00:00:00 2001 From: dal Date: Wed, 13 Aug 2025 15:45:18 -0600 Subject: [PATCH] Add download functionality for metric files and enhance UI warning component - Introduced a new API method to download metric files. - Updated MetricDataTruncatedWarning component to include a download button for large datasets. - Passed metricId as a prop to MetricDataTruncatedWarning for download functionality. - Improved user experience by providing feedback during the download process. --- .../src/api/buster_rest/metrics/requests.ts | 12 +++- .../MetricDataTruncatedWarning.tsx | 56 ++++++++++++++++--- .../MetricViewChart/MetricViewChart.tsx | 2 +- 3 files changed, 59 insertions(+), 11 deletions(-) diff --git a/apps/web/src/api/buster_rest/metrics/requests.ts b/apps/web/src/api/buster_rest/metrics/requests.ts index 730246a39..171041fd1 100644 --- a/apps/web/src/api/buster_rest/metrics/requests.ts +++ b/apps/web/src/api/buster_rest/metrics/requests.ts @@ -14,11 +14,12 @@ import type { MetricDataResponse, GetMetricResponse, UpdateMetricResponse, - ShareUpdateResponse + ShareUpdateResponse, + MetricDownloadResponse } from '@buster/server-shared/metrics'; import type { ShareDeleteRequest, ShareUpdateRequest } from '@buster/server-shared/share'; import { serverFetch } from '@/api/createServerInstance'; -import { mainApi } from '../instances'; +import { mainApi, mainApiV2 } from '../instances'; import { SharePostRequest } from '@buster/server-shared/share'; export const getMetric = async (params: GetMetricRequest): Promise => { @@ -104,3 +105,10 @@ export const updateMetricShare = async ({ .put(`/metric_files/${id}/sharing`, params) .then((res) => res.data); }; + +// Download metric file +export const downloadMetricFile = async (id: string): Promise => { + return mainApiV2 + .get(`/metric_files/${id}/download`) + .then((res) => res.data); +}; diff --git a/apps/web/src/controllers/MetricController/MetricViewChart/MetricDataTruncatedWarning.tsx b/apps/web/src/controllers/MetricController/MetricViewChart/MetricDataTruncatedWarning.tsx index c22d714c1..186fa110d 100644 --- a/apps/web/src/controllers/MetricController/MetricViewChart/MetricDataTruncatedWarning.tsx +++ b/apps/web/src/controllers/MetricController/MetricViewChart/MetricDataTruncatedWarning.tsx @@ -1,21 +1,61 @@ -import React from 'react'; +import React, { useState } from 'react'; import { Text } from '@/components/ui/typography'; +import { Button } from '@/components/ui/buttons'; +import { Download4 } from '@/components/ui/icons'; import { cn } from '@/lib/classMerge'; +import { downloadMetricFile } from '@/api/buster_rest/metrics/requests'; interface MetricDataTruncatedWarningProps { className?: string; + metricId: string; } export const MetricDataTruncatedWarning: React.FC = ({ - className + className, + metricId }) => { + const [isDownloading, setIsDownloading] = useState(false); + + const handleDownload = async () => { + try { + setIsDownloading(true); + + // Call the API to get the download URL + const response = await downloadMetricFile(metricId); + + // Simply navigate to the download URL + // The response-content-disposition header will force a download + window.location.href = response.downloadUrl; + + } catch (error) { + console.error('Failed to download metric file:', error); + // You might want to show an error toast here + } finally { + // Add a small delay before removing loading state since download happens async + setTimeout(() => { + setIsDownloading(false); + }, 1000); + } + }; + return (
- This request returned more than 5,000 records - - If you need more than that, please contact your data admin. - + className={cn('bg-background flex items-center justify-between rounded border p-4 shadow', className)}> +
+ This request returned more than 5,000 records + + To see all records, you'll need to download the results. + +
+
); -}; +}; \ No newline at end of file diff --git a/apps/web/src/controllers/MetricController/MetricViewChart/MetricViewChart.tsx b/apps/web/src/controllers/MetricController/MetricViewChart/MetricViewChart.tsx index 881144454..2378119a3 100644 --- a/apps/web/src/controllers/MetricController/MetricViewChart/MetricViewChart.tsx +++ b/apps/web/src/controllers/MetricController/MetricViewChart/MetricViewChart.tsx @@ -108,7 +108,7 @@ export const MetricViewChart: React.FC<{ /> - {!!metricData?.has_more_records && } + {!!metricData?.has_more_records && }