Merge branch 'feature/trivago' into 'develop'
Feature/trivago See merge request tjohn/cc-data!14
This commit is contained in:
commit
96c7a5575a
@ -1,16 +1,20 @@
|
|||||||
const climateArrayFormatting = require("./../util/climateArrayFormatting.js")
|
const arrayFormatting = require("../util/databaseArrayFormatting.js")
|
||||||
|
|
||||||
module.exports = async (dbConn, id) => {
|
module.exports = async (dbConn, id) => {
|
||||||
const region = await dbConn.query(
|
const res = await dbConn.query(
|
||||||
`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,
|
||||||
|
rcma.temperature_mean AS temperature_mean,
|
||||||
|
rcma.temperature_mean_min AS temperature_mean_min,
|
||||||
rcma.temperature_mean_max AS temperature_mean_max,
|
rcma.temperature_mean_max AS temperature_mean_max,
|
||||||
rcma.precipitation AS precipitation,
|
rcma.precipitation AS precipitation,
|
||||||
rcma.rain_days AS rain_days,
|
rcma.rain_days AS rain_days,
|
||||||
rcma.sun_hours AS sun_hours,
|
rcma.sun_hours AS sun_hours,
|
||||||
|
rcma.humidity AS humidity,
|
||||||
regions_byt.average_per_day_costs AS average_per_day_costs,
|
regions_byt.average_per_day_costs AS average_per_day_costs,
|
||||||
|
rtma.avg_price_relative AS avg_price_relative,
|
||||||
regions_byt.accommodation_costs AS accommodation_costs,
|
regions_byt.accommodation_costs AS accommodation_costs,
|
||||||
regions_byt.food_costs AS food_costs,
|
regions_byt.food_costs AS food_costs,
|
||||||
regions_byt.water_costs AS water_costs,
|
regions_byt.water_costs AS water_costs,
|
||||||
@ -30,25 +34,37 @@ module.exports = async (dbConn, id) => {
|
|||||||
FROM region_climate_monthly_avg AS rcma
|
FROM region_climate_monthly_avg AS rcma
|
||||||
GROUP BY rcma.region_id) rcma ON rcma.region_id = regions.id
|
GROUP BY rcma.region_id) rcma ON rcma.region_id = regions.id
|
||||||
LEFT JOIN regions_byt ON regions.id = regions_byt.region_id
|
LEFT JOIN regions_byt ON regions.id = regions_byt.region_id
|
||||||
|
LEFT JOIN (SELECT rtma.region_id,
|
||||||
|
GROUP_CONCAT(IFNULL(rtma.avg_price_relative,"") ORDER BY rtma.month SEPARATOR ', ') AS avg_price_relative
|
||||||
|
FROM regions_trivago_monthly_avg AS rtma
|
||||||
|
GROUP BY rtma.region_id) rtma
|
||||||
|
ON regions.id = rtma.region_id
|
||||||
WHERE regions_byt.travelstyle = 1
|
WHERE regions_byt.travelstyle = 1
|
||||||
AND regions.id = ?`,
|
AND regions.id = ?`,
|
||||||
[id]
|
[id]
|
||||||
);
|
);
|
||||||
|
const region = res[0]
|
||||||
|
|
||||||
for (k = 0; k < region.length; k++) {
|
region.avg_price_relative = arrayFormatting(region.avg_price_relative);
|
||||||
//region[k].temperature_mean = climateArrayFormatting(region[k].temperature_mean);
|
region.temperature_mean = arrayFormatting(region.temperature_mean);
|
||||||
//region[k].temperature_mean_min = climateArrayFormatting(region[k].temperature_mean_min);
|
region.temperature_mean_min = arrayFormatting(region.temperature_mean_min);
|
||||||
region[k].temperature_mean_max = climateArrayFormatting(region[k].temperature_mean_max);
|
region.temperature_mean_max = arrayFormatting(region.temperature_mean_max);
|
||||||
region[k].precipitation = climateArrayFormatting(region[k].precipitation);
|
region.precipitation = arrayFormatting(region.precipitation);
|
||||||
region[k].rain_days = climateArrayFormatting(region[k].rain_days);
|
region.rain_days = arrayFormatting(region.rain_days);
|
||||||
region[k].sun_hours = climateArrayFormatting(region[k].sun_hours);
|
region.sun_hours = arrayFormatting(region.sun_hours);
|
||||||
//region[k].humidity = climateArrayFormatting(region[k].humidity);
|
region.humidity = arrayFormatting(region.humidity);
|
||||||
}
|
|
||||||
const emptyArr = Array.from({ length: 12 }, () => null)
|
const emptyArr = Array.from({length: 12}, () => null)
|
||||||
|
if (region.avg_price_relative === null) region.avg_price_relative = emptyArr
|
||||||
|
if (region.temperature_mean === null) region.temperature_mean = emptyArr
|
||||||
|
if (region.temperature_mean_min === null) region.temperature_mean_min = emptyArr
|
||||||
|
if (region.temperature_mean_max === null) region.temperature_mean_max = emptyArr
|
||||||
if (region.temperature_mean_max === null) region.temperature_mean_max = emptyArr
|
if (region.temperature_mean_max === null) region.temperature_mean_max = emptyArr
|
||||||
if (region.precipitation === null) region.precipitation = emptyArr
|
if (region.precipitation === null) region.precipitation = emptyArr
|
||||||
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
|
||||||
|
|
||||||
return region;
|
return region;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -1,43 +1,71 @@
|
|||||||
exports.getRegions = async (dbConn) => {
|
const arrayFormatting = require("../util/databaseArrayFormatting.js");
|
||||||
let sql = `SELECT
|
const { takeRightWhile } = require("lodash");
|
||||||
regions.id AS region_id,
|
|
||||||
regions.region AS name,
|
|
||||||
countries.country AS country
|
|
||||||
FROM regions
|
|
||||||
JOIN countries
|
|
||||||
ON regions.country_id = countries.id`
|
|
||||||
let regions = await dbConn.query(sql);
|
|
||||||
|
|
||||||
return regions;
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.getRegionsInternal = async (dbConn) => {
|
module.exports = async (dbConn) => {
|
||||||
let regions = await dbConn.query(`SELECT
|
const regions = await dbConn.query(
|
||||||
regions.id AS region_id,
|
`SELECT regions.id AS region_id,
|
||||||
regions.region AS name,
|
|
||||||
countries.country AS country,
|
|
||||||
regions.meteostat_id AS meteostat_id
|
|
||||||
FROM regions
|
|
||||||
JOIN countries
|
|
||||||
ON regions.country_id = countries.id`);
|
|
||||||
return regions;
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.getRegionsById = async (dbConn, id) => {
|
|
||||||
const region = await dbConn.query(
|
|
||||||
`SELECT regions.id AS region_id,
|
|
||||||
regions.region AS name,
|
regions.region AS name,
|
||||||
regions.description,
|
|
||||||
countries.country AS country,
|
countries.country AS country,
|
||||||
regions.meteostat_id AS meteostat_id
|
regions.description AS description,
|
||||||
|
rcma.temperature_mean AS temperature_mean,
|
||||||
|
rcma.temperature_mean_min AS temperature_mean_min,
|
||||||
|
rcma.temperature_mean_max AS temperature_mean_max,
|
||||||
|
rcma.precipitation AS precipitation,
|
||||||
|
rcma.rain_days AS rain_days,
|
||||||
|
rcma.sun_hours AS sun_hours,
|
||||||
|
rcma.humidity AS humidity,
|
||||||
|
regions_byt.average_per_day_costs AS average_per_day_costs,
|
||||||
|
rtma.avg_price_relative AS avg_price_relative,
|
||||||
|
regions_byt.accommodation_costs AS accommodation_costs,
|
||||||
|
regions_byt.food_costs AS food_costs,
|
||||||
|
regions_byt.water_costs AS water_costs,
|
||||||
|
regions_byt.local_transportation_costs AS local_transportation_costs,
|
||||||
|
regions_byt.entertainment_costs AS entertainment_costs,
|
||||||
|
regions_byt.alcohol_costs AS alcohol_costs
|
||||||
FROM regions
|
FROM regions
|
||||||
JOIN countries
|
LEFT JOIN countries ON regions.country_id = countries.id
|
||||||
ON regions.country_id = countries.id
|
LEFT JOIN (SELECT rcma.region_id,
|
||||||
WHERE regions.id = ?`,
|
GROUP_CONCAT(IFNULL(rcma.temperature_mean,"") ORDER BY rcma.month SEPARATOR ', ') AS temperature_mean,
|
||||||
[id]
|
GROUP_CONCAT(IFNULL(rcma.temperature_mean_min, "") ORDER BY rcma.month SEPARATOR ', ') AS temperature_mean_min,
|
||||||
);
|
GROUP_CONCAT(IFNULL(rcma.temperature_mean_max, "") ORDER BY rcma.month SEPARATOR ', ') AS temperature_mean_max,
|
||||||
return region;
|
GROUP_CONCAT(IFNULL(rcma.precipitation, "") ORDER BY rcma.month SEPARATOR ', ') AS precipitation,
|
||||||
|
GROUP_CONCAT(IFNULL(rcma.rain_days, "") ORDER BY rcma.month SEPARATOR ', ') AS rain_days,
|
||||||
|
GROUP_CONCAT(IFNULL(rcma.sun_hours, "") ORDER BY rcma.month SEPARATOR ', ') AS sun_hours,
|
||||||
|
GROUP_CONCAT(IFNULL(rcma.humidity, "") ORDER BY rcma.month SEPARATOR ', ') AS humidity
|
||||||
|
FROM region_climate_monthly_avg AS rcma
|
||||||
|
GROUP BY rcma.region_id) rcma ON rcma.region_id = regions.id
|
||||||
|
LEFT JOIN regions_byt ON regions.id = regions_byt.region_id
|
||||||
|
LEFT JOIN (SELECT rtma.region_id,
|
||||||
|
GROUP_CONCAT(IFNULL(rtma.avg_price_relative,"") ORDER BY rtma.month SEPARATOR ', ') AS avg_price_relative
|
||||||
|
FROM regions_trivago_monthly_avg AS rtma
|
||||||
|
GROUP BY rtma.region_id) rtma
|
||||||
|
ON regions.id = rtma.region_id
|
||||||
|
WHERE regions_byt.travelstyle = 1`
|
||||||
|
);
|
||||||
|
|
||||||
|
for (k = 0; k < regions.length; k++) {
|
||||||
|
regions[k].avg_price_relative = arrayFormatting(regions[k].avg_price_relative);
|
||||||
|
regions[k].temperature_mean = arrayFormatting(regions[k].temperature_mean);
|
||||||
|
regions[k].temperature_mean_min = arrayFormatting(regions[k].temperature_mean_min);
|
||||||
|
regions[k].temperature_mean_max = arrayFormatting(regions[k].temperature_mean_max);
|
||||||
|
regions[k].precipitation = arrayFormatting(regions[k].precipitation);
|
||||||
|
regions[k].rain_days = arrayFormatting(regions[k].rain_days);
|
||||||
|
regions[k].sun_hours = arrayFormatting(regions[k].sun_hours);
|
||||||
|
regions[k].humidity = arrayFormatting(regions[k].humidity);
|
||||||
|
|
||||||
|
}
|
||||||
|
//console.log(regions.filter(region => region.rain_days === null))
|
||||||
|
return regions.map(region => {
|
||||||
|
const emptyArr = Array.from({ length: 12 }, () => null)
|
||||||
|
if (region.avg_price_relative === null) region.avg_price_relative = emptyArr
|
||||||
|
if (region.temperature_mean === null) region.temperature_mean = emptyArr
|
||||||
|
if (region.temperature_mean_min === null) region.temperature_mean_min = emptyArr
|
||||||
|
if (region.temperature_mean_max === null) region.temperature_mean_max = emptyArr
|
||||||
|
if (region.precipitation === null) region.precipitation = emptyArr
|
||||||
|
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
|
||||||
|
return region
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,57 +0,0 @@
|
|||||||
const climateArrayFormatting = require("./../util/climateArrayFormatting.js");
|
|
||||||
const { takeRightWhile } = require("lodash");
|
|
||||||
|
|
||||||
module.exports = async (dbConn) => {
|
|
||||||
const regions = await dbConn.query(
|
|
||||||
`SELECT regions.id AS region_id,
|
|
||||||
regions.region AS name,
|
|
||||||
countries.country AS country,
|
|
||||||
regions.description AS description,
|
|
||||||
rcma.temperature_mean_max AS temperature_mean_max,
|
|
||||||
rcma.precipitation AS precipitation,
|
|
||||||
rcma.rain_days AS rain_days,
|
|
||||||
rcma.sun_hours AS sun_hours,
|
|
||||||
regions_byt.average_per_day_costs AS average_per_day_costs,
|
|
||||||
regions_byt.accommodation_costs AS accommodation_costs,
|
|
||||||
regions_byt.food_costs AS food_costs,
|
|
||||||
regions_byt.water_costs AS water_costs,
|
|
||||||
regions_byt.local_transportation_costs AS local_transportation_costs,
|
|
||||||
regions_byt.entertainment_costs AS entertainment_costs,
|
|
||||||
regions_byt.alcohol_costs AS alcohol_costs
|
|
||||||
FROM regions
|
|
||||||
LEFT JOIN countries ON regions.country_id = countries.id
|
|
||||||
LEFT JOIN (SELECT rcma.region_id,
|
|
||||||
GROUP_CONCAT(IFNULL(rcma.temperature_mean,"") ORDER BY rcma.month SEPARATOR ', ') AS temperature_mean,
|
|
||||||
GROUP_CONCAT(IFNULL(rcma.temperature_mean_min, "") ORDER BY rcma.month SEPARATOR ', ') AS temperature_mean_min,
|
|
||||||
GROUP_CONCAT(IFNULL(rcma.temperature_mean_max, "") ORDER BY rcma.month SEPARATOR ', ') AS temperature_mean_max,
|
|
||||||
GROUP_CONCAT(IFNULL(rcma.precipitation, "") ORDER BY rcma.month SEPARATOR ', ') AS precipitation,
|
|
||||||
GROUP_CONCAT(IFNULL(rcma.rain_days, "") ORDER BY rcma.month SEPARATOR ', ') AS rain_days,
|
|
||||||
GROUP_CONCAT(IFNULL(rcma.sun_hours, "") ORDER BY rcma.month SEPARATOR ', ') AS sun_hours,
|
|
||||||
GROUP_CONCAT(IFNULL(rcma.humidity, "") ORDER BY rcma.month SEPARATOR ', ') AS humidity
|
|
||||||
FROM region_climate_monthly_avg AS rcma
|
|
||||||
GROUP BY rcma.region_id) rcma ON rcma.region_id = regions.id
|
|
||||||
LEFT JOIN regions_byt ON regions.id = regions_byt.region_id
|
|
||||||
WHERE regions_byt.travelstyle = 1`
|
|
||||||
);
|
|
||||||
|
|
||||||
for (k = 0; k < regions.length; k++) {
|
|
||||||
//regions[k].temperature_mean = climateArrayFormatting(regions[k].temperature_mean);
|
|
||||||
//regions[k].temperature_mean_min = climateArrayFormatting(regions[k].temperature_mean_min);
|
|
||||||
regions[k].temperature_mean_max = climateArrayFormatting(regions[k].temperature_mean_max);
|
|
||||||
regions[k].precipitation = climateArrayFormatting(regions[k].precipitation);
|
|
||||||
regions[k].rain_days = climateArrayFormatting(regions[k].rain_days);
|
|
||||||
regions[k].sun_hours = climateArrayFormatting(regions[k].sun_hours);
|
|
||||||
//regions[k].humidity = climateArrayFormatting(regions[k].humidity);
|
|
||||||
|
|
||||||
}
|
|
||||||
console.log(regions.filter(region => region.rain_days === null))
|
|
||||||
return regions.map(region => {
|
|
||||||
const emptyArr = Array.from({ length: 12 }, () => null)
|
|
||||||
if (region.temperature_mean_max === null) region.temperature_mean_max = emptyArr
|
|
||||||
if (region.precipitation === null) region.precipitation = emptyArr
|
|
||||||
if (region.rain_days === null) region.rain_days = emptyArr
|
|
||||||
if (region.sun_hours === null) region.sun_hours = emptyArr
|
|
||||||
return region
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
@ -1,5 +1,5 @@
|
|||||||
const router = require("express").Router();
|
const router = require("express").Router();
|
||||||
const getRegions = require("../models/getRegions2.js");
|
const getRegions = require("../models/getRegions.js");
|
||||||
const getRegionById = require("../models/getRegionById.js");
|
const getRegionById = require("../models/getRegionById.js");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
|
|||||||
@ -4,257 +4,248 @@ const getClimateMinMax = require("./getClimateMinMax.js")
|
|||||||
const oldToNewQuerySyntax = require("./oldToNewQuerySyntax.js")
|
const oldToNewQuerySyntax = require("./oldToNewQuerySyntax.js")
|
||||||
const getAllRegionsWithClimatePerMonth = require('./getAllRegionsWithClimatePerMonth')
|
const getAllRegionsWithClimatePerMonth = require('./getAllRegionsWithClimatePerMonth')
|
||||||
const score = require('./score')
|
const score = require('./score')
|
||||||
// const getRegions = require('../models/getRegions.js').getRegionsInternal
|
const getRegions = require('../models/getRegions.js')
|
||||||
const getRegions = require('../models/getRegions2.js')
|
|
||||||
|
|
||||||
const SHOW_ALL_SCOREOBJECTS = false
|
const SHOW_ALL_SCOREOBJECTS = false
|
||||||
const MULTIPLIER = {
|
const MULTIPLIER = {
|
||||||
temperature_mean_max: 5,
|
temperature_mean_max: 5,
|
||||||
precipitation: 3.5,
|
precipitation: 3.5,
|
||||||
rain_days: 3,
|
rain_days: 3,
|
||||||
sun_hours: 2.5,
|
sun_hours: 2.5,
|
||||||
accommodation_costs: 5,
|
accommodation_costs: 5,
|
||||||
food_costs: 5,
|
food_costs: 5,
|
||||||
alcohol_costs: 5,
|
alcohol_costs: 5,
|
||||||
water_costs: 5,
|
water_costs: 5,
|
||||||
public_transportation_costs: 5,
|
public_transportation_costs: 5,
|
||||||
entertainment_costs: 5,
|
entertainment_costs: 5,
|
||||||
average_per_day_costs: 5
|
average_per_day_costs: 5
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = function (dbConn) {
|
module.exports = function (dbConn) {
|
||||||
return async function (from, to, queries) {
|
return async function (from, to, queries) {
|
||||||
console.log('search')
|
console.log('search')
|
||||||
// PREPARE SEARCH
|
// PREPARE SEARCH
|
||||||
// validate dates
|
// validate dates
|
||||||
const dates = validateDates(from, to)
|
const dates = validateDates(from, to)
|
||||||
|
|
||||||
// transform syntax and seperate climate queries from price queries
|
// transform syntax and seperate climate queries from price queries
|
||||||
queries = oldToNewQuerySyntax.oldToNewQuerySyntax(queries)
|
queries = oldToNewQuerySyntax.oldToNewQuerySyntax(queries)
|
||||||
console.log(queries)
|
console.log(queries)
|
||||||
const q = prepareQueries(queries)
|
const q = prepareQueries(queries)
|
||||||
console.log('q', q)
|
console.log('q', q)
|
||||||
|
|
||||||
// for calculating average if traveldates are in more than one month
|
// for calculating average if traveldates are in more than one month
|
||||||
const travelPeriods = travelPeriodsFromDates(dates)
|
const travelPeriods = travelPeriodsFromDates(dates)
|
||||||
|
|
||||||
// FETCH DATA FROM DB
|
// FETCH DATA FROM DB
|
||||||
const boundaryClimate = await getClimateMinMax.getClimateMinMax(dbConn)
|
const boundaryClimate = await getClimateMinMax.getClimateMinMax(dbConn)
|
||||||
let regions = await getRegions(dbConn)
|
let regions = await getRegions(dbConn)
|
||||||
regions.forEach(reg => reg.scores = [])
|
regions.forEach(reg => reg.scores = [])
|
||||||
const boundaryCosts = {
|
const boundaryCosts = {
|
||||||
max: {
|
max: {
|
||||||
accommodation_costs: 500,
|
accommodation_costs: 500,
|
||||||
food_costs: 100,
|
food_costs: 100,
|
||||||
alcohol_costs: 100,
|
alcohol_costs: 100,
|
||||||
water_costs: 100,
|
water_costs: 100,
|
||||||
public_transportation_costs: 100,
|
public_transportation_costs: 100,
|
||||||
entertainment_costs: 100,
|
entertainment_costs: 100,
|
||||||
average_per_day_costs: 1000
|
average_per_day_costs: 1000
|
||||||
|
|
||||||
},
|
},
|
||||||
min: {
|
min: {
|
||||||
accommodation_costs: 0,
|
accommodation_costs: 0,
|
||||||
food_costs: 0,
|
food_costs: 0,
|
||||||
alcohol_costs: 0,
|
alcohol_costs: 0,
|
||||||
water_costs: 0,
|
water_costs: 0,
|
||||||
public_transportation_costs: 0,
|
public_transportation_costs: 0,
|
||||||
entertainment_costs: 0,
|
entertainment_costs: 0,
|
||||||
average_per_day_costs: 0
|
average_per_day_costs: 0
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// little tweak to show score object without request
|
|
||||||
if (SHOW_ALL_SCOREOBJECTS) {
|
|
||||||
if (!q.climate.temperature_mean_max) q.climate.temperature_mean_max = [null, null]
|
|
||||||
if (!q.climate.precipitation) q.climate.precipitation = [null, null]
|
|
||||||
if (!q.climate.rain_days) q.climate.rain_days = [null, null]
|
|
||||||
if (!q.climate.sun_hours) q.climate.sun_hours = [null, null]
|
|
||||||
if (!q.climate.accommodation_costs) q.climate.accommodation_costs = [null, null]
|
|
||||||
}
|
|
||||||
// CALCULATE SCORES FOR CLIMATE PROPS
|
|
||||||
regions.forEach(reg => {
|
|
||||||
Object.entries(q.climate).forEach(([key, value]) => {
|
|
||||||
// console.log('key', key)
|
|
||||||
// console.log('val', value[0], value[1])
|
|
||||||
let finalScoreObj = getScoreAndAverageFromClimate(key, travelPeriods, reg, value[0], value[1], boundaryClimate)
|
|
||||||
reg.scores.push(finalScoreObj)
|
|
||||||
});
|
|
||||||
|
|
||||||
// CALCULATE SCORES FOR PRICE PROPS
|
|
||||||
Object.entries(q.costs).forEach(([key, value]) => {
|
|
||||||
console.log('key', key)
|
|
||||||
console.log('val', value[0], value[1])
|
|
||||||
let finalScoreObj = getScoreFromCosts(key, reg, value[0], value[1], boundaryCosts)
|
|
||||||
console.log(finalScoreObj);
|
|
||||||
|
|
||||||
reg.scores.push(finalScoreObj)
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
// CALCULATE AVERAGE SCORE
|
|
||||||
reg.score = calculateAverage(reg.scores)
|
|
||||||
})
|
|
||||||
return _.orderBy(regions, ({ score }) => score || 0, 'desc') //.filter(el => !_.isNaN(el.score))
|
|
||||||
}
|
|
||||||
|
|
||||||
function calculateAverage(scores) {
|
|
||||||
let sum = 0
|
|
||||||
scores.forEach(el => sum += el.score)
|
|
||||||
//console.log(sum)
|
|
||||||
return _.round(sum / scores.length, 2)
|
|
||||||
}
|
|
||||||
|
|
||||||
function prepareQueries(queries) {
|
|
||||||
let q = {
|
|
||||||
climate: {},
|
|
||||||
costs: {}
|
|
||||||
}
|
|
||||||
// climate
|
|
||||||
if (queries.temperature_mean_max) q.climate.temperature_mean_max = queries.temperature_mean_max
|
|
||||||
if (queries.precipitation) q.climate.precipitation = queries.precipitation
|
|
||||||
if (queries.rain_days) q.climate.rain_days = queries.rain_days
|
|
||||||
if (queries.sun_hours) q.climate.sun_hours = queries.sun_hours
|
|
||||||
|
|
||||||
// costs
|
|
||||||
if (queries.accommodation_costs) q.costs.accommodation_costs = queries.accommodation_costs
|
|
||||||
if (queries.food_costs) q.costs.food_costs = queries.food_costs
|
|
||||||
if (queries.alcohol_costs) q.costs.alcohol_costs = queries.alcohol_costs
|
|
||||||
if (queries.water_costs) q.costs.water_costs = queries.water_costs
|
|
||||||
if (queries.public_transportation_costs) q.costs.public_transportation_costs = queries.public_transportation_costs
|
|
||||||
if (queries.entertainment_costs) q.costs.entertainment_costs = queries.entertainment_costs
|
|
||||||
if (queries.average_per_day_costs) q.costs.average_per_day_costs = queries.average_per_day_costs
|
|
||||||
|
|
||||||
return q
|
|
||||||
}
|
|
||||||
|
|
||||||
function travelPeriodsFromDates(dates) {
|
|
||||||
//console.log(dates);
|
|
||||||
|
|
||||||
let travelPeriods = []
|
|
||||||
if (dates.from.month === dates.to.month) {
|
|
||||||
let period = {
|
|
||||||
month: dates.from.month,
|
|
||||||
days: dates.to.day - dates.from.day
|
|
||||||
}
|
|
||||||
travelPeriods.push(period)
|
|
||||||
} else {
|
|
||||||
for (let i = dates.from.month; i <= dates.to.month; i++) {
|
|
||||||
let period = {}
|
|
||||||
if (i === dates.from.month) {
|
|
||||||
period = {
|
|
||||||
month: i,
|
|
||||||
days: 32 - dates.from.day
|
|
||||||
}
|
|
||||||
} else if (i === dates.to.month) {
|
|
||||||
period = {
|
|
||||||
month: i,
|
|
||||||
days: dates.to.day
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
period = {
|
|
||||||
month: i,
|
|
||||||
days: 30
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
travelPeriods.push(period)
|
|
||||||
}
|
// little tweak to show score object without request
|
||||||
|
if (SHOW_ALL_SCOREOBJECTS) {
|
||||||
|
if (!q.climate.temperature_mean_max) q.climate.temperature_mean_max = [null, null]
|
||||||
|
if (!q.climate.precipitation) q.climate.precipitation = [null, null]
|
||||||
|
if (!q.climate.rain_days) q.climate.rain_days = [null, null]
|
||||||
|
if (!q.climate.sun_hours) q.climate.sun_hours = [null, null]
|
||||||
|
if (!q.climate.accommodation_costs) q.climate.accommodation_costs = [null, null]
|
||||||
|
}
|
||||||
|
// CALCULATE SCORES FOR CLIMATE PROPS
|
||||||
|
regions.forEach(reg => {
|
||||||
|
Object.entries(q.climate).forEach(([key, value]) => {
|
||||||
|
let finalScoreObj = getScoreAndAverageFromClimate(key, travelPeriods, reg, value[0], value[1], boundaryClimate)
|
||||||
|
reg.scores.push(finalScoreObj)
|
||||||
|
});
|
||||||
|
|
||||||
|
// CALCULATE SCORES FOR PRICE PROPS
|
||||||
|
Object.entries(q.costs).forEach(([key, value]) => {
|
||||||
|
let finalScoreObj = getScoreFromCosts(key, reg, value[0], value[1], boundaryCosts)
|
||||||
|
|
||||||
|
reg.scores.push(finalScoreObj)
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
// CALCULATE AVERAGE SCORE
|
||||||
|
reg.score = calculateAverage(reg.scores)
|
||||||
|
})
|
||||||
|
return _.orderBy(regions, ({score}) => score || 0, 'desc') //.filter(el => !_.isNaN(el.score))
|
||||||
}
|
}
|
||||||
return travelPeriods
|
|
||||||
}
|
|
||||||
|
|
||||||
function validateDates(from, to) {
|
function calculateAverage(scores) {
|
||||||
let fromAndTo = {
|
let sum = 0
|
||||||
from: {},
|
let cnt = 0
|
||||||
to: {}
|
scores.forEach(el => {
|
||||||
|
if (el.score !== null && el.score !== undefined && !_.isNaN(el.score)) {
|
||||||
|
cnt++
|
||||||
|
sum += el.score
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return _.round(sum / cnt, 2)
|
||||||
}
|
}
|
||||||
if (_.isNumber(from) && _.isNumber(to)) {
|
|
||||||
let dateFrom = new Date(from)
|
function prepareQueries(queries) {
|
||||||
fromAndTo.from.day = dateFrom.getDate()
|
let q = {
|
||||||
fromAndTo.from.month = dateFrom.getMonth() + 1
|
climate: {},
|
||||||
let dateTo = new Date(to)
|
costs: {}
|
||||||
fromAndTo.to.day = dateTo.getDate()
|
}
|
||||||
fromAndTo.to.month = dateTo.getMonth() + 1
|
// climate
|
||||||
//console.log(dateFrom.toUTCString(), dateTo.toUTCString())
|
if (queries.temperature_mean_max) q.climate.temperature_mean_max = queries.temperature_mean_max
|
||||||
if (moment(dateFrom).add(23, 'hours').isAfter(moment(dateTo))) throw new Error("ERR: 'to' must be at least one day after 'from'.")
|
if (queries.precipitation) q.climate.precipitation = queries.precipitation
|
||||||
} else {
|
if (queries.rain_days) q.climate.rain_days = queries.rain_days
|
||||||
// this block to still support old query syntax, validating from and to parameter
|
if (queries.sun_hours) q.climate.sun_hours = queries.sun_hours
|
||||||
let re = /([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/i;
|
|
||||||
fromAndTo.from.month = Number(from.split("-")[1])
|
// costs
|
||||||
fromAndTo.to.month = Number(to.split("-")[1])
|
if (queries.accommodation_costs) q.costs.accommodation_costs = queries.accommodation_costs
|
||||||
fromAndTo.from.day = Number(from.split("-")[2])
|
if (queries.food_costs) q.costs.food_costs = queries.food_costs
|
||||||
fromAndTo.to.day = Number(to.split("-")[2])
|
if (queries.alcohol_costs) q.costs.alcohol_costs = queries.alcohol_costs
|
||||||
if (!from.match(re) || !to.match(re)) throw new Error("ERR: invalid parameter:", from, to)
|
if (queries.water_costs) q.costs.water_costs = queries.water_costs
|
||||||
if (moment(from, 'YYYY-MM-DD').add(23, 'hours').isAfter(moment(to, 'YYYY-MM-DD'))) throw new Error("ERR: 'to' must be at least one day after 'from'.")
|
if (queries.public_transportation_costs) q.costs.public_transportation_costs = queries.public_transportation_costs
|
||||||
|
if (queries.entertainment_costs) q.costs.entertainment_costs = queries.entertainment_costs
|
||||||
|
if (queries.average_per_day_costs) q.costs.average_per_day_costs = queries.average_per_day_costs
|
||||||
|
|
||||||
|
return q
|
||||||
}
|
}
|
||||||
console.log(fromAndTo)
|
|
||||||
|
|
||||||
return fromAndTo
|
function travelPeriodsFromDates(dates) {
|
||||||
}
|
//console.log(dates);
|
||||||
|
|
||||||
function getScoreAndAverageFromClimate(type, travelPeriods, region, searchLowParam, searchMaxParam, minMax) {
|
let travelPeriods = []
|
||||||
console.log('getScoreAndAverageFromClimate for', region.name, type)
|
if (dates.from.month === dates.to.month) {
|
||||||
// console.log(type, travelPeriods, searchLowParam, searchMaxParam)
|
let period = {
|
||||||
|
month: dates.from.month,
|
||||||
const singleScores = travelPeriods.map(period => {
|
days: dates.to.day - dates.from.day
|
||||||
const sc = _.round(score.calculateScoreRange(minMax.min[type], minMax.max[type], MULTIPLIER[type], region[type][period.month - 1], searchLowParam, searchMaxParam), 2)
|
}
|
||||||
let res = {
|
travelPeriods.push(period)
|
||||||
//region_id: x.region_id,
|
} else {
|
||||||
type: type,
|
for (let i = dates.from.month; i <= dates.to.month; i++) {
|
||||||
value: region[type][period.month - 1],
|
let period = {}
|
||||||
score: (region[type][period.month - 1] === null || searchLowParam === null) ? null : sc,
|
if (i === dates.from.month) {
|
||||||
days: period.days
|
period = {
|
||||||
}
|
month: i,
|
||||||
//console.log(res);
|
days: 32 - dates.from.day
|
||||||
|
}
|
||||||
return res
|
} else if (i === dates.to.month) {
|
||||||
})
|
period = {
|
||||||
|
month: i,
|
||||||
let averagedScore = {
|
days: dates.to.day
|
||||||
type: type,
|
}
|
||||||
value: 0,
|
} else {
|
||||||
score: 0,
|
period = {
|
||||||
days: 0
|
month: i,
|
||||||
|
days: 30
|
||||||
|
}
|
||||||
|
}
|
||||||
|
travelPeriods.push(period)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return travelPeriods
|
||||||
}
|
}
|
||||||
singleScores.forEach(el => {
|
|
||||||
if (el.value !== null) {
|
|
||||||
//console.log(el)
|
|
||||||
averagedScore.value += (el.value * el.days)
|
|
||||||
averagedScore.score += (el.score * el.days)
|
|
||||||
averagedScore.days += (el.days)
|
|
||||||
} else {
|
|
||||||
console.log('skip averaging')
|
|
||||||
console.log(el)
|
|
||||||
|
|
||||||
}
|
function validateDates(from, to) {
|
||||||
})
|
let fromAndTo = {
|
||||||
averagedScore.value = _.round(averagedScore.value / averagedScore.days, 1)
|
from: {},
|
||||||
averagedScore.score = _.round(averagedScore.score / averagedScore.days, 1)
|
to: {}
|
||||||
if (searchLowParam === null) averagedScore.score = null
|
}
|
||||||
delete averagedScore.days
|
if (_.isNumber(from) && _.isNumber(to)) {
|
||||||
|
let dateFrom = new Date(from)
|
||||||
|
fromAndTo.from.day = dateFrom.getDate()
|
||||||
|
fromAndTo.from.month = dateFrom.getMonth() + 1
|
||||||
|
let dateTo = new Date(to)
|
||||||
|
fromAndTo.to.day = dateTo.getDate()
|
||||||
|
fromAndTo.to.month = dateTo.getMonth() + 1
|
||||||
|
if (moment(dateFrom).add(23, 'hours').isAfter(moment(dateTo))) throw new Error("ERR: 'to' must be at least one day after 'from'.")
|
||||||
|
} else {
|
||||||
|
// this block to still support old query syntax, validating from and to parameter
|
||||||
|
let re = /([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/i;
|
||||||
|
fromAndTo.from.month = Number(from.split("-")[1])
|
||||||
|
fromAndTo.to.month = Number(to.split("-")[1])
|
||||||
|
fromAndTo.from.day = Number(from.split("-")[2])
|
||||||
|
fromAndTo.to.day = Number(to.split("-")[2])
|
||||||
|
if (!from.match(re) || !to.match(re)) throw new Error("ERR: invalid parameter:", from, to)
|
||||||
|
if (moment(from, 'YYYY-MM-DD').add(23, 'hours').isAfter(moment(to, 'YYYY-MM-DD'))) throw new Error("ERR: 'to' must be at least one day after 'from'.")
|
||||||
|
}
|
||||||
|
|
||||||
return averagedScore
|
return fromAndTo
|
||||||
}
|
|
||||||
|
|
||||||
function getScoreFromCosts(type, region, searchLowParam, searchMaxParam, minMax) {
|
|
||||||
console.log('getScoreFromCosts for', region.name, type)
|
|
||||||
// console.log(type, travelPeriods, searchLowParam, searchMaxParam)
|
|
||||||
const sc = _.round(score.calculateScoreRange(minMax.min[type], minMax.max[type], MULTIPLIER[type], region[type], searchLowParam, searchMaxParam), 2)
|
|
||||||
|
|
||||||
let finScore = {
|
|
||||||
type: type,
|
|
||||||
value: region[type],
|
|
||||||
score: sc,
|
|
||||||
}
|
}
|
||||||
finScore.value = _.round(finScore.value, 1)
|
|
||||||
finScore.score = _.round(finScore.score, 1)
|
|
||||||
if (searchLowParam === null) finScore.score = null
|
|
||||||
|
|
||||||
return finScore
|
function getScoreAndAverageFromClimate(type, travelPeriods, region, searchLowParam, searchMaxParam, minMax) {
|
||||||
}
|
console.log('getScoreAndAverageFromClimate for', region.name, type)
|
||||||
|
|
||||||
|
const singleScores = travelPeriods.map(period => {
|
||||||
|
const sc = _.round(score.calculateScoreRange(minMax.min[type], minMax.max[type], MULTIPLIER[type], region[type][period.month - 1], searchLowParam, searchMaxParam), 2)
|
||||||
|
let res = {
|
||||||
|
//region_id: x.region_id,
|
||||||
|
type: type,
|
||||||
|
value: region[type][period.month - 1],
|
||||||
|
score: (region[type][period.month - 1] === null || searchLowParam === null) ? null : sc,
|
||||||
|
days: period.days
|
||||||
|
}
|
||||||
|
|
||||||
|
return res
|
||||||
|
})
|
||||||
|
|
||||||
|
let averagedScore = {
|
||||||
|
type: type,
|
||||||
|
value: 0,
|
||||||
|
score: 0,
|
||||||
|
days: 0
|
||||||
|
}
|
||||||
|
singleScores.forEach(el => {
|
||||||
|
if (el.value !== null) {
|
||||||
|
averagedScore.value += (el.value * el.days)
|
||||||
|
averagedScore.score += (el.score * el.days)
|
||||||
|
averagedScore.days += (el.days)
|
||||||
|
} else {
|
||||||
|
console.log('skip averaging')
|
||||||
|
console.log(el)
|
||||||
|
|
||||||
|
}
|
||||||
|
})
|
||||||
|
averagedScore.value = _.round(averagedScore.value / averagedScore.days, 1)
|
||||||
|
averagedScore.score = _.round(averagedScore.score / averagedScore.days, 1)
|
||||||
|
if (searchLowParam === null) averagedScore.score = null
|
||||||
|
delete averagedScore.days
|
||||||
|
|
||||||
|
return averagedScore
|
||||||
|
}
|
||||||
|
|
||||||
|
function getScoreFromCosts(type, region, searchLowParam, searchMaxParam, minMax) {
|
||||||
|
console.log('getScoreFromCosts for', region.name, type)
|
||||||
|
const sc = _.round(score.calculateScoreRange(minMax.min[type], minMax.max[type], MULTIPLIER[type], region[type], searchLowParam, searchMaxParam), 2)
|
||||||
|
|
||||||
|
let finScore = {
|
||||||
|
type: type,
|
||||||
|
value: region[type],
|
||||||
|
score: sc,
|
||||||
|
}
|
||||||
|
finScore.value = _.round(finScore.value, 1)
|
||||||
|
finScore.score = _.round(finScore.score, 1)
|
||||||
|
if (searchLowParam === null) finScore.score = null
|
||||||
|
|
||||||
|
return finScore
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//end
|
||||||
|
|
||||||
//end
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user