preparation for price search
This commit is contained in:
parent
8b0064a5b9
commit
809951fd85
@ -4499,7 +4499,7 @@ CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW `region_climate_monthly_avg
|
||||
ROUND(AVG(rc.sunshine),1) AS sunshine,
|
||||
ROUND(AVG(rc.humidity),1) AS humidity
|
||||
FROM region_climate AS rc
|
||||
WHERE rc.year > YEAR(CURRENT_TIMESTAMP)-6
|
||||
WHERE rc.year > YEAR(CURRENT_TIMESTAMP)-11
|
||||
GROUP BY rc.region_id, rc.month ;
|
||||
|
||||
/*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */;
|
||||
|
||||
45
backend/models/getPriceData.js
Normal file
45
backend/models/getPriceData.js
Normal file
@ -0,0 +1,45 @@
|
||||
exports.getBYTdataByRegion = async (dbConn, id, travelstyle = 1) => {
|
||||
const res = await dbConn.query(
|
||||
`SELECT
|
||||
region_id,
|
||||
travelstyle,
|
||||
average_per_day AS average_per_day,
|
||||
accomodation AS accomodation_costs,
|
||||
food AS food_costs,
|
||||
water AS water_costs,
|
||||
local_transportation AS transportation_costs,
|
||||
entertainment AS entertainment_costs
|
||||
FROM regions_byt
|
||||
WHERE region_id = ? AND travelstyle = ?`,
|
||||
[id, travelstyle]
|
||||
);
|
||||
return res;
|
||||
};
|
||||
|
||||
exports.getAllBYTdata = async (dbConn, travelstyle = 1) => {
|
||||
const res = await dbConn.query(
|
||||
`SELECT
|
||||
region_id,
|
||||
travelstyle,
|
||||
average_per_day AS average_per_day,
|
||||
accomodation AS accomodation_costs,
|
||||
food AS food_costs,
|
||||
water AS water_costs,
|
||||
local_transportation AS transportation_costs,
|
||||
entertainment AS entertainment_costs
|
||||
FROM regions_byt
|
||||
WHERE travelstyle = ?`,
|
||||
[travelstyle]
|
||||
);
|
||||
return res;
|
||||
};
|
||||
|
||||
exports.getTrivagoData = async (dbConn, id) => {
|
||||
const region = await dbConn.query(
|
||||
`...`,
|
||||
[id]
|
||||
);
|
||||
return region;
|
||||
};
|
||||
|
||||
|
||||
@ -90,6 +90,6 @@ module.exports = async (dbConn, id) => {
|
||||
}
|
||||
}
|
||||
}
|
||||
return region;
|
||||
return region[0];
|
||||
};
|
||||
|
||||
|
||||
@ -1,93 +1,43 @@
|
||||
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,
|
||||
rcma.temperature_mean_min,
|
||||
rcma.temperature_mean_max,
|
||||
rcma.percipitation,
|
||||
rcma.raindays,
|
||||
rcma.sunshine,
|
||||
rcma.humidity,
|
||||
regions_byt.average_per_day,
|
||||
regions_byt.accomodation,
|
||||
regions_byt.food,
|
||||
regions_byt.water,
|
||||
regions_byt.local_transportation,
|
||||
regions_byt.entertainment,
|
||||
regions_byt.tips_and_handouts,
|
||||
regions_byt.scams_robberies_and_mishaps,
|
||||
regions_byt.alcohol
|
||||
FROM regions
|
||||
JOIN countries ON regions.country_id = countries.id
|
||||
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.percipitation, "") ORDER BY rcma.month SEPARATOR ', ') AS percipitation,
|
||||
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
|
||||
FROM region_climate_monthly_avg AS rcma
|
||||
GROUP BY rcma.region_id) rcma ON rcma.region_id = regions.id
|
||||
JOIN regions_byt ON regions.id = regions_byt.region_id
|
||||
WHERE regions_byt.travelstyle = 1`
|
||||
);
|
||||
|
||||
for (k = 0; k < regions.length; k++) {
|
||||
if (regions[k].temperature_mean !== null) {
|
||||
const temperature_mean = regions[k].temperature_mean
|
||||
regions[k].temperature_mean = temperature_mean.split(",");
|
||||
for (i = 0; i < regions[k].temperature_mean.length; i++) {
|
||||
regions[k].temperature_mean[i] = parseFloat(regions[k].temperature_mean[i])
|
||||
}
|
||||
}
|
||||
if (regions[k].temperature_mean_min !== null) {
|
||||
const temperature_mean_min = regions[k].temperature_mean_min
|
||||
regions[k].temperature_mean_min = temperature_mean_min.split(",");
|
||||
for (i = 0; i < regions[k].temperature_mean_min.length; i++) {
|
||||
regions[k].temperature_mean_min[i] = parseFloat(regions[k].temperature_mean_min[i])
|
||||
}
|
||||
}
|
||||
if (regions[k].temperature_mean_max !== null) {
|
||||
const temperature_mean_max = regions[k].temperature_mean_max
|
||||
regions[k].temperature_mean_max = temperature_mean_max.split(",");
|
||||
for (i = 0; i < regions[k].temperature_mean_max.length; i++) {
|
||||
regions[k].temperature_mean_max[i] = parseFloat(regions[k].temperature_mean_max[i])
|
||||
}
|
||||
|
||||
}
|
||||
if (regions[k].percipitation !== null) {
|
||||
const percipitation = regions[k].percipitation
|
||||
regions[k].percipitation = percipitation.split(",");
|
||||
for (i = 0; i < regions[k].percipitation.length; i++) {
|
||||
regions[k].percipitation[i] = parseFloat(regions[k].percipitation[i])
|
||||
}
|
||||
}
|
||||
if (regions[k].raindays !== null) {
|
||||
const raindays = regions[k].raindays
|
||||
regions[k].raindays = raindays.split(",");
|
||||
for (i = 0; i < regions[k].raindays.length; i++) {
|
||||
regions[k].raindays[i] = parseFloat(regions[k].raindays[i])
|
||||
}
|
||||
}
|
||||
if (regions[k].sunshine !== null) {
|
||||
const sunshine = regions[k].sunshine
|
||||
regions[k].sunshine = sunshine.split(",");
|
||||
for (i = 0; i < regions[k].sunshine.length; i++) {
|
||||
regions[k].sunshine[i] = parseFloat(regions[k].sunshine[i])
|
||||
}
|
||||
}
|
||||
if (regions[k].humidity !== null) {
|
||||
const humidity = regions[k].humidity
|
||||
regions[k].humidity = humidity.split(",");
|
||||
for (i = 0; i < regions[k].humidity.length; i++) {
|
||||
regions[k].humidity[i] = parseFloat(regions[k].humidity[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
return regions;
|
||||
exports.getRegions = async (dbConn) => {
|
||||
let sql = `SELECT
|
||||
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) => {
|
||||
let regions = await dbConn.query(`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.description,
|
||||
countries.country AS country,
|
||||
regions.meteostat_id AS meteostat_id
|
||||
FROM regions
|
||||
JOIN countries
|
||||
ON regions.country_id = countries.id
|
||||
WHERE regions.id = ?`,
|
||||
[id]
|
||||
);
|
||||
return region;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
93
backend/models/getRegions2.js
Normal file
93
backend/models/getRegions2.js
Normal file
@ -0,0 +1,93 @@
|
||||
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,
|
||||
rcma.temperature_mean_min,
|
||||
rcma.temperature_mean_max,
|
||||
rcma.percipitation,
|
||||
rcma.raindays,
|
||||
rcma.sunshine,
|
||||
rcma.humidity,
|
||||
regions_byt.average_per_day,
|
||||
regions_byt.accomodation,
|
||||
regions_byt.food,
|
||||
regions_byt.water,
|
||||
regions_byt.local_transportation,
|
||||
regions_byt.entertainment,
|
||||
regions_byt.tips_and_handouts,
|
||||
regions_byt.scams_robberies_and_mishaps,
|
||||
regions_byt.alcohol
|
||||
FROM regions
|
||||
JOIN countries ON regions.country_id = countries.id
|
||||
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.percipitation, "") ORDER BY rcma.month SEPARATOR ', ') AS percipitation,
|
||||
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
|
||||
FROM region_climate_monthly_avg AS rcma
|
||||
GROUP BY rcma.region_id) rcma ON rcma.region_id = regions.id
|
||||
JOIN regions_byt ON regions.id = regions_byt.region_id
|
||||
WHERE regions_byt.travelstyle = 1`
|
||||
);
|
||||
|
||||
for (k = 0; k < regions.length; k++) {
|
||||
if (regions[k].temperature_mean !== null) {
|
||||
const temperature_mean = regions[k].temperature_mean
|
||||
regions[k].temperature_mean = temperature_mean.split(",");
|
||||
for (i = 0; i < regions[k].temperature_mean.length; i++) {
|
||||
regions[k].temperature_mean[i] = parseFloat(regions[k].temperature_mean[i])
|
||||
}
|
||||
}
|
||||
if (regions[k].temperature_mean_min !== null) {
|
||||
const temperature_mean_min = regions[k].temperature_mean_min
|
||||
regions[k].temperature_mean_min = temperature_mean_min.split(",");
|
||||
for (i = 0; i < regions[k].temperature_mean_min.length; i++) {
|
||||
regions[k].temperature_mean_min[i] = parseFloat(regions[k].temperature_mean_min[i])
|
||||
}
|
||||
}
|
||||
if (regions[k].temperature_mean_max !== null) {
|
||||
const temperature_mean_max = regions[k].temperature_mean_max
|
||||
regions[k].temperature_mean_max = temperature_mean_max.split(",");
|
||||
for (i = 0; i < regions[k].temperature_mean_max.length; i++) {
|
||||
regions[k].temperature_mean_max[i] = parseFloat(regions[k].temperature_mean_max[i])
|
||||
}
|
||||
|
||||
}
|
||||
if (regions[k].percipitation !== null) {
|
||||
const percipitation = regions[k].percipitation
|
||||
regions[k].percipitation = percipitation.split(",");
|
||||
for (i = 0; i < regions[k].percipitation.length; i++) {
|
||||
regions[k].percipitation[i] = parseFloat(regions[k].percipitation[i])
|
||||
}
|
||||
}
|
||||
if (regions[k].raindays !== null) {
|
||||
const raindays = regions[k].raindays
|
||||
regions[k].raindays = raindays.split(",");
|
||||
for (i = 0; i < regions[k].raindays.length; i++) {
|
||||
regions[k].raindays[i] = parseFloat(regions[k].raindays[i])
|
||||
}
|
||||
}
|
||||
if (regions[k].sunshine !== null) {
|
||||
const sunshine = regions[k].sunshine
|
||||
regions[k].sunshine = sunshine.split(",");
|
||||
for (i = 0; i < regions[k].sunshine.length; i++) {
|
||||
regions[k].sunshine[i] = parseFloat(regions[k].sunshine[i])
|
||||
}
|
||||
}
|
||||
if (regions[k].humidity !== null) {
|
||||
const humidity = regions[k].humidity
|
||||
regions[k].humidity = humidity.split(",");
|
||||
for (i = 0; i < regions[k].humidity.length; i++) {
|
||||
regions[k].humidity[i] = parseFloat(regions[k].humidity[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
return regions;
|
||||
};
|
||||
|
||||
@ -2,7 +2,7 @@ module.exports = async (dbConn) => {
|
||||
let presets = await dbConn.query(
|
||||
`SELECT search_presets.id AS country_id,
|
||||
search_presets.parameter AS parameter,
|
||||
search_presets.name AS label,
|
||||
search_presets.name AS name,
|
||||
CASE
|
||||
WHEN value_2 is NULL THEN value_1
|
||||
ELSE CONCAT(search_presets.value_1,"|",search_presets.value_2)
|
||||
|
||||
@ -1,29 +0,0 @@
|
||||
const base64 = require ("../util/base64.js")
|
||||
const ss = require ("../util/scoreAndSearch.js")
|
||||
|
||||
module.exports = async (dbConn, req, res) => {
|
||||
let response = {}
|
||||
|
||||
response.meta = {
|
||||
params: req.params,
|
||||
query: req.query,
|
||||
headers: req.headers
|
||||
}
|
||||
|
||||
let q = req.query.q ? base64.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.raindays) queryObj['raindays'] = q.raindays
|
||||
if (q.sunhours) queryObj['sunhours'] = q.sunhours
|
||||
|
||||
ss.scoreAndSearch(q.from, q.to, queryObj, dbConn).then(searchResults => {
|
||||
response.data = searchResults
|
||||
res.json(response)
|
||||
}).catch(e => {
|
||||
console.log(e)
|
||||
res.json(e.message)
|
||||
})
|
||||
};
|
||||
@ -1,4 +1,5 @@
|
||||
const axios = require('axios')
|
||||
const _ = require('lodash')
|
||||
|
||||
// TODO: Automatically retrieve dates via aviable Data and get rid of random dates
|
||||
const rangeStartDate = '2010-01' // If no date is given, this date will be used as startDate
|
||||
@ -6,79 +7,84 @@ 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 => 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.'
|
||||
console.log(res)
|
||||
return res
|
||||
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}`
|
||||
return res
|
||||
}
|
||||
|
||||
async function createClimateObjectFrom(src, startDate, endDate) {
|
||||
let res
|
||||
try {
|
||||
res = await axios.get(`https://api.meteostat.net/v1/history/monthly?station=${src.meteostat_id}&start=${startDate}&end=${endDate}&key=${process.env.METEOSTAT_API_KEY}`)
|
||||
} catch (error) {
|
||||
console.log("skipping createClimateObjectFrom: couldn't find results for following region: ")
|
||||
console.log(src.region + " with meteostat_id " + src.meteostat_id)
|
||||
console.log(error)
|
||||
return []
|
||||
let res
|
||||
if (src.meteostat_id === '') {
|
||||
console.log("skipping: no meteostat id ")
|
||||
return []
|
||||
}
|
||||
try {
|
||||
res = await axios.get(`https://api.meteostat.net/v1/history/monthly?station=${src.meteostat_id}&start=${startDate}&end=${endDate}&key=${process.env.METEOSTAT_API_KEY}`)
|
||||
} catch (error) {
|
||||
console.log("error while getting data from meteostat: couldn't find results for following region: ")
|
||||
console.log(src.region + " with meteostat_id " + src.meteostat_id)
|
||||
console.log(error)
|
||||
return []
|
||||
}
|
||||
if (!res.data.data) {
|
||||
console.log("skipping: no data for station with meteostat_id " + src.meteostat_id + " (" + src.region + ")")
|
||||
return []
|
||||
}
|
||||
const retVal = res.data.data.map(element => {
|
||||
let result = {
|
||||
region: src.region,
|
||||
region_id: src.id,
|
||||
year: element.month.split("-")[0],
|
||||
month: element.month.split("-")[1],
|
||||
temperature: element.temperature_mean,
|
||||
temperature_min: element.temperature_mean_min,
|
||||
temperature_max: element.temperature_mean_max,
|
||||
precipitation: element.precipitation,
|
||||
raindays: element.raindays,
|
||||
sunshine: element.sunshine,
|
||||
humidity: element.humidity ? element.humidity : null
|
||||
}
|
||||
if (!res.data.data) {
|
||||
console.log("skipping: no data for station meteostat_id " + src.meteostat_id + " (" + src.region + ")")
|
||||
return []
|
||||
}
|
||||
const retVal = res.data.data.map(element => {
|
||||
let result = {
|
||||
region: src.region,
|
||||
region_id: src.id,
|
||||
year: element.month.split("-")[0],
|
||||
month: element.month.split("-")[1],
|
||||
temperature: element.temperature_mean,
|
||||
temperature_min: element.temperature_mean_min,
|
||||
temperature_max: element.temperature_mean_max,
|
||||
precipitation: element.precipitation,
|
||||
raindays: element.raindays,
|
||||
sunshine: element.sunshine,
|
||||
humidity: element.humidity ? element.humidity : null
|
||||
}
|
||||
//console.log(result)
|
||||
return result
|
||||
})
|
||||
return retVal
|
||||
//console.log(result)
|
||||
return result
|
||||
})
|
||||
return retVal
|
||||
}
|
||||
|
||||
async function writeToDatabase(dbConn, climateObjArr) {
|
||||
for (const element of climateObjArr) {
|
||||
//console.log(element)
|
||||
try {
|
||||
await dbConn.query(`
|
||||
INSERT INTO region_climate
|
||||
(region_id, year, month, temperature_mean, temperature_mean_min, temperature_mean_max, percipitation, 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},
|
||||
sunshine = ${element.sunshine},
|
||||
humidity = ${element.humidity},
|
||||
raindays = ${element.raindays};`)
|
||||
} catch (error) {
|
||||
if (error.code !== 'ER_DUP_ENTRY') {
|
||||
console.log("element which causes problems: ")
|
||||
console.log(element)
|
||||
console.log("query which causes problems: ")
|
||||
console.log(error)
|
||||
} else {
|
||||
console.log(element.region + ": " + error.sqlMessage)
|
||||
}
|
||||
}
|
||||
for (const element of climateObjArr) {
|
||||
//console.log(element)
|
||||
try {
|
||||
await dbConn.query(`
|
||||
INSERT INTO region_climate
|
||||
(region_id, year, month, temperature_mean, temperature_mean_min, temperature_mean_max, percipitation, 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},
|
||||
sunshine = ${element.sunshine},
|
||||
humidity = ${element.humidity},
|
||||
raindays = ${element.raindays};`)
|
||||
} catch (error) {
|
||||
if (error.code !== 'ER_DUP_ENTRY') {
|
||||
console.log("element which causes problems: ")
|
||||
console.log(element)
|
||||
console.log("query which causes problems: ")
|
||||
console.log(error)
|
||||
} else {
|
||||
console.log(element.region + ": " + error.sqlMessage)
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -1,5 +1,5 @@
|
||||
const router = require("express").Router();
|
||||
const getRegions = require("../models/getRegions.js");
|
||||
const getRegions = require("../models/getRegions2.js");
|
||||
const getRegionById = require("../models/getRegionById.js");
|
||||
const path = require("path");
|
||||
|
||||
@ -10,7 +10,6 @@ module.exports = dbConn => {
|
||||
router.get('/api/v1/regions/:id/image', (req, res) => {
|
||||
res.sendFile(path.join(__dirname, `../data/regions/images/${req.params.id}.jpg`))
|
||||
})
|
||||
|
||||
router.get("/api/v1/regions/:id", async (req, res) => {
|
||||
const id = req.params.id;
|
||||
res.json(await getRegionById(dbConn, id))
|
||||
|
||||
@ -1,10 +1,11 @@
|
||||
exports.oldToNewQuerySyntax = function (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.raindays) res.raindays = [queries.raindays.split(',')[0], queries.raindays.split(',')[1]]
|
||||
if (queries.sunhours) res.sunhours = [queries.sunhours.split(',')[0], queries.sunhours.split(',')[1]]
|
||||
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.raindays) res.raindays = [Number(queries.raindays.split(',')[0]), Number(queries.raindays.split(',')[1])]
|
||||
if (queries.sunhours) res.sunhours = [Number(queries.sunhours.split(',')[0]), Number(queries.sunhours.split(',')[1])]
|
||||
// @TimoJ hier noch Parameter hinzufügen wenn du die alte syntax nutzen willst, ansonsten egal
|
||||
console.log('queries successfully transformed');
|
||||
} catch (error) {
|
||||
console.log('queries are ok');
|
||||
|
||||
@ -5,6 +5,8 @@ const oldToNewQuerySyntax = require("./oldToNewQuerySyntax.js")
|
||||
const getAllRegionsWithClimatePerMonth = require('./getAllRegionsWithClimatePerMonth')
|
||||
const score = require('./score')
|
||||
const transformer = require('./transformer')
|
||||
const getRegions = require('../models/getRegions.js').getRegionsInternal
|
||||
// const getRegions = require('../models/getRegions2.js')
|
||||
|
||||
const MULTIPLIER = {
|
||||
temperature_mean_max: 5,
|
||||
@ -20,18 +22,7 @@ module.exports = function (dbConn) {
|
||||
// get Min and Max values for each Parameter
|
||||
const minMax = await getClimateMinMax.getClimateMinMax(dbConn)
|
||||
|
||||
// randomize if query contains randomize=true
|
||||
if (queries.randomize) {
|
||||
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 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.raindays = `${r},${r + 5}`
|
||||
queries.sunhours = `${s},${s + 50}`
|
||||
delete queries.randomize
|
||||
}
|
||||
// to still support old query syntax
|
||||
queries = oldToNewQuerySyntax.oldToNewQuerySyntax(queries)
|
||||
console.log(queries)
|
||||
|
||||
@ -50,7 +41,7 @@ module.exports = function (dbConn) {
|
||||
dayTo = dateTo.getDay()
|
||||
if (moment(dateFrom).add(23, 'hours').isAfter(moment(dateTo))) throw new Error("ERR: 'to' must be at least one day after 'from'.")
|
||||
} else {
|
||||
// to still support old query syntax
|
||||
// this block to still support old query syntax
|
||||
let re = /([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/i;
|
||||
monthFrom = Number(from.split("-")[1])
|
||||
monthTo = Number(to.split("-")[1])
|
||||
@ -95,19 +86,22 @@ module.exports = function (dbConn) {
|
||||
// calculate detail scores
|
||||
let detailScores = await Promise.all(travelPeriods.map(async period => {
|
||||
period.climate = await getAllRegionsWithClimatePerMonth(dbConn)(period.month)
|
||||
// @TimoJ hier müssten die Preise mit rein, z.B.
|
||||
// period.prices = await getAveragePricesPerMonth(dbConn)(period.month)
|
||||
period.scores = {}
|
||||
Object.entries(queries).forEach(([key, value]) => {
|
||||
// console.log('key',key)
|
||||
// console.log('val', value)
|
||||
period.scores[key] = calculateScoresWrapper(key, period.climate, value[0], value[1], minMax)
|
||||
period.scores[key] = calculateScores(key, period.climate, value[0], value[1], minMax)
|
||||
});
|
||||
return period
|
||||
}));
|
||||
|
||||
|
||||
//console.log(allRegs)
|
||||
// calculate ratio and transform into target object
|
||||
let allRegs = await getRegions(dbConn)
|
||||
return {
|
||||
results: transformer.transform(detailScores),
|
||||
results: transformer.transform(detailScores, allRegs),
|
||||
debug: {
|
||||
detailScores: detailScores,
|
||||
minMax: minMax
|
||||
@ -115,7 +109,7 @@ module.exports = function (dbConn) {
|
||||
}
|
||||
}
|
||||
|
||||
function calculateScoresWrapper(type, regionDataRows, searchLowParam, searchMaxParam, minMax) {
|
||||
function calculateScores(type, regionDataRows, searchLowParam, searchMaxParam, minMax) {
|
||||
console.log('calculateScores for', type)
|
||||
let result = regionDataRows.map(x => {
|
||||
const sc = Math.round(score.calculateScoreRange(minMax.min[type], minMax.max[type], MULTIPLIER[type], x[type], searchLowParam, searchMaxParam) * 100) / 100
|
||||
|
||||
@ -1,14 +1,13 @@
|
||||
const _ = require('lodash')
|
||||
const fs = require('fs')
|
||||
|
||||
|
||||
exports.transform = (data) => {
|
||||
exports.transform = (data, regions) => {
|
||||
// get data
|
||||
// let data = JSON.parse(fs.readFileSync('transformer-test.json'));
|
||||
const types = Object.keys(data[0].scores)
|
||||
|
||||
// STEP 1 Create Response Array with region names from first climate object
|
||||
let byRegion = data[0].climate.map(el => {
|
||||
let byRegion = regions.map(el => {
|
||||
// return el
|
||||
return {
|
||||
region_id: el.region_id,
|
||||
@ -16,6 +15,14 @@ exports.transform = (data) => {
|
||||
country: el.country,
|
||||
}
|
||||
})
|
||||
// let byRegion = getRegions().map(el => {
|
||||
// // return el
|
||||
// return {
|
||||
// region_id: el.region_id,
|
||||
// name: el.name,
|
||||
// country: el.country,
|
||||
// }
|
||||
// })
|
||||
|
||||
// STEP 2 Prepare flat scoreobject array and set days property
|
||||
scoreObjs = _.flatten(_.map(data, (period) => {
|
||||
@ -34,7 +41,7 @@ exports.transform = (data) => {
|
||||
let tempScores = _.filter(scoreObjs, { 'region_id': region.region_id, 'type': typ })
|
||||
if (_.some(tempScores, { 'score': null })) {
|
||||
console.log("found 'null' scores! skipping...")
|
||||
//console.log(tempScores)
|
||||
console.log(tempScores)
|
||||
return
|
||||
}
|
||||
let averagedScore = {
|
||||
@ -66,7 +73,7 @@ exports.transform = (data) => {
|
||||
})
|
||||
|
||||
// console.log(results)
|
||||
return _.orderBy(results, 'score', 'desc')
|
||||
return _.orderBy(results, ({ score }) => score || 0, 'desc') //.filter(el => !_.isNaN(el.score))
|
||||
//end
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user