data processing in mongo query

This commit is contained in:
2022-07-17 12:00:14 +02:00
parent 22dfa72d87
commit 480c2601ab
2 changed files with 57 additions and 1 deletions

View File

@@ -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)

View File

@@ -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: '' },