last50Weeks gets cached

This commit is contained in:
2023-06-17 17:42:15 +02:00
parent 4818db3f9d
commit 08b283c6c8
3 changed files with 52 additions and 8 deletions

33
package-lock.json generated
View File

@@ -8,6 +8,7 @@
"ejs": "^3.1.9",
"express": "^4.18.2",
"moment": "^2.29.4",
"node-cron": "^3.0.2",
"webuntis": "^2.0.3"
}
},
@@ -675,6 +676,17 @@
"node": ">= 0.6"
}
},
"node_modules/node-cron": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/node-cron/-/node-cron-3.0.2.tgz",
"integrity": "sha512-iP8l0yGlNpE0e6q1o185yOApANRe47UPbLf4YxfbiNHt/RU5eBcGB/e0oudruheSf+LQeDMezqC5BVAb5wwRcQ==",
"dependencies": {
"uuid": "8.3.2"
},
"engines": {
"node": ">=6.0.0"
}
},
"node_modules/object-inspect": {
"version": "1.12.3",
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.12.3.tgz",
@@ -919,6 +931,14 @@
"node": ">= 0.4.0"
}
},
"node_modules/uuid": {
"version": "8.3.2",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
"integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==",
"bin": {
"uuid": "dist/bin/uuid"
}
},
"node_modules/vary": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
@@ -1440,6 +1460,14 @@
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.3.tgz",
"integrity": "sha512-+EUsqGPLsM+j/zdChZjsnX51g4XrHFOIXwfnCVPGlQk/k5giakcKsuxCObBRu6DSm9opw/O6slWbJdghQM4bBg=="
},
"node-cron": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/node-cron/-/node-cron-3.0.2.tgz",
"integrity": "sha512-iP8l0yGlNpE0e6q1o185yOApANRe47UPbLf4YxfbiNHt/RU5eBcGB/e0oudruheSf+LQeDMezqC5BVAb5wwRcQ==",
"requires": {
"uuid": "8.3.2"
}
},
"object-inspect": {
"version": "1.12.3",
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.12.3.tgz",
@@ -1618,6 +1646,11 @@
"resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz",
"integrity": "sha512-pMZTvIkT1d+TFGvDOqodOclx0QWkkgi6Tdoa8gC8ffGAAqz9pzPTZWAybbsHHoED/ztMtkv/VoYTYyShUn81hA=="
},
"uuid": {
"version": "8.3.2",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
"integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg=="
},
"vary": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",

View File

@@ -3,6 +3,7 @@
"ejs": "^3.1.9",
"express": "^4.18.2",
"moment": "^2.29.4",
"node-cron": "^3.0.2",
"webuntis": "^2.0.3"
}
}

View File

@@ -4,6 +4,7 @@ const app = express();
const { spawnSync } = require('child_process');
const moment = require('moment');
const { WebUntis } = require('webuntis');
var cron = require('node-cron');
// Befehl für die Skriptausführung
const command = process.platform === 'win32' ? 'python' : 'python3';
@@ -15,21 +16,26 @@ const static = path.join(__dirname, 'static');
// Verwende EJS als Vorlagen-Engine
app.set('view engine', 'ejs');
// Funktion zum Erstellen der Liste der letzten 50 Wochen
function getLast50Weeks() {
function getlast50weeks() {
let currentWeek = moment().startOf('isoWeek');
const last50Weeks = [];
let last50WeeksCache = [];
for (let i = 0; i < 50; i++) {
const startOfWeek = currentWeek.clone().format('DD.MM.YYYY');
const endOfWeek = currentWeek.clone().add(6, 'days').format('DD.MM.YYYY');
last50Weeks.push(startOfWeek + " - " + endOfWeek);
last50WeeksCache.push(startOfWeek + " - " + endOfWeek);
currentWeek.subtract(7, 'days');
}
return last50Weeks;
return last50WeeksCache;
}
let last50Weeks = getlast50weeks();
cron.schedule('0 6 * * *', () => {
last50Weeks = getlast50weeks();
});
function getCalendarEntries(weekDate) {
const [startDateStr, endDateStr] = weekDate.split(" - ");
@@ -131,12 +137,16 @@ app.use("/", express.static(static));
// Definiere eine Route für die Startseite
app.get('/', (req, res) => {
res.render(path.join(static, 'index'), { options:getLast50Weeks() });
res.render(path.join(static, 'index'), {options:last50Weeks });
});
app.get('/getlast50weeks', (req, res) => {
res.send(last50Weeks);
});
// Definiere eine Route für die Ausgabe der Daten
app.get('/getreport', async (req, res) => {
const weekDate = getLast50Weeks()[req.query.week];
const weekDate = last50Weeks[req.query.week];
res.send({
calendar: getCalendarEntries(weekDate),
untis: await getUntis(weekDate)