Add support for more limits

This commit is contained in:
Jonas Lochmann
2020-05-18 02:00:00 +02:00
parent d021497e52
commit ef1b19a01c
79 changed files with 2785 additions and 136 deletions
@@ -1,6 +1,6 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
* Copyright (C) 2019 - 2020 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
@@ -17,6 +17,7 @@
import * as Sequelize from 'sequelize'
import { AddUsedTimeAction } from '../../../../action'
import { MinuteOfDay } from '../../../../util/minuteofday'
import { Cache } from '../cache'
export const getRoundedTimestamp = () => {
@@ -68,7 +69,9 @@ export async function dispatchAddUsedTime ({ deviceId, action, cache }: {
where: {
familyId: cache.familyId,
categoryId: categoryId,
dayOfEpoch: action.dayOfEpoch
dayOfEpoch: action.dayOfEpoch,
startMinuteOfDay: MinuteOfDay.MIN,
endMinuteOfDay: MinuteOfDay.MAX
},
transaction: cache.transaction
})
@@ -80,7 +83,9 @@ export async function dispatchAddUsedTime ({ deviceId, action, cache }: {
categoryId: categoryId,
dayOfEpoch: action.dayOfEpoch,
usedTime: action.timeToAdd,
lastUpdate: roundedTimestamp
lastUpdate: roundedTimestamp,
startMinuteOfDay: MinuteOfDay.MIN,
endMinuteOfDay: MinuteOfDay.MAX
}, {
transaction: cache.transaction
})
@@ -17,15 +17,23 @@
import * as Sequelize from 'sequelize'
import { AddUsedTimeActionVersion2 } from '../../../../action'
import { MinuteOfDay } from '../../../../util/minuteofday'
import { Cache } from '../cache'
import { getRoundedTimestamp } from './addusedtime'
import { getRoundedTimestamp as getRoundedTimestampForUsedTime } from './addusedtime'
export const getRoundedTimestampForSessionDuration = () => {
const now = Date.now()
return now - (now % (1000 * 60 * 60 * 12 /* 12 hours */))
}
export async function dispatchAddUsedTimeVersion2 ({ deviceId, action, cache }: {
deviceId: string
action: AddUsedTimeActionVersion2
cache: Cache
}) {
const roundedTimestamp = getRoundedTimestamp().toString(10)
const roundedTimestampForUsedTime = getRoundedTimestampForUsedTime().toString(10)
const roundedTimestampForSessionDuration = getRoundedTimestampForSessionDuration().toString(10)
for (let i = 0; i < action.items.length; i++) {
const item = action.items[i]
@@ -41,6 +49,7 @@ export async function dispatchAddUsedTimeVersion2 ({ deviceId, action, cache }:
'extraTimeInMillis'
]
})
// verify that the category exists
if (!categoryEntryUnsafe) {
cache.requireFullSync()
@@ -53,16 +62,19 @@ export async function dispatchAddUsedTimeVersion2 ({ deviceId, action, cache }:
extraTimeInMillis: categoryEntryUnsafe.extraTimeInMillis
}
if (item.timeToAdd !== 0) {
// tslint:disable-next-line:no-inner-declarations
async function handle (start: number, end: number) {
// try to update first
const [updatedRows] = await cache.database.usedTime.update({
usedTime: Sequelize.literal(`usedTime + ${item.timeToAdd}`) as any,
lastUpdate: roundedTimestamp
lastUpdate: roundedTimestampForUsedTime
}, {
where: {
familyId: cache.familyId,
categoryId: item.categoryId,
dayOfEpoch: action.dayOfEpoch
dayOfEpoch: action.dayOfEpoch,
startMinuteOfDay: start,
endMinuteOfDay: end
},
transaction: cache.transaction
})
@@ -74,15 +86,75 @@ export async function dispatchAddUsedTimeVersion2 ({ deviceId, action, cache }:
categoryId: item.categoryId,
dayOfEpoch: action.dayOfEpoch,
usedTime: item.timeToAdd,
lastUpdate: roundedTimestamp
lastUpdate: roundedTimestampForUsedTime,
startMinuteOfDay: start,
endMinuteOfDay: end
}, {
transaction: cache.transaction
})
}
cache.categoriesWithModifiedUsedTimes.push(item.categoryId)
}
await handle(MinuteOfDay.MIN, MinuteOfDay.MAX)
for (let j = 0; j < item.additionalCountingSlots.length; j++) {
const slot = item.additionalCountingSlots[j]
await handle(slot.start, slot.end)
}
const hasTrustedTimestamp = action.trustedTimestamp !== 0
for (let j = 0; j < item.sessionDurationLimits.length; j++) {
const limit = item.sessionDurationLimits[j]
const oldItem = await cache.database.sessionDuration.findOne({
where: {
familyId: cache.familyId,
categoryId: item.categoryId,
maxSessionDuration: limit.duration,
sessionPauseDuration: limit.pause,
startMinuteOfDay: limit.start,
endMinuteOfDay: limit.end
},
transaction: cache.transaction
})
if (oldItem) {
if (hasTrustedTimestamp) {
oldItem.lastUsage = action.trustedTimestamp.toString(10)
}
if (
hasTrustedTimestamp &&
action.trustedTimestamp > parseInt(oldItem.lastUsage, 10) + oldItem.sessionPauseDuration
) {
oldItem.lastSessionDuration = item.timeToAdd
} else {
oldItem.lastSessionDuration = oldItem.lastSessionDuration + item.timeToAdd
}
oldItem.roundedLastUpdate = roundedTimestampForSessionDuration
await oldItem.save({ transaction: cache.transaction })
} else {
await cache.database.sessionDuration.create({
familyId: cache.familyId,
categoryId: item.categoryId,
maxSessionDuration: limit.duration,
sessionPauseDuration: limit.pause,
startMinuteOfDay: limit.start,
endMinuteOfDay: limit.end,
// end of primary key
lastUsage: action.trustedTimestamp,
lastSessionDuration: item.timeToAdd,
roundedLastUpdate: roundedTimestampForSessionDuration
}, { transaction: cache.transaction })
}
}
cache.categoriesWithModifiedUsedTimes.push(item.categoryId)
if (item.extraTimeToSubtract !== 0) {
await cache.database.category.update({
extraTimeInMillis: Math.max(0, categoryEntry.extraTimeInMillis - item.extraTimeToSubtract)