clean up code

This commit is contained in:
2022-07-17 21:14:26 +02:00
parent 480c2601ab
commit aa009bd559

View File

@@ -37,22 +37,13 @@ app.get('/type/:type/startDate/:startDate/endDate/:endDate/sample/:sample', asyn
const startDate = new Date(req.params.startDate * 1000) const startDate = new Date(req.params.startDate * 1000)
const endDate = new Date(req.params.endDate * 1000) const endDate = new Date(req.params.endDate * 1000)
const sample = parseInt(req.params.sample) const sample = parseInt(req.params.sample)
// const query = { const type = req.params.type
// _id: {
// $gt: objectIdFromDate(startDate),
// $lt: objectIdFromDate(endDate)
// },
// type: req.params.type,
// value: {
// $ne: NaN
// }
// }
const agg = [ const agg = [
{ {
$match: { $match: {
$and: [ $and: [
{ _id: { $gt: objectIdFromDate(startDate), $lt: objectIdFromDate(endDate) } }, { _id: { $gt: objectIdFromDate(startDate), $lt: objectIdFromDate(endDate) } },
{ type: req.params.type }, { type },
{ value: { $ne: NaN } } { value: { $ne: NaN } }
] ]
} }
@@ -68,7 +59,6 @@ app.get('/type/:type/startDate/:startDate/endDate/:endDate/sample/:sample', asyn
} }
}, },
value: { $avg: '$value' }, value: { $avg: '$value' },
// count: { $sum: 1 },
date: { $min: { $toDate: '$_id' } } 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) => { // app.get('/v0/type/:type/startDate/:startDate/endDate/:endDate/sample/:sample', async (req, res) => {
const startDate = new Date(req.params.startDate * 1000) // const startDate = new Date(req.params.startDate * 1000)
const endDate = new Date(req.params.endDate * 1000) // const endDate = new Date(req.params.endDate * 1000)
const sample = parseInt(req.params.sample) // const sample = parseInt(req.params.sample)
const query = { // const query = {
_id: { // _id: {
$gt: objectIdFromDate(startDate), // $gt: objectIdFromDate(startDate),
$lt: objectIdFromDate(endDate) // $lt: objectIdFromDate(endDate)
}, // },
type: req.params.type, // type: req.params.type,
value: { // value: {
$ne: NaN // $ne: NaN
} // }
} // }
try { // try {
const docs = await db.collection('dht22').find(query).toArray() // const docs = await db.collection('dht22').find(query).toArray()
const sampledDocs = docs // const sampledDocs = docs
.filter((value, index) => index % sample === 0) // .filter((value, index) => index % sample === 0)
.map((doc) => { // .map((doc) => {
return { ...doc, date: dateFromObjectId(doc._id) } // return { ...doc, date: dateFromObjectId(doc._id) }
}) // })
res.json(sampledDocs) // res.json(sampledDocs)
} catch (err) { // } catch (err) {
console.log(err) // console.log(err)
} // }
}) // })
function objectIdFromDate(date) { function objectIdFromDate(date) {
return mongo.ObjectId(Math.floor(date.getTime() / 1000).toString(16) + '0000000000000000') return mongo.ObjectId(Math.floor(date.getTime() / 1000).toString(16) + '0000000000000000')
} }
function dateFromObjectId(objectId) { // function dateFromObjectId(objectId) {
return Math.floor(mongo.ObjectId(objectId).getTimestamp().getTime() / 1000) // return Math.floor(mongo.ObjectId(objectId).getTimestamp().getTime() / 1000)
} // }
app.use(express.static(path.join(__dirname, '../dist'))) app.use(express.static(path.join(__dirname, '../dist')))