Search handling for null-Subscors
This commit is contained in:
parent
961760601d
commit
b954e356a0
@ -8,252 +8,244 @@ const getRegions = require('../models/getRegions.js')
|
||||
|
||||
const SHOW_ALL_SCOREOBJECTS = false
|
||||
const MULTIPLIER = {
|
||||
temperature_mean_max: 5,
|
||||
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,
|
||||
entertainment_costs: 5,
|
||||
average_per_day_costs: 5
|
||||
temperature_mean_max: 5,
|
||||
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,
|
||||
entertainment_costs: 5,
|
||||
average_per_day_costs: 5
|
||||
}
|
||||
|
||||
module.exports = function (dbConn) {
|
||||
return async function (from, to, queries) {
|
||||
console.log('search')
|
||||
// PREPARE SEARCH
|
||||
// validate dates
|
||||
const dates = validateDates(from, to)
|
||||
return async function (from, to, queries) {
|
||||
console.log('search')
|
||||
// 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)
|
||||
// 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)
|
||||
// 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)
|
||||
regions.forEach(reg => reg.scores = [])
|
||||
const boundaryCosts = {
|
||||
max: {
|
||||
accommodation_costs: 500,
|
||||
food_costs: 100,
|
||||
alcohol_costs: 100,
|
||||
water_costs: 100,
|
||||
public_transportation_costs: 100,
|
||||
entertainment_costs: 100,
|
||||
average_per_day_costs: 1000
|
||||
// FETCH DATA FROM DB
|
||||
const boundaryClimate = await getClimateMinMax.getClimateMinMax(dbConn)
|
||||
let regions = await getRegions(dbConn)
|
||||
regions.forEach(reg => reg.scores = [])
|
||||
const boundaryCosts = {
|
||||
max: {
|
||||
accommodation_costs: 500,
|
||||
food_costs: 100,
|
||||
alcohol_costs: 100,
|
||||
water_costs: 100,
|
||||
public_transportation_costs: 100,
|
||||
entertainment_costs: 100,
|
||||
average_per_day_costs: 1000
|
||||
|
||||
},
|
||||
min: {
|
||||
accommodation_costs: 0,
|
||||
food_costs: 0,
|
||||
alcohol_costs: 0,
|
||||
water_costs: 0,
|
||||
public_transportation_costs: 0,
|
||||
entertainment_costs: 0,
|
||||
average_per_day_costs: 0
|
||||
}
|
||||
}
|
||||
|
||||
// little tweak to show score object without request
|
||||
if (SHOW_ALL_SCOREOBJECTS) {
|
||||
if (!q.climate.temperature_mean_max) q.climate.temperature_mean_max = [null, null]
|
||||
if (!q.climate.precipitation) q.climate.precipitation = [null, null]
|
||||
if (!q.climate.rain_days) q.climate.rain_days = [null, null]
|
||||
if (!q.climate.sun_hours) q.climate.sun_hours = [null, null]
|
||||
if (!q.climate.accommodation_costs) q.climate.accommodation_costs = [null, null]
|
||||
}
|
||||
// CALCULATE SCORES FOR CLIMATE PROPS
|
||||
regions.forEach(reg => {
|
||||
Object.entries(q.climate).forEach(([key, value]) => {
|
||||
// console.log('key', key)
|
||||
// console.log('val', value[0], value[1])
|
||||
let finalScoreObj = getScoreAndAverageFromClimate(key, travelPeriods, reg, value[0], value[1], boundaryClimate)
|
||||
reg.scores.push(finalScoreObj)
|
||||
});
|
||||
|
||||
// CALCULATE SCORES FOR PRICE PROPS
|
||||
Object.entries(q.costs).forEach(([key, value]) => {
|
||||
console.log('key', key)
|
||||
console.log('val', value[0], value[1])
|
||||
let finalScoreObj = getScoreFromCosts(key, reg, value[0], value[1], boundaryCosts)
|
||||
console.log(finalScoreObj);
|
||||
|
||||
reg.scores.push(finalScoreObj)
|
||||
});
|
||||
|
||||
|
||||
// CALCULATE AVERAGE SCORE
|
||||
reg.score = calculateAverage(reg.scores)
|
||||
})
|
||||
return _.orderBy(regions, ({ score }) => score || 0, 'desc') //.filter(el => !_.isNaN(el.score))
|
||||
}
|
||||
|
||||
function calculateAverage(scores) {
|
||||
let sum = 0
|
||||
scores.forEach(el => sum += el.score)
|
||||
//console.log(sum)
|
||||
return _.round(sum / scores.length, 2)
|
||||
}
|
||||
|
||||
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.public_transportation_costs) q.costs.public_transportation_costs = queries.public_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
|
||||
}
|
||||
|
||||
function travelPeriodsFromDates(dates) {
|
||||
//console.log(dates);
|
||||
|
||||
let travelPeriods = []
|
||||
if (dates.from.month === dates.to.month) {
|
||||
let period = {
|
||||
month: dates.from.month,
|
||||
days: dates.to.day - dates.from.day
|
||||
}
|
||||
travelPeriods.push(period)
|
||||
} else {
|
||||
for (let i = dates.from.month; i <= dates.to.month; i++) {
|
||||
let period = {}
|
||||
if (i === dates.from.month) {
|
||||
period = {
|
||||
month: i,
|
||||
days: 32 - dates.from.day
|
||||
}
|
||||
} else if (i === dates.to.month) {
|
||||
period = {
|
||||
month: i,
|
||||
days: dates.to.day
|
||||
}
|
||||
} else {
|
||||
period = {
|
||||
month: i,
|
||||
days: 30
|
||||
}
|
||||
},
|
||||
min: {
|
||||
accommodation_costs: 0,
|
||||
food_costs: 0,
|
||||
alcohol_costs: 0,
|
||||
water_costs: 0,
|
||||
public_transportation_costs: 0,
|
||||
entertainment_costs: 0,
|
||||
average_per_day_costs: 0
|
||||
}
|
||||
}
|
||||
travelPeriods.push(period)
|
||||
}
|
||||
|
||||
// little tweak to show score object without request
|
||||
if (SHOW_ALL_SCOREOBJECTS) {
|
||||
if (!q.climate.temperature_mean_max) q.climate.temperature_mean_max = [null, null]
|
||||
if (!q.climate.precipitation) q.climate.precipitation = [null, null]
|
||||
if (!q.climate.rain_days) q.climate.rain_days = [null, null]
|
||||
if (!q.climate.sun_hours) q.climate.sun_hours = [null, null]
|
||||
if (!q.climate.accommodation_costs) q.climate.accommodation_costs = [null, null]
|
||||
}
|
||||
// CALCULATE SCORES FOR CLIMATE PROPS
|
||||
regions.forEach(reg => {
|
||||
Object.entries(q.climate).forEach(([key, value]) => {
|
||||
let finalScoreObj = getScoreAndAverageFromClimate(key, travelPeriods, reg, value[0], value[1], boundaryClimate)
|
||||
reg.scores.push(finalScoreObj)
|
||||
});
|
||||
|
||||
// CALCULATE SCORES FOR PRICE PROPS
|
||||
Object.entries(q.costs).forEach(([key, value]) => {
|
||||
let finalScoreObj = getScoreFromCosts(key, reg, value[0], value[1], boundaryCosts)
|
||||
|
||||
reg.scores.push(finalScoreObj)
|
||||
});
|
||||
|
||||
|
||||
// CALCULATE AVERAGE SCORE
|
||||
reg.score = calculateAverage(reg.scores)
|
||||
})
|
||||
return _.orderBy(regions, ({score}) => score || 0, 'desc') //.filter(el => !_.isNaN(el.score))
|
||||
}
|
||||
return travelPeriods
|
||||
}
|
||||
|
||||
function validateDates(from, to) {
|
||||
let fromAndTo = {
|
||||
from: {},
|
||||
to: {}
|
||||
function calculateAverage(scores) {
|
||||
let sum = 0
|
||||
let cnt = 0
|
||||
scores.forEach(el => {
|
||||
if (el.score !== null && el.score !== undefined && !_.isNaN(el.score)) {
|
||||
cnt++
|
||||
sum += el.score
|
||||
}
|
||||
})
|
||||
return _.round(sum / cnt, 2)
|
||||
}
|
||||
if (_.isNumber(from) && _.isNumber(to)) {
|
||||
let dateFrom = new Date(from)
|
||||
fromAndTo.from.day = dateFrom.getDate()
|
||||
fromAndTo.from.month = dateFrom.getMonth() + 1
|
||||
let dateTo = new Date(to)
|
||||
fromAndTo.to.day = dateTo.getDate()
|
||||
fromAndTo.to.month = dateTo.getMonth() + 1
|
||||
//console.log(dateFrom.toUTCString(), dateTo.toUTCString())
|
||||
if (moment(dateFrom).add(23, 'hours').isAfter(moment(dateTo))) throw new Error("ERR: 'to' must be at least one day after 'from'.")
|
||||
} else {
|
||||
// this block to still support old query syntax, validating from and to parameter
|
||||
let re = /([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/i;
|
||||
fromAndTo.from.month = Number(from.split("-")[1])
|
||||
fromAndTo.to.month = Number(to.split("-")[1])
|
||||
fromAndTo.from.day = Number(from.split("-")[2])
|
||||
fromAndTo.to.day = 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'.")
|
||||
|
||||
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.public_transportation_costs) q.costs.public_transportation_costs = queries.public_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
|
||||
}
|
||||
console.log(fromAndTo)
|
||||
|
||||
return fromAndTo
|
||||
}
|
||||
function travelPeriodsFromDates(dates) {
|
||||
//console.log(dates);
|
||||
|
||||
function getScoreAndAverageFromClimate(type, travelPeriods, region, searchLowParam, searchMaxParam, minMax) {
|
||||
console.log('getScoreAndAverageFromClimate for', region.name, type)
|
||||
// console.log(type, travelPeriods, searchLowParam, searchMaxParam)
|
||||
|
||||
const singleScores = travelPeriods.map(period => {
|
||||
const sc = _.round(score.calculateScoreRange(minMax.min[type], minMax.max[type], MULTIPLIER[type], region[type][period.month - 1], searchLowParam, searchMaxParam), 2)
|
||||
let res = {
|
||||
//region_id: x.region_id,
|
||||
type: type,
|
||||
value: region[type][period.month - 1],
|
||||
score: (region[type][period.month - 1] === null || searchLowParam === null) ? null : sc,
|
||||
days: period.days
|
||||
}
|
||||
//console.log(res);
|
||||
|
||||
return res
|
||||
})
|
||||
|
||||
let averagedScore = {
|
||||
type: type,
|
||||
value: 0,
|
||||
score: 0,
|
||||
days: 0
|
||||
let travelPeriods = []
|
||||
if (dates.from.month === dates.to.month) {
|
||||
let period = {
|
||||
month: dates.from.month,
|
||||
days: dates.to.day - dates.from.day
|
||||
}
|
||||
travelPeriods.push(period)
|
||||
} else {
|
||||
for (let i = dates.from.month; i <= dates.to.month; i++) {
|
||||
let period = {}
|
||||
if (i === dates.from.month) {
|
||||
period = {
|
||||
month: i,
|
||||
days: 32 - dates.from.day
|
||||
}
|
||||
} else if (i === dates.to.month) {
|
||||
period = {
|
||||
month: i,
|
||||
days: dates.to.day
|
||||
}
|
||||
} else {
|
||||
period = {
|
||||
month: i,
|
||||
days: 30
|
||||
}
|
||||
}
|
||||
travelPeriods.push(period)
|
||||
}
|
||||
}
|
||||
return travelPeriods
|
||||
}
|
||||
singleScores.forEach(el => {
|
||||
if (el.value !== null) {
|
||||
//console.log(el)
|
||||
averagedScore.value += (el.value * el.days)
|
||||
averagedScore.score += (el.score * el.days)
|
||||
averagedScore.days += (el.days)
|
||||
} else {
|
||||
console.log('skip averaging')
|
||||
console.log(el)
|
||||
|
||||
}
|
||||
})
|
||||
averagedScore.value = _.round(averagedScore.value / averagedScore.days, 1)
|
||||
averagedScore.score = _.round(averagedScore.score / averagedScore.days, 1)
|
||||
if (searchLowParam === null) averagedScore.score = null
|
||||
delete averagedScore.days
|
||||
function validateDates(from, to) {
|
||||
let fromAndTo = {
|
||||
from: {},
|
||||
to: {}
|
||||
}
|
||||
if (_.isNumber(from) && _.isNumber(to)) {
|
||||
let dateFrom = new Date(from)
|
||||
fromAndTo.from.day = dateFrom.getDate()
|
||||
fromAndTo.from.month = dateFrom.getMonth() + 1
|
||||
let dateTo = new Date(to)
|
||||
fromAndTo.to.day = dateTo.getDate()
|
||||
fromAndTo.to.month = dateTo.getMonth() + 1
|
||||
if (moment(dateFrom).add(23, 'hours').isAfter(moment(dateTo))) throw new Error("ERR: 'to' must be at least one day after 'from'.")
|
||||
} else {
|
||||
// this block to still support old query syntax, validating from and to parameter
|
||||
let re = /([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/i;
|
||||
fromAndTo.from.month = Number(from.split("-")[1])
|
||||
fromAndTo.to.month = Number(to.split("-")[1])
|
||||
fromAndTo.from.day = Number(from.split("-")[2])
|
||||
fromAndTo.to.day = 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'.")
|
||||
}
|
||||
|
||||
return averagedScore
|
||||
}
|
||||
|
||||
function getScoreFromCosts(type, region, searchLowParam, searchMaxParam, minMax) {
|
||||
console.log('getScoreFromCosts for', region.name, type)
|
||||
// console.log(type, travelPeriods, searchLowParam, searchMaxParam)
|
||||
const sc = _.round(score.calculateScoreRange(minMax.min[type], minMax.max[type], MULTIPLIER[type], region[type], searchLowParam, searchMaxParam), 2)
|
||||
|
||||
let finScore = {
|
||||
type: type,
|
||||
value: region[type],
|
||||
score: sc,
|
||||
return fromAndTo
|
||||
}
|
||||
finScore.value = _.round(finScore.value, 1)
|
||||
finScore.score = _.round(finScore.score, 1)
|
||||
if (searchLowParam === null) finScore.score = null
|
||||
|
||||
return finScore
|
||||
}
|
||||
function getScoreAndAverageFromClimate(type, travelPeriods, region, searchLowParam, searchMaxParam, minMax) {
|
||||
console.log('getScoreAndAverageFromClimate for', region.name, type)
|
||||
|
||||
const singleScores = travelPeriods.map(period => {
|
||||
const sc = _.round(score.calculateScoreRange(minMax.min[type], minMax.max[type], MULTIPLIER[type], region[type][period.month - 1], searchLowParam, searchMaxParam), 2)
|
||||
let res = {
|
||||
//region_id: x.region_id,
|
||||
type: type,
|
||||
value: region[type][period.month - 1],
|
||||
score: (region[type][period.month - 1] === null || searchLowParam === null) ? null : sc,
|
||||
days: period.days
|
||||
}
|
||||
|
||||
return res
|
||||
})
|
||||
|
||||
let averagedScore = {
|
||||
type: type,
|
||||
value: 0,
|
||||
score: 0,
|
||||
days: 0
|
||||
}
|
||||
singleScores.forEach(el => {
|
||||
if (el.value !== null) {
|
||||
averagedScore.value += (el.value * el.days)
|
||||
averagedScore.score += (el.score * el.days)
|
||||
averagedScore.days += (el.days)
|
||||
} else {
|
||||
console.log('skip averaging')
|
||||
console.log(el)
|
||||
|
||||
}
|
||||
})
|
||||
averagedScore.value = _.round(averagedScore.value / averagedScore.days, 1)
|
||||
averagedScore.score = _.round(averagedScore.score / averagedScore.days, 1)
|
||||
if (searchLowParam === null) averagedScore.score = null
|
||||
delete averagedScore.days
|
||||
|
||||
return averagedScore
|
||||
}
|
||||
|
||||
function getScoreFromCosts(type, region, searchLowParam, searchMaxParam, minMax) {
|
||||
console.log('getScoreFromCosts for', region.name, type)
|
||||
const sc = _.round(score.calculateScoreRange(minMax.min[type], minMax.max[type], MULTIPLIER[type], region[type], searchLowParam, searchMaxParam), 2)
|
||||
|
||||
let finScore = {
|
||||
type: type,
|
||||
value: region[type],
|
||||
score: sc,
|
||||
}
|
||||
finScore.value = _.round(finScore.value, 1)
|
||||
finScore.score = _.round(finScore.score, 1)
|
||||
if (searchLowParam === null) finScore.score = null
|
||||
|
||||
return finScore
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//end
|
||||
//end
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user