mirror of
https://codeberg.org/timelimit/timelimit-server.git
synced 2025-10-04 18:29:42 +02:00
Add counting some events
This commit is contained in:
parent
1f210b32c8
commit
cdae013f5a
7 changed files with 192 additions and 31 deletions
|
@ -21,19 +21,36 @@ import { BadRequest, Conflict } from 'http-errors'
|
||||||
import { Database } from '../database'
|
import { Database } from '../database'
|
||||||
import { addPurchase } from '../function/purchase'
|
import { addPurchase } from '../function/purchase'
|
||||||
import { getStatusMessage, setStatusMessage } from '../function/statusmessage'
|
import { getStatusMessage, setStatusMessage } from '../function/statusmessage'
|
||||||
|
import { EventHandler } from '../monitoring/eventhandler'
|
||||||
import { generatePurchaseId } from '../util/token'
|
import { generatePurchaseId } from '../util/token'
|
||||||
import { WebsocketApi } from '../websocket'
|
import { WebsocketApi } from '../websocket'
|
||||||
|
|
||||||
export const createAdminRouter = ({ database, websocket }: {
|
export const createAdminRouter = ({ database, websocket, eventHandler }: {
|
||||||
database: Database
|
database: Database
|
||||||
websocket: WebsocketApi
|
websocket: WebsocketApi
|
||||||
|
eventHandler: EventHandler
|
||||||
}) => {
|
}) => {
|
||||||
const router = Router()
|
const router = Router()
|
||||||
|
|
||||||
router.get('/status', (_, res) => {
|
router.get('/status', async (_, res, next) => {
|
||||||
res.json({
|
try {
|
||||||
websocketClients: websocket.countConnections()
|
res.json({
|
||||||
})
|
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) => {
|
router.get('/status-message', async (_, res, next) => {
|
||||||
|
|
|
@ -19,6 +19,7 @@ import * as basicAuth from 'basic-auth'
|
||||||
import * as express from 'express'
|
import * as express from 'express'
|
||||||
import { VisibleConnectedDevicesManager } from '../connected-devices'
|
import { VisibleConnectedDevicesManager } from '../connected-devices'
|
||||||
import { Database } from '../database'
|
import { Database } from '../database'
|
||||||
|
import { EventHandler } from '../monitoring/eventhandler'
|
||||||
import { WebsocketApi } from '../websocket'
|
import { WebsocketApi } from '../websocket'
|
||||||
import { createAdminRouter } from './admin'
|
import { createAdminRouter } from './admin'
|
||||||
import { createAuthRouter } from './auth'
|
import { createAuthRouter } from './auth'
|
||||||
|
@ -29,10 +30,11 @@ import { createSyncRouter } from './sync'
|
||||||
|
|
||||||
const adminToken = process.env.ADMIN_TOKEN || ''
|
const adminToken = process.env.ADMIN_TOKEN || ''
|
||||||
|
|
||||||
export const createApi = ({ database, websocket, connectedDevicesManager }: {
|
export const createApi = ({ database, websocket, connectedDevicesManager, eventHandler }: {
|
||||||
database: Database
|
database: Database
|
||||||
websocket: WebsocketApi
|
websocket: WebsocketApi
|
||||||
connectedDevicesManager: VisibleConnectedDevicesManager
|
connectedDevicesManager: VisibleConnectedDevicesManager
|
||||||
|
eventHandler: EventHandler
|
||||||
}) => {
|
}) => {
|
||||||
const app = express()
|
const app = express()
|
||||||
|
|
||||||
|
@ -48,7 +50,7 @@ export const createApi = ({ database, websocket, connectedDevicesManager }: {
|
||||||
app.use('/child', createChildRouter({ database, websocket }))
|
app.use('/child', createChildRouter({ database, websocket }))
|
||||||
app.use('/parent', createParentRouter({ database, websocket }))
|
app.use('/parent', createParentRouter({ database, websocket }))
|
||||||
app.use('/purchase', createPurchaseRouter({ 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(
|
app.use(
|
||||||
'/admin',
|
'/admin',
|
||||||
|
@ -74,7 +76,7 @@ export const createApi = ({ database, websocket, connectedDevicesManager }: {
|
||||||
res.sendStatus(401)
|
res.sendStatus(401)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
createAdminRouter({ database, websocket })
|
createAdminRouter({ database, websocket, eventHandler })
|
||||||
)
|
)
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* server component for the TimeLimit App
|
* 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
|
* 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
|
||||||
|
@ -23,6 +23,7 @@ import { Database } from '../database'
|
||||||
import { reportDeviceRemoved } from '../function/device/report-device-removed'
|
import { reportDeviceRemoved } from '../function/device/report-device-removed'
|
||||||
import { applyActionsFromDevice } from '../function/sync/apply-actions'
|
import { applyActionsFromDevice } from '../function/sync/apply-actions'
|
||||||
import { generateServerDataStatus } from '../function/sync/get-server-data-status'
|
import { generateServerDataStatus } from '../function/sync/get-server-data-status'
|
||||||
|
import { EventHandler } from '../monitoring/eventhandler'
|
||||||
import { WebsocketApi } from '../websocket'
|
import { WebsocketApi } from '../websocket'
|
||||||
import { isClientPullChangesRequest, isClientPushChangesRequest, isRequestWithAuthToken } from './validator'
|
import { isClientPullChangesRequest, isClientPushChangesRequest, isRequestWithAuthToken } from './validator'
|
||||||
|
|
||||||
|
@ -32,10 +33,11 @@ const getRoundedTimestampForLastConnectivity = () => {
|
||||||
return now - (now % (1000 * 60 * 60 * 12 /* 12 hours */))
|
return now - (now % (1000 * 60 * 60 * 12 /* 12 hours */))
|
||||||
}
|
}
|
||||||
|
|
||||||
export const createSyncRouter = ({ database, websocket, connectedDevicesManager }: {
|
export const createSyncRouter = ({ database, websocket, connectedDevicesManager, eventHandler }: {
|
||||||
database: Database
|
database: Database
|
||||||
websocket: WebsocketApi
|
websocket: WebsocketApi
|
||||||
connectedDevicesManager: VisibleConnectedDevicesManager
|
connectedDevicesManager: VisibleConnectedDevicesManager
|
||||||
|
eventHandler: EventHandler
|
||||||
}) => {
|
}) => {
|
||||||
const router = Router()
|
const router = Router()
|
||||||
|
|
||||||
|
@ -43,7 +45,11 @@ export const createSyncRouter = ({ database, websocket, connectedDevicesManager
|
||||||
limit: '5120kb'
|
limit: '5120kb'
|
||||||
}), async (req, res, next) => {
|
}), async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
|
eventHandler.countEvent('pushChangesRequest')
|
||||||
|
|
||||||
if (!isClientPushChangesRequest(req.body)) {
|
if (!isClientPushChangesRequest(req.body)) {
|
||||||
|
eventHandler.countEvent('pushChangesRequest invalid')
|
||||||
|
|
||||||
throw new BadRequest()
|
throw new BadRequest()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,9 +57,14 @@ export const createSyncRouter = ({ database, websocket, connectedDevicesManager
|
||||||
request: req.body,
|
request: req.body,
|
||||||
database,
|
database,
|
||||||
websocket,
|
websocket,
|
||||||
connectedDevicesManager
|
connectedDevicesManager,
|
||||||
|
eventHandler
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (shouldDoFullSync) {
|
||||||
|
eventHandler.countEvent('pushChangesRequest shouldDoFullSync')
|
||||||
|
}
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
shouldDoFullSync
|
shouldDoFullSync
|
||||||
})
|
})
|
||||||
|
@ -64,9 +75,13 @@ export const createSyncRouter = ({ database, websocket, connectedDevicesManager
|
||||||
|
|
||||||
router.post('/pull-status', json(), async (req, res, next) => {
|
router.post('/pull-status', json(), async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
|
eventHandler.countEvent('pullStatusRequest')
|
||||||
|
|
||||||
const { body } = req
|
const { body } = req
|
||||||
|
|
||||||
if (!isClientPullChangesRequest(body)) {
|
if (!isClientPullChangesRequest(body)) {
|
||||||
|
eventHandler.countEvent('pullStatusRequest invalid')
|
||||||
|
|
||||||
throw new BadRequest()
|
throw new BadRequest()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,6 +119,14 @@ export const createSyncRouter = ({ database, websocket, connectedDevicesManager
|
||||||
transaction
|
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)
|
res.json(serverStatus)
|
||||||
})
|
})
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* server component for the TimeLimit App
|
* 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
|
* 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
|
||||||
|
@ -22,6 +22,7 @@ import { ClientPushChangesRequest } from '../../../api/schema'
|
||||||
import { isSerializedAppLogicAction, isSerializedChildAction, isSerializedParentAction } from '../../../api/validator'
|
import { isSerializedAppLogicAction, isSerializedChildAction, isSerializedParentAction } from '../../../api/validator'
|
||||||
import { VisibleConnectedDevicesManager } from '../../../connected-devices'
|
import { VisibleConnectedDevicesManager } from '../../../connected-devices'
|
||||||
import { Database } from '../../../database'
|
import { Database } from '../../../database'
|
||||||
|
import { EventHandler } from '../../../monitoring/eventhandler'
|
||||||
import { WebsocketApi } from '../../../websocket'
|
import { WebsocketApi } from '../../../websocket'
|
||||||
import { notifyClientsAboutChanges } from '../../websocket'
|
import { notifyClientsAboutChanges } from '../../websocket'
|
||||||
import { Cache } from './cache'
|
import { Cache } from './cache'
|
||||||
|
@ -29,13 +30,18 @@ import { dispatchAppLogicAction } from './dispatch-app-logic-action'
|
||||||
import { dispatchChildAction } from './dispatch-child-action'
|
import { dispatchChildAction } from './dispatch-child-action'
|
||||||
import { dispatchParentAction } from './dispatch-parent-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
|
database: Database
|
||||||
websocket: WebsocketApi
|
websocket: WebsocketApi
|
||||||
request: ClientPushChangesRequest
|
request: ClientPushChangesRequest
|
||||||
connectedDevicesManager: VisibleConnectedDevicesManager
|
connectedDevicesManager: VisibleConnectedDevicesManager
|
||||||
|
eventHandler: EventHandler
|
||||||
}) => {
|
}) => {
|
||||||
|
eventHandler.countEvent('applyActionsFromDevice')
|
||||||
|
|
||||||
if (request.actions.length > 50) {
|
if (request.actions.length > 50) {
|
||||||
|
eventHandler.countEvent('applyActionsFromDevice tooMuchActionsPerRequest')
|
||||||
|
|
||||||
throw new BadRequest()
|
throw new BadRequest()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,6 +96,8 @@ export const applyActionsFromDevice = async ({ database, request, websocket, con
|
||||||
if (action.sequenceNumber < nextSequenceNumber) {
|
if (action.sequenceNumber < nextSequenceNumber) {
|
||||||
// action was already received
|
// action was already received
|
||||||
|
|
||||||
|
eventHandler.countEvent('applyActionsFromDevice sequenceNumberRepeated')
|
||||||
|
|
||||||
cache.requireFullSync()
|
cache.requireFullSync()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -128,6 +136,8 @@ export const applyActionsFromDevice = async ({ database, request, websocket, con
|
||||||
const expectedIntegrityValue = createHash('sha512').update(integrityData).digest('hex')
|
const expectedIntegrityValue = createHash('sha512').update(integrityData).digest('hex')
|
||||||
|
|
||||||
if (action.integrity !== expectedIntegrityValue) {
|
if (action.integrity !== expectedIntegrityValue) {
|
||||||
|
eventHandler.countEvent('applyActionsFromDevice parentActionInvalidIntegrityValue')
|
||||||
|
|
||||||
throw new Error('invalid integrity value')
|
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')
|
const expectedIntegrityValue = createHash('sha512').update(integrityData).digest('hex')
|
||||||
|
|
||||||
if (action.integrity !== expectedIntegrityValue) {
|
if (action.integrity !== expectedIntegrityValue) {
|
||||||
|
eventHandler.countEvent('applyActionsFromDevice childActionInvalidIntegrityValue')
|
||||||
|
|
||||||
throw new Error('invalid integrity value')
|
throw new Error('invalid integrity value')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -152,52 +164,86 @@ export const applyActionsFromDevice = async ({ database, request, websocket, con
|
||||||
|
|
||||||
if (action.type === 'appLogic') {
|
if (action.type === 'appLogic') {
|
||||||
if (!isSerializedAppLogicAction(parsedSerializedAction)) {
|
if (!isSerializedAppLogicAction(parsedSerializedAction)) {
|
||||||
|
eventHandler.countEvent('applyActionsFromDevice invalidAppLogicAction')
|
||||||
|
|
||||||
throw new Error('invalid action: ' + action.encodedAction)
|
throw new Error('invalid action: ' + action.encodedAction)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
eventHandler.countEvent('applyActionsFromDevice action:' + parsedSerializedAction.type)
|
||||||
|
|
||||||
const parsedAction = parseAppLogicAction(parsedSerializedAction)
|
const parsedAction = parseAppLogicAction(parsedSerializedAction)
|
||||||
|
|
||||||
await dispatchAppLogicAction({
|
try {
|
||||||
action: parsedAction,
|
await dispatchAppLogicAction({
|
||||||
cache,
|
action: parsedAction,
|
||||||
deviceId: deviceEntry.deviceId
|
cache,
|
||||||
})
|
deviceId: deviceEntry.deviceId
|
||||||
|
})
|
||||||
|
} catch (ex) {
|
||||||
|
eventHandler.countEvent('applyActionsFromDevice actionWithError:' + parsedSerializedAction.type)
|
||||||
|
|
||||||
|
throw ex
|
||||||
|
}
|
||||||
} else if (action.type === 'parent') {
|
} else if (action.type === 'parent') {
|
||||||
if (!isSerializedParentAction(parsedSerializedAction)) {
|
if (!isSerializedParentAction(parsedSerializedAction)) {
|
||||||
|
eventHandler.countEvent('applyActionsFromDevice invalidParentAction')
|
||||||
|
|
||||||
throw new Error('invalid action' + action.encodedAction)
|
throw new Error('invalid action' + action.encodedAction)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
eventHandler.countEvent('applyActionsFromDevice action:' + parsedSerializedAction.type)
|
||||||
|
|
||||||
const parsedAction = parseParentAction(parsedSerializedAction)
|
const parsedAction = parseParentAction(parsedSerializedAction)
|
||||||
|
|
||||||
await dispatchParentAction({
|
try {
|
||||||
action: parsedAction,
|
await dispatchParentAction({
|
||||||
cache,
|
action: parsedAction,
|
||||||
parentUserId: action.userId,
|
cache,
|
||||||
sourceDeviceId: deviceEntry.deviceId
|
parentUserId: action.userId,
|
||||||
})
|
sourceDeviceId: deviceEntry.deviceId
|
||||||
|
})
|
||||||
|
} catch (ex) {
|
||||||
|
eventHandler.countEvent('applyActionsFromDevice actionWithError:' + parsedSerializedAction.type)
|
||||||
|
|
||||||
|
throw ex
|
||||||
|
}
|
||||||
} else if (action.type === 'child') {
|
} else if (action.type === 'child') {
|
||||||
if (!isSerializedChildAction(parsedSerializedAction)) {
|
if (!isSerializedChildAction(parsedSerializedAction)) {
|
||||||
|
eventHandler.countEvent('applyActionsFromDevice invalidChildAction')
|
||||||
|
|
||||||
throw new Error('invalid action: ' + action.encodedAction)
|
throw new Error('invalid action: ' + action.encodedAction)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
eventHandler.countEvent('applyActionsFromDevice action:' + parsedSerializedAction.type)
|
||||||
|
|
||||||
const parsedAction = parseChildAction(parsedSerializedAction)
|
const parsedAction = parseChildAction(parsedSerializedAction)
|
||||||
|
|
||||||
await dispatchChildAction({
|
try {
|
||||||
action: parsedAction,
|
await dispatchChildAction({
|
||||||
cache,
|
action: parsedAction,
|
||||||
childUserId: action.userId,
|
cache,
|
||||||
deviceId: deviceEntry.deviceId
|
childUserId: action.userId,
|
||||||
})
|
deviceId: deviceEntry.deviceId
|
||||||
|
})
|
||||||
|
} catch (ex) {
|
||||||
|
eventHandler.countEvent('applyActionsFromDevice actionWithError:' + parsedSerializedAction.type)
|
||||||
|
|
||||||
|
throw ex
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new Error('illegal state')
|
throw new Error('illegal state')
|
||||||
}
|
}
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
|
eventHandler.countEvent('applyActionsFromDevice errorDispatchingAction')
|
||||||
|
|
||||||
cache.requireFullSync()
|
cache.requireFullSync()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// save new next sequence number
|
// save new next sequence number
|
||||||
if (nextSequenceNumber !== deviceEntry.nextSequenceNumber) {
|
if (nextSequenceNumber !== deviceEntry.nextSequenceNumber) {
|
||||||
|
eventHandler.countEvent('applyActionsFromDevice updateSequenceNumber')
|
||||||
|
|
||||||
await database.device.update({
|
await database.device.update({
|
||||||
nextSequenceNumber
|
nextSequenceNumber
|
||||||
}, {
|
}, {
|
||||||
|
@ -219,6 +265,10 @@ export const applyActionsFromDevice = async ({ database, request, websocket, con
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (areChangesImportant) {
|
||||||
|
eventHandler.countEvent('applyActionsFromDevice areChangesImportant')
|
||||||
|
}
|
||||||
|
|
||||||
await notifyClientsAboutChanges({
|
await notifyClientsAboutChanges({
|
||||||
familyId,
|
familyId,
|
||||||
sourceDeviceId,
|
sourceDeviceId,
|
||||||
|
|
|
@ -19,12 +19,15 @@ import { Server } from 'http'
|
||||||
import { createApi } from './api'
|
import { createApi } from './api'
|
||||||
import { VisibleConnectedDevicesManager } from './connected-devices'
|
import { VisibleConnectedDevicesManager } from './connected-devices'
|
||||||
import { defaultDatabase, defaultUmzug } from './database'
|
import { defaultDatabase, defaultUmzug } from './database'
|
||||||
|
import { EventHandler } from './monitoring/eventhandler'
|
||||||
|
import { InMemoryEventHandler } from './monitoring/inmemoryeventhandler'
|
||||||
import { createWebsocketHandler } from './websocket'
|
import { createWebsocketHandler } from './websocket'
|
||||||
import { initWorkers } from './worker'
|
import { initWorkers } from './worker'
|
||||||
|
|
||||||
async function main () {
|
async function main () {
|
||||||
await defaultUmzug.up()
|
await defaultUmzug.up()
|
||||||
const database = defaultDatabase
|
const database = defaultDatabase
|
||||||
|
const eventHandler: EventHandler = new InMemoryEventHandler()
|
||||||
|
|
||||||
const connectedDevicesManager = new VisibleConnectedDevicesManager({
|
const connectedDevicesManager = new VisibleConnectedDevicesManager({
|
||||||
database
|
database
|
||||||
|
@ -43,7 +46,8 @@ async function main () {
|
||||||
const api = createApi({
|
const api = createApi({
|
||||||
database,
|
database,
|
||||||
websocket: websocketApi,
|
websocket: websocketApi,
|
||||||
connectedDevicesManager
|
connectedDevicesManager,
|
||||||
|
eventHandler
|
||||||
})
|
})
|
||||||
|
|
||||||
const server = new Server(api)
|
const server = new Server(api)
|
||||||
|
|
22
src/monitoring/eventhandler.ts
Normal file
22
src/monitoring/eventhandler.ts
Normal 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>
|
||||||
|
}
|
43
src/monitoring/inmemoryeventhandler.ts
Normal file
43
src/monitoring/inmemoryeventhandler.ts
Normal 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()
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue