Compare commits

...

24 commits

Author SHA1 Message Date
Jonas Lochmann
6189601459
Update dependencies 2025-06-14 19:08:56 +02:00
Jonas Lochmann
569e5ce62d
Update dependencies 2024-12-02 01:00:00 +01:00
Jonas Lochmann
6220cc6bb9
Update dependencies 2024-11-18 01:00:00 +01:00
Jonas Lochmann
33d9fd732f
Update dependencies 2024-10-07 02:00:00 +02:00
Jonas Lochmann
764f240707
Update dependencies 2024-09-09 02:00:00 +02:00
Jonas Lochmann
b392ca295a
Update dependencies 2024-09-09 02:00:00 +02:00
Jonas Lochmann
f5fc8e6cd6
Update dependencies 2024-08-19 02:00:00 +02:00
Jonas Lochmann
9c2048af64
Fix lint warnings 2024-07-29 02:00:00 +02:00
Jonas Lochmann
b69271f7df
Update umzug 2024-07-29 02:00:00 +02:00
Jonas Lochmann
97d2730b20
Update dependencies 2024-06-17 02:00:00 +02:00
Jonas Lochmann
0346197c23
Update dependencies 2024-06-10 02:00:00 +02:00
Jonas Lochmann
a7ed01af74
Update dependencies 2024-06-03 02:00:00 +02:00
Jonas Lochmann
f77d91ff56
Update dependencies 2024-04-29 02:00:00 +02:00
Jonas Lochmann
2d035da0da
Update dependencies 2024-04-08 02:00:00 +02:00
Jonas Lochmann
2d73cba90e
Update dependencies 2024-04-08 02:00:00 +02:00
Jonas Lochmann
1918c74277
Update dependencies 2024-03-25 01:00:00 +01:00
Jonas Lochmann
e55d1fd1a9
Update dependencies 2024-03-18 01:00:00 +01:00
Jonas Lochmann
f10b79a023
Add skipLibCheck to tsconfig.json 2024-03-18 01:00:00 +01:00
Jonas Lochmann
2c401288a3
Adjust json schema generation parameters 2024-03-18 01:00:00 +01:00
Jonas Lochmann
89f3325a18
Update dependencies 2024-03-04 01:00:00 +01:00
Jonas Lochmann
7aaad00881
Update dependencies 2024-02-05 01:00:00 +01:00
Jonas Lochmann
c7e4cfc9f9
Add workaround for session duration change logic bug 2024-01-01 01:00:00 +01:00
Jonas Lochmann
12ed5d73cd
Update dependencies 2023-09-18 02:00:00 +02:00
Jonas Lochmann
4df809a306
Update dependencies 2023-07-10 02:00:00 +02:00
7 changed files with 1879 additions and 1362 deletions

