From bb548b02c86ea2b69a997bbd387f43ca221f626e Mon Sep 17 00:00:00 2001 From: Timo Volkmann Date: Fri, 10 Jul 2020 16:05:06 +0200 Subject: [PATCH] added comments --- backend/util/oldToNewQuerySyntax.js | 11 ++-- backend/util/score.js | 15 +++++- backend/util/scoreAndSearch.js | 78 +++++++++++++++++------------ 3 files changed, 63 insertions(+), 41 deletions(-) diff --git a/backend/util/oldToNewQuerySyntax.js b/backend/util/oldToNewQuerySyntax.js index 6b19c66..f0ffae7 100644 --- a/backend/util/oldToNewQuerySyntax.js +++ b/backend/util/oldToNewQuerySyntax.js @@ -1,5 +1,9 @@ const _ = require('lodash') +/** + * restructures queries to support old and new query syntax + * @param {*} queries + */ module.exports = function (queries) { let res = _.clone(queries) console.log(res); @@ -27,12 +31,5 @@ module.exports = function (queries) { res.tags.push(queries.tags) } } - // console.log(res); - - // console.log('queries successfully transformed'); - // } catch (error) { - // console.log('oldToNewQuerySyntax error'); - // return queries - // } return res } \ No newline at end of file diff --git a/backend/util/score.js b/backend/util/score.js index 334897a..1f85291 100644 --- a/backend/util/score.js +++ b/backend/util/score.js @@ -1,11 +1,12 @@ const _ = require('lodash') +// simple averaging function for desired amount of elements exports.calculateAvgScore = (...scores) => { return avgScore = scores.reduce((total, score) => total += score) / scores.length; } +// extends scoring function to be used with high and low bounded search parameters exports.calculateScoreRange = (transitionRange, 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,24 +14,34 @@ exports.calculateScoreRange = (transitionRange, regionVal, sLowVal, sHighVal) => return this.calculateScore(transitionRange, regionVal, sVal); } +/** + * calculates a score from the distance of two values. returns value between 0 and 10 + * @param {Number} transitionRange transition range defines the width of the area between 0 and 10 + * @param {Number} regionVal actual value + * @param {Number} searchVal desired value + * @returns {Number} Score + */ exports.calculateScore = (transitionRange, regionVal, searchVal) => { let score = 1 - (Math.abs(searchVal - regionVal) / transitionRange); return (score) * 10; //return score <= 0 ? 0 : score * 10; } +// transistion function exports.linear = function (x, exponent) { if (x < 0) return 0 if (x > 10) return 10 return x } +// transistion function exports.easeOut = function (x, exponent) { if (x < 0) return 0 if (x > 10) return 10 return (1 - Math.pow(1 - (x / 10), exponent)) * 10 } +// transistion function exports.easeInOut = function (sc, exponent) { const x = (sc ) / 10 // console.log(sc, x); @@ -39,6 +50,7 @@ exports.easeInOut = function (sc, exponent) { return x < 0.5 ? Math.pow(2, exponent-1) * Math.pow(x,exponent) * 10 : (1 - Math.pow(-2 * x + 2, exponent)/2) * 10 } +// transistion function exports.easeInOutAsymmetric = function (sc, exponent) { const x = (sc ) / 10 // console.log(sc, x); @@ -47,6 +59,7 @@ exports.easeInOutAsymmetric = function (sc, exponent) { return x < 0.5 ? (2 * x) - 0.5 * 10 : (1 - Math.pow(-2 * x + 2, exponent)/2) * 10 } +// transistion function exports.sigmoid = function (x, exponent) { // const sigm = (1 / (1 + Math.pow(Math.E, 5 * -x))) * 10 + 5 // const sigm = 10 / (1 + Math.pow(Math.E, 1.2 * -x + 6)) diff --git a/backend/util/scoreAndSearch.js b/backend/util/scoreAndSearch.js index 9d3d20d..93f90aa 100644 --- a/backend/util/scoreAndSearch.js +++ b/backend/util/scoreAndSearch.js @@ -79,14 +79,10 @@ function sumForRangeAvg(from, to, avg) { return duration * avg } -function sumForRangeFromDailyValues(from, to, dailyValues) { - // NOT NEEDED YET - // for (var m = moment(from).subtract(1, 'months'); m.isSameOrBefore(moment(to).subtract(1, 'months')); m.add(1, 'day')) { - // console.log(m); - // } -} - - +/** + * calculates Average for all scoreobjects of 'scores' + * @param {[score]} scores array of score objects + */ function calculateAverage(scores) { let sum = 0 let cnt = 0 @@ -103,20 +99,16 @@ function calculateAverage(scores) { //if (sum === 0 && cnt === 0) return 0 return _.round(sum / cnt, 3) } - +/** + * Transforms common dateobject from search to travel periods needed for averaging calculations on climate and price data + * @param {customDateObject} dates + */ function travelPeriodsFromDates(dates) { let start = moment(`${dates.from.year}-${dates.from.month}-${dates.from.day}`, 'YYYY-MM-DD') let end = moment(`${dates.to.year}-${dates.to.month}-${dates.to.day}`, 'YYYY-MM-DD') console.log('start:', moment(start)); console.log('end:', moment(end)); - // console.log('start:', moment(start).toISOString()); - // console.log('end:', moment(end).toISOString()); - // console.log('start:', moment(dates.from)); - // console.log('end:', moment(dates.to)); - console.log(); - console.log(); - console.log(); - + let travelPeriods = [] @@ -137,6 +129,12 @@ function travelPeriodsFromDates(dates) { return travelPeriods } +/** + * Make sure that travel period is at least one day and "from" should not be after "to. + * supports old and new search query syntax (plain text & base64). returns common data structures for both. + * @param {*} from + * @param {*} to + */ function validateDates(from, to) { let fromAndTo = { from: {}, @@ -169,6 +167,7 @@ function validateDates(from, to) { return fromAndTo } +// helper function for calculating travelperiods function createPeriod(start, end, currentMonth, currentYear) { let period = {} console.log(start, end, currentMonth, currentYear); @@ -195,10 +194,15 @@ function createPeriod(start, end, currentMonth, currentYear) { return period } - -function calculateScoreForPeriod(type, travelPeriods, region, searchLowParam, searchMaxParam) { - // console.log('getScoreAndAverageFromClimate for', region.name, type) - +/** + * Generic function for calculating the score for a certain scoreobject (type). i.e. 'humidity' or 'avg_price_relative' + * @param {*} type can be every property of a region which consists of an array of 12 elements (one for each month) + * @param {*} travelPeriods + * @param {*} region + * @param {*} searchLowParam + * @param {*} searchMaxParam + */ +function calculateScoreForPeriod(type, travelPeriods, region, searchLowParam, searchMaxParam) { const singleScores = travelPeriods. map(period => { let res = { type: type, @@ -218,10 +222,6 @@ function calculateScoreForPeriod(type, travelPeriods, region, searchLowParam, se if (el.value !== null && !_.isNaN(el.value)) { 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, 3) @@ -240,7 +240,11 @@ function calculateScoreForPeriod(type, travelPeriods, region, searchLowParam, se return averagedScore } - +/** + * special score calculating function for region tags. + * @param {*} regionTags + * @param {*} tagStringsFromQueries + */ function scoresFromTags(regionTags, tagStringsFromQueries) { return tagStringsFromQueries.map(tagQuery => { const tag = regionTags.find(tag => tagQuery === tag.name) @@ -258,8 +262,16 @@ function scoresFromTags(regionTags, tagStringsFromQueries) { }) } +/** + * same as calculateScoreForPeriod, exept that it works for a single value property, valid for the whole year (not an array of 12) + * @param {*} type + * @param {*} region + * @param {*} searchLowParam + * @param {*} searchMaxParam + * @param {*} minMax + */ function scoreFromSimpleRegionProperty(type, region, searchLowParam, searchMaxParam, minMax) { - // console.log('getScoreFromCosts for', region.name, type) + const sc = _.round(scorer.calculateScoreRange(SETTINGS[type][0], region[type], searchLowParam, searchMaxParam), 3) let finScore = { @@ -274,9 +286,13 @@ function scoreFromSimpleRegionProperty(type, region, searchLowParam, searchMaxPa return finScore } +/** + * simple function to calculate average price tendencies for given travel time + * @param {*} travelPeriods + * @param {*} region + */ function getAverageFromTrivago(travelPeriods, region) { - // console.log('getAverageFromTrivago for', region.name) - + const singleScores = travelPeriods.map(period => { let res = { value: region.avg_price_relative[period.month - 1], @@ -294,10 +310,6 @@ function getAverageFromTrivago(travelPeriods, region) { if (el.value !== null && !_.isNaN(el.value)) { 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, 2)