-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.js
executable file
·345 lines (304 loc) · 8.99 KB
/
backend.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
#!/usr/bin/env node
/* this is a minimalistic, all inclusive, backend that implements basic APIs */
const express = require('express');
const cors = require('cors');
const _ = require('lodash');
const moment = require('moment');
const MongoClient = require('mongodb').MongoClient;
const mongoServer = `mongodb://127.0.0.1:27017/faces`;
async function MongoRead(filter) {
const client = new MongoClient(mongoServer);
await client.connect();
const retv = await client
.db()
.collection('meps')
.find(filter)
.toArray();
await client.close();
return retv;
}
async function mepStats() {
const client = new MongoClient(mongoServer);
await client.connect();
const retv = await client
.db()
.collection('meps')
.aggregate([{
'$group': {
'_id': '$nation',
'counter': {
'$sum': 1
}
}
}])
.toArray();
await client.close();
return retv;
}
async function queryMEPs(filter) {
const client = new MongoClient(mongoServer);
await client.connect();
const retv = await client
.db()
.collection('meps')
.aggregate([
{ $match: filter },
{
$lookup: {
from: 'analysis',
localField: 'id',
foreignField: 'id',
as: 'facerec',
}
}
])
.toArray();
await client.close();
return retv;
}
const app = express();
const PORT = 2023; // the year of AIAct
app.listen(PORT, () => {
console.log(`Binded sucessfully port ${PORT}`);
});
app.get('/api/meps/:filter?', cors(), async function (req, res) {
let filter = {};
try {
filter = JSON.parse(req.params.filter);
} catch (error) {
if (req.params?.filter && req.params.filter.length > 1) {
console.log(`Unable to extract a valid filter: ${error.message}`);
}
}
const data = await MongoRead(filter);
console.log(`→ MEPs API fetched ${data.length} with filter ${JSON.stringify(filter)}, returns ${JSON.stringify(_.countBy(data, 'nation'))}`);
res.json(data);
});
app.get('/api/stats', cors(), async function (req, res) {
try {
const rv = await mepStats();
res.json(rv);
} catch (error) {
console.log(`Stats error: ${error.message}`);
res.json({ error: true, message: error.message });
}
});
app.get('/api/details/:memberState', cors(), async function (req, res) {
try {
const ms = req.params.memberState;
const rv = await queryMEPs({ nation: ms });
const fixed = _.map(rv, function (mep) {
const o = _.omit(mep, ['_id']);
o.facerec = _.omit(_.first(mep.facerec), ['_id', 'id']);
o.facerec.expressions = _.reduce(o.facerec.expressions, function (memo, val, emo) {
const effective = _.round(val * 100, 1);
if (effective < 1)
return memo;
memo.push({
emotion: emo,
value: effective
})
return memo;
}, []);
return o;
});
res.json(fixed);
} catch (error) {
console.log(`Details for ${req.params.memberState} error: ${error.message}`);
res.json({ error: true, message: error.message });
}
});
app.get('/api', cors(), function (req, res) {
const implemented = [
"/api/meps/:filter?", "/api/homepage", "/api/squared",
"/api/stats", "/api/details/:memberState", "/api/emotional",
"/api/individual/:id", "/api/group/:name"];
res.status(200);
res.send(`
<code>dontspyonus.eu</code><hr/>
<h3>Implemented APIs</h3>
<pre>
${implemented.join("\n\r")}
</pre>`
);
});
app.get('/api/group/:name', cors(), async (req, res) => {
/* acquire from req.params.name the name which is URL encoded
* and should be decoded in the variable 'name' */
const name = decodeURIComponent(req.params.name);
console.log(`Group API called with ${name}`);
const data = await queryMEPs({ group: name });
if(!data.length)
throw new Error(`Unable to find group ${name}`);
const ready = _.map(data, function (mep) {
const o = _.omit(mep, ['_id']);
o.facerec = _.omit(_.first(mep.facerec), ['_id', 'id']);
o.facerec.expressions = _.reduce(o.facerec.expressions, function (memo, val, emo) {
const effective = _.round(val * 100, 1);
if (effective < 1)
return memo;
memo.push({
emotion: emo,
value: effective
})
return memo;
}, []);
return o;
});
const ordered = _.reverse(_.sortBy(ready, function (mep) {
return mep.facerec.expressions.length
}))
console.log(`Group API returning ${ready.length}`);
res.status(200);
res.json(ordered);
})
app.get('/api/emotionals', cors(), async (req, res) => {
try {
const data = await queryMEPs({});
/* neutral, happy, sad, angry,
fearful, disgusted, surprised */
const structure = {
sad: [],
angry: [],
fearful: [],
disgusted: [],
surprised: []
};
/* in the retval we ignore the first two emotions */
const emotionals = _.reduce(data, (memo, mep) => {
if (!mep.facerec.length)
return memo;
const considered = _.keys(structure);
_.each(considered, function (emotion) {
const value = _.round(mep.facerec[0].expressions[emotion] * 100, 1);
if (value > 10)
memo[emotion].push({
id: mep.id,
name: mep.name,
nation: mep.nation,
value
})
})
return memo;
}, structure);
console.log(`Returning the emotion list`);
res.json(emotionals);
} catch (error) {
res.status(500);
console.log(`Error: ${error.message}`);
res.json({
error: true,
message: error.message
})
}
});
app.get('/api/list', cors(), async (req, res) => {
/* pick all the MEPs and return them in a CSV format,
* with the following fields:
* id, name, nation, group, party, twitter */
const flist = ['id', 'name', 'nation', 'group', 'party', 'twitter'];
try {
const data = await queryMEPs({});
const selected = _.map(data, function (mep) {
return _.pick(mep, flist);
});
const textual = _.reduce(selected, function (memo, mep) {
memo.push(_.values(mep).join(","));
return memo;
}, [ flist.join(",") ] );
console.log(`List API returning ${textual.length}`);
res.status(200);
/* set the header to make the CSV download with name 'list.csv' */
res.header('Content-Disposition', 'attachment; filename="list.csv"');
res.header('Content-Type', 'text/plain');
res.send(textual.join("\n"));
}
catch (error) {
res.status(500);
console.log(`Error: ${error.message}`);
res.json({
error: true,
message: error.message
});
}
});
app.get('/api/individual/:idList', cors(), async (req, res) => {
/* pick up the MEP by id and transform the data in the same
* format as we do for the group API */
try {
const ids = req.params.idList.split(',');
const data = await queryMEPs({ id: { "$in": ids } });
if (!data.length)
throw new Error(`Unable to find MEP with id ${req.params.idList}`);
const ready = _.map(data, function (mep) {
const o = _.omit(mep, ['_id']);
o.facerec = _.omit(_.first(mep.facerec), ['_id', 'id']);
o.facerec.expressions = _.reduce(o.facerec.expressions, function (memo, val, emo) {
const effective = _.round(val * 100, 1);
if (effective < 1)
return memo;
memo.push({
emotion: emo,
value: effective
})
return memo;
}, []);
return o;
});
console.log(`Individual API returning ${ready.length} meps`);
res.status(200);
res.json(ready);
} catch(error) {
res.status(500);
console.log(`Error: ${error.message}`);
res.json({
error: true,
message: error.message
});
}
});
app.get('/api/squared', cors(), async function (req, res) {
try {
const data = await queryMEPs({});
const ready = _.compact(_.map(data, function (mep) {
// console.log(mep?.facerec[0]?.box);
if (typeof mep?.facerec[0]?.box !== typeof [])
return null;
if (mep.facerec[0].box[0] > 30)
return null;
const o = _.omit(mep, ['_id', 'urlimg']);
o.facerec = _.omit(_.first(o.facerec), ['_id', 'id']);
return o;
}));
console.log(`Squared API returning ${ready.length}`);
res.json(ready);
} catch (error) {
res.status(500);
console.log(`Squared API error: ${error.message}`);
res.json({
error: true,
message: error.message
})
}
});
app.get('/api/homepage', cors(), async function (req, res) {
/* at the moment homepage returns random */
try {
const amount = 40;
const retv = await queryMEPs({});
const random40 = _.times(amount, function (i) {
const o = _.omit(_.sample(retv), ['_id', 'urlimg']);
o.facerec = _.omit(_.first(o.facerec), ['_id', 'id']);
return o;
});
console.log(`Homepage API returning a random sample of ${random40.length}`);
res.json(_.chunk(random40, 4));
} catch (error) {
res.status(500);
console.log(`Homepage API error: ${error.message}`);
res.json({
error: true,
message: error.message
})
}
});