This repository has been archived by the owner on Jun 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
258 lines (228 loc) · 8.07 KB
/
index.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
const { Pool } = require("pg");
const { PostgresError: PGError } = require("pg-error-enum");
const { parse: parsePGArray } = require("postgres-array");
const { Server: IO } = require("socket.io");
require("dotenv").config();
const path = require("path");
const express = require("express");
const paginate = require("express-paginate");
const nodeEnv = process.env.NODE_ENV || "development";
const morgan = require("morgan");
const bodyParser = require("body-parser");
const app = express();
const port = process.env.PORT || 3000;
const pool = new Pool({
host: process.env.DATABASE_HOST || "localhost",
user: process.env.DATABASE_USER || "cr_labeling_admin",
database: process.env.DATABASE_NAME || "cr_labeling",
password: process.env.DATABASE_PASS || undefined,
port: process.env.DATABASE_PORT || 5432,
max: 5
});
app.use(morgan( nodeEnv === "development" ? "dev" : "combined" ));
app.set("views", "./views");
app.set("view engine", "ejs");
app.use("/", express.static(path.join(__dirname, "public")));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(paginate.middleware(10, 50));
app.get("/", (req, res) => {
res.render("index");
});
app.get("/login", async (_, res) => {
const { rows: reviewers } = await pool.query("SELECT * FROM reviewer");
res.render("login", { reviewers: reviewers });
});
app.post("/login", async (req, res) => {
const { rows: [ reviewer ] } = await pool.query(
"SELECT * FROM reviewer WHERE id = $1 LIMIT 1",
[ req.body.id ]
);
const target = (reviewer)
? `${req.baseUrl}/${reviewer.name}/queue`
: `${req.baseUrl}/login`;
res.redirect(target);
});
app.get("/:name/queue", async (req, res) => {
const { rows: labels } = await pool.query("SELECT * FROM label ORDER BY name");
const { rows: [ reviewer ] } = await pool.query(
"SELECT * FROM instance_review_progress WHERE name = $1 LIMIT 1",
[ req.params.name ]
);
const { rows: [ instance ] } = await pool.query(
"SELECT * FROM next_instance($1)",
[ reviewer?.id || null ]
);
res.render("review", {
reviewer: reviewer,
instance: instance,
labels: labels
});
});
app.get("/progress", async (req, res) => {
const { rows: buckets } = await pool.query("SELECT * FROM instance_review_bucket");
const { rows: reviewers } = await pool.query("SELECT * FROM instance_review_progress");
res.render("progress", {
buckets: buckets,
reviewers: reviewers
});
});
app.post("/instance/submit", async (req, res) => {
const payload = req.body;
const parameters = [
payload.instance_id,
payload.reviewer_id,
payload.is_interesting,
payload.invert_category,
payload.remarks
];
const { rows: [ { id: instance_review_id } ] } = await pool.query(
`INSERT INTO instance_review(instance_id, reviewer_id, is_interesting, invert_category, remarks)
VALUES ($1, $2, $3, $4, $5) RETURNING id`,
parameters
);
payload.label_ids.forEach(label_id => {
pool.query(
"INSERT INTO instance_review_label(label_id, instance_review_id) VALUES ($1, $2)",
[ label_id, instance_review_id ]
);
});
res.status(200).end();
});
app.post("/instance/discard", (req, res) => {
const payload = req.body;
const parameters = [ payload.instance_id, payload.reviewer_id, payload.remarks ];
pool.query(
`INSERT INTO instance_discard(instance_id, reviewer_id, remarks)
VALUES ($1, $2, $3)`,
parameters
).then(() => res.status(200).end());
});
app.get("/label", async (_, res) => {
const { rows: labels } = await pool.query("SELECT * FROM label ORDER BY name");
res.status(200).send(labels);
});
app.post("/label", (req, res) => {
pool.query("INSERT INTO label(name) VALUES ($1) RETURNING *", [ req.body.name ])
.then(({ rows }) => { res.status(201).send(rows[0]); })
.catch(({ code }) => {
const status = code === PGError.UNIQUE_VIOLATION ? 409 : 500;
res.status(status).end();
});
});
app.get("/instances", async (req, res) => {
const current = req.query.page || 1;
const limit = req.query.limit || 10;
const id = req.query.id || 1;
const order = (id > 0) ? "ASC" : "DESC";
const query = `SELECT id FROM instance ORDER BY id ${order} OFFSET $1 LIMIT $2`;
const { rows: instances } = await pool.query(query, [ (current - 1) * limit, limit ]);
const { rows: [ { count: items } ] } = await pool.query("SELECT COUNT(id) FROM instance");
const pages = Math.ceil(items / req.query.limit);
res.render("instances", {
instances: instances,
pagination: {
items: items,
pages: pages,
current: current,
limit: limit,
id: id
}
});
});
app.get("/instances/:id", async (req, res) => {
const params = [ req.params.id ];
const { rows: [ instance ] } = await pool.query("SELECT * FROM instance WHERE id = $1", params);
if (!instance) {
res.render("error", {
icon: "bi-none",
title: "Instance does not exist!"
});
} else {
res.render("instance", { instance: instance });
}
});
app.get("/conflicts", async (req, res) => {
const current = req.query.page || 1;
const limit = req.query.limit || 10;
const id = req.query.id || 1;
const order = (id > 0) ? "ASC" : "DESC";
const query = `SELECT id FROM instance_review_conflict ORDER BY id ${order} OFFSET $1 LIMIT $2`;
const { rows: conflicts } = await pool.query(query, [ (current - 1) * limit, limit ]);
const { rows: [ { count: items } ] } = await pool.query(
"SELECT COUNT(id) FROM instance_review_conflict"
);
const pages = Math.ceil(items / req.query.limit);
res.render("conflicts", {
conflicts: conflicts,
pagination: {
items: items,
pages: pages,
current: current,
limit: limit,
id: id
}
});
});
app.get("/conflicts/:id", async (req, res) => {
const params = [ req.params.id ];
const { rows: [ instance ] } = await pool.query("SELECT * FROM instance_review_conflict WHERE id = $1", params);
if (!instance) {
res.render("error", {
icon: "bi-none",
title: "Conflict not found or already resolved!"
});
} else {
instance.conflicts = parsePGArray(instance.conflicts);
const { rows: reviews } = await pool.query("SELECT * FROM instance_review_details($1)", params);
const { rows: discards } = await pool.query("SELECT * FROM instance_discard_details($1)", params);
const { rows: labels } = await pool.query("SELECT * FROM label ORDER BY name");
res.render("conflict", {
instance: instance,
reviews: reviews,
discards: discards,
labels: labels
});
}
});
app.post("/conflicts/:id/review", (req, res) => {
const params = [
req.params.id,
req.body.conflicts,
req.body.label_ids,
req.body.is_interesting,
req.body.invert_category,
req.body.remarks
];
pool.query("CALL conflict_resolution_review($1, $2, $3, $4, $5, $6)", params)
.then(() => res.status(201).end());
});
app.post("/conflicts/:id/discard", (req, res) => {
const params = [
req.params.id,
req.body.conflicts,
req.body.remarks
];
pool.query("CALL conflict_resolution_discard($1, $2, $3)", params)
.then(() => res.status(201).end());
});
app.get("/error", (_, res) => {
res.render("error");
});
app.get("*", (_, res) => {
res.render("error", {
icon: "bi-emoji-dizzy-fill",
title: "Page Not Found!"
});
});
const server = app.listen(port, () => {
if (nodeEnv === "development") {
console.log(`App listening on: http://localhost:${port}`);
}
});
const io = new IO(server);
io.on("connection", (socket) => {
socket.on("label_added", (label) => {
socket.broadcast.emit("label_append", label);
});
});