Search function works as before minus old syntax

This commit is contained in:
Timo John 2020-06-16 02:21:16 +02:00 committed by Timo Volkmann
parent 692336a0ba
commit b0f5defe1c
9 changed files with 141 additions and 17 deletions

View File

@ -1,4 +1,4 @@
module.exports = async (dbConn, name) => { module.exports = async (dbConn) => {
// TODO: Implement pulling data from database // TODO: Implement pulling data from database
const presets = require ("../mockdata/sample-presets.json") const presets = require ("../mockdata/sample-presets.json")
const res = presets const res = presets

View File

@ -1,6 +1,29 @@
module.exports = async (dbConn, name) => { const base64 = require ("../util/base64.js")
const res = await dbConn.query( const ss = require ("../util/scoreAndSearch.js")
); module.exports = async (dbConn, req, res) => {
return res[0]; let response = {}
response.meta = {
params: req.params,
query: req.query,
headers: req.headers
}
let q = req.query.q ? base64.base64ToObj(req.query.q) : req.query
console.log('Q:', q)
let queryObj = {}
if (q.temperature) queryObj['temperature_mean_max'] = q.temperature
if (q.percipitation) queryObj['percipitation'] = q.percipitation
if (q.raindays) queryObj['raindays'] = q.raindays
if (q.sunhours) queryObj['sunhours'] = q.sunhours
ss.scoreAndSearch(q.from, q.to, queryObj, dbConn).then(searchResults => {
response.data = searchResults
res.json(response)
}).catch(e => {
console.log(e)
res.json(e.message)
})
}; };

View File

@ -1,7 +1,6 @@
require('dotenv').config()
const axios = require('axios') const axios = require('axios')
// TODO: Automatically retrieve dates via aviable Data // TODO: Automatically retrieve dates via aviable Data and get rid of random dates
const rangeStartDate = '2010-01' // If no date is given, this date will be used as startDate const rangeStartDate = '2010-01' // If no date is given, this date will be used as startDate
const rangeEndDate = '2018-12'// If no date is given, this date will be used as endDate const rangeEndDate = '2018-12'// If no date is given, this date will be used as endDate

View File

@ -6,19 +6,14 @@ module.exports = dbConn => {
router.get("/api/v1/search", async (req, res) => { router.get("/api/v1/search", async (req, res) => {
const query = req.query.q; const query = req.query.q;
if (query != undefined) { if (query != undefined) {
res.json(await getSearchResults(dbConn, query)); res.json(await getSearchResults(dbConn, req));
} else { } else {
res.status(400).send(); res.status(400).send();
} }
}); });
router.get("/api/v1/search/presets", async (req, res) => { router.get("/api/v1/search/presets", async (req, res) => {
const query = req.query.q; res.json(await getSearchPresets(dbConn));
if (query != undefined) {
res.json(await getSearchPresets(dbConn, query));
} else {
res.status(400).send();
}
}); });
return router; return router;

View File

@ -1,4 +1,4 @@
async function getClimateMinMax() { exports.getClimateMinMax = async function (dbConn) {
console.log('getClimateMinMax') console.log('getClimateMinMax')
const sqlMin = `SELECT const sqlMin = `SELECT
MIN(temperature_mean) AS temperature_mean, MIN(temperature_mean) AS temperature_mean,
@ -16,7 +16,7 @@ async function getClimateMinMax() {
MAX(raindays) AS raindays, MAX(raindays) AS raindays,
MAX(sunshine) AS sunhours MAX(sunshine) AS sunhours
FROM region_climate` FROM region_climate`
const [qResMin, qResMax] = await Promise.all([getQueryRows(sqlMin), getQueryRows(sqlMax)]) const [qResMin, qResMax] = await Promise.all([await dbConn.query(sqlMin), await dbConn.query(sqlMax)])
//console.log(qResMin) //console.log(qResMin)
return { min: qResMin[0], max: qResMax[0] } return { min: qResMin[0], max: qResMax[0] }
} }

View File

@ -1,4 +1,4 @@
function oldToNewQuerySyntax(queries) { exports.oldToNewQuerySyntax = function (queries) {
let res = {} let res = {}
try { try {
if (queries.temperature_mean_max) res.temperature_mean_max = [queries.temperature_mean_max.split(',')[0], queries.temperature_mean_max.split(',')[1]] if (queries.temperature_mean_max) res.temperature_mean_max = [queries.temperature_mean_max.split(',')[0], queries.temperature_mean_max.split(',')[1]]

View File

@ -0,0 +1,107 @@
const _ = require('lodash')
const getClimateMinMax = require("./getClimateMinMax.js")
const oldToNewQuerySyntax = require("./oldToNewQuerySyntax.js")
const moment = require("moment")
exports.scoreAndSearch = async function (from, to, queries, dbConn) {
// TODO break funtion into parts when implementing non-climate queries and modularize (new file)
console.log('search')
// get Min and Max values for each Parameter
const minMax = await getClimateMinMax.getClimateMinMax(dbConn)
// randomize if empty queries
// TODO: WHY
if (_.isEmpty(queries)) {
let t = _.round(_.random(minMax.min.temperature_mean_max, minMax.max.temperature_mean_max-5),0)
let p = _.round(_.random(minMax.min.percipitation, minMax.max.percipitation - 50), 0)
let r = _.round(_.random(minMax.min.raindays, minMax.max.raindays - 5), 0)
let s = _.round(_.random(minMax.min.sunhours, minMax.max.sunhours - 50), 0)
queries.temperature_mean_max = `${t},${t + 5}`
queries.percipitation = `${p},${p + 50}`
queries.raindays = `${r},${r + 5}`
queries.sunhours = `${s},${s + 50}`
}
queries = oldToNewQuerySyntax.oldToNewQuerySyntax(queries)
console.log(queries)
// TODO simplify and remove support for old query syntaax
let monthFrom = 0
let monthTo = 0
let dayFrom = 0
let dayTo = 0
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 --
// to calculate average if traveldates are in more than one month
let travelPeriods = []
if (monthFrom === monthTo) {
let element = {
month: monthFrom,
days: dayTo - dayFrom
}
travelPeriods.push(element)
} else {
for (let index = monthFrom; index <= monthTo; index++) {
let element = {}
if (index === monthFrom) {
element = {
month: index,
days: 32 - dayFrom
}
} else if (index === monthTo) {
element = {
month: index,
days: dayTo
}
} else {
element = {
month: index,
days: 30
}
}
travelPeriods.push(element)
}
}
// calculate detail scores
let detailScores = await Promise.all(travelPeriods.map(async period => {
period.climate = await getAllRegionsWithClimatePerMonth(period.month)
period.scores = {}
Object.entries(queries).forEach(([key, value]) => {
// console.log('key',key)
// console.log('val', value)
period.scores[key] = calculateScores(key, period.climate, value[0], value[1], minMax)
});
return period
}));
// calculate ratio and transform into target object
return {
results: transformer.transform(detailScores),
debug: {
detailScores: detailScores,
minMax: minMax
}
}
}