From 4a9dca9ffccb087135c6e2d0eebae45cd567036d Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Thu, 3 Nov 2022 17:45:27 +0200 Subject: Corrected typo, added 1h30min waiting time before the foods are updated You never know if the city's servers don't get updated instantly, so it might not be a good idea to ask for the foods at (at worst) 1 ms after 00:00 at monday. --- Functions/dateFuncs.js | 7 ++++--- server.js | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Functions/dateFuncs.js b/Functions/dateFuncs.js index 55a9372..340b46e 100644 --- a/Functions/dateFuncs.js +++ b/Functions/dateFuncs.js @@ -46,9 +46,10 @@ function run_at_monday_mornings(func) { days_to_elapse = 8 - weekday; ms_to_elapse = ms_in_h * ( days_to_elapse * 24/*hours in a day*/ - - hour // removes unneccessary hours so that we update it at 0 am - ) + 1 /*+1 so that we know that it won't be exactly midnight - and possibly sunday. idk if this actually is needed.*/; + - hour + 1.5 // removes unneccessary hours so that we update it at 1:30 am + // 1:30 just in case the foods aren't updated instantly to the city's + // servers. + ); setTimeout(() => run_at_monday_mornings(func), ms_to_elapse); diff --git a/server.js b/server.js index c496b14..905fe63 100644 --- a/server.js +++ b/server.js @@ -50,7 +50,7 @@ async function init() // ...shifts and classes await updateDB.update(SQLDB, "../Updation/shifts.txt", "../Updation/vanhalops.csv", "../Updation/uusilops.csv"); // ...foods - run_at_monday_mornings(() => food.build(SQLDB)); + dateFuncs.run_at_monday_mornings(() => food.build(SQLDB)); if ((new Date()).getDay() !== 1) // update if it's not monday. if it's monday, it has already been run by the scheduler above. await food.build(SQLDB); // server code -- cgit v1.2.3 From 0f2d56eafd01b5e728f96cf327f1a3e881a21225 Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Sat, 5 Nov 2022 13:07:11 +0200 Subject: Hid vegetarian food when it is the same as the normal food. Added date logging to init.sh. --- food.js | 4 ++-- init.sh | 6 ++++-- server.js | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/food.js b/food.js index 7a9691d..8de8dcd 100644 --- a/food.js +++ b/food.js @@ -3,8 +3,8 @@ const open = require("./Functions/open.js"); const { weekdayToNumber } = require("./Functions/dateFuncs.js"); function* scrapeFood(data) -{ - const foodRegex = /(\w{2} (?:\d\d?\.){2}\d{4})<\/title><description><!\[CDATA\[(Lounas) ?:? ?(.*?)(Kasvislounas) ?:? ?(.*?)]]><\/description>/gm; +{ + const foodRegex = /<title>(\w{2} (?:\d\d?\.){2}\d{4})<\/title><description><!\[CDATA\[(Lounas) ?:? ?(.*?)<br>(Kasvislounas) ?:? ?(.*?)]]><\/description>/ const foods = data.matchAll(foodRegex); for(const food of foods) { diff --git a/init.sh b/init.sh index 5fb171f..fb5d611 100755 --- a/init.sh +++ b/init.sh @@ -2,7 +2,7 @@ exec 1>>/var/slogs exec 2>>/var/slogs echo "" -echo "# Init running" +echo "# Init running at $(date)" echo "# Waiting for connection..." while ! ping 'example.org' -c 1; do @@ -26,6 +26,8 @@ while echo "# node server.js:"; do # Sleep below, so that the loop can't cause too big a load to the server, if the server terminates very fast. sleep 5 - echo "SERVER TERMINATED!" + echo "# Server terminated at $(date)" echo "--------" + echo "" + echo "" done diff --git a/server.js b/server.js index 905fe63..c2b0449 100644 --- a/server.js +++ b/server.js @@ -283,7 +283,7 @@ async function buildMain(args) res["food-header"] = `Kouluruoka ${weekdays[day]}`; res["food"] = "Päivän ruoka puuttuu tietokannasta."; } - if (vege[0] !== undefined) { + if ((vege[0] !== undefined) && (vege[0].food !== res["food"])) { res["vege-header"] = vege[0].header; res["vege"] = vege[0].food; } else { -- cgit v1.2.3 From 5665f993de41b82ab24c4a546d442b18f0bda4d3 Mon Sep 17 00:00:00 2001 From: Joel Kronqvist <work.joelkronqvist@gmail.com> Date: Mon, 14 Nov 2022 16:29:04 +0200 Subject: Introduced (ugly) fix for food overflows. For real, the server was down for A WHOLE DAY, just because a food next week was 1 CHARACTER MORE than 256 characters! --- food.js | 4 ++-- setup.sh | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/food.js b/food.js index 8de8dcd..9a73404 100644 --- a/food.js +++ b/food.js @@ -31,14 +31,14 @@ async function buildFoods(DB) { for(const food of foods[week - 1]) { - foodInitOperations.push(DB.execute("INSERT INTO foods VALUES (?, ?, FALSE, ?, ?, ?)", [ + foodInitOperations.push(DB.execute("INSERT IGNORE INTO foods VALUES (?, ?, FALSE, ?, ?, ?)", [ week, food[0], food[2][0], food[1], food[2][1] ])); - foodInitOperations.push(DB.execute("INSERT INTO foods VALUES (?, ?, TRUE, ?, ?, ?)", [ + foodInitOperations.push(DB.execute("INSERT IGNORE INTO foods VALUES (?, ?, TRUE, ?, ?, ?)", [ week, food[0], food[3][0], diff --git a/setup.sh b/setup.sh index 729a5ce..b4111fe 100755 --- a/setup.sh +++ b/setup.sh @@ -89,7 +89,7 @@ if [ "$certans" != "n" ]; then fi echo 'Setting up MySQL database...' -echo -e "CREATE DATABASE lyllruoka;\nUSE lyllruoka;\n\nCREATE TABLE shiftnames (\n\tday INT,\n\tid INT,\n\tname VARCHAR(128) NOT NULL,\n\tPRIMARY KEY (day, id)\n);\nCREATE TABLE classes (\n\tcourse VARCHAR(6) PRIMARY KEY,\n\tclass VARCHAR(4)\n);\nCREATE TABLE shifts (\n\tday INT,\n\tshift INT,\n\tcourse VARCHAR(6),\n\tteacher VARCHAR(4),\n\tclass VARCHAR(4),\n\tPRIMARY KEY (day, course)\n);\nCREATE TABLE devs (\n\tid INT PRIMARY KEY AUTO_INCREMENT,\n\tname VARCHAR(30) NOT NULL,\n\tdescription VARCHAR(128),\n\tcontact VARCHAR(40) DEFAULT ''\n);\nCREATE TABLE stats (\n start DATE PRIMARY KEY,\n uptime INT,\n requests INT,\n requests_per_day INT\n);\nCREATE TABLE exams (\n\tstart DATE,\n\tend DATE,\n\tmessage VARCHAR(256),\n\tPRIMARY KEY (start, end)\n);\nCREATE TABLE foods ( \n week INT,\n day INT,\n vegetarian TINYINT,\n header VARCHAR(15),\n dateString VARCHAR(13),\n food VARCHAR(256)\n);\nCREATE USER '$name'@'localhost' IDENTIFIED BY '$passw';\nGRANT ALL ON lyllruoka.* TO '$name'@'localhost';\n" | sudo mysql > /dev/null +echo -e "CREATE DATABASE lyllruoka;\nUSE lyllruoka;\n\nCREATE TABLE shiftnames (\n\tday INT,\n\tid INT,\n\tname VARCHAR(128) NOT NULL,\n\tPRIMARY KEY (day, id)\n);\nCREATE TABLE classes (\n\tcourse VARCHAR(6) PRIMARY KEY,\n\tclass VARCHAR(4)\n);\nCREATE TABLE shifts (\n\tday INT,\n\tshift INT,\n\tcourse VARCHAR(6),\n\tteacher VARCHAR(4),\n\tclass VARCHAR(4),\n\tPRIMARY KEY (day, course)\n);\nCREATE TABLE devs (\n\tid INT PRIMARY KEY AUTO_INCREMENT,\n\tname VARCHAR(30) NOT NULL,\n\tdescription VARCHAR(128),\n\tcontact VARCHAR(40) DEFAULT ''\n);\nCREATE TABLE stats (\n start DATE PRIMARY KEY,\n uptime INT,\n requests INT,\n requests_per_day INT\n);\nCREATE TABLE exams (\n\tstart DATE,\n\tend DATE,\n\tmessage VARCHAR(256),\n\tPRIMARY KEY (start, end)\n);\nCREATE TABLE foods ( \n week INT,\n day INT,\n vegetarian TINYINT,\n header VARCHAR(15),\n dateString VARCHAR(13),\n food VARCHAR(280)\n);\nCREATE USER '$name'@'localhost' IDENTIFIED BY '$passw';\nGRANT ALL ON lyllruoka.* TO '$name'@'localhost';\n" | sudo mysql > /dev/null echo -e "{\n\t\"host\": \"localhost\",\n\t\"user\": \"$name\",\n\t\"password\": \"$passw\",\n\t\"database\": \"lyllruoka\"\n}" > dblogin.txt echo -e 'Done!\n' -- cgit v1.2.3 From d10272298acb4d6e07fb5b73dd7af06d653afd7b Mon Sep 17 00:00:00 2001 From: Joel Kronqvist <work.joelkronqvist@gmail.com> Date: Fri, 18 Nov 2022 18:27:40 +0200 Subject: Updated exam/vacation/etc notifications. Now they don't block the search field, which is more user friendly. They also appear only if the date searched for matches the notification, not if the current date matches the notification. --- Functions/dateFuncs.js | 8 ++++---- README.md | 6 +++--- server.js | 17 +++++++++++------ 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/Functions/dateFuncs.js b/Functions/dateFuncs.js index 340b46e..caec1c9 100644 --- a/Functions/dateFuncs.js +++ b/Functions/dateFuncs.js @@ -5,14 +5,14 @@ function stringToDate(s) { } const isBetweenDates = (date, date1, date2) => { - date = approxDate(date); - date1 = approxDate(date1); - date2 = approxDate(date2); + date = floorDate(date); + date1 = floorDate(date1); + date2 = floorDate(date2); return ((date.getTime() >= date1.getTime()) && (date.getTime() <= date2.getTime())); }; -function approxDate(d) +function floorDate(d) { return new Date(`${d.getFullYear()}-${(d.getMonth() + 1).toString().padStart(2, "0")}-${d.getDate().toString().padStart(2, "0")}`); } diff --git a/README.md b/README.md index d2f8d41..1dad685 100644 --- a/README.md +++ b/README.md @@ -134,14 +134,14 @@ If you need to troubleshoot the starting, you can find some logs in /tmp/slogs. # Adding cool data that isn't required for the server to run -## Notifying of unusual food shifts (eg. during exams) +## Notifying of unusual food shifts (eg. during exams or vacations) Currently the notifications have to be added manually to the MySQL database. Here's an example: ``` USE lyllruoka; -INSERT INTO exams VALUES ('2021-11-22', '2021-11-30', '<h2>Koeviikko</h2><br>22.11. - 30.11..<br>Kouluruokaa on tarjolla 10:45-11:30.'); +INSERT INTO exams VALUES ('2021-11-22', '2021-11-30', '<h2>Koeviikko</h2>22.11. - 30.11..<br>Kouluruokaa on tarjolla 10:45-11:30.'); ``` -The first value in the parenthesis is the start date of the notification, the second the end date of the notification and the third value is the message to display. HTML is supported. The message will override the food shift search. +The first value in the parenthesis is the start date of the notification, the second the end date of the notification and the third value is the message to display. HTML is supported. The message will override the food shift result. ## Updating the developer table diff --git a/server.js b/server.js index c2b0449..ccab1b7 100644 --- a/server.js +++ b/server.js @@ -182,7 +182,7 @@ async function buildMain(args) let day = d.getDay(); day = (day + +(day === 0) * 7) - 1; // converts from 0 = sunday to 0 = monday const actualDay = day; - day = +(!(day === 5) && !(day === 6)) * day; + day = +(!(day === 5) && !(day === 6)) * day; // resets day to monday if saturday or sunday if ((typeof query.day === "string") && (parseInt(query.day).toString() === query.day) && (!isNaN(parseInt(query.day))) && (parseInt(query.day) >= 0) && (parseInt(query.day) < 5)) day = parseInt(query.day); // set the day selected (must be done manually with this replacement system) @@ -239,16 +239,21 @@ async function buildMain(args) const examInfo = await SQLDB.query("SELECT * FROM exams"); for(let week = 0; week < examInfo.length; week++) { + // get the date of the requested day + const nextDate = new Date( + d.getFullYear(), + d.getMonth(), + d.getDate() + day - actualDay + (day < actualDay)*7 + ); + if (dateFuncs.between( - d, + nextDate, new Date(examInfo[week].start), new Date(examInfo[week].end) )) { - const message = "<div id=\"foodshift\">" + - `<div class="float-block">${examInfo[week].message}</div>` + - "</div"; - data_string = strFuncs.replaceElement(data_string, "div id=\"foodshift\"", message); + const message = `<div class="shift-result float-block">${examInfo[week].message}</div>`; + data_string = strFuncs.replaceElement(data_string, "div id=\"shift-result\" class=\"float-block\"", message); } } -- cgit v1.2.3