applay-utils 1.8.16 → 1.8.19
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 +2 -1
- package/python-scripts/aggregate.py +23 -0
- package/python-setup.js +41 -0
- package/utils/mdb.js +1 -17
- package/utils/pythonFuncs.js +38 -0
- package/utils/querys.js +10 -13
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "applay-utils",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.19",
|
|
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()
|
package/python-setup.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const { spawnSync } = require("child_process");
|
|
2
|
+
|
|
3
|
+
function findPython() {
|
|
4
|
+
for (const cmd of ["python3", "python", "py"]) {
|
|
5
|
+
const check = spawnSync(cmd, ["--version"]);
|
|
6
|
+
if (check.status === 0) return cmd;
|
|
7
|
+
}
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function ensurePyMongo(python) {
|
|
12
|
+
const check = spawnSync(python, ["-c", "import pymongo"], { encoding: "utf8" });
|
|
13
|
+
|
|
14
|
+
if (check.status === 0) {
|
|
15
|
+
console.log("✔ PyMongo já instalado");
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
console.log("📦 Instalando PyMongo...");
|
|
20
|
+
const install = spawnSync(python, ["-m", "pip", "install", "pymongo"], { stdio: "inherit" });
|
|
21
|
+
|
|
22
|
+
if (install.status !== 0) {
|
|
23
|
+
console.error("❌ Falha ao instalar PyMongo.");
|
|
24
|
+
process.exit(1);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
console.log("✔ PyMongo instalado com sucesso");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const python = findPython();
|
|
31
|
+
|
|
32
|
+
if (!python) {
|
|
33
|
+
console.error("❌ Python não encontrado. Instale Python 3.");
|
|
34
|
+
process.exit(1);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
console.log("✔ Python encontrado em:", python);
|
|
38
|
+
ensurePyMongo(python);
|
|
39
|
+
|
|
40
|
+
// Export to package.json
|
|
41
|
+
console.log(`PYTHON=${python}`);
|
package/utils/mdb.js
CHANGED
|
@@ -10,7 +10,7 @@ var MDB = {
|
|
|
10
10
|
var connect = (alias, url, callback) => {
|
|
11
11
|
if (MDB.status[url]) { return callback(MDB.clients[url]); }
|
|
12
12
|
console.log('===============>>>>>>> NOVO POOL DE CONEXÃO MDB ',url,alias)
|
|
13
|
-
let uriparams = `?appName=${url}&minPoolSize=
|
|
13
|
+
let uriparams = `?appName=${url}&minPoolSize=10&maxPoolSize=50&maxIdleTimeMS=30000&serverSelectionTimeoutMS=5000`;
|
|
14
14
|
MongoBase.MongoClient.connect(url+uriparams, { useUnifiedTopology: true }, (err, client) => {
|
|
15
15
|
if (err) { return console.log('ERRO MDB -- TRACKING -- ', err); }
|
|
16
16
|
MDB.status[url] = true;
|
|
@@ -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,38 @@
|
|
|
1
|
+
const { spawn } = require("child_process");
|
|
2
|
+
|
|
3
|
+
function aggregatePython(db, collection, pipe, options = {}) {
|
|
4
|
+
return new Promise((resolve, reject) => {
|
|
5
|
+
|
|
6
|
+
const python = spawn("python3", ["aggregate.py"]);
|
|
7
|
+
|
|
8
|
+
const payload = JSON.stringify({
|
|
9
|
+
mongoUrl: db.client.s.url, // usa a URL do próprio MongoClient do Node
|
|
10
|
+
db: db.databaseName,
|
|
11
|
+
collection,
|
|
12
|
+
pipeline: pipe,
|
|
13
|
+
options
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
let output = "";
|
|
17
|
+
let errorOut = "";
|
|
18
|
+
|
|
19
|
+
python.stdout.on("data", data => output += data.toString());
|
|
20
|
+
python.stderr.on("data", data => errorOut += data.toString());
|
|
21
|
+
|
|
22
|
+
python.on("close", code => {
|
|
23
|
+
if (code !== 0) {
|
|
24
|
+
return reject(new Error("Python error:\n" + errorOut));
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
resolve(JSON.parse(output));
|
|
28
|
+
} catch (e) {
|
|
29
|
+
reject(e);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
python.stdin.write(payload);
|
|
34
|
+
python.stdin.end();
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
module.exports = { aggregatePython };
|
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 => {
|