From 32e8e6cf59ae8d39e926edcb82bf926b3b8add0e Mon Sep 17 00:00:00 2001 From: Timo John Date: Tue, 16 Jun 2020 00:50:43 +0200 Subject: [PATCH] Split up massive app.js in small files --- backend/.env.sample | 1 + backend/index.js | 1 - backend/mockdata/multiplier.json | 6 +++++ backend/mockdata/sample-presets.json | 6 +++++ .../getAllRegionsWithClimatePerMonth.js | 15 +++++++++++++ backend/models/getClimateMinMax.js | 22 +++++++++++++++++++ backend/models/getClimatePerRegionAndMonth.js | 5 +++++ backend/models/getRegions.js | 14 ++++++++++++ backend/models/getSearchPresets.js | 8 +++---- backend/models/handleClimateUpdate.js | 10 +++++---- backend/routes/regions.js | 9 ++------ backend/util/calculateScores.js | 15 +++++++++++++ backend/util/oldToNewQuerySyntax.js | 14 ++++++++++++ backend/util/scoreAndSearch.js | 0 14 files changed, 110 insertions(+), 16 deletions(-) create mode 100644 backend/mockdata/multiplier.json create mode 100644 backend/mockdata/sample-presets.json create mode 100644 backend/models/getAllRegionsWithClimatePerMonth.js create mode 100644 backend/models/getClimateMinMax.js create mode 100644 backend/models/getClimatePerRegionAndMonth.js create mode 100644 backend/util/calculateScores.js create mode 100644 backend/util/oldToNewQuerySyntax.js create mode 100644 backend/util/scoreAndSearch.js diff --git a/backend/.env.sample b/backend/.env.sample index 47819fc..db523ba 100644 --- a/backend/.env.sample +++ b/backend/.env.sample @@ -1,3 +1,4 @@ +PORT= METEOSTAT_API_KEY= DB_HOST= DB_USER= diff --git a/backend/index.js b/backend/index.js index 3deea01..5795a84 100644 --- a/backend/index.js +++ b/backend/index.js @@ -19,7 +19,6 @@ const app = express(); (async () => { try { - // console.log(process.env); const dbConn = await dbConnection(); // Express middleware diff --git a/backend/mockdata/multiplier.json b/backend/mockdata/multiplier.json new file mode 100644 index 0000000..c2b0747 --- /dev/null +++ b/backend/mockdata/multiplier.json @@ -0,0 +1,6 @@ +{ + "temperature_mean_max": 5, + "percipitation": 3.5, + "raindays": 3, + "sunhours": 2.5 +} \ No newline at end of file diff --git a/backend/mockdata/sample-presets.json b/backend/mockdata/sample-presets.json new file mode 100644 index 0000000..5293a34 --- /dev/null +++ b/backend/mockdata/sample-presets.json @@ -0,0 +1,6 @@ +{ + "id": 29837, + "parameter": "temperature", + "label": "warm", + "values": [22, 25] +} \ No newline at end of file diff --git a/backend/models/getAllRegionsWithClimatePerMonth.js b/backend/models/getAllRegionsWithClimatePerMonth.js new file mode 100644 index 0000000..ead7583 --- /dev/null +++ b/backend/models/getAllRegionsWithClimatePerMonth.js @@ -0,0 +1,15 @@ +function getAllRegionsWithClimatePerMonth(month) { + console.log('getAllRegionsWithClimatePerMonth') + const sql = `SELECT + region_climate.region_id AS region_id, + regions.country_id AS country_id, + regions.region AS name, + 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.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` + return getQueryRows(sql) +} \ No newline at end of file diff --git a/backend/models/getClimateMinMax.js b/backend/models/getClimateMinMax.js new file mode 100644 index 0000000..d05a531 --- /dev/null +++ b/backend/models/getClimateMinMax.js @@ -0,0 +1,22 @@ +async function getClimateMinMax() { + 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(raindays) AS raindays, + MIN(sunshine) AS sunhours + FROM region_climate` + 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(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] } +} \ No newline at end of file diff --git a/backend/models/getClimatePerRegionAndMonth.js b/backend/models/getClimatePerRegionAndMonth.js new file mode 100644 index 0000000..eb187ff --- /dev/null +++ b/backend/models/getClimatePerRegionAndMonth.js @@ -0,0 +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}` + return getQueryRows(sql) +} \ No newline at end of file diff --git a/backend/models/getRegions.js b/backend/models/getRegions.js index e69de29..a1efa2e 100644 --- a/backend/models/getRegions.js +++ b/backend/models/getRegions.js @@ -0,0 +1,14 @@ +module.exports = async (dbConn, lat, long, radius) => { + const regions = await dbConn.query( + `SELECT regions.id AS region_id, + regions.region AS name, + regions.country_id AS country_id, + countries.country AS country, + regions.meteostat_id AS meteostat_id + FROM regions + JOIN countries + ON regions.country_id = countries.id` + ); + return regions; +}; + diff --git a/backend/models/getSearchPresets.js b/backend/models/getSearchPresets.js index 7d0b033..f9899df 100644 --- a/backend/models/getSearchPresets.js +++ b/backend/models/getSearchPresets.js @@ -1,6 +1,6 @@ module.exports = async (dbConn, name) => { - const res = await dbConn.query( - - ); - return res[0]; + // TODO: Implement pulling data from database + const presets = require ("../mockdata/sample-presets.json") + const res = presets + return res; }; \ No newline at end of file diff --git a/backend/models/handleClimateUpdate.js b/backend/models/handleClimateUpdate.js index 599646b..92f0f8c 100644 --- a/backend/models/handleClimateUpdate.js +++ b/backend/models/handleClimateUpdate.js @@ -1,22 +1,24 @@ require('dotenv').config() const axios = require('axios') + +// TODO: Automatically retrieve dates via aviable Data const rangeStartDate = '2010-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 +// 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 temp2 = await Promise.all(result.map(src => createClimateObjectFrom(src, startDate, endDate))) - const final2 = temp2.reduce((total, element) => total.concat(element), []) + const climateObject = await Promise.all(result.map(src => createClimateObjectFrom(src, startDate, endDate))) + const climateObjectArr = climateObject.reduce((total, element) => total.concat(element), []) - await writeToDatabase(dbConn, final2) + await writeToDatabase(dbConn, climateObjectArr) const res = 'region_climate update complete. see backend logs for info.' console.log(res) return res - } async function createClimateObjectFrom(src, startDate, endDate) { diff --git a/backend/routes/regions.js b/backend/routes/regions.js index 32a892b..44c4d91 100644 --- a/backend/routes/regions.js +++ b/backend/routes/regions.js @@ -2,13 +2,8 @@ const router = require("express").Router(); const getRegions = require("../models/getRegions.js"); module.exports = dbConn => { - router.get("/api/v1/search", async (req, res) => { - const query = req.query.q; - if (query != undefined) { - res.json(await getRegions(dbConn, query)); - } else { - res.status(400).send(); - } + router.get("/api/v1/regions", async (req, res) => { + res.json(await getRegions(dbConn)); }); return router; diff --git a/backend/util/calculateScores.js b/backend/util/calculateScores.js new file mode 100644 index 0000000..8cb63df --- /dev/null +++ b/backend/util/calculateScores.js @@ -0,0 +1,15 @@ +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 + + return { + region_id: x.region_id, + type: type, + value: x[type], + score: x[type] === null ? null : sc + } + + }) + return result +} \ No newline at end of file diff --git a/backend/util/oldToNewQuerySyntax.js b/backend/util/oldToNewQuerySyntax.js new file mode 100644 index 0000000..004b482 --- /dev/null +++ b/backend/util/oldToNewQuerySyntax.js @@ -0,0 +1,14 @@ +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.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'); + } catch (error) { + console.log('queries are ok'); + return queries + } + return res +} \ No newline at end of file diff --git a/backend/util/scoreAndSearch.js b/backend/util/scoreAndSearch.js new file mode 100644 index 0000000..e69de29