diff --git a/package-lock.json b/package-lock.json index 21e7bcd0e5942071c0c922611554d1ead101b276..d14b10d2d4dc0215de345a2489521f9c1e24d81e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { - "name": "HotTrash", - "version": "0.1.0", + "name": "jetsam-server", + "version": "1.1.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 34dda0766c73757aa5a30d05eaa4d2a2cfe96fe0..e17e724c3571cc2dcec568d8b709e8036d8443a7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jetsam-server", - "version": "1.1.0", + "version": "1.1.1", "description": "", "main": "index.js", "scripts": { diff --git a/src/http/controllers/api/content.js b/src/http/controllers/api/content.js index 893910961bca8e19f4947cf9b58e8c75a450eda4..aff20afaa49957411d1878b0b42242b8b0fbfe30 100644 --- a/src/http/controllers/api/content.js +++ b/src/http/controllers/api/content.js @@ -1,5 +1,5 @@ const HttpError = require('core/errors/HttpError') -const { Sequelize, Metric } = require('database/models') +const { Sequelize, sequelize, Metric } = require('database/models') const { Op } = Sequelize const moment = require('moment') @@ -79,22 +79,51 @@ exports.getWithin = async ctx => { [minFromLong, minFromLat], ].map(pb => pb.map(Number).join(' ')).join(',') - const attributes = format === 'marker' ? ['location', 'value', 'type'] : undefined + const query = format === 'marker' ? queryMarker : queryFull + const metrics = await query(pointBuffer, metricTypes, fromDate.toISOString(), toDate.toISOString()) - const metrics = await Metric.findAll({ + ctx.body = { metrics } +} + +async function queryMarker(pointBuffer, types, from, to) { + const snapClause = `ST_SNAPTOGRID("Metric"."location"::geometry, 0.001)` + + return Metric.findAll({ where: { [Op.and]: [ - Sequelize.literal(`ST_COVEREDBY("Metric"."location", ST_POLYGONFROMTEXT('POLYGON((${ pointBuffer }))'))`), + Sequelize.literal(`ST_COVEREDBY(${ snapClause }, ST_POLYGONFROMTEXT('POLYGON((${ pointBuffer }))')::geography::geometry)`), ], recorded_at: { - [Op.between]: [fromDate.toISOString(), toDate.toISOString()] + [Op.between]: [from, to] }, type: { - [Op.in]: metricTypes, + [Op.in]: types, } }, - attributes, + attributes: [ + 'type', + [Sequelize.fn('COUNT', Sequelize.col('value')), 'value'], + [Sequelize.literal(snapClause), 'location'] + ], + group: [ + Sequelize.literal(snapClause), + 'type', + ] }) +} - ctx.body = { metrics } -} \ No newline at end of file +async function queryFull(pointBuffer, types, from, to) { + return Metric.findAll({ + where: { + [Op.and]: [ + Sequelize.literal(`ST_COVEREDBY("Metric"."location", ST_POLYGONFROMTEXT('POLYGON((${ pointBuffer }))'))`), + ], + recorded_at: { + [Op.between]: [from, to] + }, + type: { + [Op.in]: types, + } + }, + }) +}