Add counting some events

This commit is contained in:
Jonas Lochmann 2020-02-03 01:00:00 +01:00
parent 1f210b32c8
commit cdae013f5a
No known key found for this signature in database
GPG key ID: 8B8C9AEE10FA5B36
7 changed files with 192 additions and 31 deletions

View file

@ -21,19 +21,36 @@ import { BadRequest, Conflict } from 'http-errors'
import { Database } from '../database'
import { addPurchase } from '../function/purchase'
import { getStatusMessage, setStatusMessage } from '../function/statusmessage'
import { EventHandler } from '../monitoring/eventhandler'
import { generatePurchaseId } from '../util/token'
import { WebsocketApi } from '../websocket'
export const createAdminRouter = ({ database, websocket }: {
export const createAdminRouter = ({ database, websocket, eventHandler }: {
database: Database
websocket: WebsocketApi
eventHandler: EventHandler
}) => {
const router = Router()
router.get('/status', (_, res) => {
router.get('/status', async (_, res, next) => {
try {
res.json({
websocketClients: websocket.countConnections()
websocketClients: websocket.countConnections(),
counters: await eventHandler.getCounters()
})
} catch (ex) {
next(ex)
}
})
router.post('/reset-counters', async (_, res, next) => {
try {
await eventHandler.resetCounters()
res.json({ ok: true })
} catch (ex) {
next(ex)
}
})
router.get('/status-message', async (_, res, next) => {

View file

@ -19,6 +19,7 @@ import * as basicAuth from 'basic-auth'
import * as express from 'express'
import { VisibleConnectedDevicesManager } from '../connected-devices'
import { Database } from '../database'
import { EventHandler } from '../monitoring/eventhandler'
import { WebsocketApi } from '../websocket'
import { createAdminRouter } from './admin'
import { createAuthRouter } from './auth'
@ -29,10 +30,11 @@ import { createSyncRouter } from './sync'
const adminToken = process.env.ADMIN_TOKEN || ''
export const createApi = ({ database, websocket, connectedDevicesManager }: {
export const createApi = ({ database, websocket, connectedDevicesManager, eventHandler }: {
database: Database
websocket: WebsocketApi
connectedDevicesManager: VisibleConnectedDevicesManager
eventHandler: EventHandler
}) => {
const app = express()
@ -48,7 +50,7 @@ export const createApi = ({ database, websocket, connectedDevicesManager }: {
app.use('/child', createChildRouter({ database, websocket }))
app.use('/parent', createParentRouter({ database, websocket }))
app.use('/purchase', createPurchaseRouter({ database, websocket }))
app.use('/sync', createSyncRouter({ database, websocket, connectedDevicesManager }))
app.use('/sync', createSyncRouter({ database, websocket, connectedDevicesManager, eventHandler }))
app.use(
'/admin',
@ -74,7 +76,7 @@ export const createApi = ({ database, websocket, connectedDevicesManager }: {
res.sendStatus(401)
}
},
createAdminRouter({ database, websocket })
createAdminRouter({ database, websocket, eventHandler })
)
return app

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
@ -23,6 +23,7 @@ import { Database } from '../database'
import { reportDeviceRemoved } from '../function/device/report-device-removed'
import { applyActionsFromDevice } from '../function/sync/apply-actions'
import { generateServerDataStatus } from '../function/sync/get-server-data-status'
import { EventHandler } from '../monitoring/eventhandler'
import { WebsocketApi } from '../websocket'
import { isClientPullChangesRequest, isClientPushChangesRequest, isRequestWithAuthToken } from './validator'
@ -32,10 +33,11 @@ const getRoundedTimestampForLastConnectivity = () => {
return now - (now % (1000 * 60 * 60 * 12 /* 12 hours */))
}
export const createSyncRouter = ({ database, websocket, connectedDevicesManager }: {
export const createSyncRouter = ({ database, websocket, connectedDevicesManager, eventHandler }: {
database: Database
websocket: WebsocketApi
connectedDevicesManager: VisibleConnectedDevicesManager
eventHandler: EventHandler
}) => {
const router = Router()
@ -43,7 +45,11 @@ export const createSyncRouter = ({ database, websocket, connectedDevicesManager
limit: '5120kb'
}), async (req, res, next) => {
try {
eventHandler.countEvent('pushChangesRequest')
if (!isClientPushChangesRequest(req.body)) {
eventHandler.countEvent('pushChangesRequest invalid')
throw new BadRequest()
}
@ -51,9 +57,14 @@ export const createSyncRouter = ({ database, websocket, connectedDevicesManager
request: req.body,
database,
websocket,
connectedDevicesManager
connectedDevicesManager,
eventHandler
})
if (shouldDoFullSync) {
eventHandler.countEvent('pushChangesRequest shouldDoFullSync')
}
res.json({
shouldDoFullSync
})
@ -64,9 +75,13 @@ export const createSyncRouter = ({ database, websocket, connectedDevicesManager
router.post('/pull-status', json(), async (req, res, next) => {
try {
eventHandler.countEvent('pullStatusRequest')
const { body } = req
if (!isClientPullChangesRequest(body)) {
eventHandler.countEvent('pullStatusRequest invalid')
throw new BadRequest()
}
@ -104,6 +119,14 @@ export const createSyncRouter = ({ database, websocket, connectedDevicesManager
transaction
})
if (serverStatus.devices) { eventHandler.countEvent('pullStatusRequest devices') }
if (serverStatus.apps) { eventHandler.countEvent('pullStatusRequest apps') }
if (serverStatus.categoryBase) { eventHandler.countEvent('pullStatusRequest categoryBase') }
if (serverStatus.categoryApp) { eventHandler.countEvent('pullStatusRequest categoryApp') }
if (serverStatus.usedTimes) { eventHandler.countEvent('pullStatusRequest usedTimes') }
if (serverStatus.rules) { eventHandler.countEvent('pullStatusRequest rules') }
if (serverStatus.users) { eventHandler.countEvent('pullStatusRequest users') }
res.json(serverStatus)
})
} catch (ex) {

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)
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)
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)
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,

View file

@ -19,12 +19,15 @@ import { Server } from 'http'
import { createApi } from './api'
import { VisibleConnectedDevicesManager } from './connected-devices'
import { defaultDatabase, defaultUmzug } from './database'
import { EventHandler } from './monitoring/eventhandler'
import { InMemoryEventHandler } from './monitoring/inmemoryeventhandler'
import { createWebsocketHandler } from './websocket'
import { initWorkers } from './worker'
async function main () {
await defaultUmzug.up()
const database = defaultDatabase
const eventHandler: EventHandler = new InMemoryEventHandler()
const connectedDevicesManager = new VisibleConnectedDevicesManager({
database
@ -43,7 +46,8 @@ async function main () {
const api = createApi({
database,
websocket: websocketApi,
connectedDevicesManager
connectedDevicesManager,
eventHandler
})
const server = new Server(api)

View file

@ -0,0 +1,22 @@
/*
* server component for the TimeLimit App
* 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
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
export interface EventHandler {
countEvent (name: string): void
getCounters (): Promise<{[key: string]: number}>
resetCounters (): Promise<void>
}

View file

@ -0,0 +1,43 @@
/*
* server component for the TimeLimit App
* 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
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { EventHandler } from './eventhandler'
export class InMemoryEventHandler implements EventHandler {
private counters = new Map<string, number>()
countEvent (name: string) {
this.counters.set(
name,
(this.counters.get(name) || 0) + 1
)
}
async getCounters () {
const result: {[key: string]: number} = {}
this.counters.forEach((value, key) => {
result[key] = value
})
return result
}
async resetCounters () {
this.counters.clear()
}
}