fix(all): New templates.

This commit is contained in:
Rodrigo Rodriguez 2024-08-24 00:12:50 -03:00
parent 7d0047221f
commit 01628fdd0e
2 changed files with 75 additions and 48 deletions

View file

@ -29,6 +29,8 @@
\*****************************************************************************/ \*****************************************************************************/
'use strict'; 'use strict';
import { setFlagsFromString } from 'v8';
import { runInNewContext } from 'vm';
import { IgApiClient } from 'instagram-private-api'; import { IgApiClient } from 'instagram-private-api';
import { readFileSync } from 'fs'; import { readFileSync } from 'fs';
import { resolve } from 'path'; import { resolve } from 'path';
@ -339,9 +341,22 @@ export class SystemKeywords {
} }
public async closeHandles({ pid }) { public async closeHandles({ pid }) {
const { min, user } = await DialogKeywords.getProcessInfo(pid);
delete this.cachedMerge[pid]; delete this.cachedMerge[pid];
// Capture memory usage before GC
const memoryBeforeGC = process.memoryUsage().heapUsed / 1024 / 1024; // in MB
GBLogEx.info(min, ``);
setFlagsFromString('--expose_gc');
const gc = runInNewContext('gc'); // nocommit
gc();
// Capture memory usage after GC
const memoryAfterGC = process.memoryUsage().heapUsed / 1024 / 1024; // in MB
GBLogEx.info(min, `BASIC: Closing Handles... From ${memoryBeforeGC.toFixed(2)} MB to ${memoryAfterGC.toFixed(2)} MB`);
} }
public async asPDF({ pid, data }) { public async asPDF({ pid, data }) {
let file = await this.renderTable(pid, data, true, false); let file = await this.renderTable(pid, data, true, false);
return file; return file;
@ -740,6 +755,10 @@ export class SystemKeywords {
let rowsDest = []; let rowsDest = [];
rows.forEach(row => { rows.forEach(row => {
if (GBUtil.hasSubObject(row)) {
row = this.flattenJSON(row);
}
let dst = {}; let dst = {};
let i = 0; let i = 0;
Object.keys(row).forEach(column => { Object.keys(row).forEach(column => {
@ -777,7 +796,7 @@ export class SystemKeywords {
} }
const { min } = await DialogKeywords.getProcessInfo(pid); const { min } = await DialogKeywords.getProcessInfo(pid);
GBLogEx.info(min, `Saving to storage '${table}' (SAVE).`); GBLogEx.info(min, `SAVE '${table}': 1 row.`);
const definition = this.getTableFromName(table, min); const definition = this.getTableFromName(table, min);
@ -1113,7 +1132,6 @@ export class SystemKeywords {
header[i] = resultH[0][i]; header[i] = resultH[0][i];
} }
resultH = null; resultH = null;
rows = []; rows = [];
rows[0] = header; rows[0] = header;
@ -2131,16 +2149,6 @@ export class SystemKeywords {
this.cachedMerge[pid] = { file: {} }; this.cachedMerge[pid] = { file: {} };
} }
// Check if is a tree or flat object.
const hasSubObject = t => {
for (var key in t) {
if (!t.hasOwnProperty(key)) continue;
if (typeof t[key] === 'object') return true;
}
return false;
};
// MAX LINES property. // MAX LINES property.
let maxLines = 1000; let maxLines = 1000;
@ -2228,6 +2236,7 @@ export class SystemKeywords {
header = results.text[0]; header = results.text[0];
rows = results.text; rows = results.text;
results = null;
} }
let table = []; let table = [];
@ -2238,7 +2247,7 @@ export class SystemKeywords {
if (!storage || !this.cachedMerge[pid][file]) { if (!storage || !this.cachedMerge[pid][file]) {
for (; foundIndex < rows.length; foundIndex++) { for (; foundIndex < rows.length; foundIndex++) {
let row = {}; let row = {};
const tmpRow = rows[foundIndex]; let tmpRow = rows[foundIndex];
row = tmpRow.dataValues ? tmpRow.dataValues : tmpRow; row = tmpRow.dataValues ? tmpRow.dataValues : tmpRow;
for (let colIndex = 0; colIndex < tmpRow.length; colIndex++) { for (let colIndex = 0; colIndex < tmpRow.length; colIndex++) {
@ -2255,6 +2264,8 @@ export class SystemKeywords {
} }
row['line'] = foundIndex + 1; row['line'] = foundIndex + 1;
table.push(row); table.push(row);
row = null;
tmpRow = null;
} }
if (storage) { if (storage) {
@ -2285,7 +2296,7 @@ export class SystemKeywords {
let row = data[i]; let row = data[i];
if (hasSubObject(row)) { if (GBUtil.hasSubObject(row)) {
row = this.flattenJSON(row); row = this.flattenJSON(row);
} }
@ -2301,10 +2312,11 @@ export class SystemKeywords {
} }
}); });
const foundRow = key1Index[key1Value]; let foundRow = key1Index[key1Value];
if (foundRow) { if (foundRow) {
found = table[foundRow[0]]; found = table[foundRow[0]];
} }
foundRow = null;
} }
if (found) { if (found) {
@ -2335,6 +2347,7 @@ export class SystemKeywords {
valueFound = found[e]; valueFound = found[e];
} }
}); });
found = null;
const equals = const equals =
typeof value === 'string' && typeof valueFound === 'string' typeof value === 'string' && typeof valueFound === 'string'
@ -2354,6 +2367,7 @@ export class SystemKeywords {
}, },
{ retries: 5 } { retries: 5 }
); );
obj = null;
} else { } else {
const cell = `${this.numberToLetters(j)}${i + 1}`; const cell = `${this.numberToLetters(j)}${i + 1}`;
const address = `${cell}:${cell}`; const address = `${cell}:${cell}`;
@ -2389,7 +2403,7 @@ export class SystemKeywords {
if (storage) { if (storage) {
// Uppercases fields. // Uppercases fields.
const dst = {}; let dst = {};
let i = 0; let i = 0;
Object.keys(fieldsValues).forEach(fieldSrc => { Object.keys(fieldsValues).forEach(fieldSrc => {
const name = fieldsNames[i]; const name = fieldsNames[i];
@ -2400,11 +2414,14 @@ export class SystemKeywords {
fieldsValuesList.push(dst); fieldsValuesList.push(dst);
this.cachedMerge[pid][file].push(dst); this.cachedMerge[pid][file].push(dst);
dst = null;
} else { } else {
await this.save({ pid, file, args: fieldsValues }); await this.save({ pid, file, args: fieldsValues });
} }
fieldsValues = null;
adds++; adds++;
} }
row = null;
} }
// In case of storage, persist to DB in batch. // In case of storage, persist to DB in batch.
@ -2412,6 +2429,8 @@ export class SystemKeywords {
if (fieldsValuesList.length) { if (fieldsValuesList.length) {
await this.saveToStorageBatch({ pid, table: file, rows: fieldsValuesList }); await this.saveToStorageBatch({ pid, table: file, rows: fieldsValuesList });
} }
key1Index = null;
key2Index = null;
table = null; table = null;
fieldsValuesList = null; fieldsValuesList = null;
@ -2588,7 +2607,7 @@ export class SystemKeywords {
{ {
retries: 5, retries: 5,
onRetry: err => { onRetry: err => {
GBLog.error(`Retrying SaveToStorageBatch due to: ${err.message}.`); GBLog.error(`Retrying deleteFromStorage due to: ${err.message}.`);
} }
} }
); );

View file

@ -69,7 +69,6 @@ export class GBUtil {
} }
public static async getDirectLineClient(min) { public static async getDirectLineClient(min) {
let config = { let config = {
spec: JSON.parse(Fs.readFileSync('directline-3.0.json', 'utf8')), spec: JSON.parse(Fs.readFileSync('directline-3.0.json', 'utf8')),
requestInterceptor: req => { requestInterceptor: req => {
@ -77,8 +76,10 @@ export class GBUtil {
} }
}; };
if (!GBConfigService.get('STORAGE_NAME')) { if (!GBConfigService.get('STORAGE_NAME')) {
config['spec'].url = `http://127.0.0.1:${GBConfigService.getServerPort()}/api/messages/${min.botId}`, (config['spec'].url = `http://127.0.0.1:${GBConfigService.getServerPort()}/api/messages/${min.botId}`),
config['spec'].servers = [{ url: `http://127.0.0.1:${GBConfigService.getServerPort()}/api/messages/${min.botId}` }]; (config['spec'].servers = [
{ url: `http://127.0.0.1:${GBConfigService.getServerPort()}/api/messages/${min.botId}` }
]);
config['spec'].openapi = '3.0.0'; config['spec'].openapi = '3.0.0';
delete config['spec'].host; delete config['spec'].host;
delete config['spec'].swagger; delete config['spec'].swagger;
@ -125,45 +126,52 @@ export class GBUtil {
} }
} }
public static copyIfNewerRecursive(src, dest) { public static copyIfNewerRecursive(src, dest) {
if (!Fs.existsSync(src)) { if (!Fs.existsSync(src)) {
console.error(`Source path "${src}" does not exist.`); console.error(`Source path "${src}" does not exist.`);
return; return;
} }
// Check if the source is a directory // Check if the source is a directory
if (Fs.statSync(src).isDirectory()) { if (Fs.statSync(src).isDirectory()) {
// Create the destination directory if it doesn't exist // Create the destination directory if it doesn't exist
if (!Fs.existsSync(dest)) { if (!Fs.existsSync(dest)) {
Fs.mkdirSync(dest, { recursive: true }); Fs.mkdirSync(dest, { recursive: true });
} }
// Read all files and directories in the source directory // Read all files and directories in the source directory
const entries = Fs.readdirSync(src); const entries = Fs.readdirSync(src);
for (let entry of entries) { for (let entry of entries) {
const srcEntry = path.join(src, entry); const srcEntry = path.join(src, entry);
const destEntry = path.join(dest, entry); const destEntry = path.join(dest, entry);
// Recursively copy each entry // Recursively copy each entry
this.copyIfNewerRecursive(srcEntry, destEntry); this.copyIfNewerRecursive(srcEntry, destEntry);
} }
} else { } else {
// Source is a file, check if we need to copy it // Source is a file, check if we need to copy it
if (Fs.existsSync(dest)) { if (Fs.existsSync(dest)) {
const srcStat = Fs.statSync(src); const srcStat = Fs.statSync(src);
const destStat = Fs.statSync(dest); const destStat = Fs.statSync(dest);
// Copy only if the source file is newer than the destination file // Copy only if the source file is newer than the destination file
if (srcStat.mtime > destStat.mtime) { if (srcStat.mtime > destStat.mtime) {
Fs.cpSync(src, dest, { force: true }); Fs.cpSync(src, dest, { force: true });
}
} else {
// Destination file doesn't exist, so copy it
Fs.cpSync(src, dest, { force: true });
} }
} else {
// Destination file doesn't exist, so copy it
Fs.cpSync(src, dest, { force: true });
}
} }
} }
// Check if is a tree or flat object.
public static hasSubObject(t) {
for (var key in t) {
if (!t.hasOwnProperty(key)) continue;
if (typeof t[key] === 'object') return true;
}
return false;
}
} }