Add counting some events

This commit is contained in:
Jonas Lochmann
2020-02-03 01:00:00 +01:00
parent 1f210b32c8
commit cdae013f5a
7 changed files with 192 additions and 31 deletions
+69 -19
View File
@@ -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
@@ -22,6 +22,7 @@ import { ClientPushChangesRequest } from '../../../api/schema'
import { isSerializedAppLogicAction, isSerializedChildAction, isSerializedParentAction } from '../../../api/validator'
import { VisibleConnectedDevicesManager } from '../../../connected-devices'
import { Database } from '../../../database'
import { EventHandler } from '../../../monitoring/eventhandler'
import { WebsocketApi } from '../../../websocket'
import { notifyClientsAboutChanges } from '../../websocket'
import { Cache } from './cache'
@@ -29,13 +30,18 @@ import { dispatchAppLogicAction } from './dispatch-app-logic-action'
import { dispatchChildAction } from './dispatch-child-action'
import { dispatchParentAction } from './dispatch-parent-action'
export const applyActionsFromDevice = async ({ database, request, websocket, connectedDevicesManager }: {
export const applyActionsFromDevice = async ({ database, request, websocket, connectedDevicesManager, eventHandler }: {
database: Database
websocket: WebsocketApi
request: ClientPushChangesRequest
connectedDevicesManager: VisibleConnectedDevicesManager
eventHandler: EventHandler
}) => {
eventHandler.countEvent('applyActionsFromDevice')
if (request.actions.length > 50) {
eventHandler.countEvent('applyActionsFromDevice tooMuchActionsPerRequest')
throw new BadRequest()
}
@@ -90,6 +96,8 @@ export const applyActionsFromDevice = async ({ database, request, websocket, con
if (action.sequenceNumber < nextSequenceNumber) {
// action was already received
eventHandler.countEvent('applyActionsFromDevice sequenceNumberRepeated')
cache.requireFullSync()
continue
}
@@ -128,6 +136,8 @@ export const applyActionsFromDevice = async ({ database, request, websocket, con
const expectedIntegrityValue = createHash('sha512').update(integrityData).digest('hex')
if (action.integrity !== expectedIntegrityValue) {
eventHandler.countEvent('applyActionsFromDevice parentActionInvalidIntegrityValue')
throw new Error('invalid integrity value')
}
}
@@ -144,6 +154,8 @@ export const applyActionsFromDevice = async ({ database, request, websocket, con
const expectedIntegrityValue = createHash('sha512').update(integrityData).digest('hex')
if (action.integrity !== expectedIntegrityValue) {
eventHandler.countEvent('applyActionsFromDevice childActionInvalidIntegrityValue')
throw new Error('invalid integrity value')
}
}
@@ -152,52 +164,86 @@ export const applyActionsFromDevice = async ({ database, request, websocket, con
if (action.type === 'appLogic') {
if (!isSerializedAppLogicAction(parsedSerializedAction)) {
eventHandler.countEvent('applyActionsFromDevice invalidAppLogicAction')
throw new Error('invalid action: ' + action.encodedAction)
}
eventHandler.countEvent('applyActionsFromDevice action:' + parsedSerializedAction.type)
const parsedAction = parseAppLogicAction(parsedSerializedAction)
await dispatchAppLogicAction({
action: parsedAction,
cache,
deviceId: deviceEntry.deviceId
})
try {
await dispatchAppLogicAction({
action: parsedAction,
cache,
deviceId: deviceEntry.deviceId
})
} catch (ex) {
eventHandler.countEvent('applyActionsFromDevice actionWithError:' + parsedSerializedAction.type)
throw ex
}
} else if (action.type === 'parent') {
if (!isSerializedParentAction(parsedSerializedAction)) {
eventHandler.countEvent('applyActionsFromDevice invalidParentAction')
throw new Error('invalid action' + action.encodedAction)
}
eventHandler.countEvent('applyActionsFromDevice action:' + parsedSerializedAction.type)
const parsedAction = parseParentAction(parsedSerializedAction)
await dispatchParentAction({
action: parsedAction,
cache,
parentUserId: action.userId,
sourceDeviceId: deviceEntry.deviceId
})
try {
await dispatchParentAction({
action: parsedAction,
cache,
parentUserId: action.userId,
sourceDeviceId: deviceEntry.deviceId
})
} catch (ex) {
eventHandler.countEvent('applyActionsFromDevice actionWithError:' + parsedSerializedAction.type)
throw ex
}
} else if (action.type === 'child') {
if (!isSerializedChildAction(parsedSerializedAction)) {
eventHandler.countEvent('applyActionsFromDevice invalidChildAction')
throw new Error('invalid action: ' + action.encodedAction)
}
eventHandler.countEvent('applyActionsFromDevice action:' + parsedSerializedAction.type)
const parsedAction = parseChildAction(parsedSerializedAction)
await dispatchChildAction({
action: parsedAction,
cache,
childUserId: action.userId,
deviceId: deviceEntry.deviceId
})
try {
await dispatchChildAction({
action: parsedAction,
cache,
childUserId: action.userId,
deviceId: deviceEntry.deviceId
})
} catch (ex) {
eventHandler.countEvent('applyActionsFromDevice actionWithError:' + parsedSerializedAction.type)
throw ex
}
} else {
throw new Error('illegal state')
}
} catch (ex) {
eventHandler.countEvent('applyActionsFromDevice errorDispatchingAction')
cache.requireFullSync()
}
}
// save new next sequence number
if (nextSequenceNumber !== deviceEntry.nextSequenceNumber) {
eventHandler.countEvent('applyActionsFromDevice updateSequenceNumber')
await database.device.update({
nextSequenceNumber
}, {
@@ -219,6 +265,10 @@ export const applyActionsFromDevice = async ({ database, request, websocket, con
}
})
if (areChangesImportant) {
eventHandler.countEvent('applyActionsFromDevice areChangesImportant')
}
await notifyClientsAboutChanges({
familyId,
sourceDeviceId,