-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathpool-service.js
87 lines (78 loc) · 2.64 KB
/
pool-service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const cds = require("@sap/cds");
/**
* Implementation of the Connection Pool monitoring handler
*
* By Martin Stenzig
* https://answers.sap.com/questions/13738468/monitoring-db-pool-usage.html?childToView=13738470
* and
* https://github.com/RizInno/cds-load-refsrv/blob/main/srv/handlers/pool.js
*/
class PoolService extends cds.ApplicationService {
async init() {
// Connect to database
const db = await cds.connect.to("db");
/**
* Event handler for 'getInfo' pool information function
*/
this.on("getInfo", async (req) => {
// Only execute if database is HANA
if (cds.env.requires.db.kind == "hana") {
// Create a transaction bracket
let tx = db.tx();
// Execute a dummy query - you must run a query to get the connection pool info!
await tx.run("SELECT * FROM DUMMY");
// Get reference to connection pool
const pool = tx.dbc._pool;
//
const allocatedResources = [];
pool._allObjects instanceof Set &&
pool._allObjects.forEach((resource) => {
resource.state === "ALLOCATED" && allocatedResources.push(resource);
});
const { available, borrowed, size, pending, min, max } = pool;
const poolStats = {
level: "info",
message: "generic pool statistics",
technicalInformation: {
available,
borrowed,
size,
pending,
min,
max,
allocatedResources: allocatedResources.map((resource) => {
let connectionId = null;
if (
resource.obj &&
resource.obj._connection &&
resource.obj._connection.connectOptions
) {
connectionId =
resource.obj._connection.connectOptions.connectionId;
}
return {
connectionId,
creationTime: new Date(resource.creationTime).toISOString(),
lastBorrowTime: new Date(resource.lastBorrowTime).toISOString(),
lastIdleTime: new Date(resource.lastIdleTime).toISOString(),
lastReturnTime: new Date(resource.lastReturnTime).toISOString(),
};
}),
},
fieldValues: { quantity: borrowed },
};
await tx.commit();
return poolStats;
} else {
await tx.rollback();
return (
"Connection pool info not supported for db type " +
cds.env.requires.db.kind
);
}
});
// ensure to call super.init()
await super.init();
}
}
module.exports = PoolService;