Skip to content

Commit

Permalink
Use mariadb for renfe database
Browse files Browse the repository at this point in the history
  • Loading branch information
lluisd committed Feb 16, 2024
1 parent cf32d41 commit 2690385
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/main_twitch-mz-bot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 7 additions & 1 deletion config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
60 changes: 60 additions & 0 deletions helpers/mariadbManager.js
Original file line number Diff line number Diff line change
@@ -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
}
File renamed without changes.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion services/renfe.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
31 changes: 31 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,23 @@
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"
integrity sha512-gKb0enTmRCzXSSUJDq6/sPcqrfCv2mkkG6Jt/clpn5eiCbKTY+SgZUxo+p8ZKMof5dCp9vHQUAB7wOUTod22wQ==
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"
Expand Down Expand Up @@ -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==

[email protected]:
version "2.0.0"
resolved "https://registry.yarnpkg.com/depd/-/depd-2.0.0.tgz#b696163cc757560d09cf22cc8fad1571b79e76df"
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down

0 comments on commit 2690385

Please sign in to comment.