Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Properly handle metrics aggregation with room data #224

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion websocket_server/LRUCacheStrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import StorageStrategy from './StorageStrategy.js'
import { LRUCache } from 'lru-cache'
import Room from './Room.js'

export default class LRUCacheStrategy extends StorageStrategy {

Expand Down Expand Up @@ -52,7 +53,9 @@ export default class LRUCacheStrategy extends StorageStrategy {
}

getRooms() {
return this.cache
const rooms = Array.from(this.cache.values()).filter((room) => room instanceof Room)

return rooms
}

}
2 changes: 1 addition & 1 deletion websocket_server/RedisStrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export default class RedisStrategy extends StorageStrategy {
const rooms = new Map()
for (const key of keys) {
const room = await this.get(key)
if (room) rooms.set(key, room)
if (room && !key.startsWith('token:') && !key.startsWith('socket:')) rooms.set(key, room)
}
return rooms
} catch (error) {
Expand Down
8 changes: 4 additions & 4 deletions websocket_server/SystemMonitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ export default class SystemMonitor {
getRoomStats(rooms) {
return {
activeRooms: rooms.size,
totalUsers: Array.from(rooms.values()).reduce((sum, room) => sum + Object.keys(room.users).length, 0),
totalUsers: Array.from(rooms.values()).reduce((sum, room) => sum + Object.keys(room?.users ?? {}).length, 0),
totalDataSize: this.formatBytes(Array.from(rooms.values()).reduce((sum, room) => sum + (room.data ? JSON.stringify(room.data).length : 0), 0)),
}
}

getRoomsData(rooms) {
return Array.from(rooms.entries()).map(([roomId, room]) => ({
id: roomId,
return Array.from(rooms).map((room) => ({
id: room.id,
users: Object.keys(room.users),
lastEditedUser: room.lastEditedUser,
lastActivity: new Date(room.lastActivity).toISOString(),
lastActivity: room.lastActivity ? new Date(room.lastActivity).toISOString() : null,
dataSize: this.formatBytes(JSON.stringify(room.data).length),
data: room.data, // Be cautious with this if the data is very large
}))
Expand Down
Loading