From 26903855a1593d19fc8c6b57bac757b8c7aa0271 Mon Sep 17 00:00:00 2001 From: Lluis Date: Fri, 16 Feb 2024 23:28:47 +0100 Subject: [PATCH] Use mariadb for renfe database --- .github/workflows/main_twitch-mz-bot.yml | 4 ++ config/index.js | 8 ++- helpers/mariadbManager.js | 60 ++++++++++++++++++++++ helpers/{sqlManager.js => mssqlManager.js} | 0 package.json | 1 + services/renfe.js | 2 +- yarn.lock | 31 +++++++++++ 7 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 helpers/mariadbManager.js rename helpers/{sqlManager.js => mssqlManager.js} (100%) diff --git a/.github/workflows/main_twitch-mz-bot.yml b/.github/workflows/main_twitch-mz-bot.yml index ae7fb2d..dd9064d 100644 --- a/.github/workflows/main_twitch-mz-bot.yml +++ b/.github/workflows/main_twitch-mz-bot.yml @@ -31,6 +31,10 @@ jobs: envKey_TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }} envKey_EXTERNAL_URL: ${{ secrets.EXTERNAL_URL }} envKey_BROWSERLESS_URL: ${{ secrets.BROWSERLESS_URL }} + envKey_MYSQL_HOST: ${{ secrets.MYSQL_HOST }} + envKey_MYSQL_USER: ${{ secrets.MYSQL_USER }} + envKey_MYSQL_PASSWORD: ${{ secrets.MYSQL_PASSWORD }} + envKey_MYSQL_DB: ${{ secrets.MYSQL_DB }} - name: Set up Node.js version uses: actions/setup-node@v4 with: diff --git a/config/index.js b/config/index.js index 2aa78fc..9cc643b 100644 --- a/config/index.js +++ b/config/index.js @@ -17,5 +17,11 @@ module.exports = { apiKey: process.env.TELEGRAM_TOKEN, chatId: process.env.TELEGRAM_CHAT_ID }, - browserlessUrl: process.env.BROWSERLESS_URL + browserlessUrl: process.env.BROWSERLESS_URL, + mariadb: { + host: process.env.MYSQL_HOST, + user: process.env.MYSQL_USER, + password: process.env.MYSQL_PASSWORD, + db: process.env.MYSQL_DB + } } diff --git a/helpers/mariadbManager.js b/helpers/mariadbManager.js new file mode 100644 index 0000000..d4d736e --- /dev/null +++ b/helpers/mariadbManager.js @@ -0,0 +1,60 @@ +const mariadb = require('mariadb') +const config = require('../config') +async function getCloserFutureTrain (origin, destination, type) { + this.pool = mariadb.createPool({ + host: config.mariadb.host, + user: config.mariadb.user, + password: config.mariadb.password, + connectionLimit: 5, + database: config.mariadb.db, + }) + + let conn + try { + conn = await this.pool.getConnection(); + const rows = await conn.query(`SELECT A.departure_time departure, A.stop_name origin_stop, B.stop_name destination_stop, B.arrival_time arrival FROM +(SELECT ST.trip_id , S.stop_name, ST.stop_id, ST.stop_sequence, ST.departure_time +from stop_times ST +inner join stops S ON S.stop_id = ST.stop_id +WHERE +S.stop_name LIKE '%${origin}%' and +ST.pickup_type = 0 + +) AS A + +INNER JOIN + +(SELECT ST.trip_id, ST.stop_id, ST.stop_sequence, S.stop_name, ST.arrival_time +from stop_times ST +inner join stops S ON S.stop_id = ST.stop_id +WHERE +S.stop_name LIKE '%${destination}%' and +ST.drop_off_type = 0 +) AS B + +ON A.trip_id = B.trip_id +inner join trips T ON T.trip_id = A.trip_id +inner join routes R ON R.route_id = T.route_id +left join calendar C ON C.service_id = T.service_id + +WHERE A.stop_sequence < B.stop_sequence +AND (C.start_date IS NULL OR C.start_date <= DATE_ADD(CURDATE(), INTERVAL 1 HOUR )) +and (C.end_date IS NULL OR C.end_date >= DATE_ADD(CURDATE(), INTERVAL 1 HOUR )) +AND T.service_id NOT IN (SELECT CD.service_id FROM calendar_dates CD +where CD.date = DATE_ADD(CURDATE(), INTERVAL 1 HOUR ) ) +AND route_short_name = '${type}' +AND A.departure_time > TIME(DATE_ADD(CURDATE(), INTERVAL 1 HOUR )) + +ORDER BY ABS( TIMEDIFF(A.departure_time, TIME(DATE_ADD(CURDATE(), INTERVAL 1 HOUR ))) ) +LIMIT 1`) + if (conn) await conn.end() + return rows[0] + + } catch (err) { + console.log(err) + } +} + +module.exports = { + getCloserFutureTrain +} diff --git a/helpers/sqlManager.js b/helpers/mssqlManager.js similarity index 100% rename from helpers/sqlManager.js rename to helpers/mssqlManager.js diff --git a/package.json b/package.json index 7154e1a..4de2b11 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "@twurple/auth-tmi": "^7.0.8", "dotenv": "^16.4.1", "express": "^4.18.2", + "mariadb": "^3.2.3", "mathjs": "^12.3.1", "moment": "^2.30.1", "moment-precise-range-plugin": "^1.3.0", diff --git a/services/renfe.js b/services/renfe.js index b7aec53..c450a9f 100644 --- a/services/renfe.js +++ b/services/renfe.js @@ -1,4 +1,4 @@ -const sqlManager = require('../helpers/sqlManager') +const sqlManager = require('../helpers/mariadbManager') async function getNextMD (origin, destination) { return sqlManager.getCloserFutureTrain(origin, destination, 'MD') diff --git a/yarn.lock b/yarn.lock index c72ea3d..f4b6e02 100644 --- a/yarn.lock +++ b/yarn.lock @@ -318,6 +318,11 @@ klona "^2.0.4" tslib "^2.0.3" +"@types/geojson@^7946.0.10": + version "7946.0.14" + resolved "https://registry.yarnpkg.com/@types/geojson/-/geojson-7946.0.14.tgz#319b63ad6df705ee2a65a73ef042c8271e696613" + integrity sha512-WCfD5Ht3ZesJUsONdhvm84dmzWOiOzOAqOncN0++w0lBw1o8OuDNJF2McvvCef/yBqb/HYRahp1BYtODFQ8bRg== + "@types/node@*": version "20.11.16" resolved "https://registry.yarnpkg.com/@types/node/-/node-20.11.16.tgz#4411f79411514eb8e2926f036c86c9f0e4ec6708" @@ -325,6 +330,11 @@ dependencies: undici-types "~5.26.4" +"@types/node@^17.0.45": + version "17.0.45" + resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.45.tgz#2c0fafd78705e7a18b7906b5201a522719dc5190" + integrity sha512-w+tIMs3rq2afQdsPJlODhoUEKzFP1ayaoyl1CcnwtIlsVe7K7bA1NGm4s3PraqTLlXnbIN84zuBlxBWo1u9BLw== + "@types/tmi.js@^1.7.1": version "1.8.6" resolved "https://registry.yarnpkg.com/@types/tmi.js/-/tmi.js-1.8.6.tgz#f95a397d20398d0e1333a2eb8b7c3c494ce96b55" @@ -737,6 +747,11 @@ delayed-stream@~1.0.0: resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619" integrity sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ== +denque@^2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/denque/-/denque-2.1.0.tgz#e93e1a6569fb5e66f16a3c2a2964617d349d6ab1" + integrity sha512-HVQE3AAb/pxF8fQAoiqpvg9i3evqug3hoiwakOyZAwJm+6vZehbkYXZ0l4JxS+I3QxM97v5aaRNhj8v5oBhekw== + depd@2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/depd/-/depd-2.0.0.tgz#b696163cc757560d09cf22cc8fad1571b79e76df" @@ -1575,6 +1590,11 @@ lodash@^4.17.15: resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c" integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg== +lru-cache@^10.0.1: + version "10.2.0" + resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-10.2.0.tgz#0bd445ca57363465900f4d1f9bd8db343a4d95c3" + integrity sha512-2bIM8x+VAf6JT4bKAljS1qUWgMsqZRPGJS6FSahIMPVvctcNhyVp7AJu7quxOW9jwkryBReKZY5tY5JYv2n/7Q== + lru-cache@^6.0.0: version "6.0.0" resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-6.0.0.tgz#6d6fe6570ebd96aaf90fcad1dafa3b2566db3a94" @@ -1587,6 +1607,17 @@ lru-cache@^7.14.1: resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-7.18.3.tgz#f793896e0fd0e954a59dfdd82f0773808df6aa89" integrity sha512-jumlc0BIUrS3qJGgIkWZsyfAM7NCWiBcCDhnd+3NNM5KbBmLTgHVfWBcg6W+rLUsIpzpERPsvwUP7CckAQSOoA== +mariadb@^3.2.3: + version "3.2.3" + resolved "https://registry.yarnpkg.com/mariadb/-/mariadb-3.2.3.tgz#29ae69e678a25b355f4af0605b99b112c5ba7c5b" + integrity sha512-Hyc1ehdUJwzvvzcLU2juZS528wJ6oE8pUlpgY0BAOdpKWcdN1motuugi5lC3jkpCkFpyNknHG7Yg66KASl3aPg== + dependencies: + "@types/geojson" "^7946.0.10" + "@types/node" "^17.0.45" + denque "^2.1.0" + iconv-lite "^0.6.3" + lru-cache "^10.0.1" + mathjs@^12.3.1: version "12.3.1" resolved "https://registry.yarnpkg.com/mathjs/-/mathjs-12.3.1.tgz#afbdde32cd6026b8aa3bb303538be06d82b36e11"