clean up code
This commit is contained in:
@@ -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')))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user