-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'development' of github.com:Scale3-Labs/langtrace into d…
…evelopment
- Loading branch information
Showing
5 changed files
with
229 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters