@gnpdev/rpa-tools 1.0.17 → 1.0.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/watcher.js +138 -136
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gnpdev/rpa-tools",
3
- "version": "1.0.17",
3
+ "version": "1.0.18",
4
4
  "description": "Libreria para logs y screenshot de bots",
5
5
  "author": "Sergio Antonio Trujillo del Valle",
6
6
  "main": "src/index.js",
package/src/watcher.js CHANGED
@@ -1,137 +1,139 @@
1
- 'use strict';
2
- const { uploadScreenshot } = require('./storage');
3
-
4
- class ScreenshotWatcher {
5
- /**
6
- * @param {object} p
7
- * @param {string} p.botId
8
- * @param {import('pg').Pool} p.pool
9
- * @param {import('minio').Client} p.minioClient
10
- * @param {string} p.bucket
11
- * @param {import('pino').Logger} p.logger
12
- * @param {object} [p.screenshot]
13
- * @param {'webp'|'jpeg'} [p.screenshot.format='webp']
14
- * @param {number} [p.screenshot.quality=70] - calidad conversión 1-100
15
- * @param {number} [p.screenshot.captureQuality=90] - calidad captura Playwright
16
- */
17
- constructor({ botId, pool, minioClient, bucket, logger, screenshot = {} }) {
18
- this.botId = botId;
19
- this.pool = pool;
20
- this.minioClient = minioClient;
21
- this.bucket = bucket;
22
- this.logger = logger;
23
-
24
- this.screenshotOpts = {
25
- format: screenshot.format ?? 'webp',
26
- quality: screenshot.quality ?? 70,
27
- captureQuality: screenshot.captureQuality ?? 90,
28
- };
29
-
30
- this._watchTimer = null;
31
- this._shotTimer = null;
32
- }
33
-
34
- /**
35
- * Inicia el polling que activa/desactiva screenshots según bot_debug_config.
36
- * Llama una sola vez al arrancar el bot.
37
- *
38
- * @param {import('playwright').Page} page
39
- * @param {number} [pollMs=3000] - cada cuántos ms revisar la BD
40
- */
41
- watch(page, pollMs = 3000) {
42
- if (this._watchTimer) {
43
- this.logger.warn('ScreenshotWatcher ya está corriendo, ignorando llamada duplicada');
44
- return;
45
- }
46
-
47
- this.logger.info({ pollMs }, 'Iniciando polling de debug config');
48
-
49
- this._watchTimer = setInterval(async () => {
50
- try {
51
- const { rows } = await this.pool.query(
52
- `SELECT screenshots_activo, intervalo_sec
53
- FROM bots.tb_bots
54
- WHERE id = $1`,
55
- [this.botId]
56
- );
57
-
58
- const cfg = rows[0];
59
- // console.log('DEBUG CONFIG', cfg); // <-- loguear config para debug
60
-
61
- if (!cfg) {
62
- this.logger.debug('Sin config en bot_debug_config para este bot');
63
- return;
64
- }
65
-
66
- if (cfg.screenshots_activo && !this._shotTimer) {
67
- this.logger.info({ intervalSec: cfg.intervalo_sec }, 'Screenshots activados');
68
- this._startScreenshots(page, cfg.intervalo_sec * 1000);
69
- return;
70
- }
71
-
72
- if (!cfg.screenshots_activo && this._shotTimer) {
73
- this.logger.info('Screenshots desactivados');
74
- this._stopScreenshots();
75
- }
76
- } catch (err) {
77
- this.logger.error({ err }, 'Error leyendo bot_debug_config');
78
- }
79
- }, pollMs);
80
- }
81
-
82
- /**
83
- * @private
84
- */
85
- _startScreenshots(page, intervalMs) {
86
- this._shotTimer = setInterval(async () => {
87
- const t0 = Date.now();
88
-
89
- try {
90
- // Playwright captura en jpeg (más rápido), sharp convierte a WebP después
91
- const rawBuffer = await page.screenshot({
92
- type: 'jpeg',
93
- quality: this.screenshotOpts.captureQuality,
94
- fullPage: false,
95
- });
96
-
97
- const key = await uploadScreenshot(
98
- this.minioClient,
99
- this.bucket,
100
- this.botId,
101
- rawBuffer,
102
- {
103
- format: this.screenshotOpts.format,
104
- quality: this.screenshotOpts.quality,
105
- }
106
- );
107
-
108
- this.logger.debug(
109
- { key, ms: Date.now() - t0 },
110
- 'Screenshot subido'
111
- );
112
- } catch (err) {
113
- this.logger.error({ err }, 'Error capturando o subiendo screenshot');
114
- }
115
- }, intervalMs);
116
- }
117
-
118
- /**
119
- * @private
120
- */
121
- _stopScreenshots() {
122
- clearInterval(this._shotTimer);
123
- this._shotTimer = null;
124
- }
125
-
126
- /**
127
- * Detiene todos los intervalos. Llamar siempre al cerrar el bot.
128
- */
129
- destroy() {
130
- this._stopScreenshots();
131
- clearInterval(this._watchTimer);
132
- this._watchTimer = null;
133
- this.logger.info('ScreenshotWatcher destruido');
134
- }
135
- }
136
-
1
+ 'use strict';
2
+ const { uploadScreenshot } = require('./storage');
3
+
4
+ class ScreenshotWatcher {
5
+ /**
6
+ * @param {object} p
7
+ * @param {string} p.botId
8
+ * @param {import('pg').Pool} p.pool
9
+ * @param {import('minio').Client} p.minioClient
10
+ * @param {string} p.bucket
11
+ * @param {import('pino').Logger} p.logger
12
+ * @param {object} [p.screenshot]
13
+ * @param {'webp'|'jpeg'} [p.screenshot.format='webp']
14
+ * @param {number} [p.screenshot.quality=70] - calidad conversión 1-100
15
+ * @param {number} [p.screenshot.captureQuality=90] - calidad captura Playwright
16
+ */
17
+ constructor({ botId, pool, minioClient, bucket, logger, screenshot = {} }) {
18
+ this.botId = botId;
19
+ this.pool = pool;
20
+ this.minioClient = minioClient;
21
+ this.bucket = bucket;
22
+ this.logger = logger;
23
+
24
+ this.screenshotOpts = {
25
+ format: screenshot.format ?? 'webp',
26
+ quality: screenshot.quality ?? 70,
27
+ captureQuality: screenshot.captureQuality ?? 90,
28
+ };
29
+
30
+ this._watchTimer = null;
31
+ this._shotTimer = null;
32
+ }
33
+
34
+ /**
35
+ * Inicia el polling que activa/desactiva screenshots según bot_debug_config.
36
+ * Llama una sola vez al arrancar el bot.
37
+ *
38
+ * @param {import('playwright').Page} page
39
+ * @param {number} [pollMs=3000] - cada cuántos ms revisar la BD
40
+ */
41
+ watch(page, pollMs = 3000) {
42
+ if (this._watchTimer) {
43
+ this.logger.warn('ScreenshotWatcher ya está corriendo, ignorando llamada duplicada');
44
+ return;
45
+ }
46
+
47
+ this.logger.info({ pollMs }, 'Iniciando polling de debug config');
48
+
49
+ this._watchTimer = setInterval(async () => {
50
+ try {
51
+ const { rows } = await this.pool.query(
52
+ `SELECT screenshots_activo, intervalo_sec
53
+ FROM bots.tb_bots
54
+ WHERE id = $1`,
55
+ [this.botId]
56
+ );
57
+
58
+ const cfg = rows[0];
59
+ // console.log('DEBUG CONFIG', cfg); // <-- loguear config para debug
60
+
61
+ if (!cfg) {
62
+ this.logger.debug('Sin config en bot_debug_config para este bot');
63
+ return;
64
+ }
65
+
66
+ if (cfg.screenshots_activo && !this._shotTimer) {
67
+ this.logger.info({ intervalSec: cfg.intervalo_sec }, 'Screenshots activados');
68
+ this._startScreenshots(page, cfg.intervalo_sec * 1000);
69
+ return;
70
+ }
71
+
72
+ if (!cfg.screenshots_activo && this._shotTimer) {
73
+ this.logger.info('Screenshots desactivados');
74
+ this._stopScreenshots();
75
+ }
76
+ } catch (err) {
77
+ this.logger.error({ err }, 'Error leyendo bot_debug_config');
78
+ }
79
+ }, pollMs);
80
+ }
81
+
82
+ /**
83
+ * @private
84
+ */
85
+ _startScreenshots(page, intervalMs) {
86
+ this._shotTimer = setInterval(async () => {
87
+ const t0 = Date.now();
88
+
89
+ try {
90
+ // Playwright captura en jpeg (más rápido), sharp convierte a WebP después
91
+ const rawBuffer = await page.screenshot({
92
+ type: 'jpeg',
93
+ quality: this.screenshotOpts.captureQuality,
94
+ fullPage: false,
95
+ });
96
+
97
+ const key = await uploadScreenshot(
98
+ this.minioClient,
99
+ this.bucket,
100
+ this.botId,
101
+ rawBuffer,
102
+ {
103
+ format: this.screenshotOpts.format,
104
+ quality: this.screenshotOpts.quality,
105
+ pool: this.pool,
106
+ logger: this.logger,
107
+ }
108
+ );
109
+
110
+ this.logger.debug(
111
+ { key, ms: Date.now() - t0 },
112
+ 'Screenshot subido'
113
+ );
114
+ } catch (err) {
115
+ this.logger.error({ err }, 'Error capturando o subiendo screenshot');
116
+ }
117
+ }, intervalMs);
118
+ }
119
+
120
+ /**
121
+ * @private
122
+ */
123
+ _stopScreenshots() {
124
+ clearInterval(this._shotTimer);
125
+ this._shotTimer = null;
126
+ }
127
+
128
+ /**
129
+ * Detiene todos los intervalos. Llamar siempre al cerrar el bot.
130
+ */
131
+ destroy() {
132
+ this._stopScreenshots();
133
+ clearInterval(this._watchTimer);
134
+ this._watchTimer = null;
135
+ this.logger.info('ScreenshotWatcher destruido');
136
+ }
137
+ }
138
+
137
139
  module.exports = { ScreenshotWatcher };