@mark1russell7/capacity 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +1108 -2
- package/build/index.d.ts +858 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +1713 -0
- package/build/index.js.map +1 -0
- package/build/instance-0n6WhJzF.d.ts +782 -0
- package/build/instance-0n6WhJzF.d.ts.map +1 -0
- package/build/instance-check-CAuD9L4Z.js +1711 -0
- package/build/instance-check-CAuD9L4Z.js.map +1 -0
- package/build/schema.d.ts +69 -0
- package/build/schema.d.ts.map +1 -0
- package/build/schema.js +315 -0
- package/build/schema.js.map +1 -0
- package/limits/alerts-routes.line.json +78 -0
- package/limits/alerts-scale.limits.json +10 -0
- package/limits/alerts-start-gate.limits.json +1149 -0
- package/limits/alerts-start-routes-limit-home.bracket.json +543 -0
- package/limits/alerts-start-routes-limit-mid.bracket.json +464 -0
- package/limits/alerts-start-routes-limit.bracket.json +579 -0
- package/limits/alerts-tail-gate.limits.json +610 -0
- package/package.json +85 -3
- package/src/app-check.ts +477 -0
- package/src/app-metrics.ts +105 -0
- package/src/app.ts +239 -0
- package/src/check.ts +228 -0
- package/src/composition.ts +680 -0
- package/src/demand.ts +312 -0
- package/src/fit.ts +153 -0
- package/src/forms.ts +358 -0
- package/src/gate.ts +237 -0
- package/src/index.ts +142 -0
- package/src/instance-check.ts +122 -0
- package/src/instance.ts +84 -0
- package/src/knee.ts +606 -0
- package/src/law.ts +215 -0
- package/src/limit.ts +112 -0
- package/src/names.ts +19 -0
- package/src/observe.ts +74 -0
- package/src/program.ts +74 -0
- package/src/scale.ts +617 -0
- package/src/schema.ts +260 -0
- package/src/value.ts +206 -0
- package/src/workload-check.ts +130 -0
- package/src/workload.ts +88 -0
package/src/law.ts
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import type { Quantity } from "@mark1russell7/quantity";
|
|
2
|
+
import type { Expr } from "@mark1russell7/vex";
|
|
3
|
+
|
|
4
|
+
/** This text is the format of a law file. */
|
|
5
|
+
export const LAW_FORMAT = "capacity-law/1";
|
|
6
|
+
|
|
7
|
+
/** This number is the most variables that a law can have. The check reads each corner of their ranges. */
|
|
8
|
+
export const MAX_VARIABLES = 8;
|
|
9
|
+
|
|
10
|
+
/** A scale tells how a variable moves through its range: in equal steps or in equal ratios. */
|
|
11
|
+
export type Scale = "linear" | "log";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A measure is the quantity that a law gives. Its unit is a canonical unit of
|
|
15
|
+
* `@mark1russell7/quantity`, for example `{core}·ms/{request}` or `MiB`.
|
|
16
|
+
*
|
|
17
|
+
* CPU time is in core-seconds. Thus a request rate times a CPU cost of one request converts to
|
|
18
|
+
* `{core}`. That unit is the unit of the CPU of an instance.
|
|
19
|
+
*/
|
|
20
|
+
export interface Measure {
|
|
21
|
+
readonly id: string;
|
|
22
|
+
readonly unit: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A variable is one load variable of a law. The law holds from `min` to `max`, in the unit of the
|
|
27
|
+
* variable. A value outside this range is an extrapolation.
|
|
28
|
+
*/
|
|
29
|
+
export interface Variable {
|
|
30
|
+
readonly id: string;
|
|
31
|
+
readonly unit: string;
|
|
32
|
+
readonly min: number;
|
|
33
|
+
readonly max: number;
|
|
34
|
+
readonly scale: Scale;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** A constant is one fitted number of a law. The program of the form reads it by its `id`. */
|
|
38
|
+
export interface Constant {
|
|
39
|
+
readonly id: string;
|
|
40
|
+
readonly value: number;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* A form is the math of a law. Its program is a Vex expression that reads each variable and each
|
|
45
|
+
* constant by its id. Its `id` names the form, for example `hinge`.
|
|
46
|
+
*/
|
|
47
|
+
export interface Form {
|
|
48
|
+
readonly id: string;
|
|
49
|
+
readonly program: Expr;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** A condition value is a fact that a law holds constant: a name, a number, a flag or an amount with a unit. */
|
|
53
|
+
export type ConditionValue = string | number | boolean | Quantity;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* A residual kind tells how a fit compares a measured value with a law. An absolute residual is
|
|
57
|
+
* their difference. A relative residual is their ratio less 1.
|
|
58
|
+
*/
|
|
59
|
+
export type ResidualKind = "absolute" | "relative";
|
|
60
|
+
|
|
61
|
+
/** A fit error sums the residuals of a form: the mean of their sizes, and the root of the mean of their squares. */
|
|
62
|
+
export interface FitError {
|
|
63
|
+
readonly mae: number;
|
|
64
|
+
readonly rmse: number;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** A loser is a form that the fit also tried and did not keep, with its error. */
|
|
68
|
+
export interface Loser {
|
|
69
|
+
readonly form: string;
|
|
70
|
+
readonly error: FitError;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* A fit tells how well a law agrees with its points. Each residual compares one measured point
|
|
75
|
+
* with a fit that did not use that point. The list `losers` keeps each form that lost.
|
|
76
|
+
*/
|
|
77
|
+
export interface Fit {
|
|
78
|
+
readonly method: string;
|
|
79
|
+
readonly residual: ResidualKind;
|
|
80
|
+
readonly residuals: readonly number[];
|
|
81
|
+
readonly losers: readonly Loser[];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** A run is one CI run that measured points of a law: its address, its commit and its time. */
|
|
85
|
+
export interface Run {
|
|
86
|
+
readonly url: string;
|
|
87
|
+
readonly commit: string;
|
|
88
|
+
readonly date: string;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* A run point is one point of the fit of a law. Its load gives the value of each variable of the law, in the unit of the
|
|
93
|
+
* variable. Its value is the value of the measure, in the unit of the measure.
|
|
94
|
+
*/
|
|
95
|
+
export interface RunPoint {
|
|
96
|
+
readonly load: Readonly<Record<string, number>>;
|
|
97
|
+
readonly value: number;
|
|
98
|
+
/** This number is the index of the run of the point in `evidence.runs`. */
|
|
99
|
+
readonly run: number;
|
|
100
|
+
/** This text is the start time of the job of the point, as an ISO 8601 time. A run can have more than one job. */
|
|
101
|
+
readonly job: string;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** The evidence of a law is the list of the runs that measured its points. */
|
|
105
|
+
export interface Evidence {
|
|
106
|
+
readonly runs: readonly Run[];
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* The noise of a law is the noise floor of its measure on one CPU model. A difference of two
|
|
111
|
+
* measurements below the floor is not a result. Thus a gate does not read it as a change.
|
|
112
|
+
*/
|
|
113
|
+
export interface Noise {
|
|
114
|
+
/**
|
|
115
|
+
* This amount is the noise floor, in the unit of the measure. It is two standard deviations of
|
|
116
|
+
* the differences of A/A pairs: two equal cases, each in its own container.
|
|
117
|
+
*/
|
|
118
|
+
readonly floor: number;
|
|
119
|
+
/** This name is the CPU model of the runs that measured the floor. */
|
|
120
|
+
readonly cpu: string;
|
|
121
|
+
/** These runs measured the floor. They are not always the runs of the evidence. */
|
|
122
|
+
readonly runs: readonly Run[];
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* A law gives one measure of a subject as a function of its load variables. It holds for the
|
|
127
|
+
* ranges of its variables and for its conditions.
|
|
128
|
+
*
|
|
129
|
+
* Each row of `replicates` is one more fit of the constants, in the order of `constants`. The rows
|
|
130
|
+
* come from resamples of the points. Thus the spread of the rows is the doubt of the constants
|
|
131
|
+
* together, and each query gets its interval from them. `noise` is the noise of one measurement.
|
|
132
|
+
*
|
|
133
|
+
* The list `points` keeps each point that the fit used, in the order of `fit.residuals`. A law file from before the
|
|
134
|
+
* points has no list.
|
|
135
|
+
*/
|
|
136
|
+
export interface Law {
|
|
137
|
+
readonly format: typeof LAW_FORMAT;
|
|
138
|
+
readonly id: string;
|
|
139
|
+
readonly title: string;
|
|
140
|
+
readonly subject: string;
|
|
141
|
+
readonly measure: Measure;
|
|
142
|
+
readonly variables: readonly Variable[];
|
|
143
|
+
readonly conditions: Readonly<Record<string, ConditionValue>>;
|
|
144
|
+
readonly form: Form;
|
|
145
|
+
readonly constants: readonly Constant[];
|
|
146
|
+
readonly replicates: readonly (readonly number[])[];
|
|
147
|
+
readonly fit: Fit;
|
|
148
|
+
readonly points?: readonly RunPoint[];
|
|
149
|
+
readonly evidence: Evidence;
|
|
150
|
+
readonly noise: Noise;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** A problem code names the part of a law, or of a query, that is not correct. */
|
|
154
|
+
export type ProblemCode =
|
|
155
|
+
| "shape"
|
|
156
|
+
| "format"
|
|
157
|
+
| "id"
|
|
158
|
+
| "unit"
|
|
159
|
+
| "range"
|
|
160
|
+
| "condition"
|
|
161
|
+
| "program"
|
|
162
|
+
| "read"
|
|
163
|
+
| "constant"
|
|
164
|
+
| "replicates"
|
|
165
|
+
// The replicates of a constant have one value. This is a fault of the data of the fit, not of the law file.
|
|
166
|
+
| "flat"
|
|
167
|
+
| "value"
|
|
168
|
+
| "fit"
|
|
169
|
+
| "evidence"
|
|
170
|
+
| "noise"
|
|
171
|
+
| "point"
|
|
172
|
+
| "level"
|
|
173
|
+
| "knee";
|
|
174
|
+
|
|
175
|
+
/** A problem tells what is not correct, and where: `path` is the path of the field in the record. */
|
|
176
|
+
export interface Problem<Code extends string> {
|
|
177
|
+
readonly code: Code;
|
|
178
|
+
readonly path: string;
|
|
179
|
+
readonly message: string;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** A law problem tells what is not correct in a law, or in a query of a law. */
|
|
183
|
+
export type LawProblem = Problem<ProblemCode>;
|
|
184
|
+
|
|
185
|
+
/** This number is the level of an interval when a query gives none. */
|
|
186
|
+
export const DEFAULT_LEVEL = 0.95;
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* This function gives the least count of values that can show an interval of a level. An end of
|
|
190
|
+
* the interval needs one value beyond it. Thus the level 0.95 needs 40 values.
|
|
191
|
+
*/
|
|
192
|
+
export const leastCount = (level: number): number => Math.ceil(2 / (1 - level) - 1e-9);
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* This function gives the number of different draws of a bootstrap of `groups` whole groups. A run draws as many
|
|
196
|
+
* groups as the data has, with replacement, and a fit does not see their order. Thus the number is `C(2g - 1, g)`.
|
|
197
|
+
*/
|
|
198
|
+
export const bootstrapDraws = (groups: number): number => {
|
|
199
|
+
let draws = 1;
|
|
200
|
+
for (let i = 1; i <= groups; i++) draws = (draws * (groups - 1 + i)) / i;
|
|
201
|
+
return Math.round(draws);
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* This function gives the least count of groups that a bootstrap of whole groups needs for an interval of a level.
|
|
206
|
+
* A run draws `g` groups from `g` groups with replacement, and a fit does not see their order. Thus `g` groups give
|
|
207
|
+
* `C(2g - 1, g)` different draws (`bootstrapDraws`). Each different draw gives at most one different value, and the
|
|
208
|
+
* interval needs `leastCount(level)` values. Thus the level 0.95 needs 5 groups: 4 groups give only 35 different draws.
|
|
209
|
+
*/
|
|
210
|
+
export const leastGroups = (level: number): number => {
|
|
211
|
+
const need = leastCount(level);
|
|
212
|
+
let groups = 1;
|
|
213
|
+
while (bootstrapDraws(groups) < need) groups += 1;
|
|
214
|
+
return groups;
|
|
215
|
+
};
|
package/src/limit.ts
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { err, ok, type Result } from "@mark1russell7/optional";
|
|
2
|
+
import { convertible, isQuantity, scale, valueIn, whyNotConvertible, type Quantity } from "@mark1russell7/quantity";
|
|
3
|
+
import { limitProblem, limitProblems } from "./instance-check.ts";
|
|
4
|
+
import type { Instance, Limit, LimitProblem } from "./instance.ts";
|
|
5
|
+
import { finite, repeated } from "./names.ts";
|
|
6
|
+
import type { Amounts } from "./workload.ts";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A limit use tells how much of a limit one value of its measure takes. The `amount` is the amount
|
|
10
|
+
* of the limit: its ceiling, less its headroom. The `use` is the value, divided by that amount.
|
|
11
|
+
* The limit holds when the `use` is 1 or less.
|
|
12
|
+
*/
|
|
13
|
+
export interface LimitUse {
|
|
14
|
+
/** This id is the id of the limit. */
|
|
15
|
+
readonly limit: string;
|
|
16
|
+
readonly measure: string;
|
|
17
|
+
readonly value: Quantity;
|
|
18
|
+
readonly amount: Quantity;
|
|
19
|
+
readonly use: number;
|
|
20
|
+
readonly holds: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** This function gives the amount of a limit at the path `at`, or the problem that stops it. */
|
|
24
|
+
const amountAt = (limit: Limit, instance: Instance, at: string): Result<Quantity, LimitProblem> => {
|
|
25
|
+
const bad = limitProblems(limit, at)[0];
|
|
26
|
+
if (bad !== undefined) return err(bad);
|
|
27
|
+
let ceiling: Quantity;
|
|
28
|
+
if (limit.ceiling.kind === "amount") {
|
|
29
|
+
ceiling = limit.ceiling.amount;
|
|
30
|
+
} else {
|
|
31
|
+
const name = limit.ceiling.resource;
|
|
32
|
+
if (!Object.hasOwn(instance.resources, name)) {
|
|
33
|
+
return err(limitProblem("resource", `resources.${name}`, `The instance has no resource "${name}" for the limit "${limit.id}".`));
|
|
34
|
+
}
|
|
35
|
+
const resource: unknown = instance.resources[name];
|
|
36
|
+
if (!isQuantity(resource) || !(resource.value > 0)) {
|
|
37
|
+
return err(limitProblem("resource", `resources.${name}`, `The resource "${name}" of the instance is not a finite amount above 0.`));
|
|
38
|
+
}
|
|
39
|
+
ceiling = resource;
|
|
40
|
+
}
|
|
41
|
+
const amount = scale(ceiling, 1 - limit.headroom);
|
|
42
|
+
if (!(amount.value > 0) || !finite(amount.value)) {
|
|
43
|
+
const field = at === "" ? "ceiling" : `${at}.ceiling`;
|
|
44
|
+
return err(limitProblem("amount", field, `The ceiling of the limit "${limit.id}", less its headroom, is not a finite amount above 0.`));
|
|
45
|
+
}
|
|
46
|
+
return ok(amount);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/** This function gives the use of a limit, with the path of the limit and the path of the value for a problem. */
|
|
50
|
+
const useAt = (limit: Limit, instance: Instance, value: Quantity, at: string, valuePath: string): Result<LimitUse, LimitProblem> => {
|
|
51
|
+
const amount = amountAt(limit, instance, at);
|
|
52
|
+
if (amount.tag === "err") return amount;
|
|
53
|
+
if (!isQuantity(value) || value.value < 0) {
|
|
54
|
+
return err(limitProblem("amount", valuePath, `The value of the measure "${limit.measure}" is not a finite amount of 0 or more.`));
|
|
55
|
+
}
|
|
56
|
+
if (!convertible(value.unit, amount.value.unit)) {
|
|
57
|
+
const why = whyNotConvertible(value.unit, amount.value.unit) ?? "the units are different";
|
|
58
|
+
return err(
|
|
59
|
+
limitProblem(
|
|
60
|
+
"unit",
|
|
61
|
+
valuePath,
|
|
62
|
+
`The measure "${limit.measure}" is in "${value.unit}", and the ceiling of the limit "${limit.id}" is in "${amount.value.unit}": ${why}.`,
|
|
63
|
+
),
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
const use = valueIn(value, amount.value.unit) / amount.value.value;
|
|
67
|
+
if (!finite(use)) return err(limitProblem("amount", valuePath, `The value of the measure "${limit.measure}" has no finite use.`));
|
|
68
|
+
return ok({ limit: limit.id, measure: limit.measure, value, amount: amount.value, use, holds: use <= 1 });
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* This function gives the amount of a limit on an instance: the ceiling, less the headroom. It is
|
|
73
|
+
* the largest value of the measure that holds the limit, in the unit of the ceiling.
|
|
74
|
+
*
|
|
75
|
+
* It gives a problem for a limit that is not correct. It gives a problem for a ceiling that names
|
|
76
|
+
* a resource when the instance does not have that resource.
|
|
77
|
+
*/
|
|
78
|
+
export const limitAmount = (limit: Limit, instance: Instance): Result<Quantity, LimitProblem> => amountAt(limit, instance, "");
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* This function gives the use of a limit on an instance by one value of its measure. The use is
|
|
82
|
+
* the value, divided by the amount of the limit. Thus a use of 1 is exactly at the limit.
|
|
83
|
+
*
|
|
84
|
+
* It gives each problem of `limitAmount`. It gives a problem for a value that is not a finite
|
|
85
|
+
* amount of 0 or more. It gives a problem for a value that does not convert to the unit of the ceiling.
|
|
86
|
+
*/
|
|
87
|
+
export const limitUse = (limit: Limit, instance: Instance, value: Quantity): Result<LimitUse, LimitProblem> =>
|
|
88
|
+
useAt(limit, instance, value, "", "value");
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* This function gives the use of each limit on an instance. The `values` give the value of each
|
|
92
|
+
* measure, by the id of the measure. The uses go from the largest to the smallest. Thus the first
|
|
93
|
+
* use is the limit that binds.
|
|
94
|
+
*
|
|
95
|
+
* A limit with no value is a problem, and not a limit that holds. The function also gives a
|
|
96
|
+
* problem for two limits with one id, and each problem of `limitUse`.
|
|
97
|
+
*/
|
|
98
|
+
export const limitUses = (limits: readonly Limit[], instance: Instance, values: Amounts): Result<readonly LimitUse[], LimitProblem> => {
|
|
99
|
+
const twice = repeated(limits.map((limit) => limit.id));
|
|
100
|
+
if (twice !== undefined) return err(limitProblem("id", "limits", `Two limits have the id "${twice}".`));
|
|
101
|
+
const uses: LimitUse[] = [];
|
|
102
|
+
for (const [index, limit] of limits.entries()) {
|
|
103
|
+
const at = `limits[${index}]`;
|
|
104
|
+
if (!Object.hasOwn(values, limit.measure)) {
|
|
105
|
+
return err(limitProblem("measure", `${at}.measure`, `The values give no amount for the measure "${limit.measure}" of the limit "${limit.id}".`));
|
|
106
|
+
}
|
|
107
|
+
const use = useAt(limit, instance, values[limit.measure] as Quantity, at, `values.${limit.measure}`);
|
|
108
|
+
if (use.tag === "err") return use;
|
|
109
|
+
uses.push(use.value);
|
|
110
|
+
}
|
|
111
|
+
return ok(uses.sort((a, b) => b.use - a.use));
|
|
112
|
+
};
|
package/src/names.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// The rules of the names and the times that a law file and a workload file share.
|
|
2
|
+
|
|
3
|
+
/** An id is lower-case words joined by `.`, `-` or `/`. A law, a subject, a measure, a form, a condition, a workload and a class each have an id. */
|
|
4
|
+
export const SLUG: RegExp = /^[a-z0-9]+(?:[./-][a-z0-9]+)*$/;
|
|
5
|
+
|
|
6
|
+
/** A name that a program reads: a variable, a constant or a level. */
|
|
7
|
+
export const NAME: RegExp = /^[a-z][A-Za-z0-9]*$/;
|
|
8
|
+
|
|
9
|
+
const UTC = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z$/;
|
|
10
|
+
|
|
11
|
+
/** This function tells if a text is a UTC time, for example `2026-11-18T14:00:00Z`. */
|
|
12
|
+
export const isUtc = (text: string): boolean => UTC.test(text) && Number.isFinite(Date.parse(text));
|
|
13
|
+
|
|
14
|
+
/** This function tells if a value is a finite number. */
|
|
15
|
+
export const finite = (value: number): boolean => typeof value === "number" && Number.isFinite(value);
|
|
16
|
+
|
|
17
|
+
/** This function gives the first value that a list has two times, or undefined. */
|
|
18
|
+
export const repeated = (values: readonly string[]): string | undefined =>
|
|
19
|
+
values.find((value, index) => values.indexOf(value) !== index);
|
package/src/observe.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { err, ok, type Result } from "@mark1russell7/optional";
|
|
2
|
+
import { isCanonicalUnit, isQuantity, kindOf, quantity, type Quantity } from "@mark1russell7/quantity";
|
|
3
|
+
import { isUtc } from "./names.ts";
|
|
4
|
+
import { checkWorkload, workloadProblem } from "./workload-check.ts";
|
|
5
|
+
import { WORKLOAD_FORMAT, type Amounts, type Workload, type WorkloadClass, type WorkloadProblem } from "./workload.ts";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* An observed class is the count of the requests of one class in a window of the live metrics, for
|
|
9
|
+
* example `{ value: 1200, unit: "{request}" }`. Its `request` gives each variable of one request.
|
|
10
|
+
*/
|
|
11
|
+
export interface ObservedClass {
|
|
12
|
+
readonly id: string;
|
|
13
|
+
readonly count: Quantity;
|
|
14
|
+
readonly request: Amounts;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* An observation is the live metrics of an app in one window, from `from` to `to`. Its `origin`
|
|
19
|
+
* names the place of the metrics. Each level is the level in the window.
|
|
20
|
+
*/
|
|
21
|
+
export interface Observation {
|
|
22
|
+
readonly origin: string;
|
|
23
|
+
readonly from: string;
|
|
24
|
+
readonly to: string;
|
|
25
|
+
readonly classes: readonly ObservedClass[];
|
|
26
|
+
readonly levels: Amounts;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** A workload name is the id and the title of a workload. */
|
|
30
|
+
export interface WorkloadName {
|
|
31
|
+
readonly id: string;
|
|
32
|
+
readonly title: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* This function gives the workload of an observation. The rate of a class is its count, divided by
|
|
37
|
+
* the seconds of the window. Thus the rate is the mean rate of the window. For the load at a peak,
|
|
38
|
+
* give the window of the peak.
|
|
39
|
+
*
|
|
40
|
+
* It gives each problem of an observation that is not correct, and each problem of the workload.
|
|
41
|
+
*/
|
|
42
|
+
export const observedWorkload = (observation: Observation, name: WorkloadName): Result<Workload, readonly WorkloadProblem[]> => {
|
|
43
|
+
const { origin, from, to } = observation;
|
|
44
|
+
const seconds = isUtc(from) && isUtc(to) ? (Date.parse(to) - Date.parse(from)) / 1000 : Number.NaN;
|
|
45
|
+
if (!(seconds > 0)) {
|
|
46
|
+
return err([workloadProblem("source", "source", "The window does not go from a UTC time to a later UTC time.")]);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const problems: WorkloadProblem[] = [];
|
|
50
|
+
const classes: WorkloadClass[] = [];
|
|
51
|
+
observation.classes.forEach((entry, index) => {
|
|
52
|
+
const { count } = entry;
|
|
53
|
+
const unit = isQuantity(count) && kindOf(count.unit) === "count" ? `${count.unit}/s` : undefined;
|
|
54
|
+
if (unit === undefined || !isCanonicalUnit(unit) || kindOf(unit) !== "frequency" || count.value < 0) {
|
|
55
|
+
problems.push(
|
|
56
|
+
workloadProblem("rate", `classes[${index}].count`, `The count of the class "${entry.id}" is not a count of 0 or more, for example in "{request}".`),
|
|
57
|
+
);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
classes.push({ id: entry.id, rate: quantity(count.value / seconds, unit), request: entry.request });
|
|
61
|
+
});
|
|
62
|
+
if (problems.length > 0) return err(problems);
|
|
63
|
+
|
|
64
|
+
const workload: Workload = {
|
|
65
|
+
format: WORKLOAD_FORMAT,
|
|
66
|
+
id: name.id,
|
|
67
|
+
title: name.title,
|
|
68
|
+
classes,
|
|
69
|
+
levels: observation.levels,
|
|
70
|
+
source: { kind: "metrics", origin, from, to },
|
|
71
|
+
};
|
|
72
|
+
const found = checkWorkload(workload);
|
|
73
|
+
return found.length > 0 ? err(found) : ok(workload);
|
|
74
|
+
};
|
package/src/program.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { err, ok, type Result } from "@mark1russell7/optional";
|
|
2
|
+
import { space, type AnyDomain, type Expr, type Program } from "@mark1russell7/vex";
|
|
3
|
+
import { tiered } from "@mark1russell7/vex-compiler";
|
|
4
|
+
import { NumDomain } from "@mark1russell7/vex-domains";
|
|
5
|
+
|
|
6
|
+
/** These domains give the ops of the program of a law: the ops of numbers. */
|
|
7
|
+
export const LAW_DOMAINS: readonly AnyDomain[] = [NumDomain];
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* These programs are the compiled form of each expression that a law reads. A law keeps its expression, thus each expression
|
|
11
|
+
* compiles one time. Tier 2 of the compiler runs generated code, with the results of the interpreter. A page that does not
|
|
12
|
+
* permit generated code runs tier 1.
|
|
13
|
+
*/
|
|
14
|
+
const compiled = new WeakMap<Expr, Program>();
|
|
15
|
+
|
|
16
|
+
/** This function gives the compiled program of an expression: the program of the cache, or a new program that goes into the cache. */
|
|
17
|
+
const programFor = (expr: Expr): Program => {
|
|
18
|
+
const known = compiled.get(expr);
|
|
19
|
+
if (known !== undefined) return known;
|
|
20
|
+
const made = tiered(expr, { domains: LAW_DOMAINS });
|
|
21
|
+
compiled.set(expr, made);
|
|
22
|
+
return made;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/** This function gives the result of a program at each record: a finite number, or the text of its error. */
|
|
26
|
+
const resultsOf = (expr: Expr, records: readonly Readonly<Record<string, number>>[]): readonly Result<number, string>[] => {
|
|
27
|
+
const program = programFor(expr);
|
|
28
|
+
const at = space.array(records);
|
|
29
|
+
const results: Result<number, string>[] = [];
|
|
30
|
+
for (const origin of at.keys) {
|
|
31
|
+
const result = program.run({ space: at, origin });
|
|
32
|
+
if (!result.ok) results.push(err(`${result.error.code} ${result.error.message}`));
|
|
33
|
+
else if (typeof result.value !== "number" || !Number.isFinite(result.value)) {
|
|
34
|
+
results.push(err("the program gave a value that is not a finite number"));
|
|
35
|
+
} else results.push(ok(result.value));
|
|
36
|
+
}
|
|
37
|
+
return results;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* This function gives the value of a program at each record, in the order of the records. It gives
|
|
42
|
+
* the text of the first error when a record has no finite value.
|
|
43
|
+
*/
|
|
44
|
+
export const valuesOf = (
|
|
45
|
+
program: Expr,
|
|
46
|
+
records: readonly Readonly<Record<string, number>>[],
|
|
47
|
+
): Result<readonly number[], string> => {
|
|
48
|
+
const values: number[] = [];
|
|
49
|
+
for (const result of resultsOf(program, records)) {
|
|
50
|
+
if (result.tag === "err") return result;
|
|
51
|
+
values.push(result.value);
|
|
52
|
+
}
|
|
53
|
+
return ok(values);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* This function gives the value of a program at each record, in the order of the records. A record
|
|
58
|
+
* with no finite value gives undefined.
|
|
59
|
+
*/
|
|
60
|
+
export const valuesAt = (program: Expr, records: readonly Readonly<Record<string, number>>[]): readonly (number | undefined)[] =>
|
|
61
|
+
resultsOf(program, records).map((result) => (result.tag === "ok" ? result.value : undefined));
|
|
62
|
+
|
|
63
|
+
/** This function gives the record that a program reads: the values of the variables and the constants. */
|
|
64
|
+
export const recordOf = (
|
|
65
|
+
names: readonly string[],
|
|
66
|
+
values: readonly number[],
|
|
67
|
+
base: Readonly<Record<string, number>> = {},
|
|
68
|
+
): Readonly<Record<string, number>> => {
|
|
69
|
+
const record: Record<string, number> = { ...base };
|
|
70
|
+
names.forEach((name, index) => {
|
|
71
|
+
record[name] = values[index] as number;
|
|
72
|
+
});
|
|
73
|
+
return record;
|
|
74
|
+
};
|