Skip to content

Commit

Permalink
Merge branch 'development' of github.com:Scale3-Labs/langtrace into d…
Browse files Browse the repository at this point in the history
…evelopment
  • Loading branch information
karthikscale3 committed May 9, 2024
2 parents 31f50e6 + 82bd818 commit 4e30354
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { CreateData } from "@/components/project/dataset/create-data";
import DatasetRowSkeleton from "@/components/project/dataset/dataset-row-skeleton";
import { EditData } from "@/components/project/dataset/edit-data";
import { DownloadDataset } from "@/components/shared/download-dataset";
import { Spinner } from "@/components/shared/spinner";
import { Button } from "@/components/ui/button";
import { Separator } from "@/components/ui/separator";
Expand Down Expand Up @@ -91,6 +92,7 @@ export default function Dataset() {
Back
</Button>
<CreateData datasetId={dataset_id} />
<DownloadDataset datasetId={dataset_id} disabled={fetchDataset.isLoading || currentData?.length === 0} />
</div>
<div className="flex flex-col gap-3 rounded-md border border-muted max-h-screen overflow-y-scroll">
<div className="grid grid-cols-5 items-center justify-stretch gap-3 py-3 px-4 bg-muted">
Expand All @@ -99,7 +101,7 @@ export default function Dataset() {
<p className="text-xs font-medium">Output</p>
<p className="text-xs font-medium text-end">Note</p>
</div>
{fetchDataset.isLoading && currentData.length === 0 && (
{fetchDataset.isLoading && currentData?.length === 0 && (
<div className="flex items-center justify-center">
<p className="text-muted-foreground">
No data found in this dataset
Expand Down
79 changes: 79 additions & 0 deletions app/api/dataset/download/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { authOptions } from "@/lib/auth/options";
import prisma from "@/lib/prisma";
import json2csv from 'json2csv';
import { getServerSession } from "next-auth";
import { redirect } from "next/navigation";
import { NextRequest, NextResponse } from "next/server";

export async function GET(req: NextRequest) {
try {
const session = await getServerSession(authOptions);
if (!session || !session.user) {
redirect("/login");
}
const datasetId = req.nextUrl.searchParams.get("id") as string;
const pageParam = req.nextUrl.searchParams.get("page");
let page = pageParam ? parseInt(pageParam, 10) : 1;
const pageSize = 500;
let dataset;
if (!datasetId) {
return NextResponse.json(
{
message: "No dataset id provided",
},
{ status: 404 }
);
}
else {
dataset = await prisma.dataset.findFirst({
where: {
id: datasetId,
},
include: {
Data: true,
},
});

}
if (!dataset) {
return NextResponse.json(
{
message: "No datasets found",
},
{ status: 404 }
);
}

const data = await prisma.data.findMany({
where: {
datasetId: dataset.id,
},
orderBy: {
createdAt: "desc",
},
take: pageSize,
skip: (page - 1) * pageSize,
});

const csv = json2csv.parse(data);
const datasetName = dataset.name.toLowerCase().replace(/\s+/g, '_');
const timestamp = new Date().toISOString().slice(0, 19).replace(/[-:]/g, '');
const filename = `${datasetName}_${timestamp}.csv`;

console.log(`CSV file '${filename}' `);

return new NextResponse(csv, {
headers: {
'Content-Type': 'text/csv',
'Content-Disposition': `filename:${filename}`,
},
});
} catch (error) {
return NextResponse.json(
{
message: "Error downloading dataset",
},
{ status: 500 }
);
}
}
99 changes: 99 additions & 0 deletions components/shared/download-dataset.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
"use client";

import { DownloadIcon } from "lucide-react";

import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import { useState } from 'react';
import { toast } from "sonner";

export function DownloadDataset({
datasetId,
disabled=false,
}: {
datasetId: string;
disabled?: boolean;

}) {
const [open, setOpen] = useState(false);
const [busy, setBusy] = useState(false);
const handleDownload = async () => {
setBusy(true);
try {
datasetId = datasetId.toString();
const response = await fetch(`/api/dataset/download?id=${datasetId}`, {
method: "GET",
headers: {
"Content-Type": "text/csv",
},
});

if (!response.ok) {
throw new Error("Failed to download dataset.");
}

// Extract filename from Content-Disposition header
const contentDisposition = response.headers.get('Content-Disposition');

let filename;
if (contentDisposition) {
const filenameKeyValue = contentDisposition.split(':')[1].split('.');

if (filenameKeyValue.length === 2) {
filename = filenameKeyValue[0];
}
}
// Initiate file download
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = filename?.toString() || "dataset.csv";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
window.URL.revokeObjectURL(url);

setBusy(false);
setOpen(false);
toast.success("Dataset downloaded successfully!");
} catch (error) {
toast.error("Failed to download dataset.");
setBusy(false);
}
};

return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button size={'icon'} variant={'outline'} disabled={disabled}>
<DownloadIcon className="h-4 w-4 shrink-0" />
</Button>
</DialogTrigger>
<DialogContent className="sm:max-w-[425px]">
<DialogHeader>
<DialogTitle>Download Dataset</DialogTitle>
<DialogDescription>
This will download the data as .csv and only up to a maximum of 500 records. To download the entire dataset, please contact us.
</DialogDescription>
</DialogHeader>
<DialogFooter>
<Button variant={'outline'} onClick={() => setOpen(false)} disabled={busy}>
Cancel
</Button>
<Button disabled={busy} onClick={handleDownload}>
Download
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}
46 changes: 46 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"groq-sdk": "^0.3.3",
"i": "^0.3.7",
"js-tiktoken": "^1.0.10",
"json2csv": "^6.0.0-alpha.2",
"langchain": "^0.1.20",
"lucide-react": "^0.323.0",
"mammoth": "^1.7.0",
Expand Down Expand Up @@ -104,6 +105,7 @@
"@tailwindcss/forms": "^0.5.7",
"@types/diff": "^5.2.0",
"@types/formidable": "^3.4.5",
"@types/json2csv": "^5.0.7",
"@types/markdown-it": "^13.0.7",
"@types/node": "^20.11.10",
"@types/node-fetch": "^2.6.11",
Expand Down

0 comments on commit 4e30354

Please sign in to comment.