applay-utils 1.8.17 → 1.8.20

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.
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "applay-utils",
3
- "version": "1.8.17",
3
+ "version": "1.8.20",
4
4
  "description": "Utilitary tools for Applay applications",
5
5
  "scripts": {
6
+ "postinstall": "node python-setup.js",
6
7
  "build": "npm version patch",
7
8
  "test": "echo \"Error: no test specified\" && exit 1"
8
9
  },
@@ -0,0 +1,23 @@
1
+ # aggregate.py
2
+ import sys, json
3
+ from pymongo import MongoClient
4
+
5
+ def main():
6
+ data = json.loads(sys.stdin.read())
7
+
8
+ mongo_url = data["mongoUrl"]
9
+ db_name = data["db"]
10
+ collection_name = data["collection"]
11
+ pipeline = data["pipeline"]
12
+ options = data.get("options", {})
13
+
14
+ client = MongoClient(mongo_url, maxPoolSize=50)
15
+ col = client[db_name][collection_name]
16
+
17
+ cursor = col.aggregate(pipeline, { "allowDiskUse": True, **options })
18
+ result = list(cursor)
19
+
20
+ print(json.dumps(result, ensure_ascii=False))
21
+
22
+ if __name__ == "__main__":
23
+ main()
@@ -0,0 +1,51 @@
1
+ const { spawnSync } = require("child_process");
2
+ const fs = require("fs");
3
+ const path = require("path");
4
+
5
+ function findPython() {
6
+ for (const cmd of ["python3", "python", "py"]) {
7
+ const check = spawnSync(cmd, ["--version"]);
8
+ if (check.status === 0) return cmd;
9
+ }
10
+ return null;
11
+ }
12
+
13
+ function run(cmd, args, opts = {}) {
14
+ const out = spawnSync(cmd, args, { stdio: "inherit", ...opts });
15
+ if (out.status !== 0) {
16
+ console.error(`❌ Falha ao rodar: ${cmd} ${args.join(" ")}`);
17
+ process.exit(1);
18
+ }
19
+ }
20
+
21
+ const python = findPython();
22
+ if (!python) {
23
+ console.error("❌ Nenhum Python encontrado!");
24
+ process.exit(1);
25
+ }
26
+
27
+ console.log("✔ Python encontrado em:", python);
28
+
29
+ // Caminho da venv
30
+ const venvPath = path.join(__dirname, ".python-env");
31
+
32
+ // Criar venv se não existir
33
+ if (!fs.existsSync(venvPath)) {
34
+ console.log("📦 Criando ambiente virtual Python...");
35
+ run(python, ["-m", "venv", venvPath]);
36
+ }
37
+
38
+ const pyBin = path.join(venvPath, "bin", "python");
39
+ const pipBin = path.join(venvPath, "bin", "pip");
40
+
41
+ // Verifica PyMongo dentro da venv
42
+ const check = spawnSync(pyBin, ["-c", "import pymongo"]);
43
+ if (check.status !== 0) {
44
+ console.log("📦 Instalando PyMongo dentro da venv...");
45
+ run(pyBin, ["-m", "pip", "install", "pymongo"]);
46
+ } else {
47
+ console.log("✔ PyMongo já instalado na venv.");
48
+ }
49
+
50
+ console.log("✔ Ambiente Python pronto.");
51
+ console.log(`PYTHON=${pyBin}`);
package/utils/mdb.js CHANGED
@@ -23,22 +23,6 @@ var connect = (alias, url, callback) => {
23
23
  });
24
24
  }
25
25
 
26
- // var connect = (alias, url, callback) => {
27
- // if (MDB.status[alias]) { return callback(MDB.clients[alias]); }
28
- // console.log('================================================>>>>>>> NOVO POOL DE CONEXÃO MDB ',alias, Object.keys(MDB.status).length)
29
- // let uriparams = `?appName=${alias}&minPoolSize=10&maxPoolSize=100&maxIdleTimeMS=30000&serverSelectionTimeoutMS=5000`;
30
- // MongoBase.MongoClient.connect(url+uriparams, { useUnifiedTopology: true }, (err, client) => {
31
- // if (err) { return console.log('ERRO MDB -- TRACKING -- ', err); }
32
- // MDB.status[alias] = true;
33
- // MDB.clients[alias] = client;
34
- // process.on('SIGTERM', internalStop(alias, true, 'SIGTERM'));
35
- // process.on('SIGINT', internalStop(alias, true, 'SIGINT'));
36
- // process.on('uncaughtException', internalStop(alias, true, 'uncaughtException'));
37
- // // console.log('Conectado com ', alias);
38
- // callback(client)
39
- // });
40
- // }
41
-
42
26
  var connectAsync = (alias, url) => new Promise(resolve => connect(alias, url, (client) => resolve(client)));
