changed query syntax
This commit is contained in:
parent
4f4d51b514
commit
eb5491b64b
149
backend/app.js
149
backend/app.js
@ -1,11 +1,11 @@
|
|||||||
var express = require('express')
|
const express = require('express')
|
||||||
var _ = require('lodash')
|
const moment = require('moment')
|
||||||
var sampledata = require('./sampledata')
|
const _ = require('lodash')
|
||||||
var db = require('./mysql')
|
const db = require('./mysql')
|
||||||
var score = require('./score')
|
const score = require('./score')
|
||||||
var transformer = require('./transformer')
|
const transformer = require('./transformer')
|
||||||
var climate = require('./climate')
|
const climate = require('./climate')
|
||||||
var moment = require('moment')
|
const base = require('./base64')
|
||||||
|
|
||||||
const app = express()
|
const app = express()
|
||||||
const port = 3000
|
const port = 3000
|
||||||
@ -17,13 +17,6 @@ const multiplier = {
|
|||||||
sunhours: 2.5,
|
sunhours: 2.5,
|
||||||
}
|
}
|
||||||
|
|
||||||
const sampleRegions = [
|
|
||||||
{
|
|
||||||
id: 29837,
|
|
||||||
name: "Timbuktu",
|
|
||||||
country: "Mali"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
const samplePresets = [
|
const samplePresets = [
|
||||||
{
|
{
|
||||||
id: 29837,
|
id: 29837,
|
||||||
@ -34,8 +27,8 @@ const samplePresets = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
app.get('/', (req, res) => res.send('Hello Timo!'))
|
app.get('/', (req, res) => res.send('Hello Timo!'))
|
||||||
app.get('/v1/regions', (req, res) => res.json(sampleRegions))
|
app.get('/v1/regions', (req, res) => getAllRegions().then(x => res.json({ data: x })))
|
||||||
app.get('/v1/presets', (req, res) => res.json(samplePresets))
|
app.get('/v1/presets', (req, res) => res.json({ data: samplePresets}))
|
||||||
app.get('/v1/search', searchHandler)
|
app.get('/v1/search', searchHandler)
|
||||||
app.get('/v1/update/climate', climateUpdateHandler)
|
app.get('/v1/update/climate', climateUpdateHandler)
|
||||||
|
|
||||||
@ -60,25 +53,23 @@ function climateUpdateHandler(req, res) {
|
|||||||
|
|
||||||
function searchHandler(req, res) {
|
function searchHandler(req, res) {
|
||||||
let response = {}
|
let response = {}
|
||||||
|
|
||||||
response.meta = {
|
response.meta = {
|
||||||
params: req.params,
|
params: req.params,
|
||||||
query: req.query,
|
query: req.query,
|
||||||
headers: req.headers
|
headers: req.headers
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('log params')
|
let q = req.query.q ? base.base64ToObj(req.query.q) : req.query
|
||||||
console.log(req.query.from)
|
console.log('Q:', q)
|
||||||
console.log(req.query.to)
|
|
||||||
console.log(req.query.temperature)
|
let queryObj = {}
|
||||||
|
if (q.temperature) queryObj['temperature_mean'] = q.temperature
|
||||||
let validQueries = {}
|
if (q.percipitation) queryObj['percipitation'] = q.percipitation
|
||||||
if (req.query.temperature) validQueries['temperature_mean'] = req.query.temperature
|
if (q.raindays) queryObj['raindays'] = q.raindays
|
||||||
if (req.query.percipitation) validQueries['percipitation'] = req.query.percipitation
|
if (q.sunhours) queryObj['sunhours'] = q.sunhours
|
||||||
if (req.query.raindays) validQueries['raindays'] = req.query.raindays
|
|
||||||
if (req.query.sunhours) validQueries['sunhours'] = req.query.sunhours
|
scoreAndSearch(q.from, q.to, queryObj).then(searchResults => {
|
||||||
|
|
||||||
search(req.query.from, req.query.to, validQueries).then(searchResults => {
|
|
||||||
response.data = searchResults
|
response.data = searchResults
|
||||||
res.json(response)
|
res.json(response)
|
||||||
}).catch(e => {
|
}).catch(e => {
|
||||||
@ -87,42 +78,60 @@ function searchHandler(req, res) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async function search(from, to, queries) {
|
async function scoreAndSearch(from, to, queries) {
|
||||||
|
// TODO break funtion into parts when implementing non-climate queries and modularize (new file)
|
||||||
|
|
||||||
console.log('search')
|
console.log('search')
|
||||||
|
|
||||||
// get Min and Max values for each Parameter
|
// get Min and Max values for each Parameter
|
||||||
const minMax = await getClimateMinMax()
|
const minMax = await getClimateMinMax()
|
||||||
|
|
||||||
// randomize if empty queries
|
// randomize if empty queries
|
||||||
if (_.isEmpty(queries)) {
|
if (_.isEmpty(queries)) {
|
||||||
let t = _.random(minMax.min.temperature_mean, minMax.max.temperature_mean-5)
|
let t = _.round(_.random(minMax.min.temperature_mean, minMax.max.temperature_mean-5),0)
|
||||||
let p = _.random(minMax.min.percipitation, minMax.max.percipitation-50)
|
let p = _.round(_.random(minMax.min.percipitation, minMax.max.percipitation - 50), 0)
|
||||||
let r = _.random(minMax.min.raindays, minMax.max.raindays-5)
|
let r = _.round(_.random(minMax.min.raindays, minMax.max.raindays - 5), 0)
|
||||||
let s = _.random(minMax.min.sunhours, minMax.max.sunhours-50)
|
let s = _.round(_.random(minMax.min.sunhours, minMax.max.sunhours - 50), 0)
|
||||||
queries.temperature_mean = `${t},${t + 5}`
|
queries.temperature_mean = `${t},${t + 5}`
|
||||||
queries.percipitation = `${p},${p + 50}`
|
queries.percipitation = `${p},${p + 50}`
|
||||||
queries.raindays = `${r},${r + 5}`
|
queries.raindays = `${r},${r + 5}`
|
||||||
queries.sunhours = `${s},${s + 50}`
|
queries.sunhours = `${s},${s + 50}`
|
||||||
}
|
}
|
||||||
// validate regex: YYYY-MM-DD
|
queries = oldToNewQuerySyntax(queries)
|
||||||
let re = /([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/i;
|
console.log(queries)
|
||||||
if (!from.match(re) || !to.match(re)) throw new Error("ERR: invalid parameter:",from,to)
|
|
||||||
// check for valid period
|
// TODO simplify and remove support for old query syntaax
|
||||||
if (moment(from, 'YYYY-MM-DD').isAfter(moment(to, 'YYYY-MM-DD'))) {
|
let monthFrom = 0
|
||||||
console.log(moment(from, 'YYYY-MM-DD'))
|
let monthTo = 0
|
||||||
console.log(moment(to, 'YYYY-MM-DD'))
|
let dayFrom = 0
|
||||||
console.log(moment(from, 'YYYY-MM-DD').isAfter(moment(to, 'YYYY-MM-DD')))
|
let dayTo = 0
|
||||||
throw new Error("ERR: from is before to date.")
|
|
||||||
|
if (_.isNumber(from) && _.isNumber(to)) {
|
||||||
|
let dateFrom = moment(from).toDate()
|
||||||
|
let dateTo = moment(to).toDate()
|
||||||
|
monthFrom = dateFrom.getMonth()
|
||||||
|
monthTo = dateTo.getMonth()
|
||||||
|
dayFrom = dateFrom.getDay()
|
||||||
|
dayTo = dateTo.getDay()
|
||||||
|
if (moment(dateFrom).add(23, 'hours').isAfter(moment(dateTo))) throw new Error("ERR: 'to' must be at least one day after 'from'.")
|
||||||
|
} else {
|
||||||
|
// to still support old query syntax
|
||||||
|
let re = /([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/i;
|
||||||
|
monthFrom = Number(from.split("-")[1])
|
||||||
|
monthTo = Number(to.split("-")[1])
|
||||||
|
dayFrom = Number(from.split("-")[2])
|
||||||
|
dayTo = Number(to.split("-")[2])
|
||||||
|
if (!from.match(re) || !to.match(re)) throw new Error("ERR: invalid parameter:",from,to)
|
||||||
|
if (moment(from, 'YYYY-MM-DD').add(23, 'hours').isAfter(moment(to, 'YYYY-MM-DD'))) throw new Error("ERR: 'to' must be at least one day after 'from'.")
|
||||||
}
|
}
|
||||||
|
|
||||||
// -- Prepare search --
|
// -- Prepare search --
|
||||||
// calculate average if traveldates are in more than one month
|
// to calculate average if traveldates are in more than one month
|
||||||
let monthFrom = Number(from.split("-")[1])
|
|
||||||
let monthTo = Number(to.split("-")[1])
|
|
||||||
|
|
||||||
let travelPeriods = []
|
let travelPeriods = []
|
||||||
if (monthFrom === monthTo) {
|
if (monthFrom === monthTo) {
|
||||||
let element = {
|
let element = {
|
||||||
month: monthFrom,
|
month: monthFrom,
|
||||||
days: Number(to.split("-")[2]) - Number(from.split("-")[2])
|
days: dayTo - dayFrom
|
||||||
}
|
}
|
||||||
travelPeriods.push(element)
|
travelPeriods.push(element)
|
||||||
} else {
|
} else {
|
||||||
@ -131,12 +140,12 @@ async function search(from, to, queries) {
|
|||||||
if (index === monthFrom) {
|
if (index === monthFrom) {
|
||||||
element = {
|
element = {
|
||||||
month: index,
|
month: index,
|
||||||
days: 32 - Number(from.split("-")[2])
|
days: 32 - dayFrom
|
||||||
}
|
}
|
||||||
} else if (index === monthTo) {
|
} else if (index === monthTo) {
|
||||||
element = {
|
element = {
|
||||||
month: index,
|
month: index,
|
||||||
days: Number(to.split("-")[2])
|
days: dayTo
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
element = {
|
element = {
|
||||||
@ -148,20 +157,20 @@ async function search(from, to, queries) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// calculate score
|
// calculate detail scores
|
||||||
let detailScores = await Promise.all(travelPeriods.map(async period => {
|
let detailScores = await Promise.all(travelPeriods.map(async period => {
|
||||||
period.climate = await getAllRegionsWithClimatePerMonth(period.month)
|
period.climate = await getAllRegionsWithClimatePerMonth(period.month)
|
||||||
period.scores = {}
|
period.scores = {}
|
||||||
Object.entries(queries).forEach(([key, value]) => {
|
Object.entries(queries).forEach(([key, value]) => {
|
||||||
// console.log('key',key)
|
// console.log('key',key)
|
||||||
// console.log('val', value)
|
// console.log('val', value)
|
||||||
period.scores[key] = calculateScores(key, period.climate, value.split(',')[0], value.split(',')[1], minMax)
|
period.scores[key] = calculateScores(key, period.climate, value[0], value[1], minMax)
|
||||||
});
|
});
|
||||||
return period
|
return period
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
||||||
// TODO: calculate ratio
|
// calculate ratio and transform into target object
|
||||||
return {
|
return {
|
||||||
results: transformer.transform(detailScores),
|
results: transformer.transform(detailScores),
|
||||||
debug: {
|
debug: {
|
||||||
@ -216,10 +225,17 @@ async function getQueryRows(sql) {
|
|||||||
return rows
|
return rows
|
||||||
}
|
}
|
||||||
|
|
||||||
function getRegionIdsWithMeteostatData() {
|
function getAllRegions() {
|
||||||
console.log('getRegionIdsWithMeteostatData')
|
const sql = `SELECT
|
||||||
//return await getQueryRows(`SELECT * FROM regions WHERE meteostat_id IS NOT NULL`)
|
regions.id AS region_id,
|
||||||
return getQueryRows(`SELECT region_id FROM region_climate GROUP BY region_id`)
|
regions.region AS name,
|
||||||
|
regions.country_id AS country_id,
|
||||||
|
countries.country AS country,
|
||||||
|
regions.meteostat_id AS meteostat_id
|
||||||
|
FROM regions
|
||||||
|
JOIN countries
|
||||||
|
ON regions.country_id = countries.id`
|
||||||
|
return getQueryRows(sql)
|
||||||
}
|
}
|
||||||
|
|
||||||
function getClimatePerRegionAndMonth(regionId, month) {
|
function getClimatePerRegionAndMonth(regionId, month) {
|
||||||
@ -242,4 +258,19 @@ function getAllRegionsWithClimatePerMonth(month) {
|
|||||||
ROUND(AVG(region_climate.sunshine), 1) AS sunhours
|
ROUND(AVG(region_climate.sunshine), 1) AS sunhours
|
||||||
FROM region_climate JOIN regions ON region_climate.region_id = regions.id WHERE region_climate.month = ${month} GROUP BY region_id`
|
FROM region_climate JOIN regions ON region_climate.region_id = regions.id WHERE region_climate.month = ${month} GROUP BY region_id`
|
||||||
return getQueryRows(sql)
|
return getQueryRows(sql)
|
||||||
|
}
|
||||||
|
|
||||||
|
function oldToNewQuerySyntax(queries) {
|
||||||
|
let res = {}
|
||||||
|
try {
|
||||||
|
if (queries.temperature_mean) res.temperature_mean = [queries.temperature_mean.split(',')[0], queries.temperature_mean.split(',')[1]]
|
||||||
|
if (queries.percipitation) res.percipitation = [queries.percipitation.split(',')[0], queries.percipitation.split(',')[1]]
|
||||||
|
if (queries.raindays) res.raindays = [queries.raindays.split(',')[0], queries.raindays.split(',')[1]]
|
||||||
|
if (queries.sunhours) res.sunhours = [queries.sunhours.split(',')[0], queries.sunhours.split(',')[1]]
|
||||||
|
console.log('queries successfully transformed');
|
||||||
|
} catch (error) {
|
||||||
|
console.log('queries are ok');
|
||||||
|
return queries
|
||||||
|
}
|
||||||
|
return res
|
||||||
}
|
}
|
||||||
23
backend/base64.js
Normal file
23
backend/base64.js
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
/**
|
||||||
|
* Encodes an object as base64 string..
|
||||||
|
* @param obj The object to encode
|
||||||
|
*/
|
||||||
|
exports.objToBase64 = function(obj) {
|
||||||
|
return btoa(JSON.stringify(obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decodes a base64 encoded object.
|
||||||
|
* @param base64 Encoded object
|
||||||
|
*/
|
||||||
|
exports.base64ToObj = function(base64) {
|
||||||
|
return JSON.parse(atob(base64));
|
||||||
|
}
|
||||||
|
|
||||||
|
function atob(base64) {
|
||||||
|
return Buffer.from(base64, 'base64').toString('binary')
|
||||||
|
}
|
||||||
|
|
||||||
|
function btoa(string) {
|
||||||
|
return Buffer.from(string).toString('base64')
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user