Archived
2

Merge pull request #48 in DEFINMA/definma-api from prediction-delta to master

* commit '9b75741f22c9f06dd24cd613318666fc67288522':
  Throw an error when a prediction cannot be found
  Add simple prediction comparison
  Allow predictions to be saved
  Add prediction model
This commit is contained in:
Hartenstein Ruben (PEA4-Fe) 2021-02-25 11:49:30 +01:00
commit dc662046b1
5 changed files with 47 additions and 5 deletions

View File

@ -110,14 +110,15 @@ if (process.env.NODE_ENV !== 'production') {
// Require routes // Require routes
app.use('/', require('./routes/root')); app.use('/', require('./routes/help'));
app.use('/', require('./routes/sample'));
app.use('/', require('./routes/material')); app.use('/', require('./routes/material'));
app.use('/', require('./routes/measurement')); app.use('/', require('./routes/measurement'));
app.use('/', require('./routes/template'));
app.use('/', require('./routes/model')); 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/user'));
app.use('/', require('./routes/help'));
// Static files // Static files
app.use('/static', express.static('static')); 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}`); console.info(process.env.NODE_ENV === 'test' ? '' : `Listening on http://localhost:${port}`);
}); });
module.exports = server; module.exports = server;

12
src/models/prediction.ts Normal file
View File

@ -0,0 +1,12 @@
import mongoose from 'mongoose';
const PredictionSchema = new mongoose.Schema({
name: {
type: String,
unique: true
},
value: Number
});
export default mongoose.model<any, mongoose.Model<any, any>>('prediction', PredictionSchema);

View File

@ -1,6 +1,7 @@
import mongoose from 'mongoose'; import mongoose from 'mongoose';
import db from '../db'; import db from '../db';
import ModelModel from './model'; import ModelModel from './model';
import PredictionModel from './prediction';
const UserSchema = new mongoose.Schema({ const UserSchema = new mongoose.Schema({
name: {type: String, index: {unique: true}}, name: {type: String, index: {unique: true}},
@ -11,6 +12,7 @@ const UserSchema = new mongoose.Schema({
location: String, location: String,
devices: [String], devices: [String],
models: [{type: mongoose.Schema.Types.ObjectId, ref: ModelModel}], models: [{type: mongoose.Schema.Types.ObjectId, ref: ModelModel}],
predictions: [{type: mongoose.Schema.Types.ObjectId, ref: PredictionModel}],
status: String status: String
}); });

View File

27
src/routes/prediction.ts Normal file
View File

@ -0,0 +1,27 @@
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());
});
});
router.post('/prediction/compare', async (req, res, next) => {
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);
});
module.exports = router;