data processing in mongo query
This commit is contained in:
@@ -34,6 +34,62 @@ app.use(function (req, res, next) {
|
||||
})
|
||||
|
||||
app.get('/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
|
||||
// }
|
||||
// }
|
||||
const agg = [
|
||||
{
|
||||
$match: {
|
||||
$and: [
|
||||
{ _id: { $gt: objectIdFromDate(startDate), $lt: objectIdFromDate(endDate) } },
|
||||
{ type: req.params.type },
|
||||
{ value: { $ne: NaN } }
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: {
|
||||
$toDate: {
|
||||
$subtract: [
|
||||
{ $toLong: { $toDate: '$_id' } },
|
||||
{ $mod: [{ $toLong: { $toDate: '$_id' } }, 1000 * 60 * sample] }
|
||||
]
|
||||
}
|
||||
},
|
||||
value: { $avg: '$value' },
|
||||
// count: { $sum: 1 },
|
||||
date: { $min: { $toDate: '$_id' } }
|
||||
}
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
_id: 0,
|
||||
value: { $round: ['$value', 1] },
|
||||
date: { $toLong: '$date' }
|
||||
}
|
||||
},
|
||||
{ $sort: { date: 1 } }
|
||||
]
|
||||
try {
|
||||
const docs = await db.collection('dht22').aggregate(agg).toArray()
|
||||
res.json(docs)
|
||||
} 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)
|
||||
|
||||
@@ -48,7 +48,7 @@ export default {
|
||||
}
|
||||
}
|
||||
const renderChart = () => {
|
||||
const chartData = data.value.map(({ date, value }) => ({ x: date * 1000, y: value }))
|
||||
const chartData = data.value.map(({ date, value }) => ({ x: date, y: value }))
|
||||
Highcharts.chart(chart.value, {
|
||||
chart: { styledMode: true, marginBottom: 25 },
|
||||
title: { text: '' },
|
||||
|
||||
Reference in New Issue
Block a user