Changed accordingly to database col namechanges

This commit is contained in:
Timo John 2020-06-18 12:06:55 +02:00
parent ef30abd8b5
commit aba72921ea
7 changed files with 47 additions and 54 deletions

View File

@ -1,6 +1,7 @@
PORT= PORT=3000
METEOSTAT_API_KEY= METEOSTAT_API_KEY=LMlDskju
DB_HOST= DB_HOST=lhinderberger.dev
DB_USER= DB_USER=root
DB_PASSWORD= DB_PASSWORD=devtest
DB_PORT= DB_PORT=3306
DATABASE=travopti

View File

@ -6,17 +6,17 @@ module.exports = async (dbConn, 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_max, rcma.temperature_mean_max AS temperature_mean_max,
rcma.precipitation AS precipitation, rcma.precipitation AS precipitation,
rcma.raindays AS rain_days, rcma.rain_days AS rain_days,
rcma.sunshine AS sun_hours, rcma.sun_hours AS sun_hours,
regions_byt.average_per_day AS average_per_day_costs, regions_byt.average_per_day_costs AS average_per_day_costs,
regions_byt.accomodation AS accommodation_costs, regions_byt.accommodation_costs AS accommodation_costs,
regions_byt.food AS food_costs, regions_byt.food_costs AS food_costs,
regions_byt.water AS water_costs, regions_byt.water_costs AS water_costs,
regions_byt.local_transportation AS local_transportation_costs, regions_byt.local_transportation_costs AS local_transportation_costs,
regions_byt.entertainment AS entertainment_costs, regions_byt.entertainment_costs AS entertainment_costs,
regions_byt.alcohol AS alcohol_costs regions_byt.alcohol_costs AS alcohol_costs
FROM regions FROM regions
LEFT JOIN countries ON regions.country_id = countries.id LEFT JOIN countries ON regions.country_id = countries.id
LEFT JOIN (SELECT rcma.region_id, LEFT JOIN (SELECT rcma.region_id,
@ -24,8 +24,8 @@ module.exports = async (dbConn, id) => {
GROUP_CONCAT(IFNULL(rcma.temperature_mean_min, "") ORDER BY rcma.month SEPARATOR ', ') AS temperature_mean_min, 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.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.precipitation, "") ORDER BY rcma.month SEPARATOR ', ') AS precipitation,
GROUP_CONCAT(IFNULL(rcma.raindays, "") ORDER BY rcma.month SEPARATOR ', ') AS raindays, GROUP_CONCAT(IFNULL(rcma.rain_days, "") ORDER BY rcma.month SEPARATOR ', ') AS rain_days,
GROUP_CONCAT(IFNULL(rcma.sunshine, "") ORDER BY rcma.month SEPARATOR ', ') AS sunshine, 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 GROUP_CONCAT(IFNULL(rcma.humidity, "") ORDER BY rcma.month SEPARATOR ', ') AS humidity
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

View File

@ -9,15 +9,15 @@ module.exports = async (dbConn) => {
regions.description AS description, regions.description AS description,
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.raindays AS rain_days, rcma.rain_days AS rain_days,
rcma.sunshine AS sun_hours, rcma.sun_hours AS sun_hours,
regions_byt.average_per_day AS average_per_day_costs, regions_byt.average_per_day_costs AS average_per_day_costs,
regions_byt.accomodation AS accommodation_costs, regions_byt.accommodation_costs AS accommodation_costs,
regions_byt.food AS food_costs, regions_byt.food_costs AS food_costs,
regions_byt.water AS water_costs, regions_byt.water_costs AS water_costs,
regions_byt.local_transportation AS local_transportation_costs, regions_byt.local_transportation_costs AS local_transportation_costs,
regions_byt.entertainment AS entertainment_costs, regions_byt.entertainment_costs AS entertainment_costs,
regions_byt.alcohol AS alcohol_costs regions_byt.alcohol_costs AS alcohol_costs
FROM regions FROM regions
LEFT JOIN countries ON regions.country_id = countries.id LEFT JOIN countries ON regions.country_id = countries.id
LEFT JOIN (SELECT rcma.region_id, LEFT JOIN (SELECT rcma.region_id,
@ -25,8 +25,8 @@ module.exports = async (dbConn) => {
GROUP_CONCAT(IFNULL(rcma.temperature_mean_min, "") ORDER BY rcma.month SEPARATOR ', ') AS temperature_mean_min, 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.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.precipitation, "") ORDER BY rcma.month SEPARATOR ', ') AS precipitation,
GROUP_CONCAT(IFNULL(rcma.raindays, "") ORDER BY rcma.month SEPARATOR ', ') AS raindays, GROUP_CONCAT(IFNULL(rcma.rain_days, "") ORDER BY rcma.month SEPARATOR ', ') AS rain_days,
GROUP_CONCAT(IFNULL(rcma.sunshine, "") ORDER BY rcma.month SEPARATOR ', ') AS sunshine, 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 GROUP_CONCAT(IFNULL(rcma.humidity, "") ORDER BY rcma.month SEPARATOR ', ') AS humidity
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

View File

@ -2,8 +2,8 @@ const axios = require('axios')
const _ = require('lodash') const _ = require('lodash')
// TODO: Automatically retrieve dates via aviable Data and get rid of random dates // 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 const rangeStartDate = '2019-01' // If no date is given, this date will be used as startDate
const rangeEndDate = '2018-12'// If no date is given, this date will be used as endDate const rangeEndDate = '2020-05'// If no date is given, this date will be used as endDate
// TODO: call method periodically, not over API // TODO: call method periodically, not over API
module.exports = async (dbConn, startDate = rangeStartDate, endDate = rangeEndDate) => { module.exports = async (dbConn, startDate = rangeStartDate, endDate = rangeEndDate) => {
@ -46,12 +46,12 @@ async function createClimateObjectFrom(src, startDate, endDate) {
region_id: src.id, region_id: src.id,
year: element.month.split("-")[0], year: element.month.split("-")[0],
month: element.month.split("-")[1], month: element.month.split("-")[1],
temperature: element.temperature_mean, temperature_mean: element.temperature_mean,
temperature_min: element.temperature_mean_min, temperature_mean_min: element.temperature_mean_min,
temperature_max: element.temperature_mean_max, temperature_mean_max: element.temperature_mean_max,
precipitation: element.precipitation, precipitation: element.precipitation,
raindays: element.raindays, rain_days: element.raindays,
sunshine: element.sunshine, sun_hours: element.sunshine,
humidity: element.humidity ? element.humidity : null humidity: element.humidity ? element.humidity : null
} }
//console.log(result) //console.log(result)
@ -66,16 +66,16 @@ async function writeToDatabase(dbConn, climateObjArr) {
try { try {
await dbConn.query(` await dbConn.query(`
INSERT INTO region_climate INSERT INTO region_climate
(region_id, year, month, temperature_mean, temperature_mean_min, temperature_mean_max, precipitation, sunshine, humidity, raindays) (region_id, year, month, temperature_mean, temperature_mean_min, temperature_mean_max, precipitation, sun_hours, humidity, rain_days)
VALUES (${element.region_id}, ${element.year}, ${element.month}, ${element.temperature}, ${element.temperature_min}, ${element.temperature_max}, ${element.precipitation}, ${element.sunshine}, ${element.humidity}, ${element.raindays}) VALUES (${element.region_id}, ${element.year}, ${element.month}, ${element.temperature_mean}, ${element.temperature_mean_min}, ${element.temperature_mean_max}, ${element.precipitation}, ${element.sun_hours}, ${element.humidity}, ${element.rain_days})
ON DUPLICATE KEY UPDATE ON DUPLICATE KEY UPDATE
temperature_mean = ${element.temperature}, temperature_mean = ${element.temperature_mean},
temperature_mean_min = ${element.temperature_min}, temperature_mean_min = ${element.temperature_mean_min},
temperature_mean_max = ${element.temperature_max}, temperature_mean_max = ${element.temperature_mean_max},
precipitation = ${element.precipitation}, precipitation = ${element.precipitation},
sunshine = ${element.sunshine}, sun_hours = ${element.sun_hours},
humidity = ${element.humidity}, humidity = ${element.humidity},
raindays = ${element.raindays};`) rain_days = ${element.rain_days};`)
} catch (error) { } catch (error) {
if (error.code !== 'ER_DUP_ENTRY') { if (error.code !== 'ER_DUP_ENTRY') {
console.log("element which causes problems: ") console.log("element which causes problems: ")

View File

@ -13,7 +13,7 @@ module.exports = dbConn => {
if (fs.existsSync(path.join(__dirname, `../data/regions/images/${req.params.id}.jpg`))) { if (fs.existsSync(path.join(__dirname, `../data/regions/images/${req.params.id}.jpg`))) {
res.sendFile(path.join(__dirname, `../data/regions/images/${req.params.id}.jpg`)) res.sendFile(path.join(__dirname, `../data/regions/images/${req.params.id}.jpg`))
} else { } else {
res.sendFile(path.join(__dirname, `../data/regions/images/x.jpg`)) res.sendFile(path.join(__dirname, `../data/regions/images/x.png`))
} }
}) })
router.get("/api/v1/regions/:id", async (req, res) => { router.get("/api/v1/regions/:id", async (req, res) => {

View File

@ -23,7 +23,7 @@ module.exports = async config => {
user: process.env.DB_USER, user: process.env.DB_USER,
password: process.env.DB_PASSWORD, password: process.env.DB_PASSWORD,
port: process.env.DB_PORT, port: process.env.DB_PORT,
database: 'travopti', database: process.env.DATABASE,
connectionLimit: 10 connectionLimit: 10
}); });

10
package-lock.json generated
View File

@ -1,11 +1,3 @@
{ {
"requires": true, "lockfileVersion": 1
"lockfileVersion": 1,
"dependencies": {
"httpolyglot": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/httpolyglot/-/httpolyglot-0.1.2.tgz",
"integrity": "sha1-5NNH/omEpi9GfUBg31J/GFH2mXs="
}
}
} }