43
27
 
44
28
  var internalStop = (alias, finish) => {
@@ -0,0 +1,37 @@
1
+ const path = require("path");
2
+ const { spawn } = require("child_process");
3
+
4
+ function aggregateAsync(db, collection, pipe, options = {}) {
5
+ return new Promise((resolve, reject) => {
6
+
7
+ const python = path.join(__dirname, ".python-env/bin/python");
8
+ const script = path.join(__dirname, "aggregate.py");
9
+
10
+ const payload = JSON.stringify({
11
+ mongoUrl: db.client.s.url,
12
+ db: db.databaseName,
13
+ collection,
14
+ pipeline: pipe,
15
+ options
16
+ });
17
+
18
+ const proc = spawn(python, [script]);
19
+
20
+ let out = "";
21
+ let err = "";
22
+
23
+ proc.stdout.on("data", d => out += d.toString());
24
+ proc.stderr.on("data", d => err += d.toString());
25
+
26
+ proc.on("close", code => {
27
+ if (code !== 0) return reject(new Error(err));
28
+ try { resolve(JSON.parse(out)); }
29
+ catch (e) { reject(e); }
30
+ });
31
+
32
+ proc.stdin.write(payload);
33
+ proc.stdin.end();
34
+ });
35
+ }
36
+
37
+ module.exports = { aggregateAsync };
package/utils/querys.js CHANGED
@@ -1,22 +1,11 @@
1
1
  const mdb = require('./mdb');
2
2
  const fs = require('fs');
3
+ const pythonFuncs = require("./pythonFuncs");
4
+
3
5
  var sizeof;
4
6
 
5
7
  var registerLogs = (db,collection,result,callback)=>{
6
- // if(!db.logSize){return callback(result)}
7
- // if(!sizeof){sizeof = require('object-sizeof')}
8
- // let size = sizeof(result)
9
8
  callback(result);
10
- // let line = [
11
- // db.rota,
12
- // new Date().toLocaleDateString('pt-br'),
13
- // new Date().toLocaleTimeString('pt-br'),
14
- // db?.s?.namespace?.db,
15
- // collection,
16
- // (size/1000/1000).toFixed(2)+'mb',
17
- // size
18
- // ]
19
- // fs.appendFileSync(`/home/${process.env.USER}/logs/mdb-size.csv`,`\n${line.join(';')}` );
20
9
  }
21
10
 
22
11
  var querys = {
@@ -70,6 +59,14 @@ var querys = {
70
59
  'updateSetAsync': (db, collection, itemId, itemSet) => new Promise(resolve => db.collection(collection).findOneAndUpdate(itemId, { $set: itemSet }, (err, result) => resolve(result))),
71
60
  'updateAllAsync': (db, collection, filter, itemSet) => new Promise(resolve => db.collection(collection).updateMany(filter, itemSet, (err, result) => resolve(err || result))),
72
61
  'aggregateAsync': (db, collection, pipe, options) => new Promise(resolve => db.collection(collection).aggregate(pipe, { allowDiskUse: true, ...options }).toArray((err, result) => resolve(result))),
62
+ 'aggregatePython': async (db, collection, pipe, options) => {
63
+ const res = await pythonFuncs.aggregateAsync(
64
+ db,
65
+ collection,
66
+ pipe
67
+ );
68
+ return res
69
+ },
73
70
  'deleteMany': (db, collection, filter) => new Promise(resolve => db.collection(collection).deleteMany(filter, (err, result) => resolve(result))),
74
71
  'deleteOne': (db, collection, filter) => new Promise((resolve, reject) => { db.collection(collection).deleteOne(filter, (err, result) => { if (err) { reject(err); } else { resolve(result); } }); }),
75
72
  'saveAsync': (db, collection, dataOrItem, user) => new Promise(resolve => {