@odatano/core 2.0.0-rc.14 → 2.0.0-rc.15
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 +179 -179
- package/srv/utils/agent-grants.d.ts +38 -9
- package/srv/utils/agent-grants.js +126 -11
package/package.json
CHANGED
|
@@ -1,179 +1,179 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@odatano/core",
|
|
3
|
-
"version": "2.0.0-rc.
|
|
4
|
-
"description": "SAP CAP plugin for Cardano blockchain OData V4 integration",
|
|
5
|
-
"imports": {
|
|
6
|
-
"#cds-models/*": "./@cds-models/*/index.js"
|
|
7
|
-
},
|
|
8
|
-
"main": "src/index.js",
|
|
9
|
-
"types": "src/index.d.ts",
|
|
10
|
-
"files": [
|
|
11
|
-
"cds-plugin.js",
|
|
12
|
-
"src/**/*.js",
|
|
13
|
-
"src/**/*.d.ts",
|
|
14
|
-
"srv/**/*.js",
|
|
15
|
-
"srv/**/*.d.ts",
|
|
16
|
-
"srv/**/*.cds",
|
|
17
|
-
"db/**/*.cds",
|
|
18
|
-
"@cds-models/"
|
|
19
|
-
],
|
|
20
|
-
"engines": {
|
|
21
|
-
"node": ">=22.5"
|
|
22
|
-
},
|
|
23
|
-
"peerDependencies": {
|
|
24
|
-
"@sap/cds": ">=10"
|
|
25
|
-
},
|
|
26
|
-
"dependencies": {
|
|
27
|
-
"@blockfrost/blockfrost-js": "~6.0.0",
|
|
28
|
-
"@cap-js/hana": "^3",
|
|
29
|
-
"@cardano-ogmios/client": "^6.14.0",
|
|
30
|
-
"@harmoniclabs/buildooor": "0.2.9",
|
|
31
|
-
"@harmoniclabs/cardano-costmodels-ts": "~1.6.1",
|
|
32
|
-
"@harmoniclabs/cardano-ledger-ts": "^0.5.6",
|
|
33
|
-
"@harmoniclabs/cbor": "^2.0.1",
|
|
34
|
-
"@harmoniclabs/crypto": "^0.3.0",
|
|
35
|
-
"@harmoniclabs/uint8array-utils": "^1.0.4",
|
|
36
|
-
"@odatano/cap-auth": "^0.1.0",
|
|
37
|
-
"@sap/xssec": "^4",
|
|
38
|
-
"axios": "^1.18.0",
|
|
39
|
-
"bech32": "^2.0.0",
|
|
40
|
-
"blake2b": "^2.1.4"
|
|
41
|
-
},
|
|
42
|
-
"optionalDependencies": {
|
|
43
|
-
"pkcs11js": "^2.1.6"
|
|
44
|
-
},
|
|
45
|
-
"overrides": {
|
|
46
|
-
"axios": "^1.18.0",
|
|
47
|
-
"esbuild": "0.28.1",
|
|
48
|
-
"form-data": "^4.0.6",
|
|
49
|
-
"fast-uri": "^3.1.7",
|
|
50
|
-
"ws": "7.5.11",
|
|
51
|
-
"@sap/approuter": {
|
|
52
|
-
"body-parser": "^2.2.3"
|
|
53
|
-
}
|
|
54
|
-
},
|
|
55
|
-
"devDependencies": {
|
|
56
|
-
"@cap-js/cds-test": "^0.4.1",
|
|
57
|
-
"@cap-js/cds-typer": "^0.40.0",
|
|
58
|
-
"@cap-js/cds-types": "^0.18.0",
|
|
59
|
-
"@cap-js/postgres": "^3.1.1",
|
|
60
|
-
"@cap-js/sqlite": "^3",
|
|
61
|
-
"@sap/cds": "^10",
|
|
62
|
-
"@sap/cds-dk": "^10",
|
|
63
|
-
"@sap/eslint-plugin-cds": "^4.2.4",
|
|
64
|
-
"@types/blake2b": "^2.1.3",
|
|
65
|
-
"@types/node": "^22.19.7",
|
|
66
|
-
"@typescript-eslint/eslint-plugin": "^8.50.1",
|
|
67
|
-
"@typescript-eslint/parser": "^8.50.1",
|
|
68
|
-
"@vitest/coverage-v8": "^4.1.10",
|
|
69
|
-
"dotenv": "^16.3.1",
|
|
70
|
-
"eslint": "^9.39.2",
|
|
71
|
-
"nock": "^13.5.6",
|
|
72
|
-
"ts-node": "^10.9.2",
|
|
73
|
-
"tsx": "^4.21.0",
|
|
74
|
-
"typescript": "^5.9.3",
|
|
75
|
-
"vitest": "^4.1.10"
|
|
76
|
-
},
|
|
77
|
-
"cds": {
|
|
78
|
-
"typer": {
|
|
79
|
-
"ieee754compatible": true
|
|
80
|
-
},
|
|
81
|
-
"build": {
|
|
82
|
-
"tasks": [
|
|
83
|
-
{
|
|
84
|
-
"for": "node-cf",
|
|
85
|
-
"src": "srv",
|
|
86
|
-
"dest": "../gen/srv"
|
|
87
|
-
},
|
|
88
|
-
{
|
|
89
|
-
"for": "hana",
|
|
90
|
-
"src": "db",
|
|
91
|
-
"dest": "../gen/db"
|
|
92
|
-
}
|
|
93
|
-
]
|
|
94
|
-
},
|
|
95
|
-
"requires": {
|
|
96
|
-
"db": {
|
|
97
|
-
"kind": "sqlite",
|
|
98
|
-
"credentials": {
|
|
99
|
-
"url": "db.sqlite"
|
|
100
|
-
},
|
|
101
|
-
"client": {
|
|
102
|
-
"timeout": 5000
|
|
103
|
-
}
|
|
104
|
-
},
|
|
105
|
-
"[production]": {
|
|
106
|
-
"db": {
|
|
107
|
-
"kind": "hana",
|
|
108
|
-
"pool": {
|
|
109
|
-
"acquireTimeoutMillis": 30000,
|
|
110
|
-
"min": 0,
|
|
111
|
-
"max": 10
|
|
112
|
-
}
|
|
113
|
-
},
|
|
114
|
-
"auth": "xsuaa"
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
},
|
|
118
|
-
"scripts": {
|
|
119
|
-
"clean": "node -e \"const fs=require('fs');fs.globSync('{srv,db,src}/**/*.{js,js.map,d.ts,d.ts.map}').filter(f=>!f.includes('node_modules')).forEach(f=>fs.unlinkSync(f));console.log('Clean complete')\"",
|
|
120
|
-
"clean:models": "node -e \"require('fs').rmSync('@cds-models',{recursive:true,force:true});console.log('Removed @cds-models')\"",
|
|
121
|
-
"build": "npm run cds:types && tsc",
|
|
122
|
-
"build:plugin": "npm run cds:types && tsc -p tsconfig.build.json",
|
|
123
|
-
"build:package": "npm run clean && npm run clean:models && npm run build:plugin",
|
|
124
|
-
"prepare": "npm run build:package",
|
|
125
|
-
"start": "cds-serve",
|
|
126
|
-
"cds:watch": "cds watch",
|
|
127
|
-
"cds:serve": "cds serve --with-mocks",
|
|
128
|
-
"cds:types": "cds-typer \"*\" --outputDirectory @cds-models",
|
|
129
|
-
"lint": "eslint .",
|
|
130
|
-
"typecheck": "tsc --noEmit",
|
|
131
|
-
"db:deploy": "cds deploy",
|
|
132
|
-
"pretest": "npm run clean && npm run cds:types && npm run db:deploy",
|
|
133
|
-
"test": "vitest run",
|
|
134
|
-
"pretest:coverage": "npm run clean && npm run cds:types && npm run db:deploy",
|
|
135
|
-
"test:coverage": "vitest run --coverage",
|
|
136
|
-
"pretest:ci": "npm run clean && npm run cds:types && npm run db:deploy",
|
|
137
|
-
"test:ci": "vitest run --exclude \"**/core-ogmios.test.ts\"",
|
|
138
|
-
"pretest:ci:coverage": "npm run clean && npm run cds:types && npm run db:deploy",
|
|
139
|
-
"test:ci:coverage": "vitest run --coverage --exclude \"**/core-ogmios.test.ts\"",
|
|
140
|
-
"pretest:integration": "npm run clean && npm run cds:types && npm run db:deploy",
|
|
141
|
-
"test:integration": "vitest run --project integration",
|
|
142
|
-
"test:integration:blockfrost": "vitest run test/integration/core.blockfrost.test.ts",
|
|
143
|
-
"test:integration:koios": "vitest run test/integration/core.koios.test.ts",
|
|
144
|
-
"test:integration:ogmios": "vitest run test/integration/core-ogmios.test.ts",
|
|
145
|
-
"test:integration:both": "npm run test:integration:blockfrost && npm run test:integration:koios",
|
|
146
|
-
"pretest:unit": "npm run cds:types",
|
|
147
|
-
"test:unit": "vitest run --project unit",
|
|
148
|
-
"watch-odatano": "cds watch --open odatano/index.html?sap-ui-xx-viewCache=false --livereload false",
|
|
149
|
-
"watch-wallet": "cds watch --open wallet/webapp/index.html",
|
|
150
|
-
"release:check": "node scripts/release-check.mjs"
|
|
151
|
-
},
|
|
152
|
-
"sapux": [
|
|
153
|
-
"app/odatano"
|
|
154
|
-
],
|
|
155
|
-
"directories": {
|
|
156
|
-
"doc": "docs",
|
|
157
|
-
"test": "test"
|
|
158
|
-
},
|
|
159
|
-
"repository": {
|
|
160
|
-
"type": "git",
|
|
161
|
-
"url": "git+https://github.com/ODATANO/ODATANO.git"
|
|
162
|
-
},
|
|
163
|
-
"keywords": [
|
|
164
|
-
"CAP",
|
|
165
|
-
"CDS",
|
|
166
|
-
"plugin",
|
|
167
|
-
"Cardano",
|
|
168
|
-
"blockchain",
|
|
169
|
-
"OData",
|
|
170
|
-
"SAP"
|
|
171
|
-
],
|
|
172
|
-
"author": "Maximilian Weber",
|
|
173
|
-
"license": "Apache-2.0",
|
|
174
|
-
"type": "commonjs",
|
|
175
|
-
"bugs": {
|
|
176
|
-
"url": "https://github.com/ODATANO/ODATANO/issues"
|
|
177
|
-
},
|
|
178
|
-
"homepage": "https://github.com/ODATANO/ODATANO#readme"
|
|
179
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@odatano/core",
|
|
3
|
+
"version": "2.0.0-rc.15",
|
|
4
|
+
"description": "SAP CAP plugin for Cardano blockchain OData V4 integration",
|
|
5
|
+
"imports": {
|
|
6
|
+
"#cds-models/*": "./@cds-models/*/index.js"
|
|
7
|
+
},
|
|
8
|
+
"main": "src/index.js",
|
|
9
|
+
"types": "src/index.d.ts",
|
|
10
|
+
"files": [
|
|
11
|
+
"cds-plugin.js",
|
|
12
|
+
"src/**/*.js",
|
|
13
|
+
"src/**/*.d.ts",
|
|
14
|
+
"srv/**/*.js",
|
|
15
|
+
"srv/**/*.d.ts",
|
|
16
|
+
"srv/**/*.cds",
|
|
17
|
+
"db/**/*.cds",
|
|
18
|
+
"@cds-models/"
|
|
19
|
+
],
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=22.5"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"@sap/cds": ">=10"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@blockfrost/blockfrost-js": "~6.0.0",
|
|
28
|
+
"@cap-js/hana": "^3",
|
|
29
|
+
"@cardano-ogmios/client": "^6.14.0",
|
|
30
|
+
"@harmoniclabs/buildooor": "0.2.9",
|
|
31
|
+
"@harmoniclabs/cardano-costmodels-ts": "~1.6.1",
|
|
32
|
+
"@harmoniclabs/cardano-ledger-ts": "^0.5.6",
|
|
33
|
+
"@harmoniclabs/cbor": "^2.0.1",
|
|
34
|
+
"@harmoniclabs/crypto": "^0.3.0",
|
|
35
|
+
"@harmoniclabs/uint8array-utils": "^1.0.4",
|
|
36
|
+
"@odatano/cap-auth": "^0.1.0",
|
|
37
|
+
"@sap/xssec": "^4",
|
|
38
|
+
"axios": "^1.18.0",
|
|
39
|
+
"bech32": "^2.0.0",
|
|
40
|
+
"blake2b": "^2.1.4"
|
|
41
|
+
},
|
|
42
|
+
"optionalDependencies": {
|
|
43
|
+
"pkcs11js": "^2.1.6"
|
|
44
|
+
},
|
|
45
|
+
"overrides": {
|
|
46
|
+
"axios": "^1.18.0",
|
|
47
|
+
"esbuild": "0.28.1",
|
|
48
|
+
"form-data": "^4.0.6",
|
|
49
|
+
"fast-uri": "^3.1.7",
|
|
50
|
+
"ws": "7.5.11",
|
|
51
|
+
"@sap/approuter": {
|
|
52
|
+
"body-parser": "^2.2.3"
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@cap-js/cds-test": "^0.4.1",
|
|
57
|
+
"@cap-js/cds-typer": "^0.40.0",
|
|
58
|
+
"@cap-js/cds-types": "^0.18.0",
|
|
59
|
+
"@cap-js/postgres": "^3.1.1",
|
|
60
|
+
"@cap-js/sqlite": "^3",
|
|
61
|
+
"@sap/cds": "^10",
|
|
62
|
+
"@sap/cds-dk": "^10",
|
|
63
|
+
"@sap/eslint-plugin-cds": "^4.2.4",
|
|
64
|
+
"@types/blake2b": "^2.1.3",
|
|
65
|
+
"@types/node": "^22.19.7",
|
|
66
|
+
"@typescript-eslint/eslint-plugin": "^8.50.1",
|
|
67
|
+
"@typescript-eslint/parser": "^8.50.1",
|
|
68
|
+
"@vitest/coverage-v8": "^4.1.10",
|
|
69
|
+
"dotenv": "^16.3.1",
|
|
70
|
+
"eslint": "^9.39.2",
|
|
71
|
+
"nock": "^13.5.6",
|
|
72
|
+
"ts-node": "^10.9.2",
|
|
73
|
+
"tsx": "^4.21.0",
|
|
74
|
+
"typescript": "^5.9.3",
|
|
75
|
+
"vitest": "^4.1.10"
|
|
76
|
+
},
|
|
77
|
+
"cds": {
|
|
78
|
+
"typer": {
|
|
79
|
+
"ieee754compatible": true
|
|
80
|
+
},
|
|
81
|
+
"build": {
|
|
82
|
+
"tasks": [
|
|
83
|
+
{
|
|
84
|
+
"for": "node-cf",
|
|
85
|
+
"src": "srv",
|
|
86
|
+
"dest": "../gen/srv"
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
"for": "hana",
|
|
90
|
+
"src": "db",
|
|
91
|
+
"dest": "../gen/db"
|
|
92
|
+
}
|
|
93
|
+
]
|
|
94
|
+
},
|
|
95
|
+
"requires": {
|
|
96
|
+
"db": {
|
|
97
|
+
"kind": "sqlite",
|
|
98
|
+
"credentials": {
|
|
99
|
+
"url": "db.sqlite"
|
|
100
|
+
},
|
|
101
|
+
"client": {
|
|
102
|
+
"timeout": 5000
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
"[production]": {
|
|
106
|
+
"db": {
|
|
107
|
+
"kind": "hana",
|
|
108
|
+
"pool": {
|
|
109
|
+
"acquireTimeoutMillis": 30000,
|
|
110
|
+
"min": 0,
|
|
111
|
+
"max": 10
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
"auth": "xsuaa"
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
"scripts": {
|
|
119
|
+
"clean": "node -e \"const fs=require('fs');fs.globSync('{srv,db,src}/**/*.{js,js.map,d.ts,d.ts.map}').filter(f=>!f.includes('node_modules')).forEach(f=>fs.unlinkSync(f));console.log('Clean complete')\"",
|
|
120
|
+
"clean:models": "node -e \"require('fs').rmSync('@cds-models',{recursive:true,force:true});console.log('Removed @cds-models')\"",
|
|
121
|
+
"build": "npm run cds:types && tsc",
|
|
122
|
+
"build:plugin": "npm run cds:types && tsc -p tsconfig.build.json",
|
|
123
|
+
"build:package": "npm run clean && npm run clean:models && npm run build:plugin",
|
|
124
|
+
"prepare": "npm run build:package",
|
|
125
|
+
"start": "cds-serve",
|
|
126
|
+
"cds:watch": "cds watch",
|
|
127
|
+
"cds:serve": "cds serve --with-mocks",
|
|
128
|
+
"cds:types": "cds-typer \"*\" --outputDirectory @cds-models",
|
|
129
|
+
"lint": "eslint .",
|
|
130
|
+
"typecheck": "tsc --noEmit",
|
|
131
|
+
"db:deploy": "cds deploy",
|
|
132
|
+
"pretest": "npm run clean && npm run cds:types && npm run db:deploy",
|
|
133
|
+
"test": "vitest run",
|
|
134
|
+
"pretest:coverage": "npm run clean && npm run cds:types && npm run db:deploy",
|
|
135
|
+
"test:coverage": "vitest run --coverage",
|
|
136
|
+
"pretest:ci": "npm run clean && npm run cds:types && npm run db:deploy",
|
|
137
|
+
"test:ci": "vitest run --exclude \"**/core-ogmios.test.ts\"",
|
|
138
|
+
"pretest:ci:coverage": "npm run clean && npm run cds:types && npm run db:deploy",
|
|
139
|
+
"test:ci:coverage": "vitest run --coverage --exclude \"**/core-ogmios.test.ts\"",
|
|
140
|
+
"pretest:integration": "npm run clean && npm run cds:types && npm run db:deploy",
|
|
141
|
+
"test:integration": "vitest run --project integration",
|
|
142
|
+
"test:integration:blockfrost": "vitest run test/integration/core.blockfrost.test.ts",
|
|
143
|
+
"test:integration:koios": "vitest run test/integration/core.koios.test.ts",
|
|
144
|
+
"test:integration:ogmios": "vitest run test/integration/core-ogmios.test.ts",
|
|
145
|
+
"test:integration:both": "npm run test:integration:blockfrost && npm run test:integration:koios",
|
|
146
|
+
"pretest:unit": "npm run cds:types",
|
|
147
|
+
"test:unit": "vitest run --project unit",
|
|
148
|
+
"watch-odatano": "cds watch --open odatano/index.html?sap-ui-xx-viewCache=false --livereload false",
|
|
149
|
+
"watch-wallet": "cds watch --open wallet/webapp/index.html",
|
|
150
|
+
"release:check": "node scripts/release-check.mjs"
|
|
151
|
+
},
|
|
152
|
+
"sapux": [
|
|
153
|
+
"app/odatano"
|
|
154
|
+
],
|
|
155
|
+
"directories": {
|
|
156
|
+
"doc": "docs",
|
|
157
|
+
"test": "test"
|
|
158
|
+
},
|
|
159
|
+
"repository": {
|
|
160
|
+
"type": "git",
|
|
161
|
+
"url": "git+https://github.com/ODATANO/ODATANO.git"
|
|
162
|
+
},
|
|
163
|
+
"keywords": [
|
|
164
|
+
"CAP",
|
|
165
|
+
"CDS",
|
|
166
|
+
"plugin",
|
|
167
|
+
"Cardano",
|
|
168
|
+
"blockchain",
|
|
169
|
+
"OData",
|
|
170
|
+
"SAP"
|
|
171
|
+
],
|
|
172
|
+
"author": "Maximilian Weber",
|
|
173
|
+
"license": "Apache-2.0",
|
|
174
|
+
"type": "commonjs",
|
|
175
|
+
"bugs": {
|
|
176
|
+
"url": "https://github.com/ODATANO/ODATANO/issues"
|
|
177
|
+
},
|
|
178
|
+
"homepage": "https://github.com/ODATANO/ODATANO#readme"
|
|
179
|
+
}
|
|
@@ -229,10 +229,17 @@ export interface AgentGrantUsage {
|
|
|
229
229
|
jobsUsedToday: number;
|
|
230
230
|
maxJobsPerDay: number | null;
|
|
231
231
|
}
|
|
232
|
-
/**
|
|
232
|
+
/**
|
|
233
|
+
* The grant's admitted calls in `window`, grouped by service and action (one
|
|
234
|
+
* SUM per group). `runner` is the request's transaction (the read); deferred
|
|
235
|
+
* counters (Postgres / HANA) are flushed first on `flushRunner`, the detached
|
|
236
|
+
* primary db service: written inside the request transaction they would hold
|
|
237
|
+
* the rows' locks until the GET commits and vanish with its rollback, while a
|
|
238
|
+
* separate autocommit is visible to the following SELECT under read committed.
|
|
239
|
+
*/
|
|
233
240
|
export declare function getGrantUsage(grant: AgentGrantRow, window: Extract<UsageWindow, {
|
|
234
241
|
ok: true;
|
|
235
|
-
}>, runner?: Runner): Promise<AgentGrantUsage>;
|
|
242
|
+
}>, runner?: Runner, flushRunner?: Runner): Promise<AgentGrantUsage>;
|
|
236
243
|
export interface AgentGrantStatus {
|
|
237
244
|
grantId: string;
|
|
238
245
|
agentLabel: string | null;
|
|
@@ -299,16 +306,38 @@ export declare function consumeDailyBudget(br: BudgetRunner | Runner, grant: Age
|
|
|
299
306
|
* the new day's counter: the unit was spent yesterday, yesterday keeps it.
|
|
300
307
|
*/
|
|
301
308
|
export declare function refundDailyBudget(br: BudgetRunner | Runner, grant: AgentGrantRow, window: string): Promise<void>;
|
|
309
|
+
type UsageMode = 'sync' | 'deferred';
|
|
310
|
+
/** Test seam: force a mode (null = derive from cds.db.kind). */
|
|
311
|
+
export declare function __setGrantUsageModeForTests(mode: UsageMode | null): void;
|
|
312
|
+
/** Test seam: forget buffered deltas and the pending timer. */
|
|
313
|
+
export declare function __resetGrantUsageBufferForTests(): void;
|
|
314
|
+
/** How many keys wait for the next flush (monitoring, tests). */
|
|
315
|
+
export declare function pendingGrantUsageKeys(): number;
|
|
316
|
+
/**
|
|
317
|
+
* Writes the buffered deltas: `calls + n, refunded + m` per key, an INSERT when
|
|
318
|
+
* the row does not exist yet, one more UPDATE when a racing writer created it.
|
|
319
|
+
* A delta whose write fails goes back into the buffer for the next flush.
|
|
320
|
+
* `runner` defaults to the primary db service (tests pass their fake).
|
|
321
|
+
*/
|
|
322
|
+
export declare function flushGrantUsage(runner?: Runner): Promise<{
|
|
323
|
+
flushed: number;
|
|
324
|
+
failed: number;
|
|
325
|
+
}>;
|
|
302
326
|
/**
|
|
303
|
-
* One admitted call: `calls + 1` on (grant, UTC day, service, action).
|
|
304
|
-
*
|
|
305
|
-
*
|
|
306
|
-
*
|
|
307
|
-
*
|
|
308
|
-
*
|
|
327
|
+
* One admitted call: `calls + 1` on (grant, UTC day, service, action).
|
|
328
|
+
* Deferred mode (Postgres / HANA, detached runner): the delta goes into the
|
|
329
|
+
* buffer, nothing is written here. Sync mode: a conditional UPDATE first (the
|
|
330
|
+
* common path), an INSERT when the row does not exist yet, one more UPDATE
|
|
331
|
+
* when a racing request inserted it first, AWAITED like every other write of
|
|
332
|
+
* the hook (see touchLastUsed). Never fails the request: usage is accounting,
|
|
333
|
+
* not admission. Returns the day the call was counted on (the refund names it).
|
|
309
334
|
*/
|
|
310
335
|
export declare function recordGrantUsage(br: BudgetRunner | Runner, grant: AgentGrantRow, service: string, action: string, now?: Date): Promise<string>;
|
|
311
|
-
/**
|
|
336
|
+
/**
|
|
337
|
+
* `refunded + 1` on the row the call was counted on: a refused request admitted
|
|
338
|
+
* nothing. Deferred mode: into the buffer, on the same key the call went to,
|
|
339
|
+
* whether or not that call has been flushed yet (the flush adds deltas).
|
|
340
|
+
*/
|
|
312
341
|
export declare function refundGrantUsage(br: BudgetRunner | Runner, grant: AgentGrantRow, service: string, action: string, day: string): Promise<void>;
|
|
313
342
|
export {};
|
|
314
343
|
//# sourceMappingURL=agent-grants.d.ts.map
|
|
@@ -52,6 +52,10 @@ exports.budgetRunnerFor = budgetRunnerFor;
|
|
|
52
52
|
exports.enforceAgentGrant = enforceAgentGrant;
|
|
53
53
|
exports.consumeDailyBudget = consumeDailyBudget;
|
|
54
54
|
exports.refundDailyBudget = refundDailyBudget;
|
|
55
|
+
exports.__setGrantUsageModeForTests = __setGrantUsageModeForTests;
|
|
56
|
+
exports.__resetGrantUsageBufferForTests = __resetGrantUsageBufferForTests;
|
|
57
|
+
exports.pendingGrantUsageKeys = pendingGrantUsageKeys;
|
|
58
|
+
exports.flushGrantUsage = flushGrantUsage;
|
|
55
59
|
exports.recordGrantUsage = recordGrantUsage;
|
|
56
60
|
exports.refundGrantUsage = refundGrantUsage;
|
|
57
61
|
const cds_1 = __importDefault(require("@sap/cds"));
|
|
@@ -440,8 +444,17 @@ function resolveUsageWindow(since, until, now = new Date()) {
|
|
|
440
444
|
const untilIso = new Date(toMs).toISOString();
|
|
441
445
|
return { ok: true, since: sinceIso, until: untilIso, sinceDay: sinceIso.slice(0, 10), untilDay: untilIso.slice(0, 10) };
|
|
442
446
|
}
|
|
443
|
-
/**
|
|
444
|
-
|
|
447
|
+
/**
|
|
448
|
+
* The grant's admitted calls in `window`, grouped by service and action (one
|
|
449
|
+
* SUM per group). `runner` is the request's transaction (the read); deferred
|
|
450
|
+
* counters (Postgres / HANA) are flushed first on `flushRunner`, the detached
|
|
451
|
+
* primary db service: written inside the request transaction they would hold
|
|
452
|
+
* the rows' locks until the GET commits and vanish with its rollback, while a
|
|
453
|
+
* separate autocommit is visible to the following SELECT under read committed.
|
|
454
|
+
*/
|
|
455
|
+
async function getGrantUsage(grant, window, runner = dbRunner(), flushRunner = dbRunner()) {
|
|
456
|
+
if (pendingGrantUsageKeys() > 0)
|
|
457
|
+
await flushGrantUsage(flushRunner);
|
|
445
458
|
const rows = ((await runner.run(SELECT.from(exports.GRANT_USAGE_ENTITY)
|
|
446
459
|
.columns('service', 'action', { func: 'sum', args: [{ ref: ['calls'] }], as: 'calls' }, { func: 'sum', args: [{ ref: ['refunded'] }], as: 'refunded' })
|
|
447
460
|
.where({ grant_ID: grant.ID, day: { '>=': window.sinceDay } })
|
|
@@ -612,7 +625,7 @@ function registerAgentGrantHandlers(srv) {
|
|
|
612
625
|
const grant = (await tx.run(SELECT.one.from(exports.GRANTS_ENTITY).where({ ID: data.grantId })));
|
|
613
626
|
if (!grant)
|
|
614
627
|
return req.reject(404, 'Grant not found', 'grantId');
|
|
615
|
-
return getGrantUsage(grant, window, tx);
|
|
628
|
+
return getGrantUsage(grant, window, tx, dbRunner());
|
|
616
629
|
});
|
|
617
630
|
}
|
|
618
631
|
// ---------------------------------------------------------------------------
|
|
@@ -760,18 +773,111 @@ async function refundDailyBudget(br, grant, window) {
|
|
|
760
773
|
.set({ jobsUsedToday: { '-=': 1 } })
|
|
761
774
|
.where({ ID: grant.ID, budgetWindow: window, jobsUsedToday: { '>': 0 } }));
|
|
762
775
|
}
|
|
776
|
+
const USAGE_FLUSH_INTERVAL_MS = 1000;
|
|
777
|
+
const usageBuffer = new Map();
|
|
778
|
+
let usageFlushTimer = null;
|
|
779
|
+
let usageModeOverride = null;
|
|
780
|
+
let usageShutdownHooked = false;
|
|
781
|
+
/** Deferred on the databases whose writers do not block readers; sync everywhere else (SQLite, tests). */
|
|
782
|
+
function usageMode() {
|
|
783
|
+
if (usageModeOverride)
|
|
784
|
+
return usageModeOverride;
|
|
785
|
+
const kind = String(cds_1.default.db?.kind ?? '');
|
|
786
|
+
return kind === 'postgres' || kind === 'hana' ? 'deferred' : 'sync';
|
|
787
|
+
}
|
|
788
|
+
/** Test seam: force a mode (null = derive from cds.db.kind). */
|
|
789
|
+
function __setGrantUsageModeForTests(mode) {
|
|
790
|
+
usageModeOverride = mode;
|
|
791
|
+
}
|
|
792
|
+
/** Test seam: forget buffered deltas and the pending timer. */
|
|
793
|
+
function __resetGrantUsageBufferForTests() {
|
|
794
|
+
usageBuffer.clear();
|
|
795
|
+
if (usageFlushTimer) {
|
|
796
|
+
clearTimeout(usageFlushTimer);
|
|
797
|
+
usageFlushTimer = null;
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
/** How many keys wait for the next flush (monitoring, tests). */
|
|
801
|
+
function pendingGrantUsageKeys() {
|
|
802
|
+
return usageBuffer.size;
|
|
803
|
+
}
|
|
804
|
+
function bufferUsage(key, calls, refunded) {
|
|
805
|
+
const k = `${key.grant_ID}|${key.day}|${key.service}|${key.action}`;
|
|
806
|
+
const d = usageBuffer.get(k);
|
|
807
|
+
if (d) {
|
|
808
|
+
d.calls += calls;
|
|
809
|
+
d.refunded += refunded;
|
|
810
|
+
}
|
|
811
|
+
else
|
|
812
|
+
usageBuffer.set(k, { ...key, calls, refunded });
|
|
813
|
+
if (!usageFlushTimer) {
|
|
814
|
+
usageFlushTimer = setTimeout(() => {
|
|
815
|
+
usageFlushTimer = null;
|
|
816
|
+
void flushGrantUsage().catch((err) => logger.warn(`usage flush failed: ${String(err?.message ?? err)}`));
|
|
817
|
+
}, USAGE_FLUSH_INTERVAL_MS);
|
|
818
|
+
usageFlushTimer.unref?.();
|
|
819
|
+
}
|
|
820
|
+
if (!usageShutdownHooked) {
|
|
821
|
+
usageShutdownHooked = true;
|
|
822
|
+
cds_1.default.on('shutdown', () => flushGrantUsage().catch(() => undefined));
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
/**
|
|
826
|
+
* Writes the buffered deltas: `calls + n, refunded + m` per key, an INSERT when
|
|
827
|
+
* the row does not exist yet, one more UPDATE when a racing writer created it.
|
|
828
|
+
* A delta whose write fails goes back into the buffer for the next flush.
|
|
829
|
+
* `runner` defaults to the primary db service (tests pass their fake).
|
|
830
|
+
*/
|
|
831
|
+
async function flushGrantUsage(runner = dbRunner()) {
|
|
832
|
+
if (usageBuffer.size === 0)
|
|
833
|
+
return { flushed: 0, failed: 0 };
|
|
834
|
+
const batch = [...usageBuffer.values()];
|
|
835
|
+
usageBuffer.clear();
|
|
836
|
+
const br = { runner, detached: true };
|
|
837
|
+
let flushed = 0;
|
|
838
|
+
let failed = 0;
|
|
839
|
+
for (const d of batch) {
|
|
840
|
+
const key = { grant_ID: d.grant_ID, day: d.day, service: d.service, action: d.action };
|
|
841
|
+
const bump = () => runStmt(br, UPDATE.entity(exports.GRANT_USAGE_ENTITY).set({ calls: { '+=': d.calls }, refunded: { '+=': d.refunded } }).where(key));
|
|
842
|
+
try {
|
|
843
|
+
if (Number(await bump()) === 0) {
|
|
844
|
+
try {
|
|
845
|
+
await runStmt(br, INSERT.into(exports.GRANT_USAGE_ENTITY).entries({ ...key, calls: d.calls, refunded: d.refunded }));
|
|
846
|
+
}
|
|
847
|
+
catch (insertErr) {
|
|
848
|
+
// Lost the insert race: the row exists now. Anything else (the row is
|
|
849
|
+
// still missing) is the INSERT's own failure: keep the delta.
|
|
850
|
+
if (Number(await bump()) === 0)
|
|
851
|
+
throw insertErr;
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
flushed++;
|
|
855
|
+
}
|
|
856
|
+
catch (err) {
|
|
857
|
+
failed++;
|
|
858
|
+
bufferUsage(key, d.calls, d.refunded); // retried on the next flush
|
|
859
|
+
logger.warn(`usage flush for grant ${d.grant_ID} (${d.service}.${d.action}) failed, kept for retry: ${String(err?.message ?? err)}`);
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
return { flushed, failed };
|
|
863
|
+
}
|
|
763
864
|
/**
|
|
764
|
-
* One admitted call: `calls + 1` on (grant, UTC day, service, action).
|
|
765
|
-
*
|
|
766
|
-
*
|
|
767
|
-
*
|
|
768
|
-
*
|
|
769
|
-
*
|
|
865
|
+
* One admitted call: `calls + 1` on (grant, UTC day, service, action).
|
|
866
|
+
* Deferred mode (Postgres / HANA, detached runner): the delta goes into the
|
|
867
|
+
* buffer, nothing is written here. Sync mode: a conditional UPDATE first (the
|
|
868
|
+
* common path), an INSERT when the row does not exist yet, one more UPDATE
|
|
869
|
+
* when a racing request inserted it first, AWAITED like every other write of
|
|
870
|
+
* the hook (see touchLastUsed). Never fails the request: usage is accounting,
|
|
871
|
+
* not admission. Returns the day the call was counted on (the refund names it).
|
|
770
872
|
*/
|
|
771
873
|
async function recordGrantUsage(br, grant, service, action, now = new Date()) {
|
|
772
874
|
const runner = 'runner' in br ? br : { runner: br, detached: true };
|
|
773
875
|
const day = utcDay(now);
|
|
774
876
|
const key = { grant_ID: grant.ID, day, service, action };
|
|
877
|
+
if (runner.detached && usageMode() === 'deferred') {
|
|
878
|
+
bufferUsage(key, 1, 0);
|
|
879
|
+
return day;
|
|
880
|
+
}
|
|
775
881
|
const bump = () => runStmt(runner, UPDATE.entity(exports.GRANT_USAGE_ENTITY).set({ calls: { '+=': 1 } }).where(key));
|
|
776
882
|
try {
|
|
777
883
|
if (Number(await bump()) > 0)
|
|
@@ -788,12 +894,21 @@ async function recordGrantUsage(br, grant, service, action, now = new Date()) {
|
|
|
788
894
|
}
|
|
789
895
|
return day;
|
|
790
896
|
}
|
|
791
|
-
/**
|
|
897
|
+
/**
|
|
898
|
+
* `refunded + 1` on the row the call was counted on: a refused request admitted
|
|
899
|
+
* nothing. Deferred mode: into the buffer, on the same key the call went to,
|
|
900
|
+
* whether or not that call has been flushed yet (the flush adds deltas).
|
|
901
|
+
*/
|
|
792
902
|
async function refundGrantUsage(br, grant, service, action, day) {
|
|
793
903
|
const runner = 'runner' in br ? br : { runner: br, detached: true };
|
|
904
|
+
const key = { grant_ID: grant.ID, day, service, action };
|
|
905
|
+
if (runner.detached && usageMode() === 'deferred') {
|
|
906
|
+
bufferUsage(key, 0, 1);
|
|
907
|
+
return;
|
|
908
|
+
}
|
|
794
909
|
await runStmt(runner, UPDATE.entity(exports.GRANT_USAGE_ENTITY)
|
|
795
910
|
.set({ refunded: { '+=': 1 } })
|
|
796
|
-
.where(
|
|
911
|
+
.where(key));
|
|
797
912
|
}
|
|
798
913
|
/**
|
|
799
914
|
* lastUsedAt, at most once a minute per grant. AWAITED, like the budget
|