Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
mostly fixes after dependency updates
Browse files Browse the repository at this point in the history
  • Loading branch information
hoegaarden committed Aug 25, 2013
1 parent e4b7313 commit 613c266
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 52 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Install / Run

What is this?
-
This is the server component for the dustmap.org project. The project's goal is to
This is the api server component for the dustmap.org project. The project's goal is to

- develop and deploy el-cheapo hardware (raspberrypi, arduino, ...) to measure environmental data (especially particulate matter)
- develop software for measuring, transfering, storing and visualizing the recorded data
Expand All @@ -30,7 +30,7 @@ Hints
- You can setup multiple different database connections for different `NODE_ENV`, check out the `database.json.example`
- We currently use `hstore`. Perhaps this will change in future (we'll perhaps use `JSON` as a datatype as soon as soon as 9.3 is out ... or not)
- No Auth/Sign/.../Whatsoever ... coming in future
- If you need SSL to connect to the database you need to set the environment variable `NODE_PG_FORCE_NATIVE` (this is the defaul for `npm run` and `npm start`)
- If you need SSL to connect to the database you need to set the environment variable `NODE_PG_FORCE_NATIVE` (this is the defaul for `npm test` and `npm start`)
- you can change the port(s) the server is listening on with `npm config set dustmap-server:http_port 1234`. Available config options:
- `http_port`
- `https_port`
Expand Down
8 changes: 7 additions & 1 deletion lib/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,11 @@ app.configure(function(){
app.use(paging());
});

routes(app);
app.all('*', function(req, res, next){
res.header('Access-Control-Allow-Origin', req.headers.origin || "*");
res.header('Access-Control-Allow-Methods', 'GET,POST,PUT,HEAD,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'content-Type,x-requested-with');
return next();
});

routes(app);
7 changes: 1 addition & 6 deletions models/Measurement.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var hooks = require('./hooks.js');

module.exports = function(db, cb){
var Measurement = db.define('Measurement', {
db.define('Measurement', {
data : { type: 'text', required: true }
},{
table : 'measurements'
Expand All @@ -14,10 +14,5 @@ module.exports = function(db, cb){
}
});

Measurement.hasOne('upload', db.models.Upload, {
required : true
, reverse : 'measurement'
});

return cb();
};
9 changes: 2 additions & 7 deletions models/Upload.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
"use strict";

module.exports = function(db, cb){
var Upload = db.define('Upload', {
db.define('Upload', {
ts : { type: 'date', required: true }
// , node : { type: 'number', required: true, rational: false, unsigned: true }
, node : { type: 'number', required: true, rational: false, unsigned: true }
},{
table : 'uploads'
});

Upload.hasOne('node', db.models.Node, {
required : true
, reverse : 'uploads'
});

return cb();
};
67 changes: 31 additions & 36 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,55 +102,50 @@ module.exports = function(app) {
.limit(req.page.limit)
;

if (req.query.bbox) {
// bbox = left;bottom;right;top
var bbox = req.query.bbox.split(';');
var box = [
bbox[1] , bbox[0] , bbox[3] , bbox[2]
].join(',');

query.where('box ? @> location', [box]);
}

query.run(function(err, nodes){
if (err)
return res.send(400, err);

async.map(nodes, function iterator(node, cb){
var q_upload = req.models.Upload
.find( { node : node.id } )
.order('ts', 'Z')
.limit(1)
.only('id', 'ts')
;

async.map(nodes, function(node, cb){
HAL.link(node, 'self', '/nodes/'.concat(node.id));

// TODO: make this simpler with extra view / function in postgres
return q_upload.run(function(err, uploads){
req.models.Upload.find({'node':node.id}).order('ts', 'Z').limit(1).run(function(err, uploads){
if (err)
return res.send(err);

if (uploads.length) {
var upload = uploads[0]
, ts = upload.ts
;

var q_measurement = req.models.Measurement
.find( { upload : upload.id } )
.only('data', 'id')
;
return q_measurement.run(function(err, measurements){
if (err)
return cb(err);
return cb(err);
if (uploads.length <= 0)
return cb(null, node);

var last_upload = {};
last_upload.ts = ts;
last_upload.measurements = measurements.map(function(measurement){
return measurement.data;
});
var upload = uploads[0];

HAL.link(last_upload, 'self', '/uploads/'.concat(upload.id));
req.models.Measurement.find({'upload':upload.id}).only('data', 'id').run(function(err, measurements){
if (err)
return cb(err);

HAL.embed(node, 'last_upload', last_upload);
var last_upload = {
ts : upload.ts,
measurements : measurements.map(function(m){
return m.data;
})
};

HAL.link(last_upload, 'self', '/uploads/'.concat(upload.id));

node.last_upload = last_upload;

return cb(null, node);
});
} else {
return cb(null, node);
}
});
});
}, function done(err, doc){
}, function(err, doc){
if (err)
return res.send(400, err);
return res.send(doc);
Expand Down

0 comments on commit 613c266

Please sign in to comment.