Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement datasets data model #137

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions apps/web/src/actions/datasets/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ import { z } from 'zod'

import { authProcedure } from '../procedures'

const ACCEPTED_FILE_TYPES = [
'text/csv',
'application/vnd.ms-excel',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/vnd.oasis.opendocument.spreadsheet',
]
const MAX_SIZE = 3
Copy link
Collaborator

@geclos geclos Sep 6, 2024

Choose a reason for hiding this comment

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

3mb only? that's very small, i'd push it to 25 or so

Copy link
Contributor Author

Choose a reason for hiding this comment

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

25mb of text?

Copy link
Collaborator

Choose a reason for hiding this comment

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

well the theory is that they want to evaluate on a large body of data no?, 3mb doesn't push our infra hard at all and it can be limiting for no reason

Copy link
Contributor Author

Choose a reason for hiding this comment

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

25 then. But node server will handle it?

We'll have to do this https://nextjs.org/docs/app/api-reference/next-config-js/serverActions#bodysizelimit

Although the best approach would be to direct upload to S3 and then process the file when S3 responds. But we need to change how upload is done

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll start with 15MB and see if someone complains

const MAX_UPLOAD_SIZE_IN_MB = 3 * 1024 * 1024

Expand All @@ -25,22 +19,23 @@ export const createDatasetAction = authProcedure
.refine((file) => {
return !file || file.size <= MAX_UPLOAD_SIZE_IN_MB
}, `Your dataset must be less than ${MAX_SIZE}MB in size`)
.refine((file) => {
return ACCEPTED_FILE_TYPES.includes(file.type)
}, 'Your dataset must be an Excel or CSV file'),
.refine(
(file) => file.type === 'text/csv',
'Your dataset must be a CSV file',
),
}),
{ type: 'formData' },
)
.handler(async ({ input, ctx }) => {
const result = await createDataset({
return createDataset({
workspace: ctx.workspace,
author: ctx.user,
disk: disk,
data: {
name: input.name,
file: input.dataset_file,
// TODO: Make UI radio button to pick delimiter
csvDelimiter: ';',
},
})

return result.unwrap()
}).then((r) => r.unwrap())
})
23 changes: 23 additions & 0 deletions apps/web/src/actions/datasets/destroy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use server'

import { DatasetsRepository } from '@latitude-data/core/repositories'
import { destroyDataset } from '@latitude-data/core/services/datasets/destroy'
import disk from '$/lib/disk'
import { z } from 'zod'

import { authProcedure } from '../procedures'

export const destroyDatasetAction = authProcedure
.createServerAction()
.input(
z.object({
id: z.string(),
}),
)
.handler(async ({ input, ctx }) => {
const { id } = input
const repo = new DatasetsRepository(ctx.workspace.id)
const dataset = await repo.find(id).then((r) => r.unwrap())

return await destroyDataset({ dataset, disk }).then((r) => r.unwrap())
})
12 changes: 12 additions & 0 deletions apps/web/src/actions/datasets/fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use server'

import { DatasetsRepository } from '@latitude-data/core/repositories'

import { authProcedure } from '../procedures'

