diff --git a/websocket_server/apiService.js b/websocket_server/apiService.js deleted file mode 100644 index 8c10f338..00000000 --- a/websocket_server/apiService.js +++ /dev/null @@ -1,67 +0,0 @@ -/* eslint-disable no-console */ - -/** - * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors - * SPDX-License-Identifier: AGPL-3.0-or-later - */ - -import fetch from 'node-fetch' -import https from 'https' -import dotenv from 'dotenv' -import Utils from './Utils.js' -dotenv.config() - -export default class ApiService { - - constructor(tokenGenerator) { - this.NEXTCLOUD_URL = process.env.NEXTCLOUD_URL - this.IS_DEV = Utils.parseBooleanFromEnv(process.env.IS_DEV) - this.agent = this.IS_DEV ? new https.Agent({ rejectUnauthorized: false }) : null - this.tokenGenerator = tokenGenerator - } - - fetchOptions(method, token, body = null, roomId = null, lastEditedUser = null) { - return { - method, - headers: { - 'Content-Type': 'application/json', - ...(method === 'GET' && { Authorization: `Bearer ${token}` }), - ...(method === 'PUT' && { - 'X-Whiteboard-Auth': this.tokenGenerator.handle(roomId), - 'X-Whiteboard-User': lastEditedUser || 'unknown', - }), - }, - ...(body && { body: JSON.stringify(body) }), - ...(this.agent && { agent: this.agent }), - } - } - - async fetchData(url, options) { - try { - const response = await fetch(url, options) - if (!response.ok) { - throw new Error(`HTTP error! status: ${response.status}: ${await response.text()}`) - } - return response.json() - } catch (error) { - console.error(error) - return null - } - } - - async getRoomDataFromServer(roomID, jwtToken) { - const url = `${this.NEXTCLOUD_URL}/index.php/apps/whiteboard/${roomID}` - const options = this.fetchOptions('GET', jwtToken) - return this.fetchData(url, options) - } - - async saveRoomDataToServer(roomID, roomData, lastEditedUser) { - console.log('Saving room data to file') - - const url = `${this.NEXTCLOUD_URL}/index.php/apps/whiteboard/${roomID}` - const body = { data: { elements: roomData } } - const options = this.fetchOptions('PUT', null, body, roomID, lastEditedUser) - return this.fetchData(url, options) - } - -} diff --git a/websocket_server/systemMonitor.js b/websocket_server/systemMonitor.js deleted file mode 100644 index f6ab01fc..00000000 --- a/websocket_server/systemMonitor.js +++ /dev/null @@ -1,69 +0,0 @@ -/** - * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors - * SPDX-License-Identifier: AGPL-3.0-or-later - */ - -export default class SystemMonitor { - - constructor(storageManager) { - this.storageManager = storageManager - } - - getSystemOverview() { - const rooms = this.storageManager.getRooms() - return { - memoryUsage: this.getMemoryUsage(), - roomStats: this.getRoomStats(rooms), - cacheInfo: this.getCacheInfo(rooms), - roomsData: this.getRoomsData(rooms), - } - } - - getMemoryUsage() { - const memUsage = process.memoryUsage() - return { - rss: this.formatBytes(memUsage.rss), - heapTotal: this.formatBytes(memUsage.heapTotal), - heapUsed: this.formatBytes(memUsage.heapUsed), - external: this.formatBytes(memUsage.external), - arrayBuffers: this.formatBytes(memUsage.arrayBuffers), - } - } - - getRoomStats(rooms) { - return { - activeRooms: rooms.size, - 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, - users: Object.keys(room.users), - lastEditedUser: room.lastEditedUser, - lastActivity: new Date(room.lastActivity).toISOString(), - dataSize: this.formatBytes(JSON.stringify(room.data).length), - data: room.data, // Be cautious with this if the data is very large - })) - } - - getCacheInfo(rooms) { - return { - size: rooms.size, - maxSize: rooms.max, - keys: Array.from(rooms.keys()), - recentlyUsed: Array.from(rooms.keys()).slice(0, 10), // Show 10 most recently used - } - } - - formatBytes(bytes) { - if (bytes === 0) return '0 Bytes' - const k = 1024 - const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] - const i = Math.floor(Math.log(bytes) / Math.log(k)) - return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i] - } - -} diff --git a/websocket_server/utils.js b/websocket_server/utils.js deleted file mode 100644 index ac0d606d..00000000 --- a/websocket_server/utils.js +++ /dev/null @@ -1,20 +0,0 @@ -/** - * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors - * SPDX-License-Identifier: AGPL-3.0-or-later - */ - -export default class Utils { - - static convertStringToArrayBuffer(string) { - return new TextEncoder().encode(string).buffer - } - - static convertArrayBufferToString(arrayBuffer) { - return new TextDecoder().decode(arrayBuffer) - } - - static parseBooleanFromEnv(value) { - return value === 'true' - } - -}