|
| 1 | +/* |
| 2 | + * Copyright 2021 WPPConnect Team |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { Request, Response } from 'express'; |
| 18 | + |
| 19 | +import config from '../config'; |
| 20 | +import { SessionResourceMonitor } from '../util/SessionResourceMonitor'; |
| 21 | + |
| 22 | +// Initialize the resource monitor with custom userDataDir from config |
| 23 | +const resourceMonitor = new SessionResourceMonitor( |
| 24 | + config.customUserDataDir || './userDataDir/', |
| 25 | + 5000 // 5 seconds cache |
| 26 | +); |
| 27 | + |
| 28 | +/** |
| 29 | + * Get resource usage for a specific session |
| 30 | + * GET /api/:session/resource-usage |
| 31 | + */ |
| 32 | +export async function getSessionResourceUsage(req: Request, res: Response) { |
| 33 | + /** |
| 34 | + * #swagger.tags = ["Auth"] |
| 35 | + #swagger.operationId = 'SessionResourceUsage' |
| 36 | + #swagger.autoBody=false |
| 37 | + #swagger.security = [{ |
| 38 | + "bearerAuth": [] |
| 39 | + }] |
| 40 | + #swagger.parameters["session"] = { |
| 41 | + schema: 'NERDWHATS_AMERICA' |
| 42 | + } |
| 43 | + */ |
| 44 | + try { |
| 45 | + const { session } = req.params; |
| 46 | + |
| 47 | + if (!session) { |
| 48 | + return res.status(400).json({ |
| 49 | + success: false, |
| 50 | + error: 'Session name is required', |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + const usage = await resourceMonitor.getSessionUsage(session); |
| 55 | + |
| 56 | + return res.json({ |
| 57 | + success: true, |
| 58 | + data: usage, |
| 59 | + }); |
| 60 | + } catch (error) { |
| 61 | + console.error('Error getting session resource usage:', error); |
| 62 | + return res.status(500).json({ |
| 63 | + success: false, |
| 64 | + error: 'Failed to get session resource usage', |
| 65 | + // message: error.message, |
| 66 | + }); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Get resource usage for all sessions |
| 72 | + * GET /api/sessions/resource-usage |
| 73 | + */ |
| 74 | +export async function getAllSessionsResourceUsage(req: Request, res: Response) { |
| 75 | + /** |
| 76 | + * #swagger.tags = ["Auth"] |
| 77 | + #swagger.autoBody=false |
| 78 | + #swagger.operationId = 'AllSessionsResourceUsage' |
| 79 | + #swagger.autoQuery=false |
| 80 | + #swagger.autoHeaders=false |
| 81 | + #swagger.security = [{ |
| 82 | + "bearerAuth": [] |
| 83 | + }] |
| 84 | + #swagger.parameters["secretkey"] = { |
| 85 | + schema: 'THISISMYSECURETOKEN' |
| 86 | + } |
| 87 | + */ |
| 88 | + const { secretkey } = req.params; |
| 89 | + const { authorization: token } = req.headers; |
| 90 | + |
| 91 | + let tokenDecrypt: any = ''; |
| 92 | + |
| 93 | + if (secretkey === undefined) { |
| 94 | + tokenDecrypt = token?.split(' ')[0]; |
| 95 | + } else { |
| 96 | + tokenDecrypt = secretkey; |
| 97 | + } |
| 98 | + |
| 99 | + if (tokenDecrypt !== req.serverOptions.secretKey) { |
| 100 | + res.status(400).json({ |
| 101 | + response: false, |
| 102 | + message: 'The token is incorrect', |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + try { |
| 107 | + const usage = await resourceMonitor.getAllSessionsUsage(); |
| 108 | + |
| 109 | + return res.json({ |
| 110 | + success: true, |
| 111 | + data: usage, |
| 112 | + }); |
| 113 | + } catch (error) { |
| 114 | + console.error('Error getting all sessions resource usage:', error); |
| 115 | + return res.status(500).json({ |
| 116 | + success: false, |
| 117 | + error: 'Failed to get resource usage for all sessions', |
| 118 | + // message: error.message |
| 119 | + }); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +/** |
| 124 | + * Clear resource monitor cache |
| 125 | + * POST /api/resource-usage/clear-cache |
| 126 | + */ |
| 127 | +export async function clearResourceCache(req: Request, res: Response) { |
| 128 | + try { |
| 129 | + const { session } = req.body; |
| 130 | + |
| 131 | + if (session) { |
| 132 | + resourceMonitor.clearSessionCache(session); |
| 133 | + return res.json({ |
| 134 | + success: true, |
| 135 | + message: `Cache cleared for session: ${session}`, |
| 136 | + }); |
| 137 | + } else { |
| 138 | + resourceMonitor.clearCache(); |
| 139 | + return res.json({ |
| 140 | + success: true, |
| 141 | + message: 'All cache cleared', |
| 142 | + }); |
| 143 | + } |
| 144 | + } catch (error) { |
| 145 | + console.error('Error clearing resource cache:', error); |
| 146 | + return res.status(500).json({ |
| 147 | + success: false, |
| 148 | + error: 'Failed to clear cache', |
| 149 | + // message: error.message |
| 150 | + }); |
| 151 | + } |
| 152 | +} |
0 commit comments