Compare commits
1 Commits
develop
...
refactor/c
| Author | SHA1 | Date | |
|---|---|---|---|
| bb548b02c8 |
@ -1,5 +1,9 @@
|
|||||||
const _ = require('lodash')
|
const _ = require('lodash')
|
||||||
|
|
||||||
|
/**
|
||||||
|
* restructures queries to support old and new query syntax
|
||||||
|
* @param {*} queries
|
||||||
|
*/
|
||||||
module.exports = function (queries) {
|
module.exports = function (queries) {
|
||||||
let res = _.clone(queries)
|
let res = _.clone(queries)
|
||||||
console.log(res);
|
console.log(res);
|
||||||
@ -27,12 +31,5 @@ module.exports = function (queries) {
|
|||||||
res.tags.push(queries.tags)
|
res.tags.push(queries.tags)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// console.log(res);
|
|
||||||
|
|
||||||
// console.log('queries successfully transformed');
|
|
||||||
// } catch (error) {
|
|
||||||
// console.log('oldToNewQuerySyntax error');
|
|
||||||
// return queries
|
|
||||||
// }
|
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
@ -1,11 +1,12 @@
|
|||||||
const _ = require('lodash')
|
const _ = require('lodash')
|
||||||
|
|
||||||
|
// simple averaging function for desired amount of elements
|
||||||
exports.calculateAvgScore = (...scores) => {
|
exports.calculateAvgScore = (...scores) => {
|
||||||
return avgScore = scores.reduce((total, score) => total += score) / scores.length;
|
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) => {
|
exports.calculateScoreRange = (transitionRange, regionVal, sLowVal, sHighVal) => {
|
||||||
//console.log('scores.calculateScoreRange:', min, max, multiplier, regionVal, sLowVal, sHighVal)
|
|
||||||
// return full score when in range
|
// return full score when in range
|
||||||
if (regionVal >= sLowVal && regionVal <= sHighVal) return 10;
|
if (regionVal >= sLowVal && regionVal <= sHighVal) return 10;
|
||||||
// choose value with smallest distance
|
// choose value with smallest distance
|
||||||
@ -13,24 +14,34 @@ exports.calculateScoreRange = (transitionRange, regionVal, sLowVal, sHighVal) =>
|
|||||||
return this.calculateScore(transitionRange, regionVal, sVal);
|
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) => {
|
exports.calculateScore = (transitionRange, regionVal, searchVal) => {
|
||||||
let score = 1 - (Math.abs(searchVal - regionVal) / transitionRange);
|
let score = 1 - (Math.abs(searchVal - regionVal) / transitionRange);
|
||||||
return (score) * 10;
|
return (score) * 10;
|
||||||
//return score <= 0 ? 0 : score * 10;
|
//return score <= 0 ? 0 : score * 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// transistion function
|
||||||
exports.linear = function (x, exponent) {
|
exports.linear = function (x, exponent) {
|
||||||
if (x < 0) return 0
|
if (x < 0) return 0
|
||||||
if (x > 10) return 10
|
if (x > 10) return 10
|
||||||
return x
|
return x
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// transistion function
|
||||||
exports.easeOut = function (x, exponent) {
|
exports.easeOut = function (x, exponent) {
|
||||||
if (x < 0) return 0
|
if (x < 0) return 0
|
||||||
if (x > 10) return 10
|
if (x > 10) return 10
|
||||||
return (1 - Math.pow(1 - (x / 10), exponent)) * 10
|
return (1 - Math.pow(1 - (x / 10), exponent)) * 10
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// transistion function
|
||||||
exports.easeInOut = function (sc, exponent) {
|
exports.easeInOut = function (sc, exponent) {
|
||||||
const x = (sc ) / 10
|
const x = (sc ) / 10
|
||||||
// console.log(sc, x);
|
// 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
|
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) {
|
exports.easeInOutAsymmetric = function (sc, exponent) {
|
||||||
const x = (sc ) / 10
|
const x = (sc ) / 10
|
||||||
// console.log(sc, x);
|
// 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
|
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) {
|
exports.sigmoid = function (x, exponent) {
|
||||||
// const sigm = (1 / (1 + Math.pow(Math.E, 5 * -x))) * 10 + 5
|
// const sigm = (1 / (1 + Math.pow(Math.E, 5 * -x))) * 10 + 5
|
||||||
// const sigm = 10 / (1 + Math.pow(Math.E, 1.2 * -x + 6))
|
// const sigm = 10 / (1 + Math.pow(Math.E, 1.2 * -x + 6))
|
||||||
|
|||||||
@ -79,14 +79,10 @@ function sumForRangeAvg(from, to, avg) {
|
|||||||
return duration * avg
|
return duration * avg
|
||||||
}
|
}
|
||||||
|
|
||||||
function sumForRangeFromDailyValues(from, to, dailyValues) {
|
/**
|
||||||
// NOT NEEDED YET
|
* calculates Average for all scoreobjects of 'scores'
|
||||||
// for (var m = moment(from).subtract(1, 'months'); m.isSameOrBefore(moment(to).subtract(1, 'months')); m.add(1, 'day')) {
|
* @param {[score]} scores array of score objects
|
||||||
// console.log(m);
|
*/
|
||||||
// }
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function calculateAverage(scores) {
|
function calculateAverage(scores) {
|
||||||
let sum = 0
|
let sum = 0
|
||||||
let cnt = 0
|
let cnt = 0
|
||||||
@ -103,19 +99,15 @@ function calculateAverage(scores) {
|
|||||||
//if (sum === 0 && cnt === 0) return 0
|
//if (sum === 0 && cnt === 0) return 0
|
||||||
return _.round(sum / cnt, 3)
|
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) {
|
function travelPeriodsFromDates(dates) {
|
||||||
let start = moment(`${dates.from.year}-${dates.from.month}-${dates.from.day}`, 'YYYY-MM-DD')
|
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')
|
let end = moment(`${dates.to.year}-${dates.to.month}-${dates.to.day}`, 'YYYY-MM-DD')
|
||||||
console.log('start:', moment(start));
|
console.log('start:', moment(start));
|
||||||
console.log('end:', moment(end));
|
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 = []
|
let travelPeriods = []
|
||||||
@ -137,6 +129,12 @@ function travelPeriodsFromDates(dates) {
|
|||||||
return travelPeriods
|
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) {
|
function validateDates(from, to) {
|
||||||
let fromAndTo = {
|
let fromAndTo = {
|
||||||
from: {},
|
from: {},
|
||||||
@ -169,6 +167,7 @@ function validateDates(from, to) {
|
|||||||
return fromAndTo
|
return fromAndTo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// helper function for calculating travelperiods
|
||||||
function createPeriod(start, end, currentMonth, currentYear) {
|
function createPeriod(start, end, currentMonth, currentYear) {
|
||||||
let period = {}
|
let period = {}
|
||||||
console.log(start, end, currentMonth, currentYear);
|
console.log(start, end, currentMonth, currentYear);
|
||||||
@ -195,10 +194,15 @@ function createPeriod(start, end, currentMonth, currentYear) {
|
|||||||
return period
|
return period
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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) {
|
function calculateScoreForPeriod(type, travelPeriods, region, searchLowParam, searchMaxParam) {
|
||||||
// console.log('getScoreAndAverageFromClimate for', region.name, type)
|
|
||||||
|
|
||||||
const singleScores = travelPeriods. map(period => {
|
const singleScores = travelPeriods. map(period => {
|
||||||
let res = {
|
let res = {
|
||||||
type: type,
|
type: type,
|
||||||
@ -218,10 +222,6 @@ function calculateScoreForPeriod(type, travelPeriods, region, searchLowParam, se
|
|||||||
if (el.value !== null && !_.isNaN(el.value)) {
|
if (el.value !== null && !_.isNaN(el.value)) {
|
||||||
averagedScore.value += (el.value * el.days)
|
averagedScore.value += (el.value * el.days)
|
||||||
averagedScore.days += (el.days)
|
averagedScore.days += (el.days)
|
||||||
} else {
|
|
||||||
// console.log('skip averaging')
|
|
||||||
// console.log(el)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
averagedScore.value = _.round(averagedScore.value / averagedScore.days, 3)
|
averagedScore.value = _.round(averagedScore.value / averagedScore.days, 3)
|
||||||
@ -240,7 +240,11 @@ function calculateScoreForPeriod(type, travelPeriods, region, searchLowParam, se
|
|||||||
|
|
||||||
return averagedScore
|
return averagedScore
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* special score calculating function for region tags.
|
||||||
|
* @param {*} regionTags
|
||||||
|
* @param {*} tagStringsFromQueries
|
||||||
|
*/
|
||||||
function scoresFromTags(regionTags, tagStringsFromQueries) {
|
function scoresFromTags(regionTags, tagStringsFromQueries) {
|
||||||
return tagStringsFromQueries.map(tagQuery => {
|
return tagStringsFromQueries.map(tagQuery => {
|
||||||
const tag = regionTags.find(tag => tagQuery === tag.name)
|
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) {
|
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)
|
const sc = _.round(scorer.calculateScoreRange(SETTINGS[type][0], region[type], searchLowParam, searchMaxParam), 3)
|
||||||
|
|
||||||
let finScore = {
|
let finScore = {
|
||||||
@ -274,8 +286,12 @@ function scoreFromSimpleRegionProperty(type, region, searchLowParam, searchMaxPa
|
|||||||
return finScore
|
return finScore
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* simple function to calculate average price tendencies for given travel time
|
||||||
|
* @param {*} travelPeriods
|
||||||
|
* @param {*} region
|
||||||
|
*/
|
||||||
function getAverageFromTrivago(travelPeriods, region) {
|
function getAverageFromTrivago(travelPeriods, region) {
|
||||||
// console.log('getAverageFromTrivago for', region.name)
|
|
||||||
|
|
||||||
const singleScores = travelPeriods.map(period => {
|
const singleScores = travelPeriods.map(period => {
|
||||||
let res = {
|
let res = {
|
||||||
@ -294,10 +310,6 @@ function getAverageFromTrivago(travelPeriods, region) {
|
|||||||
if (el.value !== null && !_.isNaN(el.value)) {
|
if (el.value !== null && !_.isNaN(el.value)) {
|
||||||
averagedScore.value += (el.value * el.days)
|
averagedScore.value += (el.value * el.days)
|
||||||
averagedScore.days += (el.days)
|
averagedScore.days += (el.days)
|
||||||
} else {
|
|
||||||
// console.log('skip averaging')
|
|
||||||
// console.log(el)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
averagedScore.value = _.round(averagedScore.value / averagedScore.days, 2)
|
averagedScore.value = _.round(averagedScore.value / averagedScore.days, 2)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user