Compare commits

...

2 Commits

Author SHA1 Message Date
867194ad5c added images endpoint and some tweaks 2020-06-16 10:09:25 +02:00
Timo John
24971248c9 Changed Temp from mean to mean_max
Changed parameters to temperature_mean_max

set database formatting to utf8

add .env to .gitignore

added .env.sample

.gitignore fix
2020-06-15 11:38:52 +02:00
19 changed files with 56 additions and 11 deletions

3
.gitignore vendored
View File

@ -1,3 +1,6 @@
# credentials
.env
# compiled output
/dist

22
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,22 @@
{
// Verwendet IntelliSense zum Ermitteln möglicher Attribute.
// Zeigen Sie auf vorhandene Attribute, um die zugehörigen Beschreibungen anzuzeigen.
// Weitere Informationen finden Sie unter https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"name": "NodeJS: nodemon debug",
"program": "${workspaceFolder}/backend/app.js",
"request": "launch",
"restart": true,
"runtimeExecutable": "nodemon",
"skipFiles": [
"<node_internals>/**"
],
"type": "node",
"envFile": "${workspaceFolder}/backend/.env",
}
]
}

View File

@ -12,6 +12,7 @@ Campus Cup AKMC Data Traveloptimizer
- import `setup.sql` for sample data
### Start
- If you run for the first time: `$(cd backend && npm i)`
- Run `$(cd backend && npm run start)`
- call http://localhost:3000/v1/update/climate to fetch climate data for sample entries.
@ -26,6 +27,10 @@ Campus Cup AKMC Data Traveloptimizer
- sunhours=NUMBER,NUMBER
- percipitation=NUMBER,NUMBER
Alternatively:
- q=<base64encoded>
- q supports all params from above but values are passed as array.
__Examples:__
http://localhost:3000/v1/search?from=2020-06-14&to=2020-07-29&temperature=27,29&raindays=8,12&sunhours=250,300
http://localhost:3000/v1/search?from=2020-06-14&to=2020-07-29

View File

@ -14,7 +14,7 @@
-- Exportiere Datenbank Struktur für travopti
DROP DATABASE IF EXISTS `travopti`;
CREATE DATABASE IF NOT EXISTS `travopti` /*!40100 DEFAULT CHARACTER SET latin1 */;
CREATE DATABASE IF NOT EXISTS `travopti` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `travopti`;
-- Exportiere Struktur von Tabelle travopti.countries

View File

@ -1,5 +0,0 @@
METEOSTAT_API_KEY=LMlDskju
DB_HOST=localhost
DB_USER=root
DB_PASSWORD=devtest
DB_PORT=3306

5
backend/.env.sample Normal file
View File

@ -0,0 +1,5 @@
METEOSTAT_API_KEY=
DB_HOST=
DB_USER=
DB_PASSWORD=
DB_PORT=

View File

@ -11,7 +11,7 @@ const app = express()
const port = 3000
//const multiplier_temp = 5
const multiplier = {
temperature_mean: 5,
temperature_mean_max: 5,
percipitation: 3.5,
raindays: 3,
sunhours: 2.5,
@ -28,12 +28,18 @@ 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/countries', (req, res) => getAllCountries().then(x => res.json({ data: x })))
app.get('/v1/presets', (req, res) => res.json({ data: samplePresets}))
app.get('/v1/search', searchHandler)
app.get('/v1/update/climate', climateUpdateHandler)
app.get('/v1/regions/:id/image', regionImageHandler)
app.listen(port, () => console.log(`Travopti backend listening at http://localhost:${port}`))
function regionImageHandler(req, res) {
res.sendFile(__dirname+`/data/regions/images/${req.params.id}.jpg`)
}
function climateUpdateHandler(req, res) {
let parameter = []
if (req.query.startDate) parameter.push(req.query.startDate)
@ -64,7 +70,7 @@ function searchHandler(req, res) {
console.log('Q:', q)
let queryObj = {}
if (q.temperature) queryObj['temperature_mean'] = q.temperature
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
@ -88,11 +94,11 @@ async function scoreAndSearch(from, to, queries) {
// randomize if empty queries
if (_.isEmpty(queries)) {
let t = _.round(_.random(minMax.min.temperature_mean, minMax.max.temperature_mean-5),0)
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 = `${t},${t + 5}`
queries.temperature_mean_max = `${t},${t + 5}`
queries.percipitation = `${p},${p + 50}`
queries.raindays = `${r},${r + 5}`
queries.sunhours = `${s},${s + 50}`
@ -238,6 +244,14 @@ function getAllRegions() {
return getQueryRows(sql)
}
function getAllCountries() {
const sql = `SELECT
id AS id,
country AS name,
FROM countries`
return getQueryRows(sql)
}
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}`
@ -263,7 +277,7 @@ function getAllRegionsWithClimatePerMonth(month) {
function oldToNewQuerySyntax(queries) {
let res = {}
try {
if (queries.temperature_mean) res.temperature_mean = [queries.temperature_mean.split(',')[0], queries.temperature_mean.split(',')[1]]
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]]

Binary file not shown.

After

Width:  |  Height:  |  Size: 238 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 102 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 253 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 555 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 323 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 995 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 287 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 87 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 504 KiB

View File

@ -3,6 +3,7 @@ const fs = require('fs')
exports.transform = (data) => {
data = _.cloneDeep(data)
// get data
// let data = JSON.parse(fs.readFileSync('transformer-test.json'));
const types = Object.keys(data[0].scores)