Split up massive app.js in small files
This commit is contained in:
parent
5b7c0e5bb3
commit
692336a0ba
@ -1,3 +1,4 @@
|
|||||||
|
PORT=
|
||||||
METEOSTAT_API_KEY=
|
METEOSTAT_API_KEY=
|
||||||
DB_HOST=
|
DB_HOST=
|
||||||
DB_USER=
|
DB_USER=
|
||||||
|
|||||||
@ -19,7 +19,6 @@ const app = express();
|
|||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
try {
|
try {
|
||||||
// console.log(process.env);
|
|
||||||
const dbConn = await dbConnection();
|
const dbConn = await dbConnection();
|
||||||
|
|
||||||
// Express middleware
|
// Express middleware
|
||||||
|
|||||||
6
backend/mockdata/multiplier.json
Normal file
6
backend/mockdata/multiplier.json
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"temperature_mean_max": 5,
|
||||||
|
"percipitation": 3.5,
|
||||||
|
"raindays": 3,
|
||||||
|
"sunhours": 2.5
|
||||||
|
}
|
||||||
6
backend/mockdata/sample-presets.json
Normal file
6
backend/mockdata/sample-presets.json
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"id": 29837,
|
||||||
|
"parameter": "temperature",
|
||||||
|
"label": "warm",
|
||||||
|
"values": [22, 25]
|
||||||
|
}
|
||||||
15
backend/models/getAllRegionsWithClimatePerMonth.js
Normal file
15
backend/models/getAllRegionsWithClimatePerMonth.js
Normal file
@ -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)
|
||||||
|
}
|
||||||
22
backend/models/getClimateMinMax.js
Normal file
22
backend/models/getClimateMinMax.js
Normal file
@ -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] }
|
||||||
|
}
|
||||||
5
backend/models/getClimatePerRegionAndMonth.js
Normal file
5
backend/models/getClimatePerRegionAndMonth.js
Normal file
@ -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)
|
||||||
|
}
|
||||||
@ -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;
|
||||||
|
};
|
||||||
|
|
||||||
@ -1,6 +1,6 @@
|
|||||||
module.exports = async (dbConn, name) => {
|
module.exports = async (dbConn, name) => {
|
||||||
const res = await dbConn.query(
|
// TODO: Implement pulling data from database
|
||||||
|
const presets = require ("../mockdata/sample-presets.json")
|
||||||
);
|
const res = presets
|
||||||
return res[0];
|
return res;
|
||||||
};
|
};
|
||||||
@ -1,22 +1,24 @@
|
|||||||
require('dotenv').config()
|
require('dotenv').config()
|
||||||
const axios = require('axios')
|
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 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
|
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) => {
|
module.exports = async (dbConn, startDate = rangeStartDate, endDate = rangeEndDate) => {
|
||||||
console.log('update climate with:', startDate, endDate);
|
console.log('update climate with:', startDate, endDate);
|
||||||
|
|
||||||
const result = await dbConn.query(`SELECT * FROM regions WHERE meteostat_id IS NOT NULL`)
|
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 climateObject = await Promise.all(result.map(src => createClimateObjectFrom(src, startDate, endDate)))
|
||||||
const final2 = temp2.reduce((total, element) => total.concat(element), [])
|
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.'
|
const res = 'region_climate update complete. see backend logs for info.'
|
||||||
console.log(res)
|
console.log(res)
|
||||||
return res
|
return res
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createClimateObjectFrom(src, startDate, endDate) {
|
async function createClimateObjectFrom(src, startDate, endDate) {
|
||||||
|
|||||||
@ -2,13 +2,8 @@ const router = require("express").Router();
|
|||||||
const getRegions = require("../models/getRegions.js");
|
const getRegions = require("../models/getRegions.js");
|
||||||
|
|
||||||
module.exports = dbConn => {
|
module.exports = dbConn => {
|
||||||
router.get("/api/v1/search", async (req, res) => {
|
router.get("/api/v1/regions", async (req, res) => {
|
||||||
const query = req.query.q;
|
res.json(await getRegions(dbConn));
|
||||||
if (query != undefined) {
|
|
||||||
res.json(await getRegions(dbConn, query));
|
|
||||||
} else {
|
|
||||||
res.status(400).send();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return router;
|
return router;
|
||||||
|
|||||||
15
backend/util/calculateScores.js
Normal file
15
backend/util/calculateScores.js
Normal file
@ -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
|
||||||
|
}
|
||||||
14
backend/util/oldToNewQuerySyntax.js
Normal file
14
backend/util/oldToNewQuerySyntax.js
Normal file
@ -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
|
||||||
|
}
|
||||||
0
backend/util/scoreAndSearch.js
Normal file
0
backend/util/scoreAndSearch.js
Normal file
Loading…
Reference in New Issue
Block a user