Improve handling of manually added time

This commit is contained in:
Jonas Lochmann 2020-09-07 02:00:00 +02:00
parent 6518159069
commit e8cc9fd5e6
No known key found for this signature in database
GPG key ID: 8B8C9AEE10FA5B36
3 changed files with 47 additions and 4 deletions

View file

@ -17,6 +17,7 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { AddUsedTimeActionVersion2 } from '../../../../action' import { AddUsedTimeActionVersion2 } from '../../../../action'
import { EventHandler } from '../../../../monitoring/eventhandler'
import { MinuteOfDay } from '../../../../util/minuteofday' import { MinuteOfDay } from '../../../../util/minuteofday'
import { Cache } from '../cache' import { Cache } from '../cache'
import { getRoundedTimestamp as getRoundedTimestampForUsedTime } from './addusedtime' import { getRoundedTimestamp as getRoundedTimestampForUsedTime } from './addusedtime'
@ -27,14 +28,34 @@ export const getRoundedTimestampForSessionDuration = () => {
return now - (now % (1000 * 60 * 60 * 12 /* 12 hours */)) return now - (now % (1000 * 60 * 60 * 12 /* 12 hours */))
} }
export async function dispatchAddUsedTimeVersion2 ({ deviceId, action, cache }: { export async function dispatchAddUsedTimeVersion2 ({ deviceId, action, cache, eventHandler }: {
deviceId: string deviceId: string
action: AddUsedTimeActionVersion2 action: AddUsedTimeActionVersion2
cache: Cache cache: Cache
eventHandler: EventHandler
}) { }) {
const deviceEntryUnsafe = await cache.database.device.findOne({
where: {
familyId: cache.familyId,
deviceId: deviceId
},
attributes: ['currentUserId'],
transaction: cache.transaction
})
if (!deviceEntryUnsafe) {
throw new Error('source device not found')
}
const deviceEntry = {
currentUserId: deviceEntryUnsafe.currentUserId
}
const roundedTimestampForUsedTime = getRoundedTimestampForUsedTime().toString(10) const roundedTimestampForUsedTime = getRoundedTimestampForUsedTime().toString(10)
const roundedTimestampForSessionDuration = getRoundedTimestampForSessionDuration().toString(10) const roundedTimestampForSessionDuration = getRoundedTimestampForSessionDuration().toString(10)
let addUsedTimeForADifferentUserThanTheCurrentUserOfTheDevice = false
for (let i = 0; i < action.items.length; i++) { for (let i = 0; i < action.items.length; i++) {
const item = action.items[i] const item = action.items[i]
@ -62,6 +83,10 @@ export async function dispatchAddUsedTimeVersion2 ({ deviceId, action, cache }:
extraTimeInMillis: categoryEntryUnsafe.extraTimeInMillis extraTimeInMillis: categoryEntryUnsafe.extraTimeInMillis
} }
if (categoryEntry.childId !== deviceEntry.currentUserId) {
addUsedTimeForADifferentUserThanTheCurrentUserOfTheDevice = true
}
// tslint:disable-next-line:no-inner-declarations // tslint:disable-next-line:no-inner-declarations
async function handle (start: number, end: number) { async function handle (start: number, end: number) {
const lengthInMinutes = (end - start) + 1 const lengthInMinutes = (end - start) + 1
@ -174,5 +199,20 @@ export async function dispatchAddUsedTimeVersion2 ({ deviceId, action, cache }:
cache.categoriesWithModifiedBaseData.push(item.categoryId) cache.categoriesWithModifiedBaseData.push(item.categoryId)
} }
if (addUsedTimeForADifferentUserThanTheCurrentUserOfTheDevice) {
// there are two possible causes for this:
// - a device user was changed remotely while it was used by this user for
// limited Apps (rarely)
// - a parent added time manually (rarely)
//
// For the second case, it's important to sync this change.
// As it should occur not too often, a full sync should be no problem.
// To keep an eye on it, it is counted.
cache.areChangesImportant = true
eventHandler.countEvent('add used time for a different user than the current user of the device')
}
} }
} }

View file

@ -26,6 +26,7 @@ import {
UpdateAppActivitiesAction, UpdateAppActivitiesAction,
UpdateDeviceStatusAction UpdateDeviceStatusAction
} from '../../../../action' } from '../../../../action'
import { EventHandler } from '../../../../monitoring/eventhandler'
import { Cache } from '../cache' import { Cache } from '../cache'
import { dispatchAddInstalledApps } from './addinstalledapps' import { dispatchAddInstalledApps } from './addinstalledapps'
import { dispatchAddUsedTime } from './addusedtime' import { dispatchAddUsedTime } from './addusedtime'
@ -36,17 +37,18 @@ import { dispatchTriedDisablingDeviceAdmin } from './trieddisablingdeviceadmin'
import { dispatchUpdateAppActivities } from './updateappactivities' import { dispatchUpdateAppActivities } from './updateappactivities'
import { dispatchUpdateDeviceStatus } from './updatedevicestatus' import { dispatchUpdateDeviceStatus } from './updatedevicestatus'
export const dispatchAppLogicAction = async ({ action, deviceId, cache }: { export const dispatchAppLogicAction = async ({ action, deviceId, cache, eventHandler }: {
action: AppLogicAction action: AppLogicAction
deviceId: string deviceId: string
cache: Cache cache: Cache
eventHandler: EventHandler
}) => { }) => {
if (action instanceof AddInstalledAppsAction) { if (action instanceof AddInstalledAppsAction) {
await dispatchAddInstalledApps({ deviceId, action, cache }) await dispatchAddInstalledApps({ deviceId, action, cache })
} else if (action instanceof AddUsedTimeAction) { } else if (action instanceof AddUsedTimeAction) {
await dispatchAddUsedTime({ deviceId, action, cache }) await dispatchAddUsedTime({ deviceId, action, cache })
} else if (action instanceof AddUsedTimeActionVersion2) { } else if (action instanceof AddUsedTimeActionVersion2) {
await dispatchAddUsedTimeVersion2({ deviceId, action, cache }) await dispatchAddUsedTimeVersion2({ deviceId, action, cache, eventHandler })
} else if (action instanceof RemoveInstalledAppsAction) { } else if (action instanceof RemoveInstalledAppsAction) {
await dispatchRemoveInstalledApps({ deviceId, action, cache }) await dispatchRemoveInstalledApps({ deviceId, action, cache })
} else if (action instanceof SignOutAtDeviceAction) { } else if (action instanceof SignOutAtDeviceAction) {

View file

@ -183,7 +183,8 @@ export const applyActionsFromDevice = async ({ database, request, websocket, con
await dispatchAppLogicAction({ await dispatchAppLogicAction({
action: parsedAction, action: parsedAction,
cache, cache,
deviceId: deviceEntry.deviceId deviceId: deviceEntry.deviceId,
eventHandler
}) })
} catch (ex) { } catch (ex) {
eventHandler.countEvent('applyActionsFromDevice actionWithError:' + parsedSerializedAction.type) eventHandler.countEvent('applyActionsFromDevice actionWithError:' + parsedSerializedAction.type)