budget_machines 0.4.4
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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +76 -0
- data/Cargo.lock +307 -0
- data/Cargo.toml +27 -0
- data/LICENSE.txt +21 -0
- data/README.md +306 -0
- data/ext/budget_machines_native/core/CHANGELOG.md +55 -0
- data/ext/budget_machines_native/core/Cargo.toml +17 -0
- data/ext/budget_machines_native/core/README.md +82 -0
- data/ext/budget_machines_native/core/src/ledger.rs +251 -0
- data/ext/budget_machines_native/core/src/lib.rs +180 -0
- data/ext/budget_machines_native/core/src/monitor.rs +424 -0
- data/ext/budget_machines_native/extconf.rb +2 -0
- data/ext/budget_machines_native/ffi/Cargo.toml +21 -0
- data/ext/budget_machines_native/ffi/extconf.rb +14 -0
- data/ext/budget_machines_native/ffi/src/classes.rs +50 -0
- data/ext/budget_machines_native/ffi/src/init.rs +50 -0
- data/ext/budget_machines_native/ffi/src/ledger.rs +117 -0
- data/ext/budget_machines_native/ffi/src/lib.rs +117 -0
- data/ext/budget_machines_native/ffi/src/monitor.rs +216 -0
- data/ext/budget_machines_native/ffi/src/values.rs +226 -0
- data/lib/budget_machines/version.rb +5 -0
- data/lib/budget_machines.rb +158 -0
- data/sig/budget_machines.rbs +183 -0
- metadata +89 -0
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
//! Thin binding to the error-budget ledger and burn-rate monitor.
|
|
2
|
+
mod classes;
|
|
3
|
+
mod init;
|
|
4
|
+
mod ledger;
|
|
5
|
+
mod monitor;
|
|
6
|
+
mod values;
|
|
7
|
+
|
|
8
|
+
use budget_machines::{Counts, Objective, Refusal};
|
|
9
|
+
use magnus::{Error, Ruby, Value, prelude::*};
|
|
10
|
+
use matryona_machines::ruby::{Symbols, exact_fields, integer, symbol};
|
|
11
|
+
|
|
12
|
+
use crate::classes::classes;
|
|
13
|
+
|
|
14
|
+
/// Rules one monitor can hold.
|
|
15
|
+
const CAPACITY: usize = 8;
|
|
16
|
+
|
|
17
|
+
matryona_machines::symbols! {
|
|
18
|
+
/// The core's refusals as Symbols: the snake case of each variant.
|
|
19
|
+
Refusals for Refusal as "a refusal" {
|
|
20
|
+
StalePeriod => "stale_period",
|
|
21
|
+
Overflow => "overflow",
|
|
22
|
+
BelowObserved => "below_observed",
|
|
23
|
+
Full => "full",
|
|
24
|
+
Shape => "shape",
|
|
25
|
+
TimeRegression => "time_regression",
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
fn type_error(ruby: &Ruby, message: &'static str) -> Error {
|
|
30
|
+
Error::new(ruby.exception_type_error(), message)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
fn range_error(ruby: &Ruby, message: &'static str) -> Error {
|
|
34
|
+
Error::new(ruby.exception_range_error(), message)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/// A `BudgetMachines::Objective`, rebuilt through `Objective::new`.
|
|
38
|
+
fn objective(ruby: &Ruby, value: Value) -> Result<Objective, Error> {
|
|
39
|
+
let class = classes(ruby)?.objective.get(ruby);
|
|
40
|
+
let [good, per] =
|
|
41
|
+
exact_fields::<2>(ruby, value, class, "expected a BudgetMachines::Objective")?;
|
|
42
|
+
let good = integer(good, "good")?;
|
|
43
|
+
ratio(ruby, good, integer(per, "per")?)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
fn ratio(ruby: &Ruby, good: u32, per: u32) -> Result<Objective, Error> {
|
|
47
|
+
Objective::new(good, per).ok_or_else(|| {
|
|
48
|
+
range_error(
|
|
49
|
+
ruby,
|
|
50
|
+
"objective needs good < per: a 100% target has no error budget",
|
|
51
|
+
)
|
|
52
|
+
})
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/// A `BudgetMachines::Counts`, rebuilt through `Counts::new`.
|
|
56
|
+
fn counts(ruby: &Ruby, value: Value) -> Result<Counts, Error> {
|
|
57
|
+
let class = classes(ruby)?.counts.get(ruby);
|
|
58
|
+
let [good, total] = exact_fields::<2>(ruby, value, class, "expected a BudgetMachines::Counts")?;
|
|
59
|
+
let good = integer(good, "good")?;
|
|
60
|
+
pair(ruby, good, integer(total, "total")?)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
fn pair(ruby: &Ruby, good: u64, total: u64) -> Result<Counts, Error> {
|
|
64
|
+
Counts::new(good, total).ok_or_else(|| range_error(ruby, "good exceeds total"))
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
fn counts_value(ruby: &Ruby, counts: Counts) -> Result<Value, Error> {
|
|
68
|
+
let class = classes(ruby)?.counts.get(ruby);
|
|
69
|
+
class.funcall("new", (counts.good(), counts.total()))
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
fn objective_value(ruby: &Ruby, objective: Objective) -> Result<Value, Error> {
|
|
73
|
+
let class = classes(ruby)?.objective.get(ruby);
|
|
74
|
+
class.funcall("new", (objective.good(), objective.per()))
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
fn refusal(ruby: &Ruby, refused: Refusal) -> Value {
|
|
78
|
+
Refusals::symbol(ruby, refused).as_value()
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/// `:ok` for an honoured call that answers nothing else, or the refusal.
|
|
82
|
+
fn done(ruby: &Ruby, outcome: Result<(), Refusal>) -> Value {
|
|
83
|
+
outcome.map_or_else(
|
|
84
|
+
|refused| refusal(ruby, refused),
|
|
85
|
+
|()| symbol(ruby, "ok").as_value(),
|
|
86
|
+
)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
fn allowance(ruby: &Ruby, target: Value, total: Value) -> Result<u64, Error> {
|
|
90
|
+
let total = integer(total, "total")?;
|
|
91
|
+
Ok(objective(ruby, target)?.allowance(total))
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
fn burn_milli_of(
|
|
95
|
+
ruby: &Ruby,
|
|
96
|
+
target: Value,
|
|
97
|
+
bad: Value,
|
|
98
|
+
total: Value,
|
|
99
|
+
) -> Result<Option<u64>, Error> {
|
|
100
|
+
let (bad, total) = (integer(bad, "bad")?, integer(total, "total")?);
|
|
101
|
+
Ok(objective(ruby, target)?.burn_milli_of(bad, total))
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
fn burn_milli(ruby: &Ruby, target: Value, window: Value) -> Result<Option<u64>, Error> {
|
|
105
|
+
Ok(objective(ruby, target)?.burn_milli(counts(ruby, window)?))
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
fn burns_at_least(
|
|
109
|
+
ruby: &Ruby,
|
|
110
|
+
target: Value,
|
|
111
|
+
window: Value,
|
|
112
|
+
threshold_milli: Value,
|
|
113
|
+
) -> Result<bool, Error> {
|
|
114
|
+
let threshold_milli = integer(threshold_milli, "threshold_milli")?;
|
|
115
|
+
let (target, window) = (objective(ruby, target)?, counts(ruby, window)?);
|
|
116
|
+
Ok(target.burns_at_least(window, threshold_milli))
|
|
117
|
+
}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
//! `BudgetMachines::Monitor`.
|
|
2
|
+
use std::cell::RefCell;
|
|
3
|
+
|
|
4
|
+
use budget_machines::{Change, OnNoData, Rule, Sample, State, Verdict, Windows};
|
|
5
|
+
use magnus::{Error, Integer, Ruby, Symbol, Value, prelude::*, typed_data::Obj};
|
|
6
|
+
use matryona_machines::ruby::{
|
|
7
|
+
Choice, Symbols, duration, exact_array, exact_fields, integer, keep, nanoseconds,
|
|
8
|
+
symbol_or_fields,
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
use crate::classes::classes;
|
|
12
|
+
use crate::{
|
|
13
|
+
CAPACITY, counts, objective, objective_value, range_error, refusal, type_error,
|
|
14
|
+
values::decision_fields,
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
#[magnus::wrap(class = "BudgetMachines::Monitor", free_immediately, size)]
|
|
18
|
+
pub struct Monitor {
|
|
19
|
+
core: RefCell<budget_machines::Monitor<CAPACITY>>,
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
matryona_machines::symbols! {
|
|
23
|
+
/// A rule's alert states as Symbols.
|
|
24
|
+
pub States for State as "state" {
|
|
25
|
+
NoData => "no_data",
|
|
26
|
+
Quiet => "quiet",
|
|
27
|
+
Firing => "firing",
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
matryona_machines::symbols! {
|
|
32
|
+
/// What one evaluation changed, as Symbols.
|
|
33
|
+
pub Changes for Change as "change" {
|
|
34
|
+
None => "none",
|
|
35
|
+
Raised => "raised",
|
|
36
|
+
Cleared => "cleared",
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
matryona_machines::symbols! {
|
|
41
|
+
/// The no-data policies as Symbols.
|
|
42
|
+
Policies for OnNoData as "policy" {
|
|
43
|
+
Hold => "hold",
|
|
44
|
+
Reset => "reset",
|
|
45
|
+
Fire => "fire",
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
matryona_machines::symbols! {
|
|
50
|
+
/// A window reading as Symbols: `:no_data` is read back, counts travel
|
|
51
|
+
/// as a `Sample::Counts`.
|
|
52
|
+
Samples for Sample as "a window reading" => names_only {
|
|
53
|
+
NoData => "no_data",
|
|
54
|
+
Counts(..) => "counts",
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/// A window reading: `:no_data`, or a `Sample::Counts` wrapping a `Counts`.
|
|
59
|
+
pub fn sample(ruby: &Ruby, value: Value) -> Result<Sample, Error> {
|
|
60
|
+
const MESSAGE: &str = "a window reading is :no_data or a BudgetMachines::Sample::Counts";
|
|
61
|
+
let held = [classes(ruby)?.sample.get(ruby)];
|
|
62
|
+
match symbol_or_fields(ruby, value, Samples, &held, MESSAGE)? {
|
|
63
|
+
Choice::Symbol(sample) => Ok(sample),
|
|
64
|
+
Choice::Struct(_, [window]) => counts(ruby, window).map(Sample::Counts),
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
fn windows(ruby: &Ruby, value: Value) -> Result<Vec<Windows>, Error> {
|
|
69
|
+
let rows = exact_array(ruby, value, "windows must be an Array of Windows")?;
|
|
70
|
+
let class = classes(ruby)?.windows.get(ruby);
|
|
71
|
+
let mut parsed = Vec::with_capacity(rows.len());
|
|
72
|
+
for index in 0..rows.len() {
|
|
73
|
+
let row: Value = isize::try_from(index)
|
|
74
|
+
.map_err(|_| type_error(ruby, "too many windows"))
|
|
75
|
+
.and_then(|index| rows.entry(index))?;
|
|
76
|
+
let message = "each rule needs a BudgetMachines::Windows";
|
|
77
|
+
let [long, short] = exact_fields::<2>(ruby, row, class, message)?;
|
|
78
|
+
parsed.push(Windows {
|
|
79
|
+
long: sample(ruby, long)?,
|
|
80
|
+
short: sample(ruby, short)?,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
Ok(parsed)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/// A rule from its fields, converted in order, through `Rule::new`.
|
|
87
|
+
pub fn checked_rule(
|
|
88
|
+
ruby: &Ruby,
|
|
89
|
+
fire_milli: Value,
|
|
90
|
+
clear_milli: Value,
|
|
91
|
+
severity: Value,
|
|
92
|
+
) -> Result<Rule, Error> {
|
|
93
|
+
let fire_milli = integer(fire_milli, "fire_milli")?;
|
|
94
|
+
let clear_milli = integer(clear_milli, "clear_milli")?;
|
|
95
|
+
Rule::new(fire_milli, clear_milli, integer(severity, "severity")?)
|
|
96
|
+
.ok_or_else(|| range_error(ruby, "thresholds need 0 < clear_milli <= fire_milli"))
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
fn rule(ruby: &Ruby, value: Value) -> Result<Rule, Error> {
|
|
100
|
+
let class = classes(ruby)?.rule.get(ruby);
|
|
101
|
+
let [fire_milli, clear_milli, severity] =
|
|
102
|
+
exact_fields::<3>(ruby, value, class, "expected a BudgetMachines::Rule")?;
|
|
103
|
+
checked_rule(ruby, fire_milli, clear_milli, severity)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/// A `BudgetMachines::Verdict` built from a core verdict, through `new`.
|
|
107
|
+
fn verdict_value(ruby: &Ruby, verdict: &Verdict<CAPACITY>) -> Result<Value, Error> {
|
|
108
|
+
let classes = classes(ruby)?;
|
|
109
|
+
let decision = classes.decision.get(ruby);
|
|
110
|
+
let decisions = ruby.ary_new_capa(verdict.decisions().len());
|
|
111
|
+
for each in verdict.decisions() {
|
|
112
|
+
decisions.push(decision.funcall::<_, _, Value>("new", decision_fields(ruby, each))?)?;
|
|
113
|
+
}
|
|
114
|
+
let worst = verdict.worst();
|
|
115
|
+
classes.verdict.get(ruby).funcall("new", (decisions, worst))
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/// A rule index. One that does not fit in `usize` names no rule.
|
|
119
|
+
fn index(rule: Value) -> Result<usize, Error> {
|
|
120
|
+
let rule: u64 = integer(rule, "index")?;
|
|
121
|
+
Ok(usize::try_from(rule).unwrap_or(usize::MAX))
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
impl Monitor {
|
|
125
|
+
pub fn new(
|
|
126
|
+
ruby: &Ruby,
|
|
127
|
+
target: Value,
|
|
128
|
+
on_no_data: Value,
|
|
129
|
+
min_total: Value,
|
|
130
|
+
) -> Result<Self, Error> {
|
|
131
|
+
let min_total = integer(min_total, "min_total")?;
|
|
132
|
+
let core = budget_machines::Monitor::new(
|
|
133
|
+
objective(ruby, target)?,
|
|
134
|
+
Policies::parse(ruby, on_no_data)?,
|
|
135
|
+
min_total,
|
|
136
|
+
);
|
|
137
|
+
Ok(Self {
|
|
138
|
+
core: RefCell::new(core),
|
|
139
|
+
})
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
pub fn add_rule(ruby: &Ruby, this: Obj<Self>, added: Value) -> Result<Value, Error> {
|
|
143
|
+
this.check_frozen()?;
|
|
144
|
+
let rule = rule(ruby, added)?;
|
|
145
|
+
let outcome = this.core.borrow_mut().add_rule(rule);
|
|
146
|
+
Ok(match outcome {
|
|
147
|
+
Ok(index) => ruby.integer_from_u64(index as u64).as_value(),
|
|
148
|
+
Err(refused) => refusal(ruby, refused),
|
|
149
|
+
})
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
pub fn evaluate(
|
|
153
|
+
ruby: &Ruby,
|
|
154
|
+
this: Obj<Self>,
|
|
155
|
+
now: Value,
|
|
156
|
+
readings: Value,
|
|
157
|
+
) -> Result<Value, Error> {
|
|
158
|
+
this.check_frozen()?;
|
|
159
|
+
let now = duration(now, "now")?;
|
|
160
|
+
// Everything that can raise happens before the core records `now`.
|
|
161
|
+
let readings = windows(ruby, readings)?;
|
|
162
|
+
keep(
|
|
163
|
+
ruby,
|
|
164
|
+
this,
|
|
165
|
+
&this.core,
|
|
166
|
+
|core| core.evaluate(now, &readings),
|
|
167
|
+
|outcome| match outcome {
|
|
168
|
+
Ok(verdict) => verdict_value(ruby, &verdict),
|
|
169
|
+
Err(refused) => Ok(refusal(ruby, refused)),
|
|
170
|
+
},
|
|
171
|
+
)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
pub fn objective(ruby: &Ruby, this: &Self) -> Result<Value, Error> {
|
|
175
|
+
let held = this.core.borrow().objective();
|
|
176
|
+
objective_value(ruby, held)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
pub fn rule(ruby: &Ruby, this: &Self, at: Value) -> Result<Option<Value>, Error> {
|
|
180
|
+
let rule = this.core.borrow().rule(index(at)?);
|
|
181
|
+
let class = classes(ruby)?.rule.get(ruby);
|
|
182
|
+
rule.map(|rule| {
|
|
183
|
+
class.funcall(
|
|
184
|
+
"new",
|
|
185
|
+
(rule.fire_milli(), rule.clear_milli(), rule.severity()),
|
|
186
|
+
)
|
|
187
|
+
})
|
|
188
|
+
.transpose()
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
pub fn state(ruby: &Ruby, this: &Self, at: Value) -> Result<Option<Symbol>, Error> {
|
|
192
|
+
let current = this.core.borrow().state(index(at)?);
|
|
193
|
+
Ok(current.map(|current| States::symbol(ruby, current)))
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
pub fn worst(&self) -> Option<usize> {
|
|
197
|
+
self.core.borrow().worst()
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
pub fn last(ruby: &Ruby, this: &Self) -> Option<Integer> {
|
|
201
|
+
let last = this.core.borrow().last()?;
|
|
202
|
+
Some(nanoseconds(ruby, last))
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
pub fn size(&self) -> usize {
|
|
206
|
+
self.core.borrow().len()
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
pub fn is_empty(&self) -> bool {
|
|
210
|
+
self.core.borrow().is_empty()
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
pub const fn capacity() -> usize {
|
|
214
|
+
CAPACITY
|
|
215
|
+
}
|
|
216
|
+
}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
//! The private hooks every `Data` value validates through at construction, so
|
|
2
|
+
//! a value built by hand is held to the core's constructors exactly like one
|
|
3
|
+
//! the gem returned.
|
|
4
|
+
use std::time::Duration;
|
|
5
|
+
|
|
6
|
+
use budget_machines::{Account, Decision, Standing, Verdict};
|
|
7
|
+
use magnus::{Error, Integer, RArray, Ruby, Symbol, Value, prelude::*};
|
|
8
|
+
use matryona_machines::ruby::{
|
|
9
|
+
Choice, Symbols, exact_array, exact_fields, integer, nanoseconds, optional, symbol_or_fields,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
use crate::classes::classes;
|
|
13
|
+
use crate::monitor::{self, Changes, States};
|
|
14
|
+
use crate::{CAPACITY, counts, counts_value, pair, range_error, ratio};
|
|
15
|
+
|
|
16
|
+
/// What `Account.new` stores, in field order.
|
|
17
|
+
type AccountFields = (u64, Value, u64, u64, Value, Option<u64>);
|
|
18
|
+
|
|
19
|
+
/// What `Decision.new` stores, in field order.
|
|
20
|
+
type DecisionFields = (Symbol, Symbol, Option<u64>, Option<u64>, Option<Integer>);
|
|
21
|
+
|
|
22
|
+
/// A `Decision`'s optional fields as the core takes them, in field order.
|
|
23
|
+
type Marks = (Option<u64>, Option<u64>, Option<Duration>);
|
|
24
|
+
|
|
25
|
+
matryona_machines::symbols! {
|
|
26
|
+
/// Where a period stands, as Symbols: `:no_data` is read back, the
|
|
27
|
+
/// data variants travel as their `Data` classes.
|
|
28
|
+
pub Standings for Standing as "a standing" => names_only {
|
|
29
|
+
NoData => "no_data",
|
|
30
|
+
Within { .. } => "within",
|
|
31
|
+
Exhausted { .. } => "exhausted",
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
fn standing(ruby: &Ruby, value: Value) -> Result<Standing, Error> {
|
|
36
|
+
const MESSAGE: &str = "a standing is :no_data, a Standing::Within or a Standing::Exhausted";
|
|
37
|
+
let classes = classes(ruby)?;
|
|
38
|
+
let held = [classes.within.get(ruby), classes.exhausted.get(ruby)];
|
|
39
|
+
Ok(
|
|
40
|
+
match symbol_or_fields(ruby, value, Standings, &held, MESSAGE)? {
|
|
41
|
+
Choice::Symbol(standing) => standing,
|
|
42
|
+
Choice::Struct(0, [field]) => Standing::Within {
|
|
43
|
+
remaining: integer(field, "remaining")?,
|
|
44
|
+
},
|
|
45
|
+
Choice::Struct(_, [field]) => Standing::Exhausted {
|
|
46
|
+
overspent: integer(field, "overspent")?,
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/// The optional fields of a `Decision`, converted in field order.
|
|
53
|
+
fn marks(long_milli: Value, short_milli: Value, since: Value) -> Result<Marks, Error> {
|
|
54
|
+
let long_milli = optional(long_milli, "long_milli")?;
|
|
55
|
+
let short_milli = optional(short_milli, "short_milli")?;
|
|
56
|
+
let since = optional(since, "since")?.map(Duration::from_nanos);
|
|
57
|
+
Ok((long_milli, short_milli, since))
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/// A core `Decision` from Ruby field values, as `Decision::new` judges them.
|
|
61
|
+
/// The optional fields are converted first, then the two Symbols.
|
|
62
|
+
fn build_decision(
|
|
63
|
+
ruby: &Ruby,
|
|
64
|
+
held_state: Value,
|
|
65
|
+
held_change: Value,
|
|
66
|
+
(long_milli, short_milli, since): Marks,
|
|
67
|
+
) -> Result<Decision, Error> {
|
|
68
|
+
Decision::new(
|
|
69
|
+
States::parse(ruby, held_state)?,
|
|
70
|
+
Changes::parse(ruby, held_change)?,
|
|
71
|
+
long_milli,
|
|
72
|
+
short_milli,
|
|
73
|
+
since,
|
|
74
|
+
)
|
|
75
|
+
.ok_or_else(|| range_error(ruby, "no evaluation produces this decision"))
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/// The fields `Decision.new` takes for a core decision, in order.
|
|
79
|
+
pub fn decision_fields(ruby: &Ruby, decision: &Decision) -> DecisionFields {
|
|
80
|
+
(
|
|
81
|
+
States::symbol(ruby, decision.state()),
|
|
82
|
+
Changes::symbol(ruby, decision.change()),
|
|
83
|
+
decision.long_milli(),
|
|
84
|
+
decision.short_milli(),
|
|
85
|
+
decision.since().map(|since| nanoseconds(ruby, since)),
|
|
86
|
+
)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
fn decision(ruby: &Ruby, value: Value) -> Result<Decision, Error> {
|
|
90
|
+
let class = classes(ruby)?.decision.get(ruby);
|
|
91
|
+
let [state, change, long_milli, short_milli, since] =
|
|
92
|
+
exact_fields::<5>(ruby, value, class, "expected a BudgetMachines::Decision")?;
|
|
93
|
+
let fields = marks(long_milli, short_milli, since)?;
|
|
94
|
+
build_decision(ruby, state, change, fields)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
fn checked_counts(ruby: &Ruby, good: Value, total: Value) -> Result<(u64, u64), Error> {
|
|
98
|
+
let good = integer(good, "good")?;
|
|
99
|
+
pair(ruby, good, integer(total, "total")?).map(|counts| (counts.good(), counts.total()))
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
fn checked_add(ruby: &Ruby, left: Value, right: Value) -> Result<Option<Value>, Error> {
|
|
103
|
+
let sum = counts(ruby, left)?.checked_add(counts(ruby, right)?);
|
|
104
|
+
sum.map(|sum| counts_value(ruby, sum)).transpose()
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
fn checked_objective(ruby: &Ruby, good: Value, per: Value) -> Result<(u32, u32), Error> {
|
|
108
|
+
let good = integer(good, "good")?;
|
|
109
|
+
let objective = ratio(ruby, good, integer(per, "per")?)?;
|
|
110
|
+
Ok((objective.good(), objective.per()))
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
fn checked_rule(
|
|
114
|
+
ruby: &Ruby,
|
|
115
|
+
fire_milli: Value,
|
|
116
|
+
clear_milli: Value,
|
|
117
|
+
severity: Value,
|
|
118
|
+
) -> Result<(u32, u32, u8), Error> {
|
|
119
|
+
let rule = monitor::checked_rule(ruby, fire_milli, clear_milli, severity)?;
|
|
120
|
+
Ok((rule.fire_milli(), rule.clear_milli(), rule.severity()))
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/// What `Standing::Within.new` stores.
|
|
124
|
+
fn checked_remaining(remaining: Value) -> Result<u64, Error> {
|
|
125
|
+
integer(remaining, "remaining")
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/// What `Standing::Exhausted.new` stores.
|
|
129
|
+
fn checked_overspent(overspent: Value) -> Result<u64, Error> {
|
|
130
|
+
integer(overspent, "overspent")
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
fn checked_sample(ruby: &Ruby, window: Value) -> Result<Value, Error> {
|
|
134
|
+
counts(ruby, window)?;
|
|
135
|
+
Ok(window)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
fn checked_windows(ruby: &Ruby, long: Value, short: Value) -> Result<(Value, Value), Error> {
|
|
139
|
+
monitor::sample(ruby, long)?;
|
|
140
|
+
monitor::sample(ruby, short)?;
|
|
141
|
+
Ok((long, short))
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
fn checked_account(
|
|
145
|
+
ruby: &Ruby,
|
|
146
|
+
period: Value,
|
|
147
|
+
window: Value,
|
|
148
|
+
base: Value,
|
|
149
|
+
allowance: Value,
|
|
150
|
+
held: Value,
|
|
151
|
+
consumed_milli: Value,
|
|
152
|
+
) -> Result<AccountFields, Error> {
|
|
153
|
+
let period = integer(period, "period")?;
|
|
154
|
+
let base = integer(base, "base")?;
|
|
155
|
+
let allowance = integer(allowance, "allowance")?;
|
|
156
|
+
let consumed_milli = optional(consumed_milli, "consumed_milli")?;
|
|
157
|
+
let account = Account::new(
|
|
158
|
+
period,
|
|
159
|
+
counts(ruby, window)?,
|
|
160
|
+
base,
|
|
161
|
+
allowance,
|
|
162
|
+
standing(ruby, held)?,
|
|
163
|
+
consumed_milli,
|
|
164
|
+
)
|
|
165
|
+
.ok_or_else(|| range_error(ruby, "no ledger produces this account"))?;
|
|
166
|
+
Ok((
|
|
167
|
+
account.period(),
|
|
168
|
+
window,
|
|
169
|
+
account.base(),
|
|
170
|
+
account.allowance(),
|
|
171
|
+
held,
|
|
172
|
+
account.consumed_milli(),
|
|
173
|
+
))
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
fn checked_decision(
|
|
177
|
+
ruby: &Ruby,
|
|
178
|
+
held_state: Value,
|
|
179
|
+
held_change: Value,
|
|
180
|
+
long_milli: Value,
|
|
181
|
+
short_milli: Value,
|
|
182
|
+
since: Value,
|
|
183
|
+
) -> Result<DecisionFields, Error> {
|
|
184
|
+
let fields = marks(long_milli, short_milli, since)?;
|
|
185
|
+
let decision = build_decision(ruby, held_state, held_change, fields)?;
|
|
186
|
+
Ok(decision_fields(ruby, &decision))
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
fn checked_verdict(
|
|
190
|
+
ruby: &Ruby,
|
|
191
|
+
decisions: Value,
|
|
192
|
+
worst: Value,
|
|
193
|
+
) -> Result<(RArray, Option<u64>), Error> {
|
|
194
|
+
let worst: Option<u64> = optional(worst, "worst")?;
|
|
195
|
+
let rows = exact_array(ruby, decisions, "decisions must be an Array of Decision")?;
|
|
196
|
+
let held = ruby.ary_new_capa(rows.len());
|
|
197
|
+
let mut parsed = Vec::with_capacity(rows.len());
|
|
198
|
+
for index in 0..rows.len() {
|
|
199
|
+
let row: Value = rows.entry(isize::try_from(index).unwrap_or(isize::MAX))?;
|
|
200
|
+
parsed.push(decision(ruby, row)?);
|
|
201
|
+
held.push(row)?;
|
|
202
|
+
}
|
|
203
|
+
let worst = worst.map(|index| usize::try_from(index).unwrap_or(usize::MAX));
|
|
204
|
+
let verdict = Verdict::<CAPACITY>::new(&parsed, worst)
|
|
205
|
+
.ok_or_else(|| range_error(ruby, "no evaluation produces this verdict"))?;
|
|
206
|
+
held.freeze();
|
|
207
|
+
Ok((held, verdict.worst().map(|index| index as u64)))
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/// Define the private hooks on `BudgetMachines`.
|
|
211
|
+
pub fn define(ruby: &Ruby) -> Result<(), Error> {
|
|
212
|
+
use magnus::function;
|
|
213
|
+
let module = ruby.define_module("BudgetMachines")?;
|
|
214
|
+
module.define_singleton_method("__counts", function!(checked_counts, 2))?;
|
|
215
|
+
module.define_singleton_method("__checked_add", function!(checked_add, 2))?;
|
|
216
|
+
module.define_singleton_method("__objective", function!(checked_objective, 2))?;
|
|
217
|
+
module.define_singleton_method("__rule", function!(checked_rule, 3))?;
|
|
218
|
+
module.define_singleton_method("__remaining", function!(checked_remaining, 1))?;
|
|
219
|
+
module.define_singleton_method("__overspent", function!(checked_overspent, 1))?;
|
|
220
|
+
module.define_singleton_method("__sample", function!(checked_sample, 1))?;
|
|
221
|
+
module.define_singleton_method("__windows", function!(checked_windows, 2))?;
|
|
222
|
+
module.define_singleton_method("__account", function!(checked_account, 6))?;
|
|
223
|
+
module.define_singleton_method("__decision", function!(checked_decision, 5))?;
|
|
224
|
+
module.define_singleton_method("__verdict", function!(checked_verdict, 2))?;
|
|
225
|
+
Ok(())
|
|
226
|
+
}
|