Merge branch 'bugfix/queries' into 'develop'
Bugfix/queries and randomize regions See merge request tjohn/cc-data!17
This commit is contained in:
commit
e54232842b
@ -7,7 +7,12 @@ const _ = require('lodash')
|
||||
|
||||
module.exports = dbConn => {
|
||||
router.get("/api/v1/regions", async (req, res) => {
|
||||
res.json(await getRegions(dbConn));
|
||||
const data = await getRegions(dbConn)
|
||||
if (req.query.randomize) {
|
||||
res.json(_.sampleSize(data, req.query.randomize))
|
||||
} else {
|
||||
res.json(data);
|
||||
}
|
||||
});
|
||||
router.get('/api/v1/regions/:id/image', (req, res) => {
|
||||
if (fs.existsSync(path.join(__dirname, `../data/regions/images/${req.params.id}.jpg`))) {
|
||||
|
||||
@ -3,8 +3,7 @@ const _ = require('lodash')
|
||||
const getSearchPresets = require("../models/getSearchPresets.js");
|
||||
const base64 = require("../util/base64.js")
|
||||
const sas = require("../util/scoreAndSearch.js");
|
||||
const { filter } = require("lodash");
|
||||
|
||||
const oldToNewQuerySyntax = require("../util/oldToNewQuerySyntax.js")
|
||||
|
||||
module.exports = dbConn => {
|
||||
router.get("/api/v1/search", searchHandler(dbConn));
|
||||
@ -34,28 +33,16 @@ function searchHandler(dbConn) {
|
||||
headers: req.headers
|
||||
}
|
||||
|
||||
// SWITCH TO SUPPORT OLD AND NEW BASE64 BASED QUERY SYNTAX
|
||||
let q = req.query.q ? base64.base64ToObj(req.query.q) : req.query
|
||||
console.log('Q:', q)
|
||||
|
||||
let scoreQueryObj = {}
|
||||
if (q.temperature) scoreQueryObj['temperature_mean_max'] = q.temperature
|
||||
if (q.temperature_mean_max) scoreQueryObj['temperature_mean_max'] = q.temperature_mean_max
|
||||
if (q.precipitation) scoreQueryObj['precipitation'] = q.precipitation
|
||||
if (q.rain_days) scoreQueryObj['rain_days'] = q.rain_days
|
||||
if (q.sun_hours) scoreQueryObj['sun_hours'] = q.sun_hours
|
||||
if (q.accommodation_costs) scoreQueryObj['accommodation_costs'] = q.accommodation_costs
|
||||
if (q.food_costs) scoreQueryObj['food_costs'] = q.food_costs
|
||||
if (q.alcohol_costs) scoreQueryObj['alcohol_costs'] = q.alcohol_costs
|
||||
if (q.water_costs) scoreQueryObj['water_costs'] = q.water_costs
|
||||
if (q.public_transportation_costs) scoreQueryObj['public_transportation_costs'] = q.public_transportation_costs
|
||||
if (q.entertainment_costs) scoreQueryObj['entertainment_costs'] = q.entertainment_costs
|
||||
if (q.average_per_day_costs) scoreQueryObj['average_per_day_costs'] = q.average_per_day_costs
|
||||
// transform syntax and seperate climate queries from price queries
|
||||
q = oldToNewQuerySyntax(q)
|
||||
// CHOOSE PARAMS WHICH SHALL BE PASSED TO SCORE AND SEARCH
|
||||
let scoreQueryObj = prepareQueries(q)
|
||||
|
||||
//console.log(scoreQueryObj)
|
||||
|
||||
if (_.isEmpty(scoreQueryObj)) {
|
||||
res.status(400).send('provide at least one search parameter.');
|
||||
}
|
||||
scoreAndSearch(q.from, q.to, scoreQueryObj).then(searchResults => {
|
||||
|
||||
//response.data = searchResults
|
||||
@ -65,19 +52,27 @@ function searchHandler(dbConn) {
|
||||
} else {
|
||||
response = searchResults
|
||||
}
|
||||
|
||||
// FILTER NULLSCORES
|
||||
if (_.get(q, 'showRegionsWithNullScore', false)) {
|
||||
console.log('without null scores');
|
||||
response = response.filter(el => !_.some(el.scores, score => _.isNaN(score.score) || _.isNull(score.score) || _.isUndefined(score.score)))//.filter(el => !_.isNaN(el.score))
|
||||
}
|
||||
// SEND RESPONSE
|
||||
res.json(response)
|
||||
|
||||
}).catch(e => {
|
||||
// TODO error handling
|
||||
console.log(e)
|
||||
res.json(e.message)
|
||||
res.status(400).send(e.message)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function filterByString(searchResults, filterString, boolFulltext) {
|
||||
return _.filter(searchResults, region => {
|
||||
console.log("filtering: filterString, boolFulltext");
|
||||
console.log(filterString, boolFulltext);
|
||||
// console.log("filtering: filterString, boolFulltext");
|
||||
// console.log(filterString, boolFulltext);
|
||||
filterString = filterString.toLowerCase()
|
||||
let name = region.name.toLowerCase()
|
||||
let country = region.country.toLowerCase()
|
||||
@ -88,3 +83,26 @@ function filterByString(searchResults, filterString, boolFulltext) {
|
||||
return name.includes(filterString) || country.includes(filterString)
|
||||
})
|
||||
}
|
||||
|
||||
function prepareQueries(queries) {
|
||||
let q = {
|
||||
climate: {},
|
||||
costs: {}
|
||||
}
|
||||
// climate
|
||||
if (queries.temperature_mean_max) q.climate.temperature_mean_max = queries.temperature_mean_max
|
||||
if (queries.precipitation) q.climate.precipitation = queries.precipitation
|
||||
if (queries.rain_days) q.climate.rain_days = queries.rain_days
|
||||
if (queries.sun_hours) q.climate.sun_hours = queries.sun_hours
|
||||
|
||||
// costs
|
||||
if (queries.accommodation_costs) q.costs.accommodation_costs = queries.accommodation_costs
|
||||
if (queries.food_costs) q.costs.food_costs = queries.food_costs
|
||||
if (queries.alcohol_costs) q.costs.alcohol_costs = queries.alcohol_costs
|
||||
if (queries.water_costs) q.costs.water_costs = queries.water_costs
|
||||
if (queries.local_transportation_costs) q.costs.local_transportation_costs = queries.local_transportation_costs
|
||||
if (queries.entertainment_costs) q.costs.entertainment_costs = queries.entertainment_costs
|
||||
if (queries.average_per_day_costs) q.costs.average_per_day_costs = queries.average_per_day_costs
|
||||
|
||||
return q
|
||||
}
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
exports.oldToNewQuerySyntax = function (queries) {
|
||||
let res = {}
|
||||
const _ = require('lodash')
|
||||
|
||||
module.exports = function (queries) {
|
||||
let res = _.clone(queries)
|
||||
try {
|
||||
if (queries.temperature_mean_max) res.temperature_mean_max = [Number(queries.temperature_mean_max.split(',')[0]), Number(queries.temperature_mean_max.split(',')[1])]
|
||||
if (queries.precipitation) res.precipitation = [Number(queries.precipitation.split(',')[0]), Number(queries.precipitation.split(',')[1])]
|
||||
@ -10,7 +12,7 @@ exports.oldToNewQuerySyntax = function (queries) {
|
||||
if (queries.food_costs) res.food_costs = [Number(queries.food_costs.split(',')[0]), Number(queries.food_costs.split(',')[1])]
|
||||
if (queries.alcohol_costs) res.alcohol_costs = [Number(queries.alcohol_costs.split(',')[0]), Number(queries.alcohol_costs.split(',')[1])]
|
||||
if (queries.water_costs) res.water_costs = [Number(queries.water_costs.split(',')[0]), Number(queries.water_costs.split(',')[1])]
|
||||
if (queries.public_transportation_costs) res.public_transportation_costs = [Number(queries.public_transportation_costs.split(',')[0]), Number(queries.public_transportation_costs.split(',')[1])]
|
||||
if (queries.local_transportation_costs) res.local_transportation_costs = [Number(queries.local_transportation_costs.split(',')[0]), Number(queries.local_transportation_costs.split(',')[1])]
|
||||
if (queries.entertainment_costs) res.entertainment_costs = [Number(queries.entertainment_costs.split(',')[0]), Number(queries.entertainment_costs.split(',')[1])]
|
||||
if (queries.average_per_day_costs) res.average_per_day_costs = [Number(queries.average_per_day_costs.split(',')[0]), Number(queries.average_per_day_costs.split(',')[1])]
|
||||
console.log('queries successfully transformed');
|
||||
|
||||
@ -1,8 +1,6 @@
|
||||
const _ = require('lodash')
|
||||
const moment = require("moment")
|
||||
const getClimateMinMax = require("./getClimateMinMax.js")
|
||||
const oldToNewQuerySyntax = require("./oldToNewQuerySyntax.js")
|
||||
const getAllRegionsWithClimatePerMonth = require('./getAllRegionsWithClimatePerMonth')
|
||||
const score = require('./score')
|
||||
const getRegions = require('../models/getRegions.js')
|
||||
|
||||
@ -12,31 +10,30 @@ const MULTIPLIER = {
|
||||
precipitation: 3.5,
|
||||
rain_days: 3,
|
||||
sun_hours: 2.5,
|
||||
|
||||
accommodation_costs: 5,
|
||||
food_costs: 5,
|
||||
alcohol_costs: 5,
|
||||
water_costs: 5,
|
||||
public_transportation_costs: 5,
|
||||
local_transportation_costs: 5,
|
||||
entertainment_costs: 5,
|
||||
average_per_day_costs: 5
|
||||
}
|
||||
|
||||
module.exports = function (dbConn) {
|
||||
return async function (from, to, queries) {
|
||||
return async function (from, to, q) {
|
||||
console.log('search')
|
||||
console.log((_.isEmpty(to) || _.isEmpty(from)) && !_.isEmpty(q))
|
||||
if ( (_.isEmpty(to) || _.isEmpty(from)) && (!_.isEmpty(q.climate) || !_.isEmpty(q.costs)) ) {
|
||||
throw new Error('invalid query')
|
||||
}
|
||||
// PREPARE SEARCH
|
||||
// validate dates
|
||||
const dates = validateDates(from, to)
|
||||
|
||||
// transform syntax and seperate climate queries from price queries
|
||||
queries = oldToNewQuerySyntax.oldToNewQuerySyntax(queries)
|
||||
console.log(queries)
|
||||
const q = prepareQueries(queries)
|
||||
console.log('q', q)
|
||||
|
||||
// for calculating average if traveldates are in more than one month
|
||||
const travelPeriods = travelPeriodsFromDates(dates)
|
||||
|
||||
|
||||
// FETCH DATA FROM DB
|
||||
const boundaryClimate = await getClimateMinMax.getClimateMinMax(dbConn)
|
||||
let regions = await getRegions(dbConn)
|
||||
@ -47,7 +44,7 @@ module.exports = function (dbConn) {
|
||||
food_costs: 100,
|
||||
alcohol_costs: 100,
|
||||
water_costs: 100,
|
||||
public_transportation_costs: 100,
|
||||
local_transportation_costs: 100,
|
||||
entertainment_costs: 100,
|
||||
average_per_day_costs: 1000
|
||||
|
||||
@ -57,7 +54,7 @@ module.exports = function (dbConn) {
|
||||
food_costs: 0,
|
||||
alcohol_costs: 0,
|
||||
water_costs: 0,
|
||||
public_transportation_costs: 0,
|
||||
local_transportation_costs: 0,
|
||||
entertainment_costs: 0,
|
||||
average_per_day_costs: 0
|
||||
}
|
||||
@ -85,6 +82,7 @@ module.exports = function (dbConn) {
|
||||
reg.scores.push(finalScoreObj)
|
||||
});
|
||||
|
||||
reg.price_tendency_relative = getAverageFromTrivago(travelPeriods, reg)
|
||||
|
||||
// CALCULATE AVERAGE SCORE
|
||||
reg.score = calculateAverage(reg.scores)
|
||||
@ -120,7 +118,7 @@ module.exports = function (dbConn) {
|
||||
if (queries.food_costs) q.costs.food_costs = queries.food_costs
|
||||
if (queries.alcohol_costs) q.costs.alcohol_costs = queries.alcohol_costs
|
||||
if (queries.water_costs) q.costs.water_costs = queries.water_costs
|
||||
if (queries.public_transportation_costs) q.costs.public_transportation_costs = queries.public_transportation_costs
|
||||
if (queries.local_transportation_costs) q.costs.local_transportation_costs = queries.local_transportation_costs
|
||||
if (queries.entertainment_costs) q.costs.entertainment_costs = queries.entertainment_costs
|
||||
if (queries.average_per_day_costs) q.costs.average_per_day_costs = queries.average_per_day_costs
|
||||
|
||||
@ -246,6 +244,37 @@ module.exports = function (dbConn) {
|
||||
return finScore
|
||||
}
|
||||
|
||||
function getAverageFromTrivago(travelPeriods, region) {
|
||||
console.log('getAverageFromTrivago for', region.name)
|
||||
|
||||
const singleScores = travelPeriods.map(period => {
|
||||
let res = {
|
||||
//region_id: x.region_id,
|
||||
value: region.avg_price_relative[period.month - 1],
|
||||
days: period.days
|
||||
}
|
||||
|
||||
return res
|
||||
})
|
||||
|
||||
let averagedScore = {
|
||||
value: 0,
|
||||
days: 0
|
||||
}
|
||||
singleScores.forEach(el => {
|
||||
if (el.value !== null) {
|
||||
averagedScore.value += (el.value * el.days)
|
||||
averagedScore.days += (el.days)
|
||||
} else {
|
||||
console.log('skip averaging')
|
||||
console.log(el)
|
||||
|
||||
}
|
||||
})
|
||||
averagedScore.value = _.round(averagedScore.value / averagedScore.days, 1)
|
||||
|
||||
return averagedScore.value
|
||||
}
|
||||
|
||||
//end
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user