some tag improvements

This commit is contained in:
Timo Volkmann 2020-06-24 11:17:39 +02:00
parent cd9f68234f
commit e0f8f5490e
3 changed files with 267 additions and 265 deletions

View File

@ -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
});
};

View File

@ -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
}

View File

@ -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,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 ) )) )
// 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))
}
@ -295,6 +296,3 @@ module.exports = function (dbConn) {
return averagedScore.value
}
//end
}