-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathTrainingDataset.controller.ts
More file actions
243 lines (211 loc) · 6.43 KB
/
TrainingDataset.controller.ts
File metadata and controls
243 lines (211 loc) · 6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import { Request, Response, NextFunction } from 'express'
import TrainingDatasetService from './TrainingDataset.service'
import * as multer from 'multer'
import * as path from 'path'
import * as os from 'os'
// Configure multer for file uploads
const uploadDir = path.join(os.tmpdir(), 'training-datasets')
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, uploadDir)
},
filename: (req, file, cb) => {
const timestamp = Date.now()
const random = Math.random().toString(36).substring(7)
cb(null, `${timestamp}-${random}-${file.originalname}`)
},
})
const upload = multer({
storage,
limits: { fileSize: 100 * 1024 * 1024 }, // 100MB max
fileFilter: (req, file, cb) => {
const allowedMimes = ['text/csv', 'application/json', 'text/markdown', 'text/plain']
const allowedExts = ['.csv', '.json', '.md', '.markdown', '.txt']
const ext = path.extname(file.originalname).toLowerCase()
const isMimeAllowed = allowedMimes.includes(file.mimetype)
const isExtAllowed = allowedExts.includes(ext)
if (isMimeAllowed || isExtAllowed) {
cb(null, true)
} else {
cb(new Error(`File type not supported. Allowed: ${allowedExts.join(', ')}`))
}
},
})
class TrainingDatasetController {
/**
* POST /api/v1/training-datasets/upload
* Upload and process a training dataset file
*/
async uploadDataset(req: Request, res: Response, next: NextFunction): Promise<void> {
try {
const userId = req.user?.id
if (!userId) {
res.status(401).json({ error: 'Unauthorized' })
return
}
if (!req.file) {
res.status(400).json({ error: 'No file provided' })
return
}
const { name, description } = req.body
if (!name) {
res.status(400).json({ error: 'Dataset name is required' })
return
}
// Determine file type from extension
const ext = path.extname(req.file.originalname).toLowerCase()
let fileType: 'csv' | 'json' | 'markdown' | 'text'
switch (ext) {
case '.csv':
fileType = 'csv'
break
case '.json':
fileType = 'json'
break
case '.md':
case '.markdown':
fileType = 'markdown'
break
case '.txt':
fileType = 'text'
break
default:
res.status(400).json({ error: 'Unsupported file type' })
return
}
// Create dataset record
const dataset = await TrainingDatasetService.createDataset(
userId,
req.file.originalname,
fileType,
req.file.size,
name,
description
)
// Process dataset asynchronously
TrainingDatasetService.processDataset(dataset._id.toString(), userId, req.file.path).catch(error => {
console.error('Error processing dataset:', error)
})
res.status(201).json({
success: true,
data: dataset,
message: 'Dataset uploaded successfully and is being processed',
})
} catch (error: any) {
console.error('Error uploading dataset:', error)
res.status(500).json({ error: error.message || 'Failed to upload dataset' })
}
}
/**
* GET /api/v1/training-datasets
* Get all datasets for a user
*/
async getDatasets(req: Request, res: Response, next: NextFunction): Promise<void> {
try {
const userId = req.user?.id
if (!userId) {
res.status(401).json({ error: 'Unauthorized' })
return
}
const { skip = 0, limit = 20 } = req.query
const result = await TrainingDatasetService.getDatasets(
userId,
parseInt(skip as string) || 0,
parseInt(limit as string) || 20
)
res.status(200).json({
success: true,
data: result.datasets,
pagination: {
skip: parseInt(skip as string) || 0,
limit: parseInt(limit as string) || 20,
total: result.total,
},
})
} catch (error) {
console.error('Error fetching datasets:', error)
res.status(500).json({ error: 'Failed to fetch datasets' })
}
}
/**
* GET /api/v1/training-datasets/:id
* Get dataset by ID
*/
async getDataset(req: Request, res: Response, next: NextFunction): Promise<void> {
try {
const userId = req.user?.id
const { id } = req.params
if (!userId) {
res.status(401).json({ error: 'Unauthorized' })
return
}
const dataset = await TrainingDatasetService.getDataset(id, userId)
if (!dataset) {
res.status(404).json({ error: 'Dataset not found' })
return
}
res.status(200).json({
success: true,
data: dataset,
})
} catch (error) {
console.error('Error fetching dataset:', error)
res.status(500).json({ error: 'Failed to fetch dataset' })
}
}
/**
* DELETE /api/v1/training-datasets/:id
* Delete dataset
*/
async deleteDataset(req: Request, res: Response, next: NextFunction): Promise<void> {
try {
const userId = req.user?.id
const { id } = req.params
const { deleteSamples = false } = req.query
if (!userId) {
res.status(401).json({ error: 'Unauthorized' })
return
}
const success = await TrainingDatasetService.deleteDataset(id, userId, deleteSamples === 'true')
if (!success) {
res.status(404).json({ error: 'Dataset not found' })
return
}
res.status(200).json({
success: true,
message: 'Dataset deleted successfully',
})
} catch (error) {
console.error('Error deleting dataset:', error)
res.status(500).json({ error: 'Failed to delete dataset' })
}
}
/**
* GET /api/v1/training-datasets/stats
* Get dataset statistics
*/
async getStatistics(req: Request, res: Response, next: NextFunction): Promise<void> {
try {
const userId = req.user?.id
if (!userId) {
res.status(401).json({ error: 'Unauthorized' })
return
}
const stats = await TrainingDatasetService.getStatistics(userId)
res.status(200).json({
success: true,
data: stats,
})
} catch (error) {
console.error('Error fetching statistics:', error)
res.status(500).json({ error: 'Failed to fetch statistics' })
}
}
/**
* Get multer upload middleware
*/
getUploadMiddleware() {
return upload.single('file')
}
}
export default new TrainingDatasetController()