-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
346 lines (300 loc) · 7.95 KB
/
server.js
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/**
* Backend Server to serve the REST API endpoints for the CodeSummarizer web application
*/
import supabaseClient from '@supabase/supabase-js';
import express, { request } from "express";
import cors from 'cors';
import * as Summarizer from './summarizer.js';
const app = express();
const supabase = supabaseClient.createClient();
const PORT = process.env.PORT || 4000; // Use either port 4000 or the environment port
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
app.use(cors({
origin: 'http://localhost:4000'
}));
app.use(express.json());
/**
* Tests the status of the backend server
* @return JSON { status: String }
*/
app.get("/status", (request, response) => {
response.send({
"Status": "Running"
});
});
/**
* Add a new user
* @param username: String
* @param admin: 'TRUE' || 'FALSE'
* @return Success or error message
*/
app.post('/add_user', async (req, res) => {
const { error } = await supabase
.from('Users')
.insert({
username: req.query.username,
admin: req.query.admin
});
if (error) {
res.send(error);
} else {
res.send("User Successfully Created");
}
});
/**
* Removes a user
* @param username: String
* @return Success or error message
*/
app.delete('/remove_user', async (req, res) => {
const { error } = await supabase
.from('Users')
.delete()
.eq('username', req.query.username);
if (error) {
res.send(error);
} else {
res.send("User Successfully Deleted");
}
});
/**
* Get account information on a given user
* @param username: String
* @return JSON {
* username: String,
* creation_date: Date String,
* admin: Boolean
* }
*/
app.get('/user_information', async (req, res) => {
const { data, error } = await supabase
.from('Users')
.select('username, creation_date, admin')
.eq('username', req.query.username);
if (error) {
res.send(error);
} else {
res.send(data);
}
});
/**
* Get account information for all users
* TODO Security measures?
* @return JSON [{
* username: String,
* creation_date: Date String,
* admin: Boolean
* }]
*/
app.get('/all_user_information', async (req, res) => {
const { data, error } = await supabase
.from('Users')
.select('username, creation_date, admin');
if (error) {
res.send(error);
} else {
res.send(data);
}
});
/**
* Get all the summary requests for a given user
* @param username: String
* @return JSON [{
* request_id: Int,
* creation_date: Date String,
* prompt: String,
* username: String,
* programming_language: String
* title: String,
* description: String
* }]
*/
app.get('/get_user_requests', async (req, res) => {
const { data, error } = await supabase
.from('Requests')
.select('request_id, creation_date, prompt, username, programming_language, title, description')
.eq("username", req.query.username);
if (error) {
res.send(error);
} else {
res.send(data);
}
});
/**
* Get all the responses for a user's given request
* @param request_id: Int
* @return JSON [{
* response_id: Int,
* request_id: Int,
* text: String,
* category: String,
* rating: Int,
* creation_date: Date String
* }]
*/
app.get('/get_responses', async (req, res) => {
const { data, error } = await supabase
.from('Responses')
.select('response_id, request_id, text, category, rating, creation_date')
.eq("request_id", req.query.request_id);
if (error) {
res.send(error);
} else {
res.send(data);
}
});
import multer from 'multer';
const upload = multer({ storage: multer.memoryStorage() }); // Use memory storage to handle the file as a buffer
/**
* Submit a request for a new summary
* @param username: String
* @param prompt: String
* @param programming_language: String
* @param title: String
* @param description: String
* @return JSON [{
* request_id: Int,
* response_id: Int,
* text: String,
* category: String
* }]
*/
app.post('/submit_request', upload.single('prompt'), async (req, res) => {
if (!req.file) {
return res.status(400).send('No file uploaded.');
}
// Convert buffer to string
const fileContents = req.file.buffer.toString('utf8');
const { username, programming_language, title, description } = req.body;
// console.log(fileContents)
// console.log(username, programming_language, title, description)
const { data, error } = await supabase
.from('Requests')
.insert({
prompt: fileContents, // Store the file contents as a string
username: username,
programming_language: programming_language,
title: title,
description: description,
}).select();
if (error) {
res.send(error);
}
const request_data = data;
const request_id = data[0].request_id;
if (fileContents.length > 0) { // && fileContents[0].request_id != undefined) {
// Get the list of responses and their respective catagories
// TODO: Make the programming language part work
const responses = await Summarizer.getSummaries(fileContents, programming_language);
// Add the request id to each response object
responses.forEach(response => {
response["request_id"] = request_id;
});
// Log the responses
const { data, error } = await supabase
.from("Responses")
.insert(responses)
.select();
if (error) {
res.send(error);
return;
}
// Return the response information
res.send({
request: request_data,
responses: data
});
} else {
res.send("Failed to log request");
}
});
/**
* Submit a rating for a summary response
* @param response_id: Int,
* @param rating: Int
* @return Success or error message
*/
app.post('/rate_response', async (req, res) => {
const { response_id, rating } = req.body;
const { error } = await supabase
.from('Responses')
.update({ 'rating': rating })
.eq("response_id", response_id);
if (error) {
res.status(500).json({ error: error.message });
} else {
res.json({ message: "Rating Succeeded" });
}
});
/**
* Get the summary, category, and rating statistics for a given user
* @param username: String
* @return JSON {
* programming_language_counts: {
* language_name: usage_count...
* }, topic_average_scores: {
* topic_name: topic_count...
* }
* }
*/
app.get('/user_statistics', async (req, res) => {
const languages = await supabase
.from('Requests')
.select('programming_language');
if (languages.error) {
res.send(languages.error);
}
const topics = await supabase
.from('Responses')
.select('category, rating');
const language_counter = {};
if (languages.data != null) {
languages.data.forEach(ele => {
if (language_counter[ele.programming_language]) {
language_counter[ele.programming_language] += 1;
} else {
language_counter[ele.programming_language] = 1;
}
});
}
const topics_counter = {};
if (topics.data != null) {
topics.data.forEach(ele => {
if (topics_counter[ele.category]) {
topics_counter[ele.category] += 1;
} else {
topics_counter[ele.category] = 1;
}
});
}
const topics_average_score = {};
if (topics.data != null) {
topics.data.forEach(ele => {
if (topics_average_score[ele.category]) {
topics_average_score[ele.category] += ele.rating;
} else {
topics_average_score[ele.category] = ele.rating;
}
});
}
for (const [key, value] of Object.entries(topics_average_score)) {
topics_average_score[key] = value / topics_counter[key];
}
if (languages.error) {
res.send(languages.error);
} else if (topics.error) {
res.send(topics.error);
} else {
res.send({
programming_language_counts: language_counter,
topics_counts: topics_counter,
topic_average_scores: topics_average_score,
});
}
});
/**
* Get the summary, category, and rating statistics for all users combined
* @return JSON TBD
*/
app.get('/combined_statistics', async (req, res) => {
});