From 67d59cedfe07a93aba8a02570b63fb486f440f0e Mon Sep 17 00:00:00 2001 From: "Engelbart Kai Sven (PEA4-Fe)" Date: Wed, 3 Feb 2021 14:45:41 +0100 Subject: [PATCH 1/4] Add prediction model --- src/models/prediction.ts | 8 ++++++++ src/models/user.ts | 2 ++ 2 files changed, 10 insertions(+) create mode 100644 src/models/prediction.ts diff --git a/src/models/prediction.ts b/src/models/prediction.ts new file mode 100644 index 0000000..b379eaf --- /dev/null +++ b/src/models/prediction.ts @@ -0,0 +1,8 @@ +import mongoose from 'mongoose'; + + +const PredictionSchema = new mongoose.Schema({ + values: [{ spectrumName: String, value: Number }] +}); + +export default mongoose.model>('prediction', PredictionSchema); diff --git a/src/models/user.ts b/src/models/user.ts index a70791b..51ad234 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -1,6 +1,7 @@ import mongoose from 'mongoose'; import db from '../db'; import ModelModel from './model'; +import PredictionModel from './prediction'; const UserSchema = new mongoose.Schema({ name: {type: String, index: {unique: true}}, @@ -11,6 +12,7 @@ const UserSchema = new mongoose.Schema({ location: String, devices: [String], models: [{type: mongoose.Schema.Types.ObjectId, ref: ModelModel}], + predictions: [{type: mongoose.Schema.Types.ObjectId, ref: PredictionModel}], status: String }); From 3bad5416a816653518f63e2a8805548076adccde Mon Sep 17 00:00:00 2001 From: "Engelbart Kai Sven (PEA4-Fe)" Date: Wed, 24 Feb 2021 13:59:25 +0100 Subject: [PATCH 2/4] Allow predictions to be saved --- src/index.ts | 11 ++++++----- src/models/prediction.ts | 3 ++- src/routes/prediction.spec.ts | 0 src/routes/prediction.ts | 20 ++++++++++++++++++++ 4 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 src/routes/prediction.spec.ts create mode 100644 src/routes/prediction.ts diff --git a/src/index.ts b/src/index.ts index d01d1aa..5817599 100644 --- a/src/index.ts +++ b/src/index.ts @@ -110,14 +110,15 @@ if (process.env.NODE_ENV !== 'production') { // Require routes -app.use('/', require('./routes/root')); -app.use('/', require('./routes/sample')); +app.use('/', require('./routes/help')); app.use('/', require('./routes/material')); app.use('/', require('./routes/measurement')); -app.use('/', require('./routes/template')); app.use('/', require('./routes/model')); +app.use('/', require('./routes/prediction')); +app.use('/', require('./routes/root')); +app.use('/', require('./routes/sample')); +app.use('/', require('./routes/template')); app.use('/', require('./routes/user')); -app.use('/', require('./routes/help')); // Static files app.use('/static', express.static('static')); @@ -140,4 +141,4 @@ const server = app.listen(port, () => { console.info(process.env.NODE_ENV === 'test' ? '' : `Listening on http://localhost:${port}`); }); -module.exports = server; \ No newline at end of file +module.exports = server; diff --git a/src/models/prediction.ts b/src/models/prediction.ts index b379eaf..50cad46 100644 --- a/src/models/prediction.ts +++ b/src/models/prediction.ts @@ -2,7 +2,8 @@ import mongoose from 'mongoose'; const PredictionSchema = new mongoose.Schema({ - values: [{ spectrumName: String, value: Number }] + name: String, + value: Number }); export default mongoose.model>('prediction', PredictionSchema); diff --git a/src/routes/prediction.spec.ts b/src/routes/prediction.spec.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/routes/prediction.ts b/src/routes/prediction.ts new file mode 100644 index 0000000..c45db7c --- /dev/null +++ b/src/routes/prediction.ts @@ -0,0 +1,20 @@ +import express from 'express'; +import mongoose from 'mongoose'; + +import db from '../db'; +import PredictionModel from '../models/prediction'; + +const router = express.Router(); + + +router.post('/prediction/new', (req, res, next) => { + console.log("New Prediction"); + console.log(JSON.stringify(req.body)); + new PredictionModel(req.body).save((err, data) => { + if(err) return next(err); + db.log(req, 'predictions', {_id: data._id}, data.toObject()); + }); +}); + + +module.exports = router; From f23f580a25c0a7c777cff9beb38eba153518de6a Mon Sep 17 00:00:00 2001 From: "Engelbart Kai Sven (PEA4-Fe)" Date: Thu, 25 Feb 2021 11:33:14 +0100 Subject: [PATCH 3/4] Add simple prediction comparison --- src/models/prediction.ts | 5 ++++- src/routes/prediction.ts | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/models/prediction.ts b/src/models/prediction.ts index 50cad46..dfd2b07 100644 --- a/src/models/prediction.ts +++ b/src/models/prediction.ts @@ -2,7 +2,10 @@ import mongoose from 'mongoose'; const PredictionSchema = new mongoose.Schema({ - name: String, + name: { + type: String, + unique: true + }, value: Number }); diff --git a/src/routes/prediction.ts b/src/routes/prediction.ts index c45db7c..fc50d04 100644 --- a/src/routes/prediction.ts +++ b/src/routes/prediction.ts @@ -16,5 +16,11 @@ router.post('/prediction/new', (req, res, next) => { }); }); +router.post('/prediction/compare', async (req, res, next) => { + const valA = (await PredictionModel.findOne({ name: req.body.nameA })).value; + const valB = (await PredictionModel.findOne({ name: req.body.nameB })).value; + res.json(valB - valA); +}); + module.exports = router; From 9b75741f22c9f06dd24cd613318666fc67288522 Mon Sep 17 00:00:00 2001 From: "Engelbart Kai Sven (PEA4-Fe)" Date: Thu, 25 Feb 2021 11:45:38 +0100 Subject: [PATCH 4/4] Throw an error when a prediction cannot be found --- src/routes/prediction.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/routes/prediction.ts b/src/routes/prediction.ts index fc50d04..9fc2bf6 100644 --- a/src/routes/prediction.ts +++ b/src/routes/prediction.ts @@ -17,9 +17,10 @@ router.post('/prediction/new', (req, res, next) => { }); router.post('/prediction/compare', async (req, res, next) => { - const valA = (await PredictionModel.findOne({ name: req.body.nameA })).value; - const valB = (await PredictionModel.findOne({ name: req.body.nameB })).value; - res.json(valB - valA); + const valA = await PredictionModel.findOne({ name: req.body.nameA }); + const valB = await PredictionModel.findOne({ name: req.body.nameB }); + if(!valA || !valB) next(new Error("invalid prediction name")); + res.json(valB.value - valA.value); });