@voxgig/sdkgen 4.2.5 → 4.2.7
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/bin/voxgig-sdkgen +1 -1
- package/dist/helpers/naming.js +5 -4
- package/dist/helpers/naming.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/project/.sdk/model/feature/cost.aon +43 -0
- package/project/.sdk/model/feature/feature-index.aon +1 -0
- package/project/.sdk/src/cmp/go/utility_go.ts +20 -7
- package/project/.sdk/src/cmp/js/fragment/EntityCreateOp.fragment.js +2 -1
- package/project/.sdk/src/cmp/js/fragment/EntityListOp.fragment.js +2 -1
- package/project/.sdk/src/cmp/js/fragment/EntityLoadOp.fragment.js +2 -1
- package/project/.sdk/src/cmp/js/fragment/EntityRemoveOp.fragment.js +2 -1
- package/project/.sdk/src/cmp/js/fragment/EntityUpdateOp.fragment.js +2 -1
- package/project/.sdk/src/cmp/js/utility_js.ts +20 -7
- package/project/.sdk/src/cmp/php/ReadmeExamplesTest_php.ts +144 -12
- package/project/.sdk/src/cmp/py/ReadmeExamplesTest_py.ts +98 -3
- package/project/.sdk/src/cmp/rb/ReadmeExamplesTest_rb.ts +83 -10
- package/project/.sdk/src/cmp/ts/fragment/EntityCreateOp.fragment.ts +2 -1
- package/project/.sdk/src/cmp/ts/fragment/EntityListOp.fragment.ts +2 -1
- package/project/.sdk/src/cmp/ts/fragment/EntityLoadOp.fragment.ts +2 -1
- package/project/.sdk/src/cmp/ts/fragment/EntityRemoveOp.fragment.ts +2 -1
- package/project/.sdk/src/cmp/ts/fragment/EntityUpdateOp.fragment.ts +2 -1
- package/project/.sdk/src/cmp/ts/utility_ts.ts +20 -7
- package/project/.sdk/tm/c/core/sdk.h +1 -0
- package/project/.sdk/tm/c/feature/cost.c +549 -0
- package/project/.sdk/tm/c/src/feature/cost/.gitkeep +0 -0
- package/project/.sdk/tm/clojure/src/feature/cost/.gitkeep +0 -0
- package/project/.sdk/tm/cpp/feature/cost.hpp +419 -0
- package/project/.sdk/tm/cpp/src/feature/cost/.gitkeep +0 -0
- package/project/.sdk/tm/csharp/feature/CostFeature.cs +518 -0
- package/project/.sdk/tm/csharp/src/feature/cost/.gitkeep +0 -0
- package/project/.sdk/tm/dart/lib/feature/cost/CostFeature.dart +446 -0
- package/project/.sdk/tm/dart/src/feature/cost/.gitkeep +0 -0
- package/project/.sdk/tm/elixir/lib/projectname/feature/cost.ex +442 -0
- package/project/.sdk/tm/elixir/src/feature/cost/.gitkeep +0 -0
- package/project/.sdk/tm/go/feature/cost_feature.go +424 -0
- package/project/.sdk/tm/go/src/feature/cost/.gitkeep +0 -0
- package/project/.sdk/tm/go-cli/src/feature/cost/.gitkeep +0 -0
- package/project/.sdk/tm/go-mcp/src/feature/cost/.gitkeep +0 -0
- package/project/.sdk/tm/java/feature/CostFeature.java +431 -0
- package/project/.sdk/tm/java/src/feature/cost/.gitkeep +0 -0
- package/project/.sdk/tm/js/src/feature/cost/CostFeature.js +413 -0
- package/project/.sdk/tm/js/test/feature/Corpus.test.js +264 -0
- package/project/.sdk/tm/kotlin/feature/CostFeature.kt +430 -0
- package/project/.sdk/tm/kotlin/src/feature/cost/.gitkeep +0 -0
- package/project/.sdk/tm/lean/src/feature/cost/.gitkeep +0 -0
- package/project/.sdk/tm/lua/feature/cost_feature.lua +457 -0
- package/project/.sdk/tm/lua/src/feature/cost/.gitkeep +0 -0
- package/project/.sdk/tm/ocaml/src/feature/cost/.gitkeep +0 -0
- package/project/.sdk/tm/perl/feature/cost_feature.pm +446 -0
- package/project/.sdk/tm/perl/src/feature/cost/.gitkeep +0 -0
- package/project/.sdk/tm/php/feature/CostFeature.php +439 -0
- package/project/.sdk/tm/php/feature/TestFeature.php +31 -1
- package/project/.sdk/tm/php/src/feature/cost/.gitkeep +0 -0
- package/project/.sdk/tm/py/pkg/feature/cost_feature.py +381 -0
- package/project/.sdk/tm/py/src/feature/cost/.gitkeep +0 -0
- package/project/.sdk/tm/py-data/src/feature/cost/.gitkeep +0 -0
- package/project/.sdk/tm/rb/feature/cost_feature.rb +386 -0
- package/project/.sdk/tm/rb/src/feature/cost/.gitkeep +0 -0
- package/project/.sdk/tm/rust/feature/cost.rs +467 -0
- package/project/.sdk/tm/rust/src/feature/cost/.gitkeep +0 -0
- package/project/.sdk/tm/scala/feature/CostFeature.scala +388 -0
- package/project/.sdk/tm/scala/src/feature/cost/.gitkeep +0 -0
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/CostFeature.swift +425 -0
- package/project/.sdk/tm/swift/src/feature/cost/.gitkeep +0 -0
- package/project/.sdk/tm/ts/src/feature/cost/CostFeature.ts +415 -0
- package/project/.sdk/tm/ts/test/feature/Corpus.test.ts +266 -0
- package/project/.sdk/tm/zig/feature/cost.zig +457 -0
- package/project/.sdk/tm/zig/src/feature/cost/.gitkeep +0 -0
- package/project/sdkgen-package.json +2 -1
- package/src/helpers/naming.ts +5 -4
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
// ignore_for_file: non_constant_identifier_names
|
|
2
|
+
|
|
3
|
+
import '../../utility/voxgig_struct.dart' as vs;
|
|
4
|
+
|
|
5
|
+
import '../base/BaseFeature.dart';
|
|
6
|
+
|
|
7
|
+
// Cost tracking and spend budget. Uses BOTH seams, which is the point of
|
|
8
|
+
// the feature: money is spent per HTTP ATTEMPT (a retried call is charged
|
|
9
|
+
// again, because the upstream API charges it again), but it is owed by an
|
|
10
|
+
// OPERATION. So the transport wrap prices each attempt, and PreDone
|
|
11
|
+
// attributes the running total to `<entity>.<op>` and to the caller
|
|
12
|
+
// (`ctrl.actor`, the same actor the audit feature records).
|
|
13
|
+
//
|
|
14
|
+
// The price of an attempt comes from the first source that answers: a
|
|
15
|
+
// response header (`header` x `perUnit`), the rate table (`rates`, keyed
|
|
16
|
+
// '<entity>.<op>' / '<op>' / '*'), then the flat `unit`. A body figure
|
|
17
|
+
// (`path` x `perUnit`, e.g. 'usage.total_tokens') is read at PreDone
|
|
18
|
+
// instead, from the already-parsed result. A body figure describes the
|
|
19
|
+
// whole call, so it REPLACES the per-attempt estimate rather than adding
|
|
20
|
+
// to it.
|
|
21
|
+
//
|
|
22
|
+
// `budget` caps total spend. With `onBudget` = 'deny' a further operation
|
|
23
|
+
// is refused at PrePoint, before an endpoint is resolved and before
|
|
24
|
+
// anything reaches the network.
|
|
25
|
+
//
|
|
26
|
+
// ORDER MATTERS. Cost must sit INSIDE the cache, or a response served from
|
|
27
|
+
// cache is charged for money that was never spent. Aggregates live on the
|
|
28
|
+
// client track (`cost`).
|
|
29
|
+
class CostFeature extends BaseFeature {
|
|
30
|
+
dynamic _client;
|
|
31
|
+
final Map<dynamic, dynamic> _pending = {};
|
|
32
|
+
int _seq = 0;
|
|
33
|
+
|
|
34
|
+
CostFeature() {
|
|
35
|
+
version = '0.0.1';
|
|
36
|
+
name = 'cost';
|
|
37
|
+
active = true;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
@override
|
|
41
|
+
dynamic init(dynamic ctx, dynamic opts) {
|
|
42
|
+
_client = ctx.client;
|
|
43
|
+
options = opts is Map ? Map<String, dynamic>.from(opts) : {};
|
|
44
|
+
active = true == options['active'];
|
|
45
|
+
_pending.clear();
|
|
46
|
+
_seq = 0;
|
|
47
|
+
|
|
48
|
+
final limit = _limit();
|
|
49
|
+
final track = _client.track;
|
|
50
|
+
if (null == track['cost']) {
|
|
51
|
+
track['cost'] = <String, dynamic>{
|
|
52
|
+
'currency': options['currency'] ?? 'USD',
|
|
53
|
+
'total': <String, dynamic>{
|
|
54
|
+
'calls': 0,
|
|
55
|
+
'attempts': 0,
|
|
56
|
+
'amount': 0,
|
|
57
|
+
'reported': 0,
|
|
58
|
+
'estimated': 0,
|
|
59
|
+
},
|
|
60
|
+
'ops': <String, dynamic>{},
|
|
61
|
+
'actors': <String, dynamic>{},
|
|
62
|
+
'budget': <String, dynamic>{
|
|
63
|
+
'limit': limit,
|
|
64
|
+
'spent': 0,
|
|
65
|
+
'remaining': limit,
|
|
66
|
+
'exceeded': false,
|
|
67
|
+
},
|
|
68
|
+
'last': null,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (!active) {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
final self = this;
|
|
77
|
+
final utility = ctx.utility;
|
|
78
|
+
final inner = utility.fetcher;
|
|
79
|
+
|
|
80
|
+
utility.fetcher = (dynamic ctx2, dynamic url, dynamic fetchdef) async {
|
|
81
|
+
return self._charge(ctx2, url, fetchdef, inner);
|
|
82
|
+
};
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Budget gate. Runs before endpoint resolution, so a refused call costs
|
|
87
|
+
// nothing at all.
|
|
88
|
+
@override
|
|
89
|
+
dynamic PrePoint(dynamic ctx) {
|
|
90
|
+
if (!active) {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Mark the context as running through the pipeline, so _charge knows a
|
|
95
|
+
// PreDone is coming and does not commit the spend itself.
|
|
96
|
+
var pending = _pending[ctx];
|
|
97
|
+
if (null == pending) {
|
|
98
|
+
pending = _pending[ctx] = _newPending();
|
|
99
|
+
}
|
|
100
|
+
pending['piped'] = true;
|
|
101
|
+
|
|
102
|
+
final limit = _limit();
|
|
103
|
+
if (0 >= limit) {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
final cost = _client.track['cost'];
|
|
108
|
+
if (cost['total']['amount'] < limit) {
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
cost['budget']['exceeded'] = true;
|
|
113
|
+
|
|
114
|
+
if ('deny' != options['onBudget']) {
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
final err = ctx.error(
|
|
119
|
+
'cost_budget',
|
|
120
|
+
'Cost budget of ' +
|
|
121
|
+
limit.toString() +
|
|
122
|
+
' ' +
|
|
123
|
+
cost['currency'].toString() +
|
|
124
|
+
' is spent (' +
|
|
125
|
+
cost['total']['amount'].toString() +
|
|
126
|
+
' ' +
|
|
127
|
+
cost['currency'].toString() +
|
|
128
|
+
' used)');
|
|
129
|
+
// Short-circuit endpoint resolution; the pipeline surfaces this error.
|
|
130
|
+
ctx.out['point'] = err;
|
|
131
|
+
return err;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
Future<dynamic> _charge(
|
|
135
|
+
dynamic ctx, dynamic url, dynamic fetchdef, dynamic inner) async {
|
|
136
|
+
dynamic res;
|
|
137
|
+
var threw = false;
|
|
138
|
+
|
|
139
|
+
// A throwing transport still costs an attempt. Without this, a run of
|
|
140
|
+
// connection-level failures under `retry` (which catches the throw and
|
|
141
|
+
// tries again) would be charged nothing at all, and an onBudget =
|
|
142
|
+
// 'deny' ceiling could never stop it.
|
|
143
|
+
try {
|
|
144
|
+
res = await Future.value(inner(ctx, url, fetchdef));
|
|
145
|
+
} catch (err) {
|
|
146
|
+
threw = true;
|
|
147
|
+
res = err;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
final priced = _price(ctx, threw ? null : res);
|
|
151
|
+
final amount = priced['amount'];
|
|
152
|
+
final source = priced['source'];
|
|
153
|
+
|
|
154
|
+
final cost = _client.track['cost'];
|
|
155
|
+
|
|
156
|
+
var pending = _pending[ctx];
|
|
157
|
+
if (null == pending) {
|
|
158
|
+
pending = _pending[ctx] = _newPending();
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
pending['attempts'] = pending['attempts'] + 1;
|
|
162
|
+
|
|
163
|
+
// Accumulated here, committed once at PreDone. Adding each attempt to
|
|
164
|
+
// the running total and then subtracting it again when a body figure
|
|
165
|
+
// supersedes it loses precision to catastrophic cancellation
|
|
166
|
+
// (5 + (0.01 - 5) is not 0.01 in binary floating point).
|
|
167
|
+
//
|
|
168
|
+
// Reported and estimated are kept apart per ATTEMPT, not per operation:
|
|
169
|
+
// a 503 priced from the rate table followed by a 200 carrying the cost
|
|
170
|
+
// header is part estimate, part reported, and collapsing both into the
|
|
171
|
+
// final attempt's category would corrupt the split.
|
|
172
|
+
pending['amount'] = pending['amount'] + amount;
|
|
173
|
+
final bucket = ('header' == source || 'body' == source) ? 'reported' : 'estimated';
|
|
174
|
+
pending[bucket] = pending[bucket] + amount;
|
|
175
|
+
pending['source'] = source;
|
|
176
|
+
|
|
177
|
+
cost['total']['attempts'] = cost['total']['attempts'] + 1;
|
|
178
|
+
|
|
179
|
+
// direct() and graphql() reach the transport without dispatching any
|
|
180
|
+
// pipeline hooks - no PrePoint to gate on, and no PreDone to commit.
|
|
181
|
+
// Their spend is committed here instead, or it would never be counted
|
|
182
|
+
// and could run past an onBudget = 'deny' ceiling indefinitely.
|
|
183
|
+
// `piped` is set by PrePoint, so its absence is the signal.
|
|
184
|
+
if (true != pending['piped']) {
|
|
185
|
+
_commit(ctx, pending, '_', 'direct');
|
|
186
|
+
_pending.remove(ctx);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (threw) {
|
|
190
|
+
throw res;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return res;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
Map<String, dynamic> _newPending() {
|
|
197
|
+
return <String, dynamic>{
|
|
198
|
+
'attempts': 0,
|
|
199
|
+
'amount': 0,
|
|
200
|
+
'reported': 0,
|
|
201
|
+
'estimated': 0,
|
|
202
|
+
'source': 'none',
|
|
203
|
+
'piped': false,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// Attribute the operation's spend once the call is finished.
|
|
208
|
+
@override
|
|
209
|
+
dynamic PreDone(dynamic ctx) {
|
|
210
|
+
_finish(ctx, true);
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// A failed operation still spent the money. When the pipeline throws,
|
|
215
|
+
// PreDone never runs, so without this the attempts are counted and the
|
|
216
|
+
// spend is not, and a budget could never see the cost of a failed call.
|
|
217
|
+
// Whichever hook fires first consumes the pending entry, so it commits
|
|
218
|
+
// exactly once.
|
|
219
|
+
@override
|
|
220
|
+
dynamic PreUnexpected(dynamic ctx) {
|
|
221
|
+
_finish(ctx, false);
|
|
222
|
+
return null;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
void _finish(dynamic ctx, bool done) {
|
|
226
|
+
if (!active) {
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
if (!_pending.containsKey(ctx)) {
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
final pending = _pending[ctx];
|
|
233
|
+
_pending.remove(ctx);
|
|
234
|
+
|
|
235
|
+
// A FAILED operation that made no attempt never reached the network:
|
|
236
|
+
// PrePoint creates the pending entry to mark the context as piped, and
|
|
237
|
+
// then the budget gate refuses the call (rbac, or an unresolvable
|
|
238
|
+
// endpoint, short-circuits just as early). Committing it would count a
|
|
239
|
+
// call that never happened and file a zero-amount record as `last`.
|
|
240
|
+
//
|
|
241
|
+
// A SUCCEEDED operation that made no attempt is the opposite case: it
|
|
242
|
+
// was served from the cache. That is a real call, and the fact that it
|
|
243
|
+
// cost nothing is the whole point of ordering cost inside the cache.
|
|
244
|
+
if (!done && 0 == pending['attempts']) {
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
final entity = (null == ctx.op ? '_' : (ctx.op.entity ?? '_')).toString();
|
|
249
|
+
final opname = (null == ctx.op ? '_' : (ctx.op.name ?? '_')).toString();
|
|
250
|
+
|
|
251
|
+
_commit(ctx, pending, entity, opname);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Commit one operation's spend: totals, budget, per-op and per-actor
|
|
255
|
+
// attribution, and the record. Shared by _finish and the raw-request path
|
|
256
|
+
// in _charge, which has no PreDone to reach.
|
|
257
|
+
void _commit(dynamic ctx, dynamic pending, String entity, String opname) {
|
|
258
|
+
final cost = _client.track['cost'];
|
|
259
|
+
|
|
260
|
+
var amount = pending['amount'];
|
|
261
|
+
var reported = pending['reported'];
|
|
262
|
+
var estimated = pending['estimated'];
|
|
263
|
+
var source = pending['source'];
|
|
264
|
+
|
|
265
|
+
// A body figure prices the whole call, so it replaces the per-attempt
|
|
266
|
+
// estimate rather than adding to it - and, being server-stated, the
|
|
267
|
+
// whole amount counts as reported.
|
|
268
|
+
final body = _body(ctx);
|
|
269
|
+
if (null != body) {
|
|
270
|
+
amount = body;
|
|
271
|
+
reported = body;
|
|
272
|
+
estimated = 0;
|
|
273
|
+
source = 'body';
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
_spend(cost, amount, reported, estimated);
|
|
277
|
+
|
|
278
|
+
final actor = (ctx.ctrl is Map ? ctx.ctrl['actor'] : null) ??
|
|
279
|
+
options['actor'] ??
|
|
280
|
+
'anonymous';
|
|
281
|
+
|
|
282
|
+
cost['total']['calls'] = cost['total']['calls'] + 1;
|
|
283
|
+
_bump(cost['ops'], entity + '.' + opname, amount);
|
|
284
|
+
_bump(cost['actors'], actor.toString(), amount);
|
|
285
|
+
|
|
286
|
+
_seq = _seq + 1;
|
|
287
|
+
final record = <String, dynamic>{
|
|
288
|
+
'seq': _seq,
|
|
289
|
+
'entity': entity,
|
|
290
|
+
'op': opname,
|
|
291
|
+
'actor': actor,
|
|
292
|
+
'amount': amount,
|
|
293
|
+
'currency': cost['currency'],
|
|
294
|
+
'source': source,
|
|
295
|
+
'attempts': pending['attempts'],
|
|
296
|
+
};
|
|
297
|
+
cost['last'] = record;
|
|
298
|
+
|
|
299
|
+
final sink = options['sink'];
|
|
300
|
+
if (sink is Function) {
|
|
301
|
+
// A sink must never break the call it is reporting on.
|
|
302
|
+
try {
|
|
303
|
+
sink(record);
|
|
304
|
+
} catch (_e) {}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// Price one attempt: a reported header figure, else the rate table, else
|
|
309
|
+
// the flat unit.
|
|
310
|
+
Map<String, dynamic> _price(dynamic ctx, dynamic res) {
|
|
311
|
+
final header = options['header'];
|
|
312
|
+
if (header is String && '' != header) {
|
|
313
|
+
final v = _header(res, header);
|
|
314
|
+
if (null != v) {
|
|
315
|
+
return <String, dynamic>{'amount': v * _perUnit(), 'source': 'header'};
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
final rate = _rate(ctx);
|
|
320
|
+
if (null != rate) {
|
|
321
|
+
return <String, dynamic>{'amount': rate, 'source': 'table'};
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
final unit = options['unit'];
|
|
325
|
+
if (unit is num && 0 != unit) {
|
|
326
|
+
return <String, dynamic>{'amount': unit, 'source': 'unit'};
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
return <String, dynamic>{'amount': 0, 'source': 'none'};
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// The rate table uses the same lookup grammar as rbac's rules:
|
|
333
|
+
// '<entity>.<op>', then '<op>', then '*'.
|
|
334
|
+
num? _rate(dynamic ctx) {
|
|
335
|
+
final rates = options['rates'] ?? {};
|
|
336
|
+
|
|
337
|
+
var entity = _entname(ctx.entity);
|
|
338
|
+
if ('' == entity) {
|
|
339
|
+
entity = (null == ctx.op ? '' : (ctx.op.entity ?? '')).toString();
|
|
340
|
+
}
|
|
341
|
+
final opname = (null == ctx.op ? '' : (ctx.op.name ?? '')).toString();
|
|
342
|
+
|
|
343
|
+
for (final key in [entity + '.' + opname, opname, '*']) {
|
|
344
|
+
final r = vs.getprop(rates, key);
|
|
345
|
+
if (r is num) {
|
|
346
|
+
return r;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
return null;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// A usage figure from the parsed result body, priced by perUnit. Read
|
|
353
|
+
// here, not at the transport seam, because the body is consumed once.
|
|
354
|
+
num? _body(dynamic ctx) {
|
|
355
|
+
final path = options['path'];
|
|
356
|
+
if (path is! String || '' == path) {
|
|
357
|
+
return null;
|
|
358
|
+
}
|
|
359
|
+
if (null == ctx.result) {
|
|
360
|
+
return null;
|
|
361
|
+
}
|
|
362
|
+
final body = ctx.result.body;
|
|
363
|
+
if (body is! Map) {
|
|
364
|
+
return null;
|
|
365
|
+
}
|
|
366
|
+
final v = vs.getpath(body, path);
|
|
367
|
+
final n = v is num ? v : num.tryParse((v ?? '').toString());
|
|
368
|
+
if (null == n) {
|
|
369
|
+
return null;
|
|
370
|
+
}
|
|
371
|
+
return n * _perUnit();
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
void _spend(dynamic cost, num amount, num reported, num estimated) {
|
|
375
|
+
final total = cost['total'];
|
|
376
|
+
total['amount'] = total['amount'] + amount;
|
|
377
|
+
total['reported'] = total['reported'] + reported;
|
|
378
|
+
total['estimated'] = total['estimated'] + estimated;
|
|
379
|
+
|
|
380
|
+
final budget = cost['budget'];
|
|
381
|
+
final limit = budget['limit'];
|
|
382
|
+
budget['spent'] = total['amount'];
|
|
383
|
+
if (0 < limit) {
|
|
384
|
+
final left = limit - total['amount'];
|
|
385
|
+
budget['remaining'] = left < 0 ? 0 : left;
|
|
386
|
+
if (total['amount'] >= limit) {
|
|
387
|
+
budget['exceeded'] = true;
|
|
388
|
+
}
|
|
389
|
+
} else {
|
|
390
|
+
budget['remaining'] = 0;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
void _bump(dynamic bucket, String key, num amount) {
|
|
395
|
+
var b = bucket[key];
|
|
396
|
+
if (null == b) {
|
|
397
|
+
b = bucket[key] = <String, dynamic>{'calls': 0, 'amount': 0};
|
|
398
|
+
}
|
|
399
|
+
b['calls'] = b['calls'] + 1;
|
|
400
|
+
b['amount'] = b['amount'] + amount;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// HTTP header names are case-insensitive and a custom transport keeps
|
|
404
|
+
// conventional casing ('X-Request-Cost'), so scan rather than index.
|
|
405
|
+
num? _header(dynamic res, String name) {
|
|
406
|
+
if (null == res) {
|
|
407
|
+
return null;
|
|
408
|
+
}
|
|
409
|
+
final headers = vs.getprop(res, 'headers');
|
|
410
|
+
if (headers is! Map) {
|
|
411
|
+
return null;
|
|
412
|
+
}
|
|
413
|
+
final lower = name.toLowerCase();
|
|
414
|
+
for (final k in headers.keys) {
|
|
415
|
+
if (k.toString().toLowerCase() == lower) {
|
|
416
|
+
final v = headers[k];
|
|
417
|
+
return v is num ? v : num.tryParse(v.toString());
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
return null;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
String _entname(dynamic ent) {
|
|
424
|
+
if (null == ent) {
|
|
425
|
+
return '';
|
|
426
|
+
}
|
|
427
|
+
if (ent is Map) {
|
|
428
|
+
return (vs.getprop(ent, 'name', '') ?? '').toString();
|
|
429
|
+
}
|
|
430
|
+
try {
|
|
431
|
+
return (ent.name ?? '').toString();
|
|
432
|
+
} catch (_e) {
|
|
433
|
+
return '';
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
num _perUnit() {
|
|
438
|
+
final p = options['perUnit'];
|
|
439
|
+
return p is num ? p : 0;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
num _limit() {
|
|
443
|
+
final b = options['budget'];
|
|
444
|
+
return b is num ? b : 0;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
File without changes
|