export const getDatasetsAction = authProcedure
.createServerAction()
.handler(async ({ ctx }) => {
const scope = new DatasetsRepository(ctx.workspace.id)
return await scope.findAll().then((r) => r.unwrap())
})
2 changes: 1 addition & 1 deletion apps/web/src/app/(private)/dashboard/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default async function DashboardLayout({
return (
<AppLayout
navigationLinks={NAV_LINKS}
currentUser={{ ...user }}
currentUser={user}
breadcrumbs={breadcrumbs}
sectionLinks={MAIN_NAV_LINKS}
>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
'use client'

import { useState } from 'react'

import { Dataset } from '@latitude-data/core/browser'
import {
Button,
dateFormatter,
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
Text,
} from '@latitude-data/web-ui'
import DeleteDatasetModal from '$/app/(private)/datasets/_components/DeleteDatasetModal'
import useDatasets from '$/stores/datasets'

export function DatasetsTable({
datasets: serverDatasets,
}: {
datasets: Dataset[]
}) {
const [deletable, setDeletable] = useState<Dataset | null>(null)
const { data: datasets } = useDatasets(undefined, {
fallbackData: serverDatasets,
})
return (
<>
<DeleteDatasetModal dataset={deletable} setDataset={setDeletable} />
<Table>
<TableHeader>
<TableRow verticalPadding>
<TableHead>Name</TableHead>
<TableHead>Rows</TableHead>
<TableHead>Columns</TableHead>
<TableHead>Author</TableHead>
<TableHead>Created at</TableHead>
<TableHead />
</TableRow>
</TableHeader>
<TableBody>
{datasets.map((dataset) => (
<TableRow key={dataset.id} verticalPadding>
<TableCell>
<Text.H4>{dataset.name}</Text.H4>
</TableCell>
<TableCell>
<Text.H4>{dataset.fileMetadata.rowCount}</Text.H4>
</TableCell>
<TableCell>
<Text.H4>{dataset.fileMetadata.headers.length}</Text.H4>
</TableCell>
<TableCell>
<Text.H4>{dataset.author?.name}</Text.H4>
</TableCell>
<TableCell>
<Text.H4 color='foregroundMuted'>
{dateFormatter.formatDate(dataset.createdAt)}
</Text.H4>
</TableCell>
<TableCell align='center'>
<Button
onClick={() => setDeletable(dataset)}
variant='nope'
iconProps={{ name: 'trash', color: 'destructive' }}
/>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Dataset } from '@latitude-data/core/browser'
import { ReactStateDispatch } from '@latitude-data/web-ui'
import { destroyDatasetAction } from '$/actions/datasets/destroy'
import DestroyModal from '$/components/modals/DestroyModal'
import useDatasets from '$/stores/datasets'
import { useRouter } from 'next/navigation'

export default function DeleteDatasetModal({
dataset,
setDataset,
}: {
dataset: Dataset | null
setDataset: ReactStateDispatch<Dataset | null>
}) {
const router = useRouter()
const { data, destroy } = useDatasets()
const isLast = data?.length === 1
if (!dataset) return null

return (
<DestroyModal<typeof destroyDatasetAction>
title={`Delete ${dataset.name}`}
description='Deleted datasets will no longer accessible to generate new evaluations.'
onOpenChange={(open: boolean) => !open && setDataset(null)}
action={destroy}
submitStr='Delete dataset'
model={dataset}
onSuccess={() => {
if (isLast) router.refresh()

setDataset(null)
}}
/>
)
}
33 changes: 22 additions & 11 deletions apps/web/src/app/(private)/datasets/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { ReactNode } from 'react'

import { DatasetsRepository } from '@latitude-data/core/repositories'
import {
Container,
TableBlankSlate,
TableWithHeader,
Text,
} from '@latitude-data/web-ui'
import { DatasetsTable } from '$/app/(private)/datasets/_components/DatasetsTable'
import { AppLayout } from '$/components/layouts'
import { getCurrentUser } from '$/services/auth/getCurrentUser'
import { ROUTES } from '$/services/routes'
Expand All @@ -19,11 +21,12 @@ export default async function DatasetsList({
children: ReactNode
}>) {
const { workspace, user } = await getCurrentUser()

const scope = new DatasetsRepository(workspace.id)
const datasets = await scope.findAll().then((r) => r.unwrap())
return (
<AppLayout
navigationLinks={NAV_LINKS}
currentUser={{ ...user }}
currentUser={user}
sectionLinks={MAIN_NAV_LINKS}
breadcrumbs={[
{
Expand All @@ -43,15 +46,23 @@ export default async function DatasetsList({
<TableWithHeader.Button>Upload dataset</TableWithHeader.Button>
</Link>
}
/>
<TableBlankSlate
description='There are no datasets yet. Create one to start testing your prompts.'
link={
<Link href={ROUTES.datasets.new.root}>
<TableBlankSlate.Button>
Create your first dataset
</TableBlankSlate.Button>
</Link>
table={
<>
{datasets.length > 0 ? (
<DatasetsTable datasets={datasets} />
) : (
<TableBlankSlate
description='There are no datasets yet. Create one to start testing your prompts.'
link={
<Link href={ROUTES.datasets.new.root}>
<TableBlankSlate.Button>
Create your first dataset
</TableBlankSlate.Button>
</Link>
}
/>
)}
</>
}
/>
</Container>
Expand Down
17 changes: 9 additions & 8 deletions apps/web/src/app/(private)/datasets/new/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import {
FormWrapper,
Input,
Modal,
// useToast,
} from '@latitude-data/web-ui'
import { createDatasetAction } from '$/actions/datasets/create'
import useLatitudeAction from '$/hooks/useLatitudeAction'
import { useNavigate } from '$/hooks/useNavigate'
import { ROUTES } from '$/services/routes'
import useDatasets from '$/stores/datasets'

export default function NewDataset() {
const data = { name: '' }
const navigate = useNavigate()
const { error, executeFormAction } = useLatitudeAction(createDatasetAction)
const errors = error?.fieldErrors
const { createError, createFormAction } = useDatasets({
onCreateSuccess: () => navigate.push(ROUTES.datasets.root),
})
const errors = createError?.fieldErrors
return (
<Modal
open
Expand All @@ -37,7 +37,7 @@ export default function NewDataset() {
<form
className='min-w-0'
id='createDatasetForm'
action={executeFormAction}
action={createFormAction}
>
<FormWrapper>
<Input
Expand All @@ -49,12 +49,13 @@ export default function NewDataset() {
placeholder='Amazing dataset'
/>
<DropzoneInput
multiple={false}
accept='.csv'
label='Upload dataset'
name='dataset_file'
errors={errors?.dataset_file}
placeholder='Upload csv'
multiple={false}
accept='.csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel'
description='The first line of the uploaded .csv will be used as headers. The delimiter symbol must be ";"'
/>
</FormWrapper>
</form>
Expand Down
40 changes: 19 additions & 21 deletions apps/web/src/components/layouts/AppLayout/Header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ export default function AppHeader({
const pathname = usePathname()
const info = currentUser ? getUserInfoFromSession(currentUser) : null
return (
<header className='sticky top-0 flex flex-col bg-background-gray border-b border-b-border'>
<div className='px-6 py-3 flex flex-row items-center justify-between border-b border-b-border'>
<header className='px-6 sticky top-0 flex flex-col bg-background-gray border-b border-b-border'>
<div className='py-3 flex flex-row items-center justify-between border-b border-b-border'>
<Breadcrump breadcrumbs={breadcrumbs} />
<div className='flex flex-row items-center gap-x-6'>
<nav className='flex flex-row gap-x-4'>
Expand All @@ -126,28 +126,26 @@ export default function AppHeader({
</div>
</div>
{sectionLinks.length > 0 ? (
<div className='px-6'>
<NavTabGroup>
{sectionLinks.map((link, idx) => {
let { href, label, index } = link
href = href?.startsWith('/') ? href : `/${href}`
<NavTabGroup>
{sectionLinks.map((link, idx) => {
let { href, label, index } = link
href = href?.startsWith('/') ? href : `/${href}`

const selected = href
? index
? pathname === href
: pathname?.startsWith(href)
: false
const selected = href
? index
? pathname === href
: pathname?.startsWith(href)
: false

if (!href) return null
if (!href) return null

return (
<Link href={href} key={idx}>
<NavTabItem label={label} selected={selected} />
</Link>
)
})}
</NavTabGroup>
</div>
return (
<Link href={href} key={idx}>
<NavTabItem label={label} selected={selected} />
</Link>
)
})}
</NavTabGroup>
) : null}
</header>
)
Expand Down
Loading
Loading