134 lines
3.5 KiB
JavaScript
134 lines
3.5 KiB
JavaScript
/* eslint-env node */
|
|
require('dotenv').config()
|
|
const express = require('express')
|
|
const app = express()
|
|
const path = require('path')
|
|
const mongo = require('mongodb')
|
|
const MongoClient = mongo.MongoClient
|
|
console.log('Starting with parameters:')
|
|
const parameters = ['PORT', 'MONGO_SERVER', 'MONGO_DB']
|
|
parameters.forEach((parameter) => console.log(`${parameter}=${process.env[parameter]}`))
|
|
app.listen(process.env.PORT, () => {
|
|
console.log('Listening on ' + process.env.PORT)
|
|
})
|
|
|
|
let db
|
|
async function connectToMongo() {
|
|
try {
|
|
const mongoUrl = `mongodb://${process.env.MONGO_SERVER}:27017`
|
|
const client = await MongoClient.connect(mongoUrl, { useNewUrlParser: true })
|
|
db = client.db(process.env.MONGO_DB)
|
|
console.log('Connected succesfully to server')
|
|
} catch (e) {
|
|
console.log(e)
|
|
setTimeout(connectToMongo, 5000)
|
|
}
|
|
}
|
|
|
|
connectToMongo()
|
|
|
|
app.use(function (req, res, next) {
|
|
res.header('Access-Control-Allow-Origin', '*')
|
|
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
|
|
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)
|
|
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)
|
|
}
|
|
|
|
app.use(express.static(path.join(__dirname, '../dist')))
|
|
|
|
app.get('/*', function (req, res) {
|
|
res.sendFile(path.join(__dirname, '../dist/index.html'))
|
|
})
|
|
|
|
module.exports = app
|