3124
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -38,12 +38,11 @@
"@types/lodash": "^4.14.166", "@types/lodash": "^4.14.166",
"@types/node": "^16.11.59", "@types/node": "^16.11.59",
"@types/nodemailer": "^6.4.4", "@types/nodemailer": "^6.4.4",
"@types/umzug": "^2.3.0", "@typescript-eslint/eslint-plugin": "^7.18.0",
"@typescript-eslint/eslint-plugin": "^5.10.0", "@typescript-eslint/parser": "^7.18.0",
"@typescript-eslint/parser": "^5.10.0",
"eslint": "^8.7.0", "eslint": "^8.7.0",
"rimraf": "^3.0.2", "rimraf": "^3.0.2",
"typescript": "^4.4.4", "typescript": "^5.5.4",
"typescript-json-schema": "^0.52.0" "typescript-json-schema": "^0.52.0"
}, },
"dependencies": { "dependencies": {
@ -63,8 +62,7 @@
"rate-limiter-flexible": "^2.1.15", "rate-limiter-flexible": "^2.1.15",
"sequelize": "^6.25.5", "sequelize": "^6.25.5",
"socket.io": "^4.0.1", "socket.io": "^4.0.1",
"sqlite3": "^4.0.0", "umzug": "^3.8.1"
"umzug": "^2.3.0"
}, },
"optionalDependencies": { "optionalDependencies": {
"sqlite3": "^5.0.0" "sqlite3": "^5.0.0"

View file

@ -1,6 +1,6 @@
/* /*
* server component for the TimeLimit App * server component for the TimeLimit App
* Copyright (C) 2019 - 2023 Jonas Lochmann * Copyright (C) 2019 - 2024 Jonas Lochmann
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as * it under the terms of the GNU Affero General Public License as
@ -58,7 +58,9 @@ const allTypes = [
const settings = { const settings = {
required: true, required: true,
noExtraProps: true noExtraProps: true,
// otherwise it finds errors in dependencies that we don't care about
ignoreErrors: true
}; };
const compilerOptions = { const compilerOptions = {

View file

@ -1,6 +1,6 @@
/* /*
* server component for the TimeLimit App * server component for the TimeLimit App
* Copyright (C) 2019 - 2022 Jonas Lochmann * Copyright (C) 2019 - 2024 Jonas Lochmann
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as * it under the terms of the GNU Affero General Public License as
@ -26,7 +26,7 @@ export const config = {
expireTimeRounding: 1000 * 60 * 15 expireTimeRounding: 1000 * 60 * 15
} }
export function calculateExpireTime(now: bigint): BigInt { export function calculateExpireTime(now: bigint): bigint {
const expireBaseTime = now + BigInt(config.expireDelay) const expireBaseTime = now + BigInt(config.expireDelay)
const expireTime = expireBaseTime - expireBaseTime % BigInt(config.expireTimeRounding) + BigInt(config.expireTimeRounding) const expireTime = expireBaseTime - expireBaseTime % BigInt(config.expireTimeRounding) + BigInt(config.expireTimeRounding)

View file

@ -1,6 +1,6 @@
/* /*
* server component for the TimeLimit App * server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann * Copyright (C) 2019 - 2024 Jonas Lochmann
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as * it under the terms of the GNU Affero General Public License as
@ -17,18 +17,26 @@
import { resolve } from 'path' import { resolve } from 'path'
import { Sequelize } from 'sequelize' import { Sequelize } from 'sequelize'
import * as Umzug from 'umzug' import { Umzug, SequelizeStorage } from 'umzug'
export const createUmzug = (sequelize: Sequelize) => ( export const createUmzug = (sequelize: Sequelize) => (
new Umzug({ new Umzug({
storage: 'sequelize', storage: new SequelizeStorage({ sequelize }),
storageOptions: {
sequelize
},
migrations: { migrations: {
params: [sequelize.getQueryInterface(), sequelize], glob: resolve(__dirname, '../../../build/database/migration/migrations/*.js'),
path: resolve(__dirname, '../../../build/database/migration/migrations'), resolve: ({ name, path }) => {
pattern: /^\d+[\w-]+\.js$/ if (!path) throw new Error()
}
// eslint-disable-next-line @typescript-eslint/no-var-requires
const migration = require(path)
return {
name,
up: async () => migration.up(sequelize.getQueryInterface(), sequelize),
down: async () => migration.down(sequelize.getQueryInterface(), sequelize),
}
},
},
logger: console
}) })
) )

View file

@ -1,6 +1,6 @@
/* /*
* server component for the TimeLimit App * server component for the TimeLimit App
* Copyright (C) 2019 - 2022 Jonas Lochmann * Copyright (C) 2019 - 2023 Jonas Lochmann
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as * it under the terms of the GNU Affero General Public License as
@ -15,6 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import * as Sequelize from 'sequelize'
import { AddUsedTimeActionVersion2 } from '../../../../action' import { AddUsedTimeActionVersion2 } from '../../../../action'
import { EventHandler } from '../../../../monitoring/eventhandler' import { EventHandler } from '../../../../monitoring/eventhandler'
import { MinuteOfDay } from '../../../../util/minuteofday' import { MinuteOfDay } from '../../../../util/minuteofday'
@ -22,6 +23,8 @@ import { Cache } from '../cache'
import { IllegalStateException, SourceDeviceNotFoundException } from '../exception/illegal-state' import { IllegalStateException, SourceDeviceNotFoundException } from '../exception/illegal-state'
import { getRoundedTimestamp as getRoundedTimestampForUsedTime } from './addusedtime' import { getRoundedTimestamp as getRoundedTimestampForUsedTime } from './addusedtime'
const tolerance = 5 * 1000 // 5 seconds
export const getRoundedTimestampForSessionDuration = () => { export const getRoundedTimestampForSessionDuration = () => {
const now = Date.now() const now = Date.now()
@ -129,11 +132,32 @@ export async function dispatchAddUsedTimeVersion2 ({ deviceId, action, cache, ev
} }
} }
} else { } else {
const oldTime: number | null = await cache.database.usedTime.aggregate(
'usedTime',
'MAX',
{
where: {
familyId: cache.familyId,
categoryId: item.categoryId,
dayOfEpoch: action.dayOfEpoch,
startMinuteOfDay: {
[Sequelize.Op.gte]: start
},
endMinuteOfDay: {
[Sequelize.Op.lte]: end
}
},
transaction: cache.transaction
}
) || 0
if (oldTime !== null && typeof oldTime !== 'number') throw new Error()
await cache.database.usedTime.create({ await cache.database.usedTime.create({
familyId: cache.familyId, familyId: cache.familyId,
categoryId: item.categoryId, categoryId: item.categoryId,
dayOfEpoch: action.dayOfEpoch, dayOfEpoch: action.dayOfEpoch,
usedTime: Math.max(0, Math.min(item.timeToAdd, lengthInMs)), usedTime: Math.max(0, Math.min(oldTime + item.timeToAdd, lengthInMs)),
lastUpdate: roundedTimestampForUsedTime, lastUpdate: roundedTimestampForUsedTime,
startMinuteOfDay: start, startMinuteOfDay: start,
endMinuteOfDay: end endMinuteOfDay: end
@ -168,6 +192,39 @@ export async function dispatchAddUsedTimeVersion2 ({ deviceId, action, cache, ev
transaction: cache.transaction transaction: cache.transaction
}) })
const oldDuration: () => Promise<number> = async () => {
const fittingDurationItems = await cache.database.sessionDuration.findAll({
where: {
familyId: cache.familyId,
categoryId: item.categoryId,
startMinuteOfDay: {
[Sequelize.Op.gte]: limit.start
},
endMinuteOfDay: {
[Sequelize.Op.lte]: limit.end
},
maxSessionDuration: {
[Sequelize.Op.gte]: limit.duration
},
sessionPauseDuration: {
[Sequelize.Op.lte]: limit.pause
}
},
transaction: cache.transaction
})
const fittingDurationItemsLastUsageFiltered =
hasTrustedTimestamp ?
fittingDurationItems.filter((it) => {
action.trustedTimestamp - item.timeToAdd <=
parseInt(it.lastUsage, 10) + it.sessionPauseDuration - tolerance
}) : fittingDurationItems
return fittingDurationItemsLastUsageFiltered
.map((it) => it.lastSessionDuration)
.reduce((a, b) => Math.max(a, b), 0)
}
if (oldItem) { if (oldItem) {
let extendSession: boolean let extendSession: boolean
@ -188,14 +245,13 @@ export async function dispatchAddUsedTimeVersion2 ({ deviceId, action, cache, ev
* Due to this, a session is reset if it would be over in a few seconds, too. * Due to this, a session is reset if it would be over in a few seconds, too.
*/ */
const tolerance = 5 * 1000 // 5 seconds
const timeWhenStartingCurrentUsage = action.trustedTimestamp - item.timeToAdd const timeWhenStartingCurrentUsage = action.trustedTimestamp - item.timeToAdd
const nextSessionStart = parseInt(oldItem.lastUsage, 10) + oldItem.sessionPauseDuration - tolerance const nextSessionStart = parseInt(oldItem.lastUsage, 10) + oldItem.sessionPauseDuration - tolerance
extendSession = timeWhenStartingCurrentUsage <= nextSessionStart extendSession = timeWhenStartingCurrentUsage <= nextSessionStart
} }
oldItem.lastSessionDuration = extendSession ? oldItem.lastSessionDuration + item.timeToAdd : item.timeToAdd oldItem.lastSessionDuration = extendSession ? oldItem.lastSessionDuration + item.timeToAdd : await oldDuration() + item.timeToAdd
oldItem.roundedLastUpdate = roundedTimestampForSessionDuration oldItem.roundedLastUpdate = roundedTimestampForSessionDuration
if (hasTrustedTimestamp) { if (hasTrustedTimestamp) {
@ -215,7 +271,7 @@ export async function dispatchAddUsedTimeVersion2 ({ deviceId, action, cache, ev
endMinuteOfDay: limit.end, endMinuteOfDay: limit.end,
// end of primary key // end of primary key
lastUsage: action.trustedTimestamp.toString(10), lastUsage: action.trustedTimestamp.toString(10),
lastSessionDuration: item.timeToAdd, lastSessionDuration: await oldDuration() + item.timeToAdd,
roundedLastUpdate: roundedTimestampForSessionDuration roundedLastUpdate: roundedTimestampForSessionDuration
}, { transaction: cache.transaction }) }, { transaction: cache.transaction })
} }

View file

@ -11,7 +11,8 @@
"noUnusedParameters": false, "noUnusedParameters": false,
"noImplicitReturns": true, "noImplicitReturns": true,
"noFallthroughCasesInSwitch": true, "noFallthroughCasesInSwitch": true,
"sourceMap": true "sourceMap": true,
"skipLibCheck": true
}, },
"include": [ "include": [
"./src/**/*" "./src/**/*"