applay-utils 1.8.24 → 1.8.26
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 +1 -1
- package/utils/logger.js +9 -15
- package/README.md +0 -5
- package/python-scripts/aggregate.py +0 -29
- package/python-setup.js +0 -51
- package/utils/pythonFuncs.js +0 -38
package/package.json
CHANGED
package/utils/logger.js
CHANGED
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
var pino = require('pino');
|
|
2
2
|
|
|
3
|
+
const destinationCache = {};
|
|
4
|
+
|
|
3
5
|
var logger = (prefix, filelog, nivel) => {
|
|
4
6
|
var options = {
|
|
5
7
|
timestamp: () => {
|
|
6
8
|
return `,"time":"${new Date().toLocaleString('pt-BR')}"`;
|
|
7
9
|
}
|
|
8
10
|
}
|
|
9
|
-
|
|
11
|
+
|
|
12
|
+
if (filelog && !destinationCache[filelog]) {
|
|
13
|
+
destinationCache[filelog] = pino.destination(filelog);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
var modLogger = !filelog ? pino(options) : pino(options, destinationCache[filelog]);
|
|
10
17
|
var pinoCaller = require('../libs-custom/pino-caller')(modLogger, { relativeTo: 'any', stackAdjustment: nivel || 1, prefix })
|
|
11
18
|
var consoleFunc = {
|
|
12
19
|
log: (...args) => {
|
|
@@ -17,20 +24,7 @@ var logger = (prefix, filelog, nivel) => {
|
|
|
17
24
|
} else {
|
|
18
25
|
return pinoCaller.info(msg)
|
|
19
26
|
}
|
|
20
|
-
}
|
|
21
|
-
// unhandledRejection: (reason, p) => {
|
|
22
|
-
// var errolist = reason.stack.toString().split('\n');
|
|
23
|
-
// // console.log(errolist);
|
|
24
|
-
// var position = errolist[1].replace(')', '').split(':').slice(1);
|
|
25
|
-
// var msg = `${errolist[0]}`;
|
|
26
|
-
// if (errolist[0].includes('evalmachine.<anonymous>')) {
|
|
27
|
-
// position = [errolist[0].split(':')[1], 1];
|
|
28
|
-
// msg = errolist[4] + '\n\n' + errolist[1] + '\n' + errolist[2]
|
|
29
|
-
// }
|
|
30
|
-
// var pinoCallerError = require('../libs-custom/pino-caller')(modLogger, { relativeTo: 'any', stackAdjustment: nivel || 1, prefix, position })
|
|
31
|
-
// process.removeListener('unhandledRejection', consoleFunc.unhandledRejection);
|
|
32
|
-
// return pinoCallerError.error(msg)
|
|
33
|
-
// }
|
|
27
|
+
}
|
|
34
28
|
}
|
|
35
29
|
return consoleFunc;
|
|
36
30
|
};
|
package/README.md
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
# aggregate.py
|
|
2
|
-
import sys, json
|
|
3
|
-
from pymongo import MongoClient
|
|
4
|
-
from bson.json_util import dumps # <-- IMPORTANTE
|
|
5
|
-
|
|
6
|
-
def main():
|
|
7
|
-
data = json.loads(sys.stdin.read())
|
|
8
|
-
|
|
9
|
-
mongo_url = data["mongoUrl"]
|
|
10
|
-
db_name = data["db"]
|
|
11
|
-
collection_name = data["collection"]
|
|
12
|
-
pipeline = data["pipeline"]
|
|
13
|
-
|
|
14
|
-
client = MongoClient(mongo_url, maxPoolSize=50)
|
|
15
|
-
col = client[db_name][collection_name]
|
|
16
|
-
|
|
17
|
-
cursor = col.aggregate(
|
|
18
|
-
pipeline,
|
|
19
|
-
allowDiskUse=True,
|
|
20
|
-
session=None
|
|
21
|
-
)
|
|
22
|
-
|
|
23
|
-
result = list(cursor)
|
|
24
|
-
|
|
25
|
-
# dumps() serializa ObjectId, datetime, Binary, etc
|
|
26
|
-
print(dumps(result, ensure_ascii=False))
|
|
27
|
-
|
|
28
|
-
if __name__ == "__main__":
|
|
29
|
-
main()
|
package/python-setup.js
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
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/pythonFuncs.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
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, "../python-scripts/aggregate.py");
|
|
9
|
-
|
|
10
|
-
const data = {
|
|
11
|
-
mongoUrl: db.databaseUrl,
|
|
12
|
-
db: db.databaseName,
|
|
13
|
-
collection,
|
|
14
|
-
pipeline: pipe
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const payload = JSON.stringify(data);
|
|
18
|
-
|
|
19
|
-
const proc = spawn(python, [script]);
|
|
20
|
-
|
|
21
|
-
let out = "";
|
|
22
|
-
let err = "";
|
|
23
|
-
|
|
24
|
-
proc.stdout.on("data", d => out += d.toString());
|
|
25
|
-
proc.stderr.on("data", d => err += d.toString());
|
|
26
|
-
|
|
27
|
-
proc.on("close", code => {
|
|
28
|
-
if (code !== 0) return reject(new Error(err));
|
|
29
|
-
try { resolve(JSON.parse(out)); }
|
|
30
|
-
catch (e) { reject(e); }
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
proc.stdin.write(payload);
|
|
34
|
-
proc.stdin.end();
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
module.exports = { aggregateAsync };
|