some tag improvements
This commit is contained in:
parent
cd9f68234f
commit
e0f8f5490e
@ -1,9 +1,9 @@
|
|||||||
const arrayFormatting = require("../util/databaseArrayFormatting.js");
|
const arrayFormatting = require("../util/databaseArrayFormatting.js");
|
||||||
const { takeRightWhile } = require("lodash");
|
const { takeRightWhile } = require("lodash");
|
||||||
|
const { allTagsWithValues } = require("./getTags.js");
|
||||||
|
|
||||||
module.exports = async (dbConn) => {
|
module.exports = async (dbConn) => {
|
||||||
const regions = await dbConn.query(
|
const sqlRegions = `SELECT regions.id AS region_id,
|
||||||
`SELECT regions.id AS region_id,
|
|
||||||
regions.region AS name,
|
regions.region AS name,
|
||||||
countries.country AS country,
|
countries.country AS country,
|
||||||
regions.description AS description,
|
regions.description AS description,
|
||||||
@ -43,7 +43,7 @@ module.exports = async (dbConn) => {
|
|||||||
GROUP BY rtma.region_id) rtma
|
GROUP BY rtma.region_id) rtma
|
||||||
ON regions.id = rtma.region_id
|
ON regions.id = rtma.region_id
|
||||||
WHERE regions_byt.travelstyle = 1`
|
WHERE regions_byt.travelstyle = 1`
|
||||||
);
|
const [regions, tags] = await Promise.all([dbConn.query(sqlRegions), allTagsWithValues(dbConn)])
|
||||||
|
|
||||||
for (k = 0; k < regions.length; k++) {
|
for (k = 0; k < regions.length; k++) {
|
||||||
regions[k].avg_price_relative = arrayFormatting(regions[k].avg_price_relative);
|
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.rain_days === null) region.rain_days = emptyArr
|
||||||
if (region.sun_hours === null) region.sun_hours = emptyArr
|
if (region.sun_hours === null) region.sun_hours = emptyArr
|
||||||
if (region.humidity === null) region.humidity = 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
|
return region
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@ -7,7 +7,7 @@ const getSearchPresets = require("../models/getSearchPresets.js");
|
|||||||
// Utils
|
// Utils
|
||||||
const _ = require('lodash')
|
const _ = require('lodash')
|
||||||
const base64 = require("../util/base64.js")
|
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 oldToNewQuerySyntax = require("../util/oldToNewQuerySyntax.js")
|
||||||
const { allTagsWithValues, getUniqueTags } = require("../models/getTags.js");
|
const { allTagsWithValues, getUniqueTags } = require("../models/getTags.js");
|
||||||
const { getClimateMinMax } = require("../util/getClimateMinMax.js");
|
const { getClimateMinMax } = require("../util/getClimateMinMax.js");
|
||||||
@ -41,7 +41,6 @@ function presetHandler(dbConn) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function searchHandler(dbConn) {
|
function searchHandler(dbConn) {
|
||||||
const scoreAndSearch = sas(dbConn)
|
|
||||||
return async function (req, res) {
|
return async function (req, res) {
|
||||||
let response = {}
|
let response = {}
|
||||||
|
|
||||||
@ -62,10 +61,9 @@ function searchHandler(dbConn) {
|
|||||||
// CHOOSE PARAMS WHICH SHALL BE PASSED TO SCORE AND SEARCH
|
// CHOOSE PARAMS WHICH SHALL BE PASSED TO SCORE AND SEARCH
|
||||||
let scoreQueryObj = prepareQueries(q)
|
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 = {
|
let data = {
|
||||||
regions: regions,
|
regions: regions,
|
||||||
tags: tags,
|
|
||||||
boundaryClimate: boundaryClimate
|
boundaryClimate: boundaryClimate
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -22,13 +22,12 @@ const SETTINGS = {
|
|||||||
avg_price_relative: [3, 'easeOut', 2],
|
avg_price_relative: [3, 'easeOut', 2],
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = function (dbConn) {
|
module.exports = async function (data, from, to, q) {
|
||||||
return async function (data, from, to, q) {
|
|
||||||
console.log('search')
|
console.log('search')
|
||||||
if ((_.isNil(to) || _.isNil(from)) && !(_.isEmpty(q.climate) || _.isEmpty(q.costs) || _.isEmpty(q.others))) {
|
if ((_.isNil(to) || _.isNil(from)) && !(_.isEmpty(q.climate) || _.isEmpty(q.costs) || _.isEmpty(q.others))) {
|
||||||
throw new Error('invalid query')
|
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')
|
throw new Error('database error')
|
||||||
}
|
}
|
||||||
let regionsArr = data.regions
|
let regionsArr = data.regions
|
||||||
@ -64,8 +63,9 @@ module.exports = function (dbConn) {
|
|||||||
|
|
||||||
// CALCULATE PROPERTIES FOR EACH REGION
|
// CALCULATE PROPERTIES FOR EACH REGION
|
||||||
regionsArr.forEach(reg => {
|
regionsArr.forEach(reg => {
|
||||||
reg.scores = []
|
|
||||||
// CALCULATE SCORES FOR CLIMATE PROPS
|
// CALCULATE SCORES FOR CLIMATE PROPS
|
||||||
|
reg.scores = []
|
||||||
Object.entries(q.climate).forEach(([key, value]) => {
|
Object.entries(q.climate).forEach(([key, value]) => {
|
||||||
let finalScoreObj = calculateScoreForPeriod(key, travelPeriods, reg, value[0], value[1], data.boundaryClimate)
|
let finalScoreObj = calculateScoreForPeriod(key, travelPeriods, reg, value[0], value[1], data.boundaryClimate)
|
||||||
reg.scores.push(finalScoreObj)
|
reg.scores.push(finalScoreObj)
|
||||||
@ -103,8 +103,9 @@ module.exports = function (dbConn) {
|
|||||||
if (!_.isEmpty(q.others)) scoreSubGroups.push(calculateAverage(reg.scores.filter(el => _.some(Object.keys(q.others), entry => entry === el.type ) )) )
|
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
|
// CALCULATE AVERAGE SCORE Stage 2
|
||||||
reg.score = calculateAverage(reg.scores)
|
// reg.score = calculateAverage(reg.scores)
|
||||||
// reg.score = _.sum(scoreSubGroups) / scoreSubGroups.length
|
reg.score = _.sum(scoreSubGroups) / scoreSubGroups.length
|
||||||
|
|
||||||
})
|
})
|
||||||
return _.orderBy(regionsArr, ({ score }) => score || 0, 'desc') //.filter(el => !_.isNaN(el.score))
|
return _.orderBy(regionsArr, ({ score }) => score || 0, 'desc') //.filter(el => !_.isNaN(el.score))
|
||||||
}
|
}
|
||||||
@ -295,6 +296,3 @@ module.exports = function (dbConn) {
|
|||||||
|
|
||||||
return averagedScore.value
|
return averagedScore.value
|
||||||
}
|
}
|
||||||
|
|
||||||
//end
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user