some tag improvements
This commit is contained in:
parent
cd9f68234f
commit
e0f8f5490e
@ -1,9 +1,9 @@
|
||||
const arrayFormatting = require("../util/databaseArrayFormatting.js");
|
||||
const { takeRightWhile } = require("lodash");
|
||||
const { allTagsWithValues } = require("./getTags.js");
|
||||
|
||||
module.exports = async (dbConn) => {
|
||||
const regions = await dbConn.query(
|
||||
`SELECT regions.id AS region_id,
|
||||
const sqlRegions = `SELECT regions.id AS region_id,
|
||||
regions.region AS name,
|
||||
countries.country AS country,
|
||||
regions.description AS description,
|
||||
@ -43,7 +43,7 @@ module.exports = async (dbConn) => {
|
||||
GROUP BY rtma.region_id) rtma
|
||||
ON regions.id = rtma.region_id
|
||||
WHERE regions_byt.travelstyle = 1`
|
||||
);
|
||||
const [regions, tags] = await Promise.all([dbConn.query(sqlRegions), allTagsWithValues(dbConn)])
|
||||
|
||||
for (k = 0; k < regions.length; k++) {
|
||||
regions[k].avg_price_relative = arrayFormatting(regions[k].avg_price_relative);
|
||||
@ -67,6 +67,12 @@ module.exports = async (dbConn) => {
|
||||
if (region.rain_days === null) region.rain_days = emptyArr
|
||||
if (region.sun_hours === null) region.sun_hours = emptyArr
|
||||
if (region.humidity === null) region.humidity = emptyArr
|
||||
|
||||
region.tags = tags.filter(tag => tag.region_id === region.region_id).map(tag => {
|
||||
delete tag.region_id
|
||||
return tag
|
||||
})
|
||||
|
||||
return region
|
||||
});
|
||||
};
|
||||
|
||||
@ -7,7 +7,7 @@ const getSearchPresets = require("../models/getSearchPresets.js");
|
||||
// Utils
|
||||
const _ = require('lodash')
|
||||
const base64 = require("../util/base64.js")
|
||||
const sas = require("../util/scoreAndSearch.js");
|
||||
const scoreAndSearch = require("../util/scoreAndSearch.js");
|
||||
const oldToNewQuerySyntax = require("../util/oldToNewQuerySyntax.js")
|
||||
const { allTagsWithValues, getUniqueTags } = require("../models/getTags.js");
|
||||
const { getClimateMinMax } = require("../util/getClimateMinMax.js");
|
||||
@ -41,7 +41,6 @@ function presetHandler(dbConn) {
|
||||
}
|
||||
|
||||
function searchHandler(dbConn) {
|
||||
const scoreAndSearch = sas(dbConn)
|
||||
return async function (req, res) {
|
||||
let response = {}
|
||||
|
||||
@ -62,10 +61,9 @@ function searchHandler(dbConn) {
|
||||
// CHOOSE PARAMS WHICH SHALL BE PASSED TO SCORE AND SEARCH
|
||||
let scoreQueryObj = prepareQueries(q)
|
||||
|
||||
let [regions, tags, boundaryClimate] = await Promise.all([getRegions(dbConn), allTagsWithValues(dbConn), getClimateMinMax(dbConn)])
|
||||
let [regions, boundaryClimate] = await Promise.all([getRegions(dbConn), getClimateMinMax(dbConn)])
|
||||
let data = {
|
||||
regions: regions,
|
||||
tags: tags,
|
||||
boundaryClimate: boundaryClimate
|
||||
}
|
||||
|
||||
|
||||
@ -22,13 +22,12 @@ const SETTINGS = {
|
||||
avg_price_relative: [3, 'easeOut', 2],
|
||||
}
|
||||
|
||||
module.exports = function (dbConn) {
|
||||
return async function (data, from, to, q) {
|
||||
module.exports = async function (data, from, to, q) {
|
||||
console.log('search')
|
||||
if ((_.isNil(to) || _.isNil(from)) && !(_.isEmpty(q.climate) || _.isEmpty(q.costs) || _.isEmpty(q.others))) {
|
||||
throw new Error('invalid query')
|
||||
}
|
||||
if (_.isNil(data) || _.isEmpty(data.regions) || _.isEmpty(data.tags) || _.isEmpty(data.boundaryClimate)) {
|
||||
if (_.isNil(data) || _.isEmpty(data.regions) || _.isEmpty(data.boundaryClimate)) {
|
||||
throw new Error('database error')
|
||||
}
|
||||
let regionsArr = data.regions
|
||||
@ -64,8 +63,9 @@ module.exports = function (dbConn) {
|
||||
|
||||
// CALCULATE PROPERTIES FOR EACH REGION
|
||||
regionsArr.forEach(reg => {
|
||||
reg.scores = []
|
||||
|
||||
// CALCULATE SCORES FOR CLIMATE PROPS
|
||||
reg.scores = []
|
||||
Object.entries(q.climate).forEach(([key, value]) => {
|
||||
let finalScoreObj = calculateScoreForPeriod(key, travelPeriods, reg, value[0], value[1], data.boundaryClimate)
|
||||
reg.scores.push(finalScoreObj)
|
||||
@ -103,26 +103,27 @@ module.exports = function (dbConn) {
|
||||
if (!_.isEmpty(q.others)) scoreSubGroups.push(calculateAverage(reg.scores.filter(el => _.some(Object.keys(q.others), entry => entry === el.type ) )) )
|
||||
|
||||
// CALCULATE AVERAGE SCORE Stage 2
|
||||
reg.score = calculateAverage(reg.scores)
|
||||
// reg.score = _.sum(scoreSubGroups) / scoreSubGroups.length
|
||||
// reg.score = calculateAverage(reg.scores)
|
||||
reg.score = _.sum(scoreSubGroups) / scoreSubGroups.length
|
||||
|
||||
})
|
||||
return _.orderBy(regionsArr, ({ score }) => score || 0, 'desc') //.filter(el => !_.isNaN(el.score))
|
||||
}
|
||||
}
|
||||
|
||||
function sumForRangeAvg(from, to, avg) {
|
||||
function sumForRangeAvg(from, to, avg) {
|
||||
let duration = moment(to).diff(moment(from), 'days')
|
||||
return duration * avg
|
||||
}
|
||||
}
|
||||
|
||||
function sumForRangeFromDailyValues(from, to, dailyValues) {
|
||||
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);
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function calculateAverage(scores) {
|
||||
function calculateAverage(scores) {
|
||||
let sum = 0
|
||||
let cnt = 0
|
||||
scores.forEach(el => {
|
||||
@ -137,9 +138,9 @@ module.exports = function (dbConn) {
|
||||
})
|
||||
//if (sum === 0 && cnt === 0) return 0
|
||||
return _.round(sum / cnt, 3)
|
||||
}
|
||||
}
|
||||
|
||||
function travelPeriodsFromDates(dates) {
|
||||
function travelPeriodsFromDates(dates) {
|
||||
let travelPeriods = []
|
||||
|
||||
if (dates.from.month === dates.to.month && dates.from.year === dates.to.year) {
|
||||
@ -155,9 +156,9 @@ module.exports = function (dbConn) {
|
||||
}
|
||||
}
|
||||
return travelPeriods
|
||||
}
|
||||
}
|
||||
|
||||
function validateDates(from, to) {
|
||||
function validateDates(from, to) {
|
||||
let fromAndTo = {
|
||||
from: {},
|
||||
to: {}
|
||||
@ -186,9 +187,9 @@ module.exports = function (dbConn) {
|
||||
|
||||
if (moment(fromAndTo.from).add(23, 'hours').isAfter(moment(fromAndTo.to))) throw new Error("ERR: 'to' must be at least one day after 'from'.")
|
||||
return fromAndTo
|
||||
}
|
||||
}
|
||||
|
||||
function createPeriod(from, to, currentMonth, currentYear) {
|
||||
function createPeriod(from, to, currentMonth, currentYear) {
|
||||
let period = {}
|
||||
if (currentMonth === from.month && currentYear === from.year) {
|
||||
period = {
|
||||
@ -207,10 +208,10 @@ module.exports = function (dbConn) {
|
||||
}
|
||||
}
|
||||
return period
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function calculateScoreForPeriod(type, travelPeriods, region, searchLowParam, searchMaxParam, minMax) {
|
||||
function calculateScoreForPeriod(type, travelPeriods, region, searchLowParam, searchMaxParam, minMax) {
|
||||
// console.log('getScoreAndAverageFromClimate for', region.name, type)
|
||||
|
||||
const singleScores = travelPeriods.map(period => {
|
||||
@ -247,9 +248,9 @@ module.exports = function (dbConn) {
|
||||
if (searchLowParam === null) averagedScore.score = null
|
||||
|
||||
return averagedScore
|
||||
}
|
||||
}
|
||||
|
||||
function calculateSimpleScore(type, region, searchLowParam, searchMaxParam, minMax) {
|
||||
function calculateSimpleScore(type, region, searchLowParam, searchMaxParam, minMax) {
|
||||
// console.log('getScoreFromCosts for', region.name, type)
|
||||
const sc = _.round(scorer.calculateScoreRange(minMax.min[type], minMax.max[type], SETTINGS[type][0], region[type], searchLowParam, searchMaxParam), 3)
|
||||
|
||||
@ -263,9 +264,9 @@ module.exports = function (dbConn) {
|
||||
if (searchLowParam === null) finScore.score = null
|
||||
|
||||
return finScore
|
||||
}
|
||||
}
|
||||
|
||||
function getAverageFromTrivago(travelPeriods, region) {
|
||||
function getAverageFromTrivago(travelPeriods, region) {
|
||||
// console.log('getAverageFromTrivago for', region.name)
|
||||
|
||||
const singleScores = travelPeriods.map(period => {
|
||||
@ -294,7 +295,4 @@ module.exports = function (dbConn) {
|
||||
averagedScore.value = _.round(averagedScore.value / averagedScore.days, 2)
|
||||
|
||||
return averagedScore.value
|
||||
}
|
||||
|
||||
//end
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user