From aa009bd55928fa925296ad9054865036b798a683 Mon Sep 17 00:00:00 2001 From: Marco Crapts Date: Sun, 17 Jul 2022 21:14:26 +0200 Subject: [PATCH] clean up code --- server/index.js | 72 +++++++++++++++++++++---------------------------- 1 file changed, 31 insertions(+), 41 deletions(-) diff --git a/server/index.js b/server/index.js index 14712a7..496efa6 100644 --- a/server/index.js +++ b/server/index.js @@ -37,22 +37,13 @@ app.get('/type/:type/startDate/:startDate/endDate/:endDate/sample/:sample', asyn const startDate = new Date(req.params.startDate * 1000) const endDate = new Date(req.params.endDate * 1000) const sample = parseInt(req.params.sample) - // const query = { - // _id: { - // $gt: objectIdFromDate(startDate), - // $lt: objectIdFromDate(endDate) - // }, - // type: req.params.type, - // value: { - // $ne: NaN - // } - // } + const type = req.params.type const agg = [ { $match: { $and: [ { _id: { $gt: objectIdFromDate(startDate), $lt: objectIdFromDate(endDate) } }, - { type: req.params.type }, + { type }, { value: { $ne: NaN } } ] } @@ -68,7 +59,6 @@ app.get('/type/:type/startDate/:startDate/endDate/:endDate/sample/:sample', asyn } }, value: { $avg: '$value' }, - // count: { $sum: 1 }, date: { $min: { $toDate: '$_id' } } } }, @@ -89,40 +79,40 @@ app.get('/type/:type/startDate/:startDate/endDate/:endDate/sample/:sample', asyn } }) -app.get('/v0/type/:type/startDate/:startDate/endDate/:endDate/sample/:sample', async (req, res) => { - const startDate = new Date(req.params.startDate * 1000) - const endDate = new Date(req.params.endDate * 1000) - const sample = parseInt(req.params.sample) - const query = { - _id: { - $gt: objectIdFromDate(startDate), - $lt: objectIdFromDate(endDate) - }, - type: req.params.type, - value: { - $ne: NaN - } - } - try { - const docs = await db.collection('dht22').find(query).toArray() - const sampledDocs = docs - .filter((value, index) => index % sample === 0) - .map((doc) => { - return { ...doc, date: dateFromObjectId(doc._id) } - }) - res.json(sampledDocs) - } catch (err) { - console.log(err) - } -}) +// app.get('/v0/type/:type/startDate/:startDate/endDate/:endDate/sample/:sample', async (req, res) => { +// const startDate = new Date(req.params.startDate * 1000) +// const endDate = new Date(req.params.endDate * 1000) +// const sample = parseInt(req.params.sample) +// const query = { +// _id: { +// $gt: objectIdFromDate(startDate), +// $lt: objectIdFromDate(endDate) +// }, +// type: req.params.type, +// value: { +// $ne: NaN +// } +// } +// try { +// const docs = await db.collection('dht22').find(query).toArray() +// const sampledDocs = docs +// .filter((value, index) => index % sample === 0) +// .map((doc) => { +// return { ...doc, date: dateFromObjectId(doc._id) } +// }) +// res.json(sampledDocs) +// } catch (err) { +// console.log(err) +// } +// }) function objectIdFromDate(date) { return mongo.ObjectId(Math.floor(date.getTime() / 1000).toString(16) + '0000000000000000') } -function dateFromObjectId(objectId) { - return Math.floor(mongo.ObjectId(objectId).getTimestamp().getTime() / 1000) -} +// function dateFromObjectId(objectId) { +// return Math.floor(mongo.ObjectId(objectId).getTimestamp().getTime() / 1000) +// } app.use(express.static(path.join(__dirname, '../dist')))