some fixes by TVM
This commit is contained in:
parent
33249bf817
commit
0586a7a0ba
@ -24,7 +24,7 @@ Campus Cup AKMC Data Traveloptimizer
|
||||
- temperature=NUMBER,NUMBER
|
||||
- raindays=NUMBER,NUMBER
|
||||
- sunhours=NUMBER,NUMBER
|
||||
- percipitation=NUMBER,NUMBER
|
||||
- precipitation=NUMBER,NUMBER
|
||||
|
||||
__Examples:__
|
||||
http://localhost:3000/v1/search?from=2020-06-14&to=2020-07-29&temperature=27,29&raindays=8,12&sunhours=250,300
|
||||
|
||||
@ -1523,7 +1523,7 @@ CREATE TABLE IF NOT EXISTS `region_climate` (
|
||||
`year` int(11) NOT NULL,
|
||||
`month` int(11) NOT NULL,
|
||||
`temperature` float DEFAULT NULL,
|
||||
`percipitation` float DEFAULT NULL,
|
||||
`precipitation` float DEFAULT NULL,
|
||||
`raindays` int(11) DEFAULT NULL,
|
||||
`sunshine` float DEFAULT NULL,
|
||||
`humidity` float DEFAULT NULL,
|
||||
|
||||
@ -1285,7 +1285,7 @@ CREATE TABLE IF NOT EXISTS `region_climate` (
|
||||
`temperature_mean` float DEFAULT NULL,
|
||||
`temperature_mean_min` float DEFAULT NULL,
|
||||
`temperature_mean_max` float DEFAULT NULL,
|
||||
`percipitation` float DEFAULT NULL,
|
||||
`precipitation` float DEFAULT NULL,
|
||||
`raindays` int(11) DEFAULT NULL,
|
||||
`sunshine` float DEFAULT NULL,
|
||||
`humidity` float DEFAULT NULL,
|
||||
@ -1300,7 +1300,7 @@ CREATE TABLE IF NOT EXISTS `region_climate` (
|
||||
-- Exportiere Daten aus Tabelle travopti.region_climate: ~3.094 rows (ungefähr)
|
||||
DELETE FROM `region_climate`;
|
||||
/*!40000 ALTER TABLE `region_climate` DISABLE KEYS */;
|
||||
INSERT INTO `region_climate` (`id`, `region_id`, `year`, `month`, `temperature_mean`, `temperature_mean_min`, `temperature_mean_max`, `percipitation`, `raindays`, `sunshine`, `humidity`, `created_at`, `updated_at`) VALUES
|
||||
INSERT INTO `region_climate` (`id`, `region_id`, `year`, `month`, `temperature_mean`, `temperature_mean_min`, `temperature_mean_max`, `precipitation`, `raindays`, `sunshine`, `humidity`, `created_at`, `updated_at`) VALUES
|
||||
(3173, 2, 2016, 5, 13.9, 9.7, 18.1, 46, 8, 162, NULL, '2020-06-15 23:32:05', '2020-06-16 00:14:58'),
|
||||
(3174, 2, 2016, 6, 10, 6.5, 13.5, 61, 11, 116, NULL, '2020-06-15 23:32:05', '2020-06-15 23:32:05'),
|
||||
(3175, 2, 2016, 7, 10.3, 6.8, 13.8, 62, 10, 120, NULL, '2020-06-15 23:32:05', '2020-06-15 23:32:05'),
|
||||
@ -4406,7 +4406,7 @@ CREATE TABLE `region_climate_monthly_avg` (
|
||||
`temperature_mean` DOUBLE(18,1) NULL,
|
||||
`temperature_mean_min` DOUBLE(18,1) NULL,
|
||||
`temperature_mean_max` DOUBLE(18,1) NULL,
|
||||
`percipitation` DOUBLE(18,1) NULL,
|
||||
`precipitation` DOUBLE(18,1) NULL,
|
||||
`raindays` DECIMAL(12,1) NULL,
|
||||
`sunshine` DOUBLE(18,1) NULL,
|
||||
`humidity` DOUBLE(18,1) NULL
|
||||
@ -4437,9 +4437,9 @@ INSERT INTO `search_presets` (`id`, `parameter`, `name`, `value_1`, `value_2`, `
|
||||
(5, 'temperature', 'cold', -30, -5, '2020-06-16 18:28:50', '2020-06-16 18:41:42'),
|
||||
(6, 'sunhours', 'sunny', 250, 500, '2020-06-16 18:38:44', '2020-06-16 18:40:40'),
|
||||
(7, 'sunhours', 'dark', 0, 50, '2020-06-16 18:40:56', '2020-06-16 18:41:00'),
|
||||
(8, 'percipitation', 'almost_no_rain', 0, 5, '2020-06-16 18:43:42', '2020-06-16 18:46:47'),
|
||||
(9, 'percipitation', 'little_rain', 5, 50, '2020-06-16 18:44:26', '2020-06-16 18:44:26'),
|
||||
(10, 'percipitation', 'floodlike_rain', 500, 2000, '2020-06-16 18:45:30', '2020-06-16 18:45:30'),
|
||||
(8, 'precipitation', 'almost_no_rain', 0, 5, '2020-06-16 18:43:42', '2020-06-16 18:46:47'),
|
||||
(9, 'precipitation', 'little_rain', 5, 50, '2020-06-16 18:44:26', '2020-06-16 18:44:26'),
|
||||
(10, 'precipitation', 'floodlike_rain', 500, 2000, '2020-06-16 18:45:30', '2020-06-16 18:45:30'),
|
||||
(11, 'raindays', 'few_raindays', 0, 2, '2020-06-16 18:46:34', '2020-06-16 18:46:34'),
|
||||
(12, 'raindays', 'many_raindays', 20, 31, '2020-06-16 18:47:34', '2020-06-16 18:47:34'),
|
||||
(13, 'alcohol', 'cheap_alcohol', 0, 3, '2020-06-16 18:48:54', '2020-06-16 18:48:54'),
|
||||
@ -4494,7 +4494,7 @@ CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW `region_climate_monthly_avg
|
||||
ROUND(AVG(rc.temperature_mean),1) AS temperature_mean,
|
||||
ROUND(AVG(rc.temperature_mean_min),1) AS temperature_mean_min,
|
||||
ROUND(AVG(rc.temperature_mean_max),1) AS temperature_mean_max,
|
||||
ROUND(AVG(rc.percipitation),1) AS percipitation,
|
||||
ROUND(AVG(rc.precipitation),1) AS precipitation,
|
||||
ROUND(AVG(rc.raindays),1) AS raindays,
|
||||
ROUND(AVG(rc.sunshine),1) AS sunshine,
|
||||
ROUND(AVG(rc.humidity),1) AS humidity
|
||||
|
||||
@ -11,7 +11,7 @@ const port = 3000
|
||||
//const multiplier_temp = 5
|
||||
const multiplier = {
|
||||
temperature_mean_max: 5,
|
||||
percipitation: 3.5,
|
||||
precipitation: 3.5,
|
||||
raindays: 3,
|
||||
sunhours: 2.5,
|
||||
}
|
||||
@ -27,7 +27,7 @@ const samplePresets = [
|
||||
|
||||
app.get('/', (req, res) => res.send('Hello Timo!'))
|
||||
app.get('/v1/regions', (req, res) => getAllRegions().then(x => res.json({ data: x })))
|
||||
app.get('/v1/presets', (req, res) => res.json({ data: samplePresets}))
|
||||
app.get('/v1/presets', (req, res) => res.json({ data: samplePresets }))
|
||||
app.get('/v1/search', searchHandler)
|
||||
app.get('/v1/climate/update', climateUpdateHandler)
|
||||
|
||||
@ -52,22 +52,22 @@ function climateUpdateHandler(req, res) {
|
||||
|
||||
function searchHandler(req, res) {
|
||||
let response = {}
|
||||
|
||||
|
||||
response.meta = {
|
||||
params: req.params,
|
||||
query: req.query,
|
||||
headers: req.headers
|
||||
}
|
||||
|
||||
|
||||
let q = req.query.q ? base.base64ToObj(req.query.q) : req.query
|
||||
console.log('Q:', q)
|
||||
|
||||
|
||||
let queryObj = {}
|
||||
if (q.temperature) queryObj['temperature_mean_max'] = q.temperature
|
||||
if (q.percipitation) queryObj['percipitation'] = q.percipitation
|
||||
if (q.precipitation) queryObj['precipitation'] = q.precipitation
|
||||
if (q.raindays) queryObj['raindays'] = q.raindays
|
||||
if (q.sunhours) queryObj['sunhours'] = q.sunhours
|
||||
|
||||
|
||||
scoreAndSearch(q.from, q.to, queryObj).then(searchResults => {
|
||||
response.data = searchResults
|
||||
res.json(response)
|
||||
@ -84,27 +84,27 @@ async function scoreAndSearch(from, to, queries) {
|
||||
|
||||
// get Min and Max values for each Parameter
|
||||
const minMax = await getClimateMinMax()
|
||||
|
||||
|
||||
// randomize if empty queries
|
||||
if (_.isEmpty(queries)) {
|
||||
let t = _.round(_.random(minMax.min.temperature_mean_max, minMax.max.temperature_mean_max-5),0)
|
||||
let p = _.round(_.random(minMax.min.percipitation, minMax.max.percipitation - 50), 0)
|
||||
let t = _.round(_.random(minMax.min.temperature_mean_max, minMax.max.temperature_mean_max - 5), 0)
|
||||
let p = _.round(_.random(minMax.min.precipitation, minMax.max.precipitation - 50), 0)
|
||||
let r = _.round(_.random(minMax.min.raindays, minMax.max.raindays - 5), 0)
|
||||
let s = _.round(_.random(minMax.min.sunhours, minMax.max.sunhours - 50), 0)
|
||||
queries.temperature_mean_max = `${t},${t + 5}`
|
||||
queries.percipitation = `${p},${p + 50}`
|
||||
queries.precipitation = `${p},${p + 50}`
|
||||
queries.raindays = `${r},${r + 5}`
|
||||
queries.sunhours = `${s},${s + 50}`
|
||||
}
|
||||
queries = oldToNewQuerySyntax(queries)
|
||||
console.log(queries)
|
||||
|
||||
|
||||
// TODO simplify and remove support for old query syntaax
|
||||
let monthFrom = 0
|
||||
let monthTo = 0
|
||||
let dayFrom = 0
|
||||
let dayTo = 0
|
||||
|
||||
|
||||
if (_.isNumber(from) && _.isNumber(to)) {
|
||||
let dateFrom = moment(from).toDate()
|
||||
let dateTo = moment(to).toDate()
|
||||
@ -120,10 +120,10 @@ async function scoreAndSearch(from, to, queries) {
|
||||
monthTo = Number(to.split("-")[1])
|
||||
dayFrom = Number(from.split("-")[2])
|
||||
dayTo = Number(to.split("-")[2])
|
||||
if (!from.match(re) || !to.match(re)) throw new Error("ERR: invalid parameter:",from,to)
|
||||
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'.")
|
||||
}
|
||||
|
||||
|
||||
// -- Prepare search --
|
||||
// to calculate average if traveldates are in more than one month
|
||||
let travelPeriods = []
|
||||
@ -167,8 +167,8 @@ async function scoreAndSearch(from, to, queries) {
|
||||
});
|
||||
return period
|
||||
}));
|
||||
|
||||
|
||||
|
||||
|
||||
// calculate ratio and transform into target object
|
||||
return {
|
||||
results: transformer.transform(detailScores),
|
||||
@ -190,7 +190,7 @@ function calculateScores(type, regionDataRows, searchLowParam, searchMaxParam, m
|
||||
value: x[type],
|
||||
score: x[type] === null ? null : sc
|
||||
}
|
||||
|
||||
|
||||
})
|
||||
return result
|
||||
}
|
||||
@ -201,7 +201,7 @@ async function getClimateMinMax() {
|
||||
MIN(temperature_mean) AS temperature_mean,
|
||||
MIN(temperature_mean_min) AS temperature_mean_min,
|
||||
MIN(temperature_mean_max) AS temperature_mean_max,
|
||||
MIN(percipitation) AS percipitation,
|
||||
MIN(precipitation) AS precipitation,
|
||||
MIN(raindays) AS raindays,
|
||||
MIN(sunshine) AS sunhours
|
||||
FROM region_climate`
|
||||
@ -209,13 +209,13 @@ async function getClimateMinMax() {
|
||||
MAX(temperature_mean) AS temperature_mean,
|
||||
MAX(temperature_mean_min) AS temperature_mean_min,
|
||||
MAX(temperature_mean_max) AS temperature_mean_max,
|
||||
MAX(percipitation) AS percipitation,
|
||||
MAX(precipitation) AS precipitation,
|
||||
MAX(raindays) AS raindays,
|
||||
MAX(sunshine) AS sunhours
|
||||
FROM region_climate`
|
||||
const [qResMin, qResMax] = await Promise.all([getQueryRows(sqlMin), getQueryRows(sqlMax)])
|
||||
//console.log(qResMin)
|
||||
return { min: qResMin[0], max: qResMax[0] }
|
||||
return { min: qResMin[0], max: qResMax[0] }
|
||||
}
|
||||
|
||||
async function getQueryRows(sql) {
|
||||
@ -239,7 +239,7 @@ function getAllRegions() {
|
||||
|
||||
function getClimatePerRegionAndMonth(regionId, month) {
|
||||
console.log('getClimatePerRegionAndMonth')
|
||||
const sql = `SELECT region_id, AVG(temperature_mean), AVG(temperature_mean_min), AVG(temperature_mean_max), AVG(percipitation), AVG(sunshine) FROM region_climate WHERE month = ${month} AND region_id = ${regionId}`
|
||||
const sql = `SELECT region_id, AVG(temperature_mean), AVG(temperature_mean_min), AVG(temperature_mean_max), AVG(precipitation), AVG(sunshine) FROM region_climate WHERE month = ${month} AND region_id = ${regionId}`
|
||||
return getQueryRows(sql)
|
||||
}
|
||||
|
||||
@ -252,7 +252,7 @@ function getAllRegionsWithClimatePerMonth(month) {
|
||||
ROUND(AVG(region_climate.temperature_mean), 1) AS temperature_mean,
|
||||
ROUND(AVG(region_climate.temperature_mean_min), 1) AS temperature_mean_min,
|
||||
ROUND(AVG(region_climate.temperature_mean_max), 1) AS temperature_mean_max,
|
||||
ROUND(AVG(region_climate.percipitation), 1) AS percipitation,
|
||||
ROUND(AVG(region_climate.precipitation), 1) AS precipitation,
|
||||
ROUND(AVG(region_climate.raindays), 1) AS raindays,
|
||||
ROUND(AVG(region_climate.sunshine), 1) AS sunhours
|
||||
FROM region_climate JOIN regions ON region_climate.region_id = regions.id WHERE region_climate.month = ${month} GROUP BY region_id`
|
||||
@ -263,7 +263,7 @@ function oldToNewQuerySyntax(queries) {
|
||||
let res = {}
|
||||
try {
|
||||
if (queries.temperature_mean_max) res.temperature_mean_max = [queries.temperature_mean_max.split(',')[0], queries.temperature_mean_max.split(',')[1]]
|
||||
if (queries.percipitation) res.percipitation = [queries.percipitation.split(',')[0], queries.percipitation.split(',')[1]]
|
||||
if (queries.precipitation) res.precipitation = [queries.precipitation.split(',')[0], queries.precipitation.split(',')[1]]
|
||||
if (queries.raindays) res.raindays = [queries.raindays.split(',')[0], queries.raindays.split(',')[1]]
|
||||
if (queries.sunhours) res.sunhours = [queries.sunhours.split(',')[0], queries.sunhours.split(',')[1]]
|
||||
console.log('queries successfully transformed');
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"temperature_mean_max": 5,
|
||||
"percipitation": 3.5,
|
||||
"precipitation": 3.5,
|
||||
"raindays": 3,
|
||||
"sunhours": 2.5
|
||||
}
|
||||
@ -3,8 +3,8 @@ exports.getBYTdataByRegion = async (dbConn, id, travelstyle = 1) => {
|
||||
`SELECT
|
||||
region_id,
|
||||
travelstyle,
|
||||
average_per_day AS average_per_day,
|
||||
accomodation AS accomodation_costs,
|
||||
average_per_day AS average_per_day_costs,
|
||||
accomodation AS accommodation_costs,
|
||||
food AS food_costs,
|
||||
water AS water_costs,
|
||||
local_transportation AS local_transportation_costs,
|
||||
@ -21,8 +21,8 @@ exports.getAllBYTdata = async (dbConn, travelstyle = 1) => {
|
||||
`SELECT
|
||||
region_id,
|
||||
travelstyle,
|
||||
average_per_day AS average_per_day,
|
||||
accomodation AS accomodation_costs,
|
||||
average_per_day AS average_per_day_costs,
|
||||
accomodation AS accommodation_costs,
|
||||
food AS food_costs,
|
||||
water AS water_costs,
|
||||
local_transportation AS local_transportation_costs,
|
||||
|
||||
@ -1,15 +1,15 @@
|
||||
module.exports = async (dbConn, id) => {
|
||||
const region = await dbConn.query(
|
||||
`SELECT regions.id AS region_id,
|
||||
`SELECT regions.id AS region_id,
|
||||
regions.region AS name,
|
||||
countries.country AS country,
|
||||
regions.description AS description,
|
||||
rcma.temperature_mean_max,
|
||||
rcma.percipitation AS percipitation,
|
||||
rcma.precipitation AS precipitation,
|
||||
rcma.raindays AS rain_days,
|
||||
rcma.sunshine AS sun_hours,
|
||||
regions_byt.average_per_day,
|
||||
regions_byt.accomodation AS accomodation_costs,
|
||||
regions_byt.average_per_day AS average_per_day_costs,
|
||||
regions_byt.accomodation AS accommodation_costs,
|
||||
regions_byt.food AS food_costs,
|
||||
regions_byt.water AS water_costs,
|
||||
regions_byt.local_transportation AS local_transportation_costs,
|
||||
@ -21,7 +21,7 @@ module.exports = async (dbConn, 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.percipitation, "") ORDER BY rcma.month SEPARATOR ', ') AS percipitation,
|
||||
GROUP_CONCAT(IFNULL(rcma.precipitation, "") ORDER BY rcma.month SEPARATOR ', ') AS precipitation,
|
||||
GROUP_CONCAT(IFNULL(rcma.raindays, "") ORDER BY rcma.month SEPARATOR ', ') AS raindays,
|
||||
GROUP_CONCAT(IFNULL(rcma.sunshine, "") ORDER BY rcma.month SEPARATOR ', ') AS sunshine,
|
||||
GROUP_CONCAT(IFNULL(rcma.humidity, "") ORDER BY rcma.month SEPARATOR ', ') AS humidity
|
||||
|
||||
@ -1,15 +1,15 @@
|
||||
module.exports = async (dbConn) => {
|
||||
const regions = await dbConn.query(
|
||||
`SELECT regions.id AS region_id,
|
||||
`SELECT regions.id AS region_id,
|
||||
regions.region AS name,
|
||||
countries.country AS country,
|
||||
regions.description AS description,
|
||||
rcma.temperature_mean_max,
|
||||
rcma.percipitation,
|
||||
rcma.temperature_mean_max AS temperature_mean_max,
|
||||
rcma.precipitation AS precipitation,
|
||||
rcma.raindays AS rain_days,
|
||||
rcma.sunshine AS sun_hours,
|
||||
regions_byt.average_per_day,
|
||||
regions_byt.accomodation AS accomodation_costs,
|
||||
regions_byt.average_per_day AS average_per_day_costs,
|
||||
regions_byt.accomodation AS accommodation_costs,
|
||||
regions_byt.food AS food_costs,
|
||||
regions_byt.water AS water_costs,
|
||||
regions_byt.local_transportation AS local_transportation_costs,
|
||||
@ -21,7 +21,7 @@ module.exports = async (dbConn) => {
|
||||
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.percipitation, "") ORDER BY rcma.month SEPARATOR ', ') AS percipitation,
|
||||
GROUP_CONCAT(IFNULL(rcma.precipitation, "") ORDER BY rcma.month SEPARATOR ', ') AS precipitation,
|
||||
GROUP_CONCAT(IFNULL(rcma.raindays, "") ORDER BY rcma.month SEPARATOR ', ') AS raindays,
|
||||
GROUP_CONCAT(IFNULL(rcma.sunshine, "") ORDER BY rcma.month SEPARATOR ', ') AS sunshine,
|
||||
GROUP_CONCAT(IFNULL(rcma.humidity, "") ORDER BY rcma.month SEPARATOR ', ') AS humidity
|
||||
|
||||
@ -8,17 +8,17 @@ const rangeEndDate = '2018-12'// If no date is given, this date will be used as
|
||||
// TODO: call method periodically, not over API
|
||||
module.exports = async (dbConn, startDate = rangeStartDate, endDate = rangeEndDate) => {
|
||||
console.log('update climate with:', startDate, endDate);
|
||||
|
||||
|
||||
const result = await dbConn.query(`SELECT * FROM regions WHERE meteostat_id IS NOT NULL`)
|
||||
|
||||
|
||||
const climateObject = await Promise.all(result.map(src => {
|
||||
return createClimateObjectFrom(src, startDate, endDate)
|
||||
}))
|
||||
const climateObjectArr = climateObject.reduce((total, element) => total.concat(element), [])
|
||||
|
||||
|
||||
await writeToDatabase(dbConn, climateObjectArr)
|
||||
|
||||
const res = `region_climate update complete. see backend logs for info. Success: ${successCounter} Errors: ${errorCounter}`
|
||||
|
||||
const res = `region_climate update complete. see backend logs for info.`
|
||||
return res
|
||||
}
|
||||
|
||||
@ -66,13 +66,13 @@ async function writeToDatabase(dbConn, climateObjArr) {
|
||||
try {
|
||||
await dbConn.query(`
|
||||
INSERT INTO region_climate
|
||||
(region_id, year, month, temperature_mean, temperature_mean_min, temperature_mean_max, percipitation, sunshine, humidity, raindays)
|
||||
(region_id, year, month, temperature_mean, temperature_mean_min, temperature_mean_max, precipitation, sunshine, humidity, raindays)
|
||||
VALUES (${element.region_id}, ${element.year}, ${element.month}, ${element.temperature}, ${element.temperature_min}, ${element.temperature_max}, ${element.precipitation}, ${element.sunshine}, ${element.humidity}, ${element.raindays})
|
||||
ON DUPLICATE KEY UPDATE
|
||||
temperature_mean = ${element.temperature},
|
||||
temperature_mean_min = ${element.temperature_min},
|
||||
temperature_mean_max = ${element.temperature_max},
|
||||
percipitation = ${element.precipitation},
|
||||
precipitation = ${element.precipitation},
|
||||
sunshine = ${element.sunshine},
|
||||
humidity = ${element.humidity},
|
||||
raindays = ${element.raindays};`)
|
||||
|
||||
@ -38,10 +38,13 @@ function searchHandler(dbConn) {
|
||||
|
||||
let scoreQueryObj = {}
|
||||
if (q.temperature) scoreQueryObj['temperature_mean_max'] = q.temperature
|
||||
if (q.percipitation) scoreQueryObj['percipitation'] = q.percipitation
|
||||
if (q.temperature_mean_max) scoreQueryObj['temperature_mean_max'] = q.temperature_mean_max
|
||||
if (q.precipitation) scoreQueryObj['precipitation'] = q.precipitation
|
||||
if (q.rain_days) scoreQueryObj['rain_days'] = q.rain_days
|
||||
if (q.sun_hours) scoreQueryObj['sun_hours'] = q.sun_hours
|
||||
// @TimoJ 1. hier die Parameter die gescored werden sollen hinufügen
|
||||
if (q.accommodation_costs) scoreQueryObj['accommodation_costs'] = q.accomodation_costs
|
||||
|
||||
//console.log(scoreQueryObj)
|
||||
|
||||
if (_.isEmpty(scoreQueryObj)) {
|
||||
res.status(400).send('provide at least one search parameter.');
|
||||
|
||||
@ -11,7 +11,7 @@ module.exports = function (dbConn) {
|
||||
ROUND(AVG(region_climate.temperature_mean), 1) AS temperature_mean,
|
||||
ROUND(AVG(region_climate.temperature_mean_min), 1) AS temperature_mean_min,
|
||||
ROUND(AVG(region_climate.temperature_mean_max), 1) AS temperature_mean_max,
|
||||
ROUND(AVG(region_climate.percipitation), 1) AS percipitation,
|
||||
ROUND(AVG(region_climate.precipitation), 1) AS precipitation,
|
||||
ROUND(AVG(region_climate.raindays), 1) AS rain_days,
|
||||
ROUND(AVG(region_climate.sunshine), 1) AS sun_hours
|
||||
FROM region_climate
|
||||
|
||||
@ -1,22 +1,22 @@
|
||||
exports.getClimateMinMax = async function (dbConn) {
|
||||
console.log('getClimateMinMax')
|
||||
const sqlMin = `SELECT
|
||||
console.log('getClimateMinMax')
|
||||
const sqlMin = `SELECT
|
||||
MIN(temperature_mean) AS temperature_mean,
|
||||
MIN(temperature_mean_min) AS temperature_mean_min,
|
||||
MIN(temperature_mean_max) AS temperature_mean_max,
|
||||
MIN(percipitation) AS percipitation,
|
||||
MIN(precipitation) AS precipitation,
|
||||
MIN(raindays) AS rain_days,
|
||||
MIN(sunshine) AS sun_hours
|
||||
FROM region_climate`
|
||||
const sqlMax = `SELECT
|
||||
const sqlMax = `SELECT
|
||||
MAX(temperature_mean) AS temperature_mean,
|
||||
MAX(temperature_mean_min) AS temperature_mean_min,
|
||||
MAX(temperature_mean_max) AS temperature_mean_max,
|
||||
MAX(percipitation) AS percipitation,
|
||||
MAX(precipitation) AS precipitation,
|
||||
MAX(raindays) AS rain_days,
|
||||
MAX(sunshine) AS sun_hours
|
||||
FROM region_climate`
|
||||
const [qResMin, qResMax] = await Promise.all([dbConn.query(sqlMin), dbConn.query(sqlMax)])
|
||||
// console.log(qResMin)
|
||||
return { min: qResMin[0], max: qResMax[0] }
|
||||
const [qResMin, qResMax] = await Promise.all([dbConn.query(sqlMin), dbConn.query(sqlMax)])
|
||||
// console.log(qResMin)
|
||||
return { min: qResMin[0], max: qResMax[0] }
|
||||
}
|
||||
@ -1,5 +1,5 @@
|
||||
function getClimatePerRegionAndMonth(regionId, month) {
|
||||
console.log('getClimatePerRegionAndMonth')
|
||||
const sql = `SELECT region_id, AVG(temperature_mean), AVG(temperature_mean_min), AVG(temperature_mean_max), AVG(percipitation), AVG(sunshine) FROM region_climate WHERE month = ${month} AND region_id = ${regionId}`
|
||||
const sql = `SELECT region_id, AVG(temperature_mean), AVG(temperature_mean_min), AVG(temperature_mean_max), AVG(precipitation), AVG(sunshine) FROM region_climate WHERE month = ${month} AND region_id = ${regionId}`
|
||||
return getQueryRows(sql)
|
||||
}
|
||||
@ -2,7 +2,7 @@ exports.oldToNewQuerySyntax = function (queries) {
|
||||
let res = {}
|
||||
try {
|
||||
if (queries.temperature_mean_max) res.temperature_mean_max = [Number(queries.temperature_mean_max.split(',')[0]), Number(queries.temperature_mean_max.split(',')[1])]
|
||||
if (queries.percipitation) res.percipitation = [Number(queries.percipitation.split(',')[0]), Number(queries.percipitation.split(',')[1])]
|
||||
if (queries.precipitation) res.precipitation = [Number(queries.precipitation.split(',')[0]), Number(queries.precipitation.split(',')[1])]
|
||||
if (queries.rain_days) res.rain_days = [Number(queries.rain_days.split(',')[0]), Number(queries.rain_days.split(',')[1])]
|
||||
if (queries.sun_hours) res.sun_hours = [Number(queries.sun_hours.split(',')[0]), Number(queries.sun_hours.split(',')[1])]
|
||||
if (queries.accomodation_costs) res.sun_hours = [Number(queries.accomodation_costs.split(',')[0]), Number(queries.accomodation_costs.split(',')[1])]
|
||||
|
||||
@ -5,7 +5,7 @@ exports.calculateAvgScore = (...scores) => {
|
||||
}
|
||||
|
||||
exports.calculateScoreRange = (min, max, multiplier, regionVal, sLowVal, sHighVal) => {
|
||||
console.log('scores.calculateScoreRange:', min, max, multiplier, regionVal, sLowVal, sHighVal)
|
||||
//console.log('scores.calculateScoreRange:', min, max, multiplier, regionVal, sLowVal, sHighVal)
|
||||
// return full score when in range
|
||||
if (regionVal >= sLowVal && regionVal <= sHighVal) return 10;
|
||||
// choose value with smallest distance
|
||||
|
||||
@ -10,7 +10,7 @@ const getRegions = require('../models/getRegions2.js')
|
||||
const SHOW_ALL_SCOREOBJECTS = false
|
||||
const MULTIPLIER = {
|
||||
temperature_mean_max: 5,
|
||||
percipitation: 3.5,
|
||||
precipitation: 3.5,
|
||||
rain_days: 3,
|
||||
sun_hours: 2.5,
|
||||
accomodation_costs: 1
|
||||
@ -25,36 +25,51 @@ module.exports = function (dbConn) {
|
||||
|
||||
// transform syntax and seperate climate queries from price queries
|
||||
queries = oldToNewQuerySyntax.oldToNewQuerySyntax(queries)
|
||||
// console.log(queries)
|
||||
const q = prepareQueries(queries)
|
||||
// console.log(q)
|
||||
|
||||
// for calculating average if traveldates are in more than one month
|
||||
const travelPeriods = travelPeriodsFromDates(dates)
|
||||
|
||||
// FETCH DATA FROM DB
|
||||
// fetch all data relevant before calculating
|
||||
const minMax = await getClimateMinMax.getClimateMinMax(dbConn)
|
||||
let regions = await getRegions(dbConn)
|
||||
regions.forEach(reg => reg.scores = [])
|
||||
regions.forEach(reg => reg.scores = [])
|
||||
|
||||
// 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.percipitation) q.climate.percipitation = [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]
|
||||
}
|
||||
// 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 = getScoreAndAverage(key, travelPeriods, reg, value[0], value[1], minMax)
|
||||
reg.scores.push(finalScoreObj)
|
||||
reg.scores.push(finalScoreObj)
|
||||
});
|
||||
// CALCULATE SCORES FOR PRICE PROPS
|
||||
|
||||
|
||||
// CALCULATE AVERAGE SCORE
|
||||
reg.score = calculateAverage(reg.scores)
|
||||
})
|
||||
return {
|
||||
results: regions
|
||||
results: _.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: {},
|
||||
@ -62,7 +77,7 @@ module.exports = function (dbConn) {
|
||||
}
|
||||
// climate
|
||||
if (queries.temperature_mean_max) q.climate.temperature_mean_max = queries.temperature_mean_max
|
||||
if (queries.percipitation) q.climate.percipitation = queries.percipitation
|
||||
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
|
||||
@ -72,6 +87,8 @@ module.exports = function (dbConn) {
|
||||
}
|
||||
|
||||
function travelPeriodsFromDates(dates) {
|
||||
//console.log(dates);
|
||||
|
||||
let travelPeriods = []
|
||||
if (dates.from.month === dates.to.month) {
|
||||
let period = {
|
||||
@ -128,23 +145,27 @@ module.exports = function (dbConn) {
|
||||
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'.")
|
||||
}
|
||||
console.log(fromAndTo)
|
||||
|
||||
return fromAndTo
|
||||
}
|
||||
|
||||
function getScoreAndAverage(type, travelPeriods, region, searchLowParam, searchMaxParam, minMax) {
|
||||
console.log('calculateScores for', region.name, type)
|
||||
//console.log(type, travelPeriods, region, searchLowParam, searchMaxParam, minMax)
|
||||
|
||||
// console.log(type, travelPeriods, searchLowParam, searchMaxParam)
|
||||
|
||||
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)
|
||||
console.log(sc)
|
||||
return {
|
||||
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
|
||||
}
|
||||
console.log(res);
|
||||
|
||||
return res
|
||||
})
|
||||
|
||||
let averagedScore = {
|
||||
@ -162,7 +183,7 @@ module.exports = function (dbConn) {
|
||||
} else {
|
||||
console.log('skip averaging')
|
||||
console.log(el)
|
||||
|
||||
|
||||
}
|
||||
})
|
||||
averagedScore.value = _.round(averagedScore.value / averagedScore.days, 1)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user