Search handling for null-Subscors

This commit is contained in:
Timo John 2020-06-18 16:07:57 +02:00
parent 961760601d
commit b954e356a0

View File

@ -74,18 +74,13 @@ module.exports = function (dbConn) {
// 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)
});
@ -99,9 +94,14 @@ module.exports = function (dbConn) {
function calculateAverage(scores) {
let sum = 0
scores.forEach(el => sum += el.score)
//console.log(sum)
return _.round(sum / scores.length, 2)
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)
}
function prepareQueries(queries) {
@ -174,7 +174,6 @@ module.exports = function (dbConn) {
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
@ -186,14 +185,12 @@ module.exports = function (dbConn) {
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'.")
}
console.log(fromAndTo)
return fromAndTo
}
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)
@ -204,7 +201,6 @@ module.exports = function (dbConn) {
score: (region[type][period.month - 1] === null || searchLowParam === null) ? null : sc,
days: period.days
}
//console.log(res);
return res
})
@ -217,7 +213,6 @@ module.exports = function (dbConn) {
}
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)
@ -237,7 +232,6 @@ module.exports = function (dbConn) {
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 = {
@ -253,7 +247,5 @@ module.exports = function (dbConn) {
}
//end
}