implemented scoring for prices
This commit is contained in:
parent
0d82057383
commit
337c1e49cf
5
.vscode/launch.json
vendored
5
.vscode/launch.json
vendored
@ -10,8 +10,9 @@
|
||||
"name": "NodeJS: nodemon debug",
|
||||
"program": "${workspaceFolder}/backend/index.js",
|
||||
"request": "launch",
|
||||
"restart": true,
|
||||
"runtimeExecutable": "nodemon",
|
||||
// "restart": true,
|
||||
"runtimeExecutable": "node",
|
||||
// "runtimeExecutable": "nodemon",
|
||||
"skipFiles": [
|
||||
"<node_internals>/**"
|
||||
],
|
||||
|
||||
@ -42,7 +42,13 @@ function searchHandler(dbConn) {
|
||||
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.accomodation_costs
|
||||
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
|
||||
|
||||
//console.log(scoreQueryObj)
|
||||
|
||||
|
||||
@ -5,11 +5,17 @@ exports.oldToNewQuerySyntax = function (queries) {
|
||||
if (queries.precipitation) res.precipitation = [Number(queries.precipitation.split(',')[0]), Number(queries.precipitation.split(',')[1])]
|
||||
if (queries.rain_days) res.rain_days = [Number(queries.rain_days.split(',')[0]), Number(queries.rain_days.split(',')[1])]
|
||||
if (queries.sun_hours) res.sun_hours = [Number(queries.sun_hours.split(',')[0]), Number(queries.sun_hours.split(',')[1])]
|
||||
if (queries.accomodation_costs) res.sun_hours = [Number(queries.accomodation_costs.split(',')[0]), Number(queries.accomodation_costs.split(',')[1])]
|
||||
// @TimoJ hier noch Parameter hinzufügen wenn du die alte syntax nutzen willst, ansonsten egal
|
||||
|
||||
if (queries.accommodation_costs) res.accommodation_costs = [Number(queries.accommodation_costs.split(',')[0]), Number(queries.accommodation_costs.split(',')[1])]
|
||||
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.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');
|
||||
} catch (error) {
|
||||
console.log('queries are ok');
|
||||
console.log('oldToNewQuerySyntax error');
|
||||
return queries
|
||||
}
|
||||
return res
|
||||
|
||||
@ -5,7 +5,7 @@ exports.calculateAvgScore = (...scores) => {
|
||||
}
|
||||
|
||||
exports.calculateScoreRange = (min, max, multiplier, regionVal, sLowVal, sHighVal) => {
|
||||
//console.log('scores.calculateScoreRange:', min, max, multiplier, regionVal, sLowVal, sHighVal)
|
||||
console.log('scores.calculateScoreRange:', min, max, multiplier, regionVal, sLowVal, sHighVal)
|
||||
// return full score when in range
|
||||
if (regionVal >= sLowVal && regionVal <= sHighVal) return 10;
|
||||
// choose value with smallest distance
|
||||
|
||||
@ -13,7 +13,13 @@ const MULTIPLIER = {
|
||||
precipitation: 3.5,
|
||||
rain_days: 3,
|
||||
sun_hours: 2.5,
|
||||
accomodation_costs: 1
|
||||
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) {
|
||||
@ -25,17 +31,38 @@ module.exports = function (dbConn) {
|
||||
|
||||
// transform syntax and seperate climate queries from price queries
|
||||
queries = oldToNewQuerySyntax.oldToNewQuerySyntax(queries)
|
||||
// console.log(queries)
|
||||
console.log(queries)
|
||||
const q = prepareQueries(queries)
|
||||
// console.log(q)
|
||||
console.log('q', q)
|
||||
|
||||
// for calculating average if traveldates are in more than one month
|
||||
const travelPeriods = travelPeriodsFromDates(dates)
|
||||
|
||||
// FETCH DATA FROM DB
|
||||
const minMax = await getClimateMinMax.getClimateMinMax(dbConn)
|
||||
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) {
|
||||
@ -43,16 +70,26 @@ module.exports = function (dbConn) {
|
||||
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 = getScoreAndAverage(key, travelPeriods, reg, value[0], value[1], minMax)
|
||||
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
|
||||
@ -78,15 +115,22 @@ module.exports = function (dbConn) {
|
||||
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.accomodation_costs) q.climate.accomodation_costs = queries.accomodation_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 = {
|
||||
@ -148,8 +192,8 @@ module.exports = function (dbConn) {
|
||||
return fromAndTo
|
||||
}
|
||||
|
||||
function getScoreAndAverage(type, travelPeriods, region, searchLowParam, searchMaxParam, minMax) {
|
||||
console.log('calculateScores for', region.name, type)
|
||||
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 => {
|
||||
@ -162,7 +206,7 @@ module.exports = function (dbConn) {
|
||||
days: period.days
|
||||
}
|
||||
//console.log(res);
|
||||
|
||||
|
||||
return res
|
||||
})
|
||||
|
||||
@ -191,4 +235,26 @@ module.exports = function (dbConn) {
|
||||
|
||||
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,
|
||||
}
|
||||
finScore.value = _.round(finScore.value, 1)
|
||||
finScore.score = _.round(finScore.score, 1)
|
||||
if (searchLowParam === null) finScore.score = null
|
||||
|
||||
return finScore
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//end
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user