Skip to content

Instantly share code, notes, and snippets.

@deecewan
Created October 28, 2016 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deecewan/8aec8a8a3bb420634faebcf8e73ec64c to your computer and use it in GitHub Desktop.
Save deecewan/8aec8a8a3bb420634faebcf8e73ec64c to your computer and use it in GitHub Desktop.
@ghiculescu beat me to it by 10 minutes πŸ˜₯πŸš€πŸ’―
import Express from 'express';
let db = {};
const app = new Express();
app.post('/clear_data', (req, res) => {
db = {};
res.sendStatus(200);
});
app.post('/:device/:timestamp', (req, res) => {
const device = req.params.device;
if (!db[device]) {
db[device] = [];
}
db[device].push(new Date(parseInt(req.params.timestamp, 10) * 1000));
res.sendStatus(200);
});
app.get('/devices', (req, res) => {
res.status(200).json(Object.keys(db));
});
app.get('/:device/:date', (req, res) => {
const date = new Date(req.params.date);
const day1ahead = new Date(date.getTime());
day1ahead.setDate(date.getDate() + 1);
const result = db[req.params.device].filter(timestamp => {
return timestamp.getTime() >= date.getTime() && timestamp.getTime() < day1ahead.getTime()
});
return res.status(200).json(result);
});
function getDeviceData(device, from1, to1) {
let from = from1.split('-');
let to = to1.split('-');
if (from.length === 3) {
from = new Date(from);
} else {
from = new Date(parseInt(from1, 10) * 1000)
}
if (to.length === 3) {
to = new Date(to);
} else {
to = new Date(parseInt(to1, 10) * 1000)
}
if (!db[device]) { return []; }
return db[device].filter(t => {
return t >= from && t < to;
});
}
app.get('/all/:from/:to', (req, res) => {
const result = {};
Object.keys(db).forEach(k => {result[k] = getShit(k, req.params.from, req.params.to)});
return res.status(200).json(result);
});
app.get('/:device/:from/:to', (req, res) => {
return res.status(200).json(getShit(req.params.device,
req.params.from, req.params.to));
});
app.listen(3000, '0.0.0.0', () => console.log('listening on 0.0.0.0:3000'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment