From c6cf0916e9219ea1b3adea90c8cbf7fd72625213 Mon Sep 17 00:00:00 2001 From: ashish Date: Wed, 25 Oct 2023 18:44:51 +0530 Subject: [PATCH] logs-analytics --- packages/taskserver/src/collect-analytics.ts | 38 ++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/packages/taskserver/src/collect-analytics.ts b/packages/taskserver/src/collect-analytics.ts index 96782f8693..c9fae3b0a0 100644 --- a/packages/taskserver/src/collect-analytics.ts +++ b/packages/taskserver/src/collect-analytics.ts @@ -38,6 +38,9 @@ const DEFAULT_INTERVAL_SECONDS = 1800 const configInterval = parseInt(config.taskserver.processInterval) const interval = (configInterval || DEFAULT_INTERVAL_SECONDS) * 1000 +let lastTimestamp: string +let analyticsData: any[] = [] +const collectedData: any[] = [] export default (app): void => { setInterval(async () => { logger.info('Collecting analytics at %s.', new Date().toString()) @@ -49,6 +52,25 @@ export default (app): void => { })) as ChannelType[] const knexClient: Knex = app.get('knexClient') + const currentTimestamp = new Date().toISOString() + + if (lastTimestamp) { + analyticsData = analyticsData.filter((data) => { + if (data.timestamp) { + return new Date(data.timestamp) > new Date(lastTimestamp) + } + return false + }) + } + + if (collectedData.length > 0) { + // Log the collected events + collectedData.forEach((data) => { + logger.info(data) + }) + } + + lastTimestamp = currentTimestamp const instanceUsers = await knexClient .from(userPath) @@ -58,6 +80,12 @@ export default (app): void => { .select() .options({ nestTables: true }) + for (const user of instanceUsers) { + collectedData.push({ + timestamp: user.updatedAt, + user + }) + } const channelUsers = await knexClient .from(userPath) .join(instanceAttendancePath, `${instanceAttendancePath}.userId`, `${userPath}.id`) @@ -66,6 +94,12 @@ export default (app): void => { .select() .options({ nestTables: true }) + for (const user of channelUsers) { + collectedData.push({ + timestamp: user.updatedAt, + user + }) + } const activeInstances = await app.service(instancePath).find({ query: { ended: { @@ -76,6 +110,10 @@ export default (app): void => { }) for (const instance of activeInstances.data) { + collectedData.push({ + timestamp: instance.updatedAt, + instance + }) if (instance.location) { if (activeLocations.indexOf(instance.location.id) < 0) activeLocations.push(instance.location.id) if (activeScenes.indexOf(instance.location.sceneId) < 0) activeScenes.push(instance.location.sceneId)