124 lines
5.3 KiB
JavaScript
124 lines
5.3 KiB
JavaScript
require('dotenv').config()
|
|
const mysql = require('mysql2/promise');
|
|
const axios = require('axios')
|
|
|
|
const rangeStartDate = '2010-01'
|
|
const rangeEndDate = '2018-12'
|
|
|
|
exports.update = async function (startDate = rangeStartDate, endDate = rangeEndDate) {
|
|
console.log('update climate with:', startDate, endDate);
|
|
|
|
const connection = await mysql.createConnection({
|
|
host: process.env.DB_HOST,
|
|
user: process.env.DB_USER,
|
|
password: process.env.DB_PASSWORD,
|
|
port: process.env.DB_PORT,
|
|
database: 'travopti'
|
|
});
|
|
const [result, fields] = await connection.execute(`SELECT * FROM regions WHERE meteostat_id IS NOT NULL`)
|
|
|
|
// let temp = await Promise.all(result.map(x => createClimateObject(x)))
|
|
// let final = temp.reduce((total, element) => total.concat(element), [])
|
|
|
|
// await writeToDatabase(connection, final)
|
|
|
|
let temp2 = await Promise.all(result.map(x => createClimateObjectFrom(x, startDate, endDate)))
|
|
let final2 = temp2.reduce((total, element) => total.concat(element), [])
|
|
|
|
await writeToDatabase(connection, final2)
|
|
|
|
connection.end();
|
|
let response = 'database update complete. see backend logs for info.'
|
|
console.log(response)
|
|
return response
|
|
}
|
|
|
|
// async function createClimateObject(src) {
|
|
// let response
|
|
// try {
|
|
// response = await axios.get(`https://api.meteostat.net/v1/climate/normals?station=${src.meteostat_id}&key=${process.env.METEOSTAT_API_KEY}`)
|
|
// } catch (error) {
|
|
// console.log("skipping: couldn't find results for following region: ")
|
|
// console.log(src.region + " with meteostat_id " + src.meteostat_id)
|
|
// return []
|
|
// }
|
|
// if (!response.data.data) {
|
|
// console.log("skipping: no data for station meteostat_id " + src.meteostat_id + " (" + src.region + ")")
|
|
// return []
|
|
// }
|
|
// let results = []
|
|
// for (let index = 1; index <= 12; index++) {
|
|
// let result = {
|
|
// region: src.region,
|
|
// region_id: src.id,
|
|
// month: index,
|
|
// temperature: Object.values(response.data.data.temperature)[index - 1] ? Object.values(response.data.data.temperature)[index - 1] : null,
|
|
// temperature_min: Object.values(response.data.data.temperature_min)[index - 1] ? Object.values(response.data.data.temperature_min)[index - 1] : null,
|
|
// temperature_max: Object.values(response.data.data.temperature_max)[index - 1] ? Object.values(response.data.data.temperature_max)[index - 1] : null,
|
|
// precipitation: Object.values(response.data.data.precipitation)[index - 1] ? Object.values(response.data.data.precipitation)[index - 1] : null,
|
|
// sunshine: Object.values(response.data.data.sunshine)[index - 1] ? Object.values(response.data.data.sunshine)[index - 1] : null,
|
|
// }
|
|
// results.push(result)
|
|
// }
|
|
// return results
|
|
// }
|
|
|
|
async function createClimateObjectFrom(src, startDate, endDate) {
|
|
let response
|
|
try {
|
|
response = 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 []
|
|
}
|
|
if (!response.data.data) {
|
|
console.log("skipping: no data for station meteostat_id " + src.meteostat_id + " (" + src.region + ")")
|
|
return []
|
|
}
|
|
let results = response.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 results
|
|
}
|
|
|
|
async function writeToDatabase(dbConnection, climateObjArr) {
|
|
climateObjArr.forEach(async (element) => {
|
|
//console.log(element)
|
|
try {
|
|
if (!element.year) {
|
|
await dbConnection.execute(`
|
|
REPLACE INTO region_climate (region_id, year, month, temperature_mean, temperature_mean_min, temperature_mean_max, percipitation, sunshine)
|
|
VALUES (${element.region_id}, 0, ${element.month}, ${element.temperature}, ${element.temperature_min}, ${element.temperature_max}, ${element.precipitation}, ${element.sunshine});`)
|
|
} else {
|
|
await dbConnection.execute(`
|
|
REPLACE 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});`)
|
|
}
|
|
} 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)
|
|
}
|
|
}
|
|
});
|
|
} |