@mark1russell7/capacity 0.0.0-stage → 0.1.1
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 +1110 -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-check-Bt-SI8va.js +1732 -0
- package/build/instance-check-Bt-SI8va.js.map +1 -0
- package/build/instance-sZmVOOvz.d.ts +801 -0
- package/build/instance-sZmVOOvz.d.ts.map +1 -0
- package/build/schema.d.ts +69 -0
- package/build/schema.d.ts.map +1 -0
- package/build/schema.js +325 -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 +253 -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 +143 -0
- package/src/instance-check.ts +122 -0
- package/src/instance.ts +84 -0
- package/src/knee.ts +606 -0
- package/src/law.ts +236 -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 +263 -0
- package/src/value.ts +206 -0
- package/src/workload-check.ts +130 -0
- package/src/workload.ts +88 -0
|
@@ -0,0 +1,1732 @@
|
|
|
1
|
+
import { UnitError, add, compare, convertible, divideUnits, factorOf, isCanonicalUnit, isQuantity, kindOf, multiplyUnits, parseUnit, quantity, scale, valueIn, whyNotConvertible } from "@mark1russell7/quantity";
|
|
2
|
+
import { deps, parse, space } from "@mark1russell7/vex";
|
|
3
|
+
import { err, ok } from "@mark1russell7/optional";
|
|
4
|
+
import { tiered } from "@mark1russell7/vex-compiler";
|
|
5
|
+
import { NumDomain } from "@mark1russell7/vex-domains";
|
|
6
|
+
import { quantile } from "@mark1russell7/rigor-statistics";
|
|
7
|
+
// src/law.ts
|
|
8
|
+
/** This text is the format of a law file. */
|
|
9
|
+
const LAW_FORMAT = "capacity-law/1";
|
|
10
|
+
/** This number is the most variables that a law can have. The check reads each corner of their ranges. */
|
|
11
|
+
const MAX_VARIABLES = 8;
|
|
12
|
+
/** This number is the level of an interval when a query gives none. */
|
|
13
|
+
const DEFAULT_LEVEL = .95;
|
|
14
|
+
/**
|
|
15
|
+
* This function gives the least count of values that can show an interval of a level. An end of
|
|
16
|
+
* the interval needs one value beyond it. Thus the level 0.95 needs 40 values.
|
|
17
|
+
*/
|
|
18
|
+
const leastCount = (level) => Math.ceil(2 / (1 - level) - 1e-9);
|
|
19
|
+
/**
|
|
20
|
+
* This function gives the number of different draws of a bootstrap of `groups` whole groups. A run draws as many
|
|
21
|
+
* groups as the data has, with replacement, and a fit does not see their order. Thus the number is `C(2g - 1, g)`.
|
|
22
|
+
*/
|
|
23
|
+
const bootstrapDraws = (groups) => {
|
|
24
|
+
let draws = 1;
|
|
25
|
+
for (let i = 1; i <= groups; i++) draws = draws * (groups - 1 + i) / i;
|
|
26
|
+
return Math.round(draws);
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* This function gives the least count of groups that a bootstrap of whole groups needs for an interval of a level.
|
|
30
|
+
* A run draws `g` groups from `g` groups with replacement, and a fit does not see their order. Thus `g` groups give
|
|
31
|
+
* `C(2g - 1, g)` different draws (`bootstrapDraws`). Each different draw gives at most one different value, and the
|
|
32
|
+
* interval needs `leastCount(level)` values. Thus the level 0.95 needs 5 groups: 4 groups give only 35 different draws.
|
|
33
|
+
*/
|
|
34
|
+
const leastGroups = (level) => {
|
|
35
|
+
const need = leastCount(level);
|
|
36
|
+
let groups = 1;
|
|
37
|
+
while (bootstrapDraws(groups) < need) groups += 1;
|
|
38
|
+
return groups;
|
|
39
|
+
};
|
|
40
|
+
// src/names.ts
|
|
41
|
+
/** 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. */
|
|
42
|
+
const SLUG = /^[a-z0-9]+(?:[./-][a-z0-9]+)*$/;
|
|
43
|
+
/** A name that a program reads: a variable, a constant or a level. */
|
|
44
|
+
const NAME = /^[a-z][A-Za-z0-9]*$/;
|
|
45
|
+
const UTC = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z$/;
|
|
46
|
+
/** This function tells if a text is a UTC time, for example `2026-11-18T14:00:00Z`. */
|
|
47
|
+
const isUtc = (text) => UTC.test(text) && Number.isFinite(Date.parse(text));
|
|
48
|
+
/** This function tells if a value is a finite number. */
|
|
49
|
+
const finite = (value) => typeof value === "number" && Number.isFinite(value);
|
|
50
|
+
/** This function gives the first value that a list has two times, or undefined. */
|
|
51
|
+
const repeated = (values) => values.find((value, index) => values.indexOf(value) !== index);
|
|
52
|
+
// src/program.ts
|
|
53
|
+
/** These domains give the ops of the program of a law: the ops of numbers. */
|
|
54
|
+
const LAW_DOMAINS = [NumDomain];
|
|
55
|
+
/**
|
|
56
|
+
* These programs are the compiled form of each expression that a law reads. A law keeps its expression, thus each expression
|
|
57
|
+
* compiles one time. Tier 2 of the compiler runs generated code, with the results of the interpreter. A page that does not
|
|
58
|
+
* permit generated code runs tier 1.
|
|
59
|
+
*/
|
|
60
|
+
const compiled = /* @__PURE__ */ new WeakMap();
|
|
61
|
+
/** This function gives the compiled program of an expression: the program of the cache, or a new program that goes into the cache. */
|
|
62
|
+
const programFor = (expr) => {
|
|
63
|
+
const known = compiled.get(expr);
|
|
64
|
+
if (known !== void 0) return known;
|
|
65
|
+
const made = tiered(expr, { domains: LAW_DOMAINS });
|
|
66
|
+
compiled.set(expr, made);
|
|
67
|
+
return made;
|
|
68
|
+
};
|
|
69
|
+
/** This function gives the result of a program at each record: a finite number, or the text of its error. */
|
|
70
|
+
const resultsOf = (expr, records) => {
|
|
71
|
+
const program = programFor(expr);
|
|
72
|
+
const at = space.array(records);
|
|
73
|
+
const results = [];
|
|
74
|
+
for (const origin of at.keys) {
|
|
75
|
+
const result = program.run({
|
|
76
|
+
space: at,
|
|
77
|
+
origin
|
|
78
|
+
});
|
|
79
|
+
if (!result.ok) results.push(err(`${result.error.code} ${result.error.message}`));
|
|
80
|
+
else if (typeof result.value !== "number" || !Number.isFinite(result.value)) results.push(err("the program gave a value that is not a finite number"));
|
|
81
|
+
else results.push(ok(result.value));
|
|
82
|
+
}
|
|
83
|
+
return results;
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* This function gives the value of a program at each record, in the order of the records. It gives
|
|
87
|
+
* the text of the first error when a record has no finite value.
|
|
88
|
+
*/
|
|
89
|
+
const valuesOf = (program, records) => {
|
|
90
|
+
const values = [];
|
|
91
|
+
for (const result of resultsOf(program, records)) {
|
|
92
|
+
if (result.tag === "err") return result;
|
|
93
|
+
values.push(result.value);
|
|
94
|
+
}
|
|
95
|
+
return ok(values);
|
|
96
|
+
};
|
|
97
|
+
/**
|
|
98
|
+
* This function gives the value of a program at each record, in the order of the records. A record
|
|
99
|
+
* with no finite value gives undefined.
|
|
100
|
+
*/
|
|
101
|
+
const valuesAt = (program, records) => resultsOf(program, records).map((result) => result.tag === "ok" ? result.value : void 0);
|
|
102
|
+
/** This function gives the record that a program reads: the values of the variables and the constants. */
|
|
103
|
+
const recordOf = (names, values, base = {}) => {
|
|
104
|
+
const record = { ...base };
|
|
105
|
+
names.forEach((name, index) => {
|
|
106
|
+
record[name] = values[index];
|
|
107
|
+
});
|
|
108
|
+
return record;
|
|
109
|
+
};
|
|
110
|
+
// src/check.ts
|
|
111
|
+
const COMMIT = /^[0-9a-f]{40}$/;
|
|
112
|
+
/**
|
|
113
|
+
* A column of replicates is flat when its width is this share of its largest size or less. The search of a shape
|
|
114
|
+
* stops at a bracket of `SEPARABLE_SEARCH_TOLERANCE` (1e-10) of its start width. Thus a shape at the same edge of its
|
|
115
|
+
* search in each fit gives a flat column. A bootstrap of measured values gives a much wider column.
|
|
116
|
+
*/
|
|
117
|
+
const FLAT_SHARE = 1e-9;
|
|
118
|
+
/** This function gives the middle of the range of a variable, on its scale. */
|
|
119
|
+
const middleOf = (variable) => variable.scale === "log" ? Math.sqrt(variable.min * variable.max) : (variable.min + variable.max) / 2;
|
|
120
|
+
/** This function gives each corner of the ranges of some variables, and their middle. */
|
|
121
|
+
const cornersOf = (variables) => {
|
|
122
|
+
const corners = [];
|
|
123
|
+
for (let mask = 0; mask < 2 ** variables.length; mask += 1) corners.push(variables.map((variable, index) => (mask >> index & 1) === 1 ? variable.max : variable.min));
|
|
124
|
+
return [...corners, variables.map(middleOf)];
|
|
125
|
+
};
|
|
126
|
+
/**
|
|
127
|
+
* This function examines a law and gives each problem that it finds. A law with no problem gives
|
|
128
|
+
* an empty list.
|
|
129
|
+
*
|
|
130
|
+
* It examines the ids, the units and the ranges. It examines that the program reads each variable
|
|
131
|
+
* and each constant, and no other name. It examines that the law has a finite value at each corner
|
|
132
|
+
* and at the middle of its ranges. It examines that the replicates and the residuals can show an
|
|
133
|
+
* interval of the level 0.95, and that the evidence names its runs. It examines that the noise
|
|
134
|
+
* has a floor of 0 or more, a CPU model and its runs.
|
|
135
|
+
*
|
|
136
|
+
* A law with points must have one point for each residual. Each point must be in the ranges of the
|
|
137
|
+
* variables and name a run of the evidence.
|
|
138
|
+
*
|
|
139
|
+
* The interval of each constant must have a width above 0. A constant with the same value in each
|
|
140
|
+
* replicate is a bound of a search, not a measured value. For example, a shape at the edge of its
|
|
141
|
+
* search in each fit of the bootstrap gives this.
|
|
142
|
+
*/
|
|
143
|
+
const checkLaw = (law) => {
|
|
144
|
+
const problems = [];
|
|
145
|
+
const add = (code, path, message) => {
|
|
146
|
+
problems.push({
|
|
147
|
+
code,
|
|
148
|
+
path,
|
|
149
|
+
message
|
|
150
|
+
});
|
|
151
|
+
};
|
|
152
|
+
const slug = (path, value) => {
|
|
153
|
+
if (!SLUG.test(value)) add("id", path, `The id "${value}" is not lower-case words joined by ".", "-" or "/".`);
|
|
154
|
+
};
|
|
155
|
+
if (law.format !== "capacity-law/1") add("format", "format", `The format is "${String(law.format)}", not "${LAW_FORMAT}".`);
|
|
156
|
+
slug("id", law.id);
|
|
157
|
+
slug("subject", law.subject);
|
|
158
|
+
slug("measure.id", law.measure.id);
|
|
159
|
+
slug("form.id", law.form.id);
|
|
160
|
+
if (law.title.trim() === "") add("id", "title", "The title is empty.");
|
|
161
|
+
if (!isCanonicalUnit(law.measure.unit)) add("unit", "measure.unit", `"${law.measure.unit}" is not a canonical unit.`);
|
|
162
|
+
if (law.variables.length > 8) add("range", "variables", `The law has ${law.variables.length} variables. The most is 8.`);
|
|
163
|
+
law.variables.forEach((variable, index) => {
|
|
164
|
+
const path = `variables[${index}]`;
|
|
165
|
+
if (!NAME.test(variable.id)) add("id", `${path}.id`, `The name "${variable.id}" is not letters and digits that start with a lower-case letter.`);
|
|
166
|
+
if (!isCanonicalUnit(variable.unit)) add("unit", `${path}.unit`, `"${variable.unit}" is not a canonical unit.`);
|
|
167
|
+
if (!finite(variable.min) || !finite(variable.max) || !(variable.min < variable.max)) add("range", path, `The range of "${variable.id}" does not go from a finite number to a larger finite number.`);
|
|
168
|
+
else if (variable.scale === "log" && !(variable.min > 0)) add("range", path, `The variable "${variable.id}" has the scale "log", thus its range starts above 0.`);
|
|
169
|
+
});
|
|
170
|
+
const variables = law.variables.map((variable) => variable.id);
|
|
171
|
+
const twoVariables = repeated(variables);
|
|
172
|
+
if (twoVariables !== void 0) add("id", "variables", `Two variables have the name "${twoVariables}".`);
|
|
173
|
+
for (const [name, value] of Object.entries(law.conditions)) {
|
|
174
|
+
slug(`conditions.${name}`, name);
|
|
175
|
+
if (typeof value === "number" && !finite(value)) add("condition", `conditions.${name}`, `The condition "${name}" is not a finite number.`);
|
|
176
|
+
if (typeof value === "object" && !isQuantity(value)) add("condition", `conditions.${name}`, `The condition "${name}" is not a finite amount in a canonical unit.`);
|
|
177
|
+
}
|
|
178
|
+
const constants = law.constants.map((constant) => constant.id);
|
|
179
|
+
if (constants.length === 0) add("constant", "constants", "The law has no constant.");
|
|
180
|
+
law.constants.forEach((constant, index) => {
|
|
181
|
+
const path = `constants[${index}]`;
|
|
182
|
+
if (!NAME.test(constant.id)) add("id", `${path}.id`, `The name "${constant.id}" is not letters and digits that start with a lower-case letter.`);
|
|
183
|
+
if (!finite(constant.value)) add("constant", `${path}.value`, `The constant "${constant.id}" is not a finite number.`);
|
|
184
|
+
if (variables.includes(constant.id)) add("id", `${path}.id`, `"${constant.id}" names a variable and a constant.`);
|
|
185
|
+
});
|
|
186
|
+
const twoConstants = repeated(constants);
|
|
187
|
+
if (twoConstants !== void 0) add("id", "constants", `Two constants have the name "${twoConstants}".`);
|
|
188
|
+
const least = leastCount(DEFAULT_LEVEL);
|
|
189
|
+
if (law.replicates.length < least) add("replicates", "replicates", `The law has ${law.replicates.length} replicates. The level ${DEFAULT_LEVEL} needs ${least}.`);
|
|
190
|
+
const badRow = law.replicates.findIndex((row) => row.length !== constants.length || !row.every(finite));
|
|
191
|
+
const rowsAreGood = badRow === -1;
|
|
192
|
+
if (!rowsAreGood) add("replicates", `replicates[${badRow}]`, `The replicate ${badRow} does not have one finite number for each constant.`);
|
|
193
|
+
else if (law.replicates.length > 0) law.constants.forEach((constant, index) => {
|
|
194
|
+
const column = law.replicates.map((row) => row[index]);
|
|
195
|
+
const low = Math.min(...column);
|
|
196
|
+
const high = Math.max(...column);
|
|
197
|
+
if (finite(constant.value) && (constant.value < low || constant.value > high)) add("replicates", `constants[${index}].value`, `The constant "${constant.id}" is outside each of its replicates.`);
|
|
198
|
+
if (high - low <= FLAT_SHARE * Math.max(Math.abs(low), Math.abs(high))) add("flat", `constants[${index}]`, `Each replicate of the constant "${constant.id}" has the value ${low}, thus its interval has a width of 0. A shape at the edge of its search in each fit gives this.`);
|
|
199
|
+
});
|
|
200
|
+
const text = JSON.stringify(law.form.program);
|
|
201
|
+
const parsed = text === void 0 ? void 0 : parse(text, LAW_DOMAINS);
|
|
202
|
+
if (parsed === void 0 || !parsed.ok) add("program", "form.program", "The program is not a Vex expression.");
|
|
203
|
+
else {
|
|
204
|
+
const names = [...variables, ...constants];
|
|
205
|
+
const reads = /* @__PURE__ */ new Set();
|
|
206
|
+
for (const read of deps(parsed.value)) {
|
|
207
|
+
const name = read.path.join(".");
|
|
208
|
+
if (read.at.length > 0 || read.axes.length > 0) add("read", "form.program", `The program reads "${name}" at a different record. A law has one record.`);
|
|
209
|
+
else if (read.path.length !== 1 || !names.includes(name)) add("read", "form.program", `The program reads "${name}". The law has no variable and no constant with this name.`);
|
|
210
|
+
else reads.add(name);
|
|
211
|
+
}
|
|
212
|
+
for (const name of names) if (!reads.has(name)) add("read", "form.program", `The program does not read "${name}".`);
|
|
213
|
+
if (!problems.some((found) => found.code === "range" || found.code === "id" || found.code === "constant") && rowsAreGood && !problems.some((found) => found.code === "read")) {
|
|
214
|
+
const estimate = law.constants.map((constant) => constant.value);
|
|
215
|
+
const corners = cornersOf(law.variables).map((corner) => recordOf(constants, estimate, recordOf(variables, corner)));
|
|
216
|
+
const atCorners = valuesOf(parsed.value, corners);
|
|
217
|
+
if (atCorners.tag === "err") add("value", "form.program", `The law has no value at a corner or at the middle of its ranges: ${atCorners.error}.`);
|
|
218
|
+
const middle = recordOf(variables, law.variables.map(middleOf));
|
|
219
|
+
const atMiddle = valuesOf(parsed.value, law.replicates.map((row) => recordOf(constants, row, middle)));
|
|
220
|
+
if (atMiddle.tag === "err") add("value", "replicates", `A replicate has no value at the middle of the ranges: ${atMiddle.error}.`);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
if (law.fit.method.trim() === "") add("fit", "fit.method", "The method of the fit is empty.");
|
|
224
|
+
if (law.fit.residuals.length < least) add("fit", "fit.residuals", `The fit has ${law.fit.residuals.length} residuals. The level ${DEFAULT_LEVEL} needs ${least}.`);
|
|
225
|
+
if (!law.fit.residuals.every(finite)) add("fit", "fit.residuals", "A residual is not a finite number.");
|
|
226
|
+
law.fit.losers.forEach((loser, index) => {
|
|
227
|
+
const path = `fit.losers[${index}]`;
|
|
228
|
+
slug(`${path}.form`, loser.form);
|
|
229
|
+
if (loser.form === law.form.id) add("fit", `${path}.form`, `The form "${loser.form}" is the form of the law, thus it did not lose.`);
|
|
230
|
+
const { mae, rmse } = loser.error;
|
|
231
|
+
if (!finite(mae) || !finite(rmse) || mae < 0 || rmse < mae) add("fit", `${path}.error`, `The error of the form "${loser.form}" is not correct: 0 <= mae <= rmse.`);
|
|
232
|
+
});
|
|
233
|
+
const twoLosers = repeated(law.fit.losers.map((loser) => loser.form));
|
|
234
|
+
if (twoLosers !== void 0) add("fit", "fit.losers", `Two losers have the form "${twoLosers}".`);
|
|
235
|
+
const runs = (code, path, list) => {
|
|
236
|
+
if (list.length === 0) add(code, path, "The law names no run.");
|
|
237
|
+
list.forEach((run, index) => {
|
|
238
|
+
const at = `${path}[${index}]`;
|
|
239
|
+
if (!run.url.startsWith("https://")) add(code, `${at}.url`, `The address "${run.url}" does not start with "https://".`);
|
|
240
|
+
if (!COMMIT.test(run.commit)) add(code, `${at}.commit`, `The commit "${run.commit}" is not 40 hexadecimal digits.`);
|
|
241
|
+
if (!isUtc(run.date)) add(code, `${at}.date`, `The date "${run.date}" is not a UTC time, for example "2026-11-18T14:00:00Z".`);
|
|
242
|
+
});
|
|
243
|
+
};
|
|
244
|
+
runs("evidence", "evidence.runs", law.evidence.runs);
|
|
245
|
+
if (law.points !== void 0) {
|
|
246
|
+
if (law.points.length !== law.fit.residuals.length) add("point", "points", `The law has ${law.points.length} points and ${law.fit.residuals.length} residuals. Each point has one residual.`);
|
|
247
|
+
law.points.forEach((point, index) => {
|
|
248
|
+
const path = `points[${index}]`;
|
|
249
|
+
const other = Object.keys(point.load).find((name) => !variables.includes(name));
|
|
250
|
+
if (other !== void 0) add("point", `${path}.load.${other}`, `The law has no variable "${other}".`);
|
|
251
|
+
for (const variable of law.variables) {
|
|
252
|
+
const load = point.load[variable.id];
|
|
253
|
+
if (load === void 0 || !finite(load)) add("point", `${path}.load.${variable.id}`, `The point gives no finite value for the variable "${variable.id}".`);
|
|
254
|
+
else if (load < variable.min || load > variable.max) add("point", `${path}.load.${variable.id}`, `The value ${load} is outside the range of "${variable.id}".`);
|
|
255
|
+
}
|
|
256
|
+
if (!finite(point.value)) add("point", `${path}.value`, "The value is not a finite number.");
|
|
257
|
+
if (!Number.isInteger(point.run) || point.run < 0 || point.run >= law.evidence.runs.length) add("point", `${path}.run`, `The run ${point.run} is not an index of the ${law.evidence.runs.length} runs of the evidence.`);
|
|
258
|
+
if (!Number.isFinite(Date.parse(point.job))) add("point", `${path}.job`, `The job "${point.job}" is not a time.`);
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
if (!finite(law.noise.floor) || law.noise.floor < 0) add("noise", "noise.floor", "The noise floor is not a finite number, 0 or more.");
|
|
262
|
+
if (law.noise.cpu.trim() === "") add("noise", "noise.cpu", "The noise names no CPU model.");
|
|
263
|
+
runs("noise", "noise.runs", law.noise.runs);
|
|
264
|
+
const net = law.conditions["idle"] === "net";
|
|
265
|
+
const { idle } = law;
|
|
266
|
+
if (idle === void 0) {
|
|
267
|
+
if (net) add("idle", "idle", "The law has the condition idle \"net\", thus it must keep its idle base.");
|
|
268
|
+
} else {
|
|
269
|
+
if (!net) add("idle", "idle", "The law has an idle base, thus it must have the condition idle \"net\".");
|
|
270
|
+
if (idle.load.trim() === "") add("idle", "idle.load", "The idle base names no load.");
|
|
271
|
+
if (idle.cpu.trim() === "") add("idle", "idle.cpu", "The idle base names no CPU model.");
|
|
272
|
+
const whole = (path, value) => {
|
|
273
|
+
if (!Number.isInteger(value) || value < 1) add("idle", `idle.${path}`, `The count of ${path} is ${value}, not a whole number of 1 or more.`);
|
|
274
|
+
};
|
|
275
|
+
whole("bases", idle.bases);
|
|
276
|
+
whole("runs", idle.runs);
|
|
277
|
+
if (Number.isInteger(idle.bases) && Number.isInteger(idle.runs) && idle.runs < idle.bases) add("idle", "idle.runs", `The idle base has ${idle.runs} runs and ${idle.bases} bases. Each base has 1 run or more.`);
|
|
278
|
+
const measures = Object.entries(idle.measures);
|
|
279
|
+
if (measures.length === 0) add("idle", "idle.measures", "The idle base has no measure.");
|
|
280
|
+
for (const [name, amount] of measures) {
|
|
281
|
+
slug(`idle.measures.${name}`, name);
|
|
282
|
+
if (!isQuantity(amount)) add("idle", `idle.measures.${name}`, `The idle measure "${name}" is not a finite amount in a canonical unit.`);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
return problems;
|
|
286
|
+
};
|
|
287
|
+
// src/value.ts
|
|
288
|
+
const problem$1 = (code, path, message) => ({
|
|
289
|
+
code,
|
|
290
|
+
path,
|
|
291
|
+
message
|
|
292
|
+
});
|
|
293
|
+
/** This function gives the two ends of the share `level` of some values. */
|
|
294
|
+
const rangeOf = (values, level) => ({
|
|
295
|
+
low: quantile(values, (1 - level) / 2),
|
|
296
|
+
high: quantile(values, (1 + level) / 2)
|
|
297
|
+
});
|
|
298
|
+
/** This function gives the problem of a level that the law cannot show, or undefined. */
|
|
299
|
+
const levelProblem = (law, level, residuals) => {
|
|
300
|
+
if (!(level > 0 && level < 1)) return problem$1("level", "level", `The level ${level} is not between 0 and 1.`);
|
|
301
|
+
const least = leastCount(level);
|
|
302
|
+
if (law.replicates.length < least) return problem$1("level", "replicates", `The level ${level} needs ${least} replicates. The law has ${law.replicates.length}.`);
|
|
303
|
+
if (residuals && law.fit.residuals.length < least) return problem$1("level", "fit.residuals", `The level ${level} needs ${least} residuals. The law has ${law.fit.residuals.length}.`);
|
|
304
|
+
};
|
|
305
|
+
/** This function gives the range of one measured run: the interval of the law, made wider by the residuals. */
|
|
306
|
+
const runOf = (law, interval, level) => {
|
|
307
|
+
const band = rangeOf(law.fit.residuals, level);
|
|
308
|
+
if (law.fit.residual === "absolute") return {
|
|
309
|
+
low: interval.low + band.low,
|
|
310
|
+
high: interval.high + band.high
|
|
311
|
+
};
|
|
312
|
+
const corners = [interval.low, interval.high].flatMap((end) => [end * (1 + band.low), end * (1 + band.high)]);
|
|
313
|
+
return {
|
|
314
|
+
low: Math.min(...corners),
|
|
315
|
+
high: Math.max(...corners)
|
|
316
|
+
};
|
|
317
|
+
};
|
|
318
|
+
/**
|
|
319
|
+
* This function gives the nearest point of a load point in the ranges of the variables of a law. A point in each range
|
|
320
|
+
* gives undefined. A name that is not a variable of the law, or a value that is not a number, does not change.
|
|
321
|
+
*/
|
|
322
|
+
const edgePointOf = (law, point) => {
|
|
323
|
+
let edge;
|
|
324
|
+
for (const variable of law.variables) {
|
|
325
|
+
const value = point[variable.id];
|
|
326
|
+
if (typeof value !== "number" || Number.isNaN(value)) continue;
|
|
327
|
+
const held = Math.min(variable.max, Math.max(variable.min, value));
|
|
328
|
+
if (held !== value) (edge ??= { ...point })[variable.id] = held;
|
|
329
|
+
}
|
|
330
|
+
return edge;
|
|
331
|
+
};
|
|
332
|
+
/**
|
|
333
|
+
* This function holds a value of a law past its measures. Each measure of a law is a cost, and no run measured the
|
|
334
|
+
* cost past the edge. Thus the value is the higher of the value of the law and its value at the nearest edge. A
|
|
335
|
+
* cost that falls with the load then stays flat past the edge, and a cost that rises keeps its law. A missing value
|
|
336
|
+
* stays missing, and a missing value at the edge keeps the value of the law.
|
|
337
|
+
*/
|
|
338
|
+
const heldValue = (value, edge) => value === void 0 || edge === void 0 || !Number.isFinite(edge) ? value : Math.max(value, edge);
|
|
339
|
+
/** This count is the most edge points that the cache keeps for one law. A law with one variable has at most two. */
|
|
340
|
+
const EDGE_POINTS = 64;
|
|
341
|
+
/** This cache keeps the values of each law at each edge point, one value for each row: the constants, then each replicate. */
|
|
342
|
+
const edges = /* @__PURE__ */ new WeakMap();
|
|
343
|
+
/**
|
|
344
|
+
* This function gives the values of a law at an edge point, one for each row: row 0 with the constants, then each
|
|
345
|
+
* replicate. Each load past the top of a variable has the same edge point. Thus a search past the measures evaluates
|
|
346
|
+
* the law at the edge one time, and the cache gives the values after that.
|
|
347
|
+
*/
|
|
348
|
+
const edgeValuesOf = (law, edge) => {
|
|
349
|
+
const key = law.variables.map((variable) => edge[variable.id]).join(",");
|
|
350
|
+
let byPoint = edges.get(law);
|
|
351
|
+
const known = byPoint?.get(key);
|
|
352
|
+
if (known !== void 0) return known;
|
|
353
|
+
const names = law.constants.map((constant) => constant.id);
|
|
354
|
+
const rows = [law.constants.map((constant) => constant.value), ...law.replicates];
|
|
355
|
+
const values = valuesAt(law.form.program, rows.map((row) => recordOf(names, row, edge)));
|
|
356
|
+
if (byPoint === void 0 || byPoint.size >= EDGE_POINTS) edges.set(law, byPoint = /* @__PURE__ */ new Map());
|
|
357
|
+
byPoint.set(key, values);
|
|
358
|
+
return values;
|
|
359
|
+
};
|
|
360
|
+
/**
|
|
361
|
+
* This function evaluates a law at a load point with its constants, then with each replicate. The
|
|
362
|
+
* point gives each variable of the law, and no other name.
|
|
363
|
+
*
|
|
364
|
+
* Past the range of a variable, each value holds, as heldValue tells.
|
|
365
|
+
* It gives a problem for a point that the law cannot read, and for a value that is not finite.
|
|
366
|
+
*/
|
|
367
|
+
const pointValuesOf = (law, point) => {
|
|
368
|
+
const variables = law.variables.map((variable) => variable.id);
|
|
369
|
+
const unknown = Object.keys(point).find((name) => !variables.includes(name));
|
|
370
|
+
if (unknown !== void 0) return err(problem$1("point", unknown, `The law has no variable "${unknown}".`));
|
|
371
|
+
const load = {};
|
|
372
|
+
for (const name of variables) {
|
|
373
|
+
const value = point[name];
|
|
374
|
+
if (typeof value !== "number" || !Number.isFinite(value)) return err(problem$1("point", name, `The point gives no finite value for the variable "${name}".`));
|
|
375
|
+
load[name] = value;
|
|
376
|
+
}
|
|
377
|
+
const names = law.constants.map((constant) => constant.id);
|
|
378
|
+
const constants = law.constants.map((constant) => constant.value);
|
|
379
|
+
const values = valuesOf(law.form.program, [recordOf(names, constants, load), ...law.replicates.map((row) => recordOf(names, row, load))]);
|
|
380
|
+
if (values.tag === "err") return err(problem$1("value", "form.program", `The law has no value at this point: ${values.error}.`));
|
|
381
|
+
const outside = law.variables.filter((variable) => load[variable.id] < variable.min || load[variable.id] > variable.max).map((variable) => variable.id);
|
|
382
|
+
const edge = outside.length === 0 ? void 0 : edgePointOf(law, load);
|
|
383
|
+
if (edge === void 0) return ok({
|
|
384
|
+
values: values.value,
|
|
385
|
+
outside
|
|
386
|
+
});
|
|
387
|
+
const atEdge = edgeValuesOf(law, edge);
|
|
388
|
+
const held = values.value.map((value, row) => heldValue(value, atEdge[row]) ?? value);
|
|
389
|
+
return ok({
|
|
390
|
+
values: held,
|
|
391
|
+
outside
|
|
392
|
+
});
|
|
393
|
+
};
|
|
394
|
+
/** This function gives the interval of some replicate values at the share `level`, made wider to hold `value`. */
|
|
395
|
+
const intervalOf = (value, spread, level) => {
|
|
396
|
+
const share = rangeOf(spread, level);
|
|
397
|
+
return {
|
|
398
|
+
low: Math.min(share.low, value),
|
|
399
|
+
high: Math.max(share.high, value)
|
|
400
|
+
};
|
|
401
|
+
};
|
|
402
|
+
/**
|
|
403
|
+
* This function gives the value of a law at a load point, with its interval. The point gives each
|
|
404
|
+
* variable of the law, and no other name.
|
|
405
|
+
*
|
|
406
|
+
* The function evaluates the program with the constants, then with each replicate. The interval is
|
|
407
|
+
* the share `level` of the values of the replicates, made wider to hold the value.
|
|
408
|
+
*
|
|
409
|
+
* The function gives a problem for a point that the law cannot read. It also gives a problem for a
|
|
410
|
+
* level that the law cannot show, and for a value that is not finite.
|
|
411
|
+
*/
|
|
412
|
+
const lawValue = (law, point, options = {}) => {
|
|
413
|
+
const level = options.level ?? .95;
|
|
414
|
+
const noLevel = levelProblem(law, level, true);
|
|
415
|
+
if (noLevel !== void 0) return err(noLevel);
|
|
416
|
+
const evaluated = pointValuesOf(law, point);
|
|
417
|
+
if (evaluated.tag === "err") return evaluated;
|
|
418
|
+
const [value, ...spread] = evaluated.value.values;
|
|
419
|
+
const interval = intervalOf(value, spread, level);
|
|
420
|
+
return ok({
|
|
421
|
+
value,
|
|
422
|
+
unit: law.measure.unit,
|
|
423
|
+
level,
|
|
424
|
+
interval,
|
|
425
|
+
run: runOf(law, interval, level),
|
|
426
|
+
outside: evaluated.value.outside
|
|
427
|
+
});
|
|
428
|
+
};
|
|
429
|
+
/**
|
|
430
|
+
* This function gives each constant of a law with the interval of its replicates. It gives a
|
|
431
|
+
* problem for a level that the law cannot show.
|
|
432
|
+
*/
|
|
433
|
+
const constantIntervals = (law, options = {}) => {
|
|
434
|
+
const level = options.level ?? .95;
|
|
435
|
+
const noLevel = levelProblem(law, level, false);
|
|
436
|
+
if (noLevel !== void 0) return err(noLevel);
|
|
437
|
+
return ok(law.constants.map((constant, index) => ({
|
|
438
|
+
id: constant.id,
|
|
439
|
+
value: constant.value,
|
|
440
|
+
level,
|
|
441
|
+
interval: rangeOf(law.replicates.map((row) => row[index]), level)
|
|
442
|
+
})));
|
|
443
|
+
};
|
|
444
|
+
// src/workload.ts
|
|
445
|
+
/** This text is the format of a workload file. */
|
|
446
|
+
const WORKLOAD_FORMAT = "capacity-workload/1";
|
|
447
|
+
/** This name is the variable that gives the rate of a class. A class and a level cannot use it for a different amount. */
|
|
448
|
+
const RATE = "rate";
|
|
449
|
+
/** This demand has no class and no level. It is the zero of the sum of demands. */
|
|
450
|
+
const NO_DEMAND = {
|
|
451
|
+
classes: [],
|
|
452
|
+
levels: {}
|
|
453
|
+
};
|
|
454
|
+
// src/workload-check.ts
|
|
455
|
+
/** This function makes a problem of a workload. */
|
|
456
|
+
const workloadProblem = (code, path, message) => ({
|
|
457
|
+
code,
|
|
458
|
+
path,
|
|
459
|
+
message
|
|
460
|
+
});
|
|
461
|
+
/** This function gives the problems of some amounts: the name and the amount of each. */
|
|
462
|
+
const amountProblems = (amounts, path, owner) => {
|
|
463
|
+
const problems = [];
|
|
464
|
+
for (const [name, amount] of Object.entries(amounts)) {
|
|
465
|
+
const at = `${path}.${name}`;
|
|
466
|
+
if (!NAME.test(name)) problems.push(workloadProblem("id", at, `The name "${name}" is not letters and digits that start with a lower-case letter.`));
|
|
467
|
+
if (name === "rate") problems.push(workloadProblem("id", at, `The name "${RATE}" is the rate of a class. ${owner} cannot use it.`));
|
|
468
|
+
if (!isQuantity(amount)) problems.push(workloadProblem("amount", at, `"${name}" is not a finite amount in a canonical unit.`));
|
|
469
|
+
}
|
|
470
|
+
return problems;
|
|
471
|
+
};
|
|
472
|
+
/**
|
|
473
|
+
* This function examines a demand and gives each problem that it finds. A demand with no problem
|
|
474
|
+
* gives an empty list.
|
|
475
|
+
*
|
|
476
|
+
* It examines the id, the class of the app and the rate of each class. It examines the name and the amount of
|
|
477
|
+
* each variable and each level. It examines that a variable has one kind of unit in each class, and that no name is
|
|
478
|
+
* a level and a variable.
|
|
479
|
+
*/
|
|
480
|
+
const demandProblems = (demand) => {
|
|
481
|
+
const problems = [];
|
|
482
|
+
/** The class that first gives each variable of a request, and the unit of the variable there. */
|
|
483
|
+
const first = /* @__PURE__ */ new Map();
|
|
484
|
+
const ids = /* @__PURE__ */ new Set();
|
|
485
|
+
let twice;
|
|
486
|
+
demand.classes.forEach((entry, index) => {
|
|
487
|
+
const path = `classes[${index}]`;
|
|
488
|
+
if (ids.has(entry.id)) twice ??= entry.id;
|
|
489
|
+
ids.add(entry.id);
|
|
490
|
+
if (!SLUG.test(entry.id)) problems.push(workloadProblem("id", `${path}.id`, `The id "${entry.id}" is not lower-case words joined by ".", "-" or "/".`));
|
|
491
|
+
if (entry.of !== void 0 && !SLUG.test(entry.of)) problems.push(workloadProblem("class", `${path}.of`, `The class "${entry.of}" of the app is not lower-case words joined by ".", "-" or "/".`));
|
|
492
|
+
if (!isQuantity(entry.rate)) problems.push(workloadProblem("rate", `${path}.rate`, `The rate of the class "${entry.id}" is not a finite amount in a canonical unit.`));
|
|
493
|
+
else if (kindOf(entry.rate.unit) !== "frequency") problems.push(workloadProblem("rate", `${path}.rate`, `The rate of the class "${entry.id}" is in "${entry.rate.unit}". A rate is a count in a unit of time, for example in "{request}/s".`));
|
|
494
|
+
else if (entry.rate.value < 0) problems.push(workloadProblem("rate", `${path}.rate`, `The rate of the class "${entry.id}" is below 0.`));
|
|
495
|
+
problems.push(...amountProblems(entry.request, `${path}.request`, "A variable of a request"));
|
|
496
|
+
for (const [name, amount] of Object.entries(entry.request)) {
|
|
497
|
+
const unit = isQuantity(amount) ? amount.unit : void 0;
|
|
498
|
+
const before = first.get(name);
|
|
499
|
+
if (before === void 0) first.set(name, {
|
|
500
|
+
class: entry.id,
|
|
501
|
+
unit
|
|
502
|
+
});
|
|
503
|
+
else if (unit !== void 0 && before.unit !== void 0 && !convertible(before.unit, unit)) problems.push(workloadProblem("unit", `${path}.request.${name}`, `The variable "${name}" is in "${unit}" here, and it is in "${before.unit}" in the class "${before.class}".`));
|
|
504
|
+
}
|
|
505
|
+
});
|
|
506
|
+
if (twice !== void 0) problems.push(workloadProblem("id", "classes", `Two classes have the id "${twice}".`));
|
|
507
|
+
problems.push(...amountProblems(demand.levels, "levels", "A level"));
|
|
508
|
+
for (const name of Object.keys(demand.levels)) {
|
|
509
|
+
const before = first.get(name);
|
|
510
|
+
if (before !== void 0) problems.push(workloadProblem("id", `levels.${name}`, `"${name}" names a level and a variable of the class "${before.class}".`));
|
|
511
|
+
}
|
|
512
|
+
return problems;
|
|
513
|
+
};
|
|
514
|
+
/**
|
|
515
|
+
* This function examines a workload and gives each problem that it finds. A workload with no
|
|
516
|
+
* problem gives an empty list.
|
|
517
|
+
*
|
|
518
|
+
* It examines the format, the id and the title. It examines the demand with `demandProblems`. It
|
|
519
|
+
* examines that a plan has a note. It examines that the window of a metrics source goes from a UTC
|
|
520
|
+
* time to a later UTC time.
|
|
521
|
+
*/
|
|
522
|
+
const checkWorkload = (workload) => {
|
|
523
|
+
const problems = [];
|
|
524
|
+
if (workload.format !== "capacity-workload/1") problems.push(workloadProblem("format", "format", `The format is "${String(workload.format)}", not "${WORKLOAD_FORMAT}".`));
|
|
525
|
+
if (!SLUG.test(workload.id)) problems.push(workloadProblem("id", "id", `The id "${workload.id}" is not lower-case words joined by ".", "-" or "/".`));
|
|
526
|
+
if (workload.title.trim() === "") problems.push(workloadProblem("id", "title", "The title is empty."));
|
|
527
|
+
problems.push(...demandProblems(workload));
|
|
528
|
+
const source = workload.source;
|
|
529
|
+
if (source.kind === "plan") {
|
|
530
|
+
if (source.note.trim() === "") problems.push(workloadProblem("source", "source.note", "The note of the plan is empty."));
|
|
531
|
+
return problems;
|
|
532
|
+
}
|
|
533
|
+
if (source.origin.trim() === "") problems.push(workloadProblem("source", "source.origin", "The origin of the metrics is empty."));
|
|
534
|
+
for (const end of ["from", "to"]) if (!isUtc(source[end])) problems.push(workloadProblem("source", `source.${end}`, `The time "${source[end]}" is not a UTC time, for example "2026-11-18T14:00:00Z".`));
|
|
535
|
+
if (isUtc(source.from) && isUtc(source.to) && !(Date.parse(source.from) < Date.parse(source.to))) problems.push(workloadProblem("source", "source", "The window of the metrics does not go from a time to a later time."));
|
|
536
|
+
return problems;
|
|
537
|
+
};
|
|
538
|
+
// src/app.ts
|
|
539
|
+
/** This text is the format of the contract file of an app. */
|
|
540
|
+
const APP_FORMAT = "capacity-app/1";
|
|
541
|
+
/** This name is the contract file of an app, at the root of its repository. */
|
|
542
|
+
const APP_FILE = "capacity-app.json";
|
|
543
|
+
/** The kinds of the lines of a run that the lab records. The lab knows the fields of each kind. */
|
|
544
|
+
const APP_LINE_KINDS = [
|
|
545
|
+
"pass",
|
|
546
|
+
"write",
|
|
547
|
+
"readings",
|
|
548
|
+
"amounts"
|
|
549
|
+
];
|
|
550
|
+
// src/app-check.ts
|
|
551
|
+
/** An HTTPS URL: the scheme `https`, a host, and then a path, a query or a fragment with no space. */
|
|
552
|
+
const HTTPS = /^https:\/\/[^\s/?#@]+(?:[/?#]\S*)?$/i;
|
|
553
|
+
/** This function tells if a text is an HTTPS URL. The package uses no global of a browser or of Node.js. */
|
|
554
|
+
const isHttps = (text) => HTTPS.test(text);
|
|
555
|
+
/** This function makes a problem of a contract or of a workload against a contract. */
|
|
556
|
+
const appProblem = (code, path, message) => ({
|
|
557
|
+
code,
|
|
558
|
+
path,
|
|
559
|
+
message
|
|
560
|
+
});
|
|
561
|
+
/** The name of a series: letters, digits, `_`, `.`, `/` and `-`, that start with a letter. */
|
|
562
|
+
const METRIC = /^[A-Za-z][A-Za-z0-9_./-]*$/;
|
|
563
|
+
/** The name of an environment variable: upper-case letters, digits and `_`, that start with a letter or `_`. */
|
|
564
|
+
const ENV = /^[A-Z_][A-Z0-9_]*$/;
|
|
565
|
+
const PLACEHOLDER = /\{([^{}]*)\}/g;
|
|
566
|
+
/** These names are the plain placeholders that each place accepts. `service` and `variable` take an id after a `.`. */
|
|
567
|
+
const PLAIN = {
|
|
568
|
+
install: [],
|
|
569
|
+
check: [],
|
|
570
|
+
seed: ["store"],
|
|
571
|
+
run: ["port", "store"],
|
|
572
|
+
fake: ["port", "workload"]
|
|
573
|
+
};
|
|
574
|
+
/** This function gives the names of the placeholders in a text, in their order. */
|
|
575
|
+
const placeholdersOf = (text) => [...text.matchAll(PLACEHOLDER)].map((match) => match[1] ?? "");
|
|
576
|
+
/**
|
|
577
|
+
* This function puts the value of each placeholder into a text. It gives undefined when the text holds a
|
|
578
|
+
* placeholder that `values` does not give.
|
|
579
|
+
*/
|
|
580
|
+
const fillPlaceholders = (text, values) => {
|
|
581
|
+
if (placeholdersOf(text).some((name) => !Object.hasOwn(values, name))) return void 0;
|
|
582
|
+
return text.replace(PLACEHOLDER, (_match, name) => values[name] ?? "");
|
|
583
|
+
};
|
|
584
|
+
/**
|
|
585
|
+
* This function gives the path of a request of a class of HTTP at some amounts. Each amount is a number in
|
|
586
|
+
* the unit of its variable. It gives undefined when `values` does not give each variable of the path.
|
|
587
|
+
*/
|
|
588
|
+
const requestPath = (entry, values) => fillPlaceholders(entry.path, Object.fromEntries(Object.entries(values).map(([name, value]) => [name, encodeURIComponent(String(value))])));
|
|
589
|
+
/** This function gives the problems of a range: its unit, its two ends and its scale. */
|
|
590
|
+
const rangeProblems = (range, path, owner) => {
|
|
591
|
+
const problems = [];
|
|
592
|
+
if (!isCanonicalUnit(range.unit)) problems.push(appProblem("unit", `${path}.unit`, `The unit "${range.unit}" of ${owner} is not a canonical unit.`));
|
|
593
|
+
if (!finite(range.min) || !finite(range.max) || range.min > range.max) problems.push(appProblem("range", path, `The range of ${owner} does not go from a finite amount to the same or a larger amount.`));
|
|
594
|
+
else if (range.scale === "log" && !(range.min > 0)) problems.push(appProblem("range", `${path}.min`, `The range of ${owner} is on the scale "log", thus it starts above 0.`));
|
|
595
|
+
return problems;
|
|
596
|
+
};
|
|
597
|
+
/** This function gives the problems of the variables: the id, the title and the range of each. */
|
|
598
|
+
const variableProblems = (variables) => {
|
|
599
|
+
const problems = [];
|
|
600
|
+
const ids = /* @__PURE__ */ new Set();
|
|
601
|
+
variables.forEach((variable, index) => {
|
|
602
|
+
const path = `variables[${index}]`;
|
|
603
|
+
if (!NAME.test(variable.id)) problems.push(appProblem("id", `${path}.id`, `The id "${variable.id}" is not letters and digits that start with a lower-case letter.`));
|
|
604
|
+
else if (variable.id === "rate") problems.push(appProblem("id", `${path}.id`, `The id "${RATE}" is the rate of a class. A variable cannot use it.`));
|
|
605
|
+
else if (ids.has(variable.id)) problems.push(appProblem("id", `${path}.id`, `Two variables have the id "${variable.id}".`));
|
|
606
|
+
ids.add(variable.id);
|
|
607
|
+
if (variable.title.trim() === "") problems.push(appProblem("id", `${path}.title`, `The title of the variable "${variable.id}" is empty.`));
|
|
608
|
+
problems.push(...rangeProblems(variable, path, `the variable "${variable.id}"`));
|
|
609
|
+
});
|
|
610
|
+
return problems;
|
|
611
|
+
};
|
|
612
|
+
/** This function gives the problems of the entry of a class. An entry of HTTP sets each request variable of the class. */
|
|
613
|
+
const entryProblems = (app, entry, path) => {
|
|
614
|
+
const problems = [];
|
|
615
|
+
const at = `${path}.entry`;
|
|
616
|
+
if (entry.entry.kind === "service") {
|
|
617
|
+
const { service } = entry.entry;
|
|
618
|
+
if (!app.services.some((each) => each.id === service)) problems.push(appProblem("service", `${at}.service`, `The class "${entry.id}" names the service "${service}", and the app has no service with that id.`));
|
|
619
|
+
return problems;
|
|
620
|
+
}
|
|
621
|
+
const { path: template, body } = entry.entry;
|
|
622
|
+
if (!template.startsWith("/")) problems.push(appProblem("entry", `${at}.path`, `The path of the class "${entry.id}" does not start with "/".`));
|
|
623
|
+
const named = placeholdersOf(template);
|
|
624
|
+
for (const name of named) if (!entry.request.includes(name)) problems.push(appProblem("placeholder", `${at}.path`, `The path of the class "${entry.id}" holds "{${name}}", and "${name}" is not a request variable of the class.`));
|
|
625
|
+
if (body !== void 0) {
|
|
626
|
+
const variable = app.variables.find((each) => each.id === body);
|
|
627
|
+
if (!entry.request.includes(body)) problems.push(appProblem("entry", `${at}.body`, `The body of the class "${entry.id}" is the variable "${body}", and it is not a request variable of the class.`));
|
|
628
|
+
else if (variable !== void 0 && isCanonicalUnit(variable.unit) && !convertible(variable.unit, "B")) problems.push(appProblem("unit", `${at}.body`, `The body of the class "${entry.id}" is the variable "${body}" in "${variable.unit}", not in bytes.`));
|
|
629
|
+
}
|
|
630
|
+
for (const name of entry.request) if (!named.includes(name) && name !== body) problems.push(appProblem("entry", at, `A request of the class "${entry.id}" cannot set the variable "${name}": neither the path nor the body holds it.`));
|
|
631
|
+
return problems;
|
|
632
|
+
};
|
|
633
|
+
/** This function gives the problems of the classes: the id, the rate, the request variables and the entry of each. */
|
|
634
|
+
const classProblems = (app) => {
|
|
635
|
+
const problems = [];
|
|
636
|
+
const ids = /* @__PURE__ */ new Set();
|
|
637
|
+
app.classes.forEach((entry, index) => {
|
|
638
|
+
const path = `classes[${index}]`;
|
|
639
|
+
if (!SLUG.test(entry.id)) problems.push(appProblem("id", `${path}.id`, `The id "${entry.id}" is not lower-case words joined by ".", "-" or "/".`));
|
|
640
|
+
else if (ids.has(entry.id)) problems.push(appProblem("id", `${path}.id`, `Two classes have the id "${entry.id}".`));
|
|
641
|
+
ids.add(entry.id);
|
|
642
|
+
if (entry.title.trim() === "") problems.push(appProblem("id", `${path}.title`, `The title of the class "${entry.id}" is empty.`));
|
|
643
|
+
problems.push(...rangeProblems(entry.rate, `${path}.rate`, `the rate of the class "${entry.id}"`));
|
|
644
|
+
if (isCanonicalUnit(entry.rate.unit) && kindOf(entry.rate.unit) !== "frequency") problems.push(appProblem("unit", `${path}.rate.unit`, `The rate of the class "${entry.id}" is in "${entry.rate.unit}". A rate is a count in a unit of time, for example "{request}/s".`));
|
|
645
|
+
entry.request.forEach((name, place) => {
|
|
646
|
+
const variable = app.variables.find((each) => each.id === name);
|
|
647
|
+
if (variable === void 0) problems.push(appProblem("variable", `${path}.request[${place}]`, `The class "${entry.id}" names the variable "${name}", and the app has no variable with that id.`));
|
|
648
|
+
else if (variable.kind !== "request") problems.push(appProblem("variable", `${path}.request[${place}]`, `The variable "${name}" is a ${variable.kind} variable, not a request variable.`));
|
|
649
|
+
else if (entry.request.indexOf(name) !== place) problems.push(appProblem("variable", `${path}.request[${place}]`, `The class "${entry.id}" names the variable "${name}" two times.`));
|
|
650
|
+
});
|
|
651
|
+
problems.push(...entryProblems(app, entry, path));
|
|
652
|
+
});
|
|
653
|
+
app.variables.forEach((variable, index) => {
|
|
654
|
+
if (variable.kind === "request" && !app.classes.some((entry) => entry.request.includes(variable.id))) problems.push(appProblem("variable", `variables[${index}]`, `No class has the request variable "${variable.id}".`));
|
|
655
|
+
});
|
|
656
|
+
return problems;
|
|
657
|
+
};
|
|
658
|
+
/** This function gives the problems of the placeholders of a command at its place in the contract. */
|
|
659
|
+
const placeholderProblems = (app, command, path, place) => {
|
|
660
|
+
const problems = [];
|
|
661
|
+
const texts = [...command.argv.map((text, index) => [`${path}.argv[${index}]`, text]), ...Object.entries(command.env).map(([name, text]) => [`${path}.env.${name}`, text])];
|
|
662
|
+
for (const [at, text] of texts) for (const name of placeholdersOf(text)) {
|
|
663
|
+
const [head, ...rest] = name.split(".");
|
|
664
|
+
const id = rest.join(".");
|
|
665
|
+
if (rest.length === 0 && PLAIN[place].includes(name)) continue;
|
|
666
|
+
if (head === "service" && place === "run" && id !== "") {
|
|
667
|
+
if (!app.services.some((each) => each.id === id)) problems.push(appProblem("placeholder", at, `The placeholder "{${name}}" names the service "${id}", and the app has no service with that id.`));
|
|
668
|
+
continue;
|
|
669
|
+
}
|
|
670
|
+
if (head === "variable" && place === "seed" && id !== "") {
|
|
671
|
+
if (!app.variables.some((each) => each.id === id && (each.kind === "store" || each.kind === "level"))) problems.push(appProblem("placeholder", at, `The placeholder "{${name}}" names "${id}", and the app has no store or level variable with that id.`));
|
|
672
|
+
continue;
|
|
673
|
+
}
|
|
674
|
+
problems.push(appProblem("placeholder", at, `The command ${place} cannot hold the placeholder "{${name}}".`));
|
|
675
|
+
}
|
|
676
|
+
return problems;
|
|
677
|
+
};
|
|
678
|
+
/** This function gives the problems of a ready signal. */
|
|
679
|
+
const readyProblems = (ready, path) => {
|
|
680
|
+
if (ready.kind === "line") return ready.text.trim() === "" ? [appProblem("command", `${path}.text`, "The text of the ready line is empty.")] : [];
|
|
681
|
+
return ready.path.startsWith("/") ? [] : [appProblem("command", `${path}.path`, `The ready path "${ready.path}" does not start with "/".`)];
|
|
682
|
+
};
|
|
683
|
+
/**
|
|
684
|
+
* This function gives the problems of the lines of a run. Each line has a text `ev` that is not empty, and no two lines
|
|
685
|
+
* have the same kind or the same `ev`.
|
|
686
|
+
*/
|
|
687
|
+
const lineProblems = (lines, path) => {
|
|
688
|
+
const problems = [];
|
|
689
|
+
const kinds = /* @__PURE__ */ new Set();
|
|
690
|
+
const evs = /* @__PURE__ */ new Set();
|
|
691
|
+
lines.forEach((line, index) => {
|
|
692
|
+
const at = `${path}[${index}]`;
|
|
693
|
+
if (line.ev.trim() === "") problems.push(appProblem("command", `${at}.ev`, "The field ev of the line is empty."));
|
|
694
|
+
if (kinds.has(line.kind)) problems.push(appProblem("command", `${at}.kind`, `Two lines of the run have the kind "${line.kind}".`));
|
|
695
|
+
if (evs.has(line.ev)) problems.push(appProblem("command", `${at}.ev`, `Two lines of the run have the ev "${line.ev}".`));
|
|
696
|
+
kinds.add(line.kind);
|
|
697
|
+
evs.add(line.ev);
|
|
698
|
+
});
|
|
699
|
+
return problems;
|
|
700
|
+
};
|
|
701
|
+
/** This function gives the problems of a command: its program, its folder, the names of its variables and its placeholders. */
|
|
702
|
+
const commandProblems = (app, command, path, place) => {
|
|
703
|
+
const problems = [];
|
|
704
|
+
if (command.argv.length === 0 || (command.argv[0] ?? "").trim() === "") problems.push(appProblem("command", `${path}.argv`, `The command ${place} has no program.`));
|
|
705
|
+
const parts = command.cwd.split("/");
|
|
706
|
+
if (command.cwd.startsWith("/") || command.cwd.includes("\\") || parts.some((part) => part === "..") || command.cwd === "") problems.push(appProblem("command", `${path}.cwd`, `The folder "${command.cwd}" is not a path in the repository, with "/" and no "..". The root is ".".`));
|
|
707
|
+
for (const name of Object.keys(command.env)) if (!ENV.test(name)) problems.push(appProblem("command", `${path}.env.${name}`, `The name "${name}" is not an environment variable: upper-case letters, digits and "_".`));
|
|
708
|
+
problems.push(...placeholderProblems(app, command, path, place));
|
|
709
|
+
return problems;
|
|
710
|
+
};
|
|
711
|
+
/** This function tells if a command holds a placeholder. */
|
|
712
|
+
const holds = (command, placeholder) => [...command.argv, ...Object.values(command.env)].some((text) => placeholdersOf(text).includes(placeholder));
|
|
713
|
+
/**
|
|
714
|
+
* This function gives the problems of the start and of the outside services. Each process gets its port, the
|
|
715
|
+
* seed and the run get the store, and the run gets the URL of each service. The seed sets each store variable.
|
|
716
|
+
*/
|
|
717
|
+
const startProblems = (app) => {
|
|
718
|
+
const problems = [];
|
|
719
|
+
const { install, check, seed, run } = app.start;
|
|
720
|
+
if (install !== void 0) problems.push(...commandProblems(app, install, "start.install", "install"));
|
|
721
|
+
if (check !== void 0) problems.push(...commandProblems(app, check, "start.check", "check"));
|
|
722
|
+
problems.push(...commandProblems(app, run, "start.run", "run"), ...readyProblems(run.ready, "start.run.ready"), ...lineProblems(run.lines ?? [], "start.run.lines"));
|
|
723
|
+
if ((run.ready.kind === "http" || app.classes.some((entry) => entry.entry.kind === "http")) && !holds(run, "port")) problems.push(appProblem("command", "start.run", `The run does not get its port: it does not hold "{port}".`));
|
|
724
|
+
const measured = app.variables.filter((variable) => variable.kind === "run");
|
|
725
|
+
if (measured.length > 0 && !(run.lines ?? []).some((line) => line.kind === "amounts")) problems.push(appProblem("command", "start.run.lines", `The app has the run variable "${measured[0]?.id}", and the run has no line of the kind "amounts" that gives it.`));
|
|
726
|
+
const stores = app.variables.filter((variable) => variable.kind === "store");
|
|
727
|
+
if (stores.length > 0 && !holds(run, "store")) problems.push(appProblem("command", "start.run", `The app has a store, and the run does not get its folder: the run does not hold "{store}".`));
|
|
728
|
+
if (seed === void 0) {
|
|
729
|
+
if (stores.length > 0) problems.push(appProblem("command", "start", `The app has the store variables ${stores.map((each) => `"${each.id}"`).join(", ")}, and no seed.`));
|
|
730
|
+
} else {
|
|
731
|
+
problems.push(...commandProblems(app, seed, "start.seed", "seed"));
|
|
732
|
+
if (stores.length === 0) problems.push(appProblem("command", "start.seed", "The app has a seed, and no store variable for it to set."));
|
|
733
|
+
else if (!holds(seed, "store")) problems.push(appProblem("command", "start.seed", `The seed does not get the folder of the store: it does not hold "{store}".`));
|
|
734
|
+
for (const variable of stores) if (!holds(seed, `variable.${variable.id}`)) problems.push(appProblem("command", "start.seed", `The seed cannot set the store variable "${variable.id}": it does not hold "{variable.${variable.id}}".`));
|
|
735
|
+
}
|
|
736
|
+
const ids = /* @__PURE__ */ new Set();
|
|
737
|
+
app.services.forEach((service, index) => {
|
|
738
|
+
const path = `services[${index}]`;
|
|
739
|
+
if (!SLUG.test(service.id)) problems.push(appProblem("id", `${path}.id`, `The id "${service.id}" is not lower-case words joined by ".", "-" or "/".`));
|
|
740
|
+
else if (ids.has(service.id)) problems.push(appProblem("id", `${path}.id`, `Two services have the id "${service.id}".`));
|
|
741
|
+
ids.add(service.id);
|
|
742
|
+
if (service.title.trim() === "") problems.push(appProblem("id", `${path}.title`, `The title of the service "${service.id}" is empty.`));
|
|
743
|
+
problems.push(...commandProblems(app, service.fake, `${path}.fake`, "fake"), ...readyProblems(service.fake.ready, `${path}.fake.ready`));
|
|
744
|
+
if (!holds(service.fake, "port")) problems.push(appProblem("command", `${path}.fake`, `The fake of "${service.id}" does not get its port: it does not hold "{port}".`));
|
|
745
|
+
if (app.classes.some((entry) => entry.entry.kind === "service" && entry.entry.service === service.id) && !holds(service.fake, "workload")) problems.push(appProblem("command", `${path}.fake`, `A class comes from "${service.id}", and its fake does not get the rates: it does not hold "{workload}".`));
|
|
746
|
+
if (!holds(run, `service.${service.id}`)) problems.push(appProblem("service", path, `The app does not get the URL of the fake of "${service.id}": the run does not hold "{service.${service.id}}".`));
|
|
747
|
+
});
|
|
748
|
+
return problems;
|
|
749
|
+
};
|
|
750
|
+
/** This function gives the key of the target of a series. Two series with one key measure the same thing. */
|
|
751
|
+
const targetKey = (series) => JSON.stringify(Object.entries(series.of).sort(([a], [b]) => a.localeCompare(b)));
|
|
752
|
+
/** This function gives the problems of one series: its target, its kind and its unit. */
|
|
753
|
+
const seriesProblems = (app, series, path) => {
|
|
754
|
+
const problems = [];
|
|
755
|
+
const at = `${path}.of`;
|
|
756
|
+
const target = series.of;
|
|
757
|
+
const kind = (wanted, what) => {
|
|
758
|
+
if (series.kind !== wanted) problems.push(appProblem("series", `${path}.kind`, `The series "${series.id}" gives ${what}, thus it is a ${wanted}.`));
|
|
759
|
+
};
|
|
760
|
+
const unit = (wanted, what) => {
|
|
761
|
+
if (isCanonicalUnit(series.unit) && isCanonicalUnit(wanted) && !convertible(series.unit, wanted)) problems.push(appProblem("unit", `${path}.unit`, `The series "${series.id}" is in "${series.unit}". ${what} is in "${wanted}".`));
|
|
762
|
+
};
|
|
763
|
+
if ("rate" in target) {
|
|
764
|
+
const entry = app.classes.find((each) => each.id === target.rate);
|
|
765
|
+
kind("counter", "the rate of a class");
|
|
766
|
+
if (entry === void 0) return [...problems, appProblem("series", at, `The series "${series.id}" names the class "${target.rate}", and the app has no class with that id.`)];
|
|
767
|
+
const perSecond = isCanonicalUnit(series.unit) ? divideUnits(series.unit, "s") : void 0;
|
|
768
|
+
if (perSecond !== void 0 && isCanonicalUnit(entry.rate.unit) && kindOf(entry.rate.unit) === "frequency" && !convertible(perSecond, entry.rate.unit)) problems.push(appProblem("unit", `${path}.unit`, `The series "${series.id}" gives a rate in "${perSecond}". The rate of the class "${entry.id}" is in "${entry.rate.unit}".`));
|
|
769
|
+
return problems;
|
|
770
|
+
}
|
|
771
|
+
if ("level" in target) {
|
|
772
|
+
const variable = app.variables.find((each) => each.id === target.level);
|
|
773
|
+
kind("gauge", "a level");
|
|
774
|
+
if (variable === void 0 || variable.kind !== "level") return [...problems, appProblem("series", at, `The series "${series.id}" names "${target.level}", and the app has no level variable with that id.`)];
|
|
775
|
+
unit(variable.unit, `The level "${variable.id}"`);
|
|
776
|
+
return problems;
|
|
777
|
+
}
|
|
778
|
+
if ("total" in target) {
|
|
779
|
+
const entry = app.classes.find((each) => each.id === target.class);
|
|
780
|
+
const variable = app.variables.find((each) => each.id === target.total);
|
|
781
|
+
kind("counter", "a total over the requests of a class");
|
|
782
|
+
if (entry === void 0) return [...problems, appProblem("series", at, `The series "${series.id}" names the class "${target.class}", and the app has no class with that id.`)];
|
|
783
|
+
if (variable === void 0 || !entry.request.includes(variable.id)) return [...problems, appProblem("series", at, `The series "${series.id}" names "${target.total}", and it is not a request variable of the class "${entry.id}".`)];
|
|
784
|
+
unit(variable.unit, `The variable "${variable.id}"`);
|
|
785
|
+
return problems;
|
|
786
|
+
}
|
|
787
|
+
if (!SLUG.test(target.measure)) problems.push(appProblem("series", `${at}.measure`, `The measure "${target.measure}" is not lower-case words joined by ".", "-" or "/".`));
|
|
788
|
+
return problems;
|
|
789
|
+
};
|
|
790
|
+
/** This function gives the problems of the live metrics: the id, the unit and the target of each series. */
|
|
791
|
+
const metricsProblems = (app) => {
|
|
792
|
+
const problems = [];
|
|
793
|
+
const ids = /* @__PURE__ */ new Set();
|
|
794
|
+
const targets = /* @__PURE__ */ new Map();
|
|
795
|
+
app.metrics.forEach((series, index) => {
|
|
796
|
+
const path = `metrics[${index}]`;
|
|
797
|
+
if (!METRIC.test(series.id)) problems.push(appProblem("id", `${path}.id`, `The id "${series.id}" is not a name of a metric: letters, digits, "_", ".", "/" and "-".`));
|
|
798
|
+
else if (ids.has(series.id)) problems.push(appProblem("id", `${path}.id`, `Two series have the id "${series.id}".`));
|
|
799
|
+
ids.add(series.id);
|
|
800
|
+
if (!isCanonicalUnit(series.unit)) problems.push(appProblem("unit", `${path}.unit`, `The unit "${series.unit}" of the series "${series.id}" is not a canonical unit.`));
|
|
801
|
+
const key = targetKey(series);
|
|
802
|
+
const before = targets.get(key);
|
|
803
|
+
if (before !== void 0) problems.push(appProblem("series", `${path}.of`, `The series "${series.id}" and "${before}" measure the same thing.`));
|
|
804
|
+
else targets.set(key, series.id);
|
|
805
|
+
problems.push(...seriesProblems(app, series, path));
|
|
806
|
+
});
|
|
807
|
+
return problems;
|
|
808
|
+
};
|
|
809
|
+
/**
|
|
810
|
+
* This function examines the contract of an app and gives each problem that it finds. A contract with no
|
|
811
|
+
* problem gives an empty list.
|
|
812
|
+
*
|
|
813
|
+
* It examines the format, the id, the title and the repository. It examines each variable, each class and
|
|
814
|
+
* its entry, each command and its placeholders, each outside service and each series of the live metrics.
|
|
815
|
+
*/
|
|
816
|
+
const checkApp = (app) => {
|
|
817
|
+
const problems = [];
|
|
818
|
+
if (app.format !== "capacity-app/1") problems.push(appProblem("format", "format", `The format is "${String(app.format)}", not "${APP_FORMAT}".`));
|
|
819
|
+
if (!SLUG.test(app.id)) problems.push(appProblem("id", "id", `The id "${app.id}" is not lower-case words joined by ".", "-" or "/".`));
|
|
820
|
+
if (app.title.trim() === "") problems.push(appProblem("id", "title", "The title is empty."));
|
|
821
|
+
if (!isHttps(app.repository)) problems.push(appProblem("id", "repository", `The repository "${app.repository}" is not an HTTPS URL.`));
|
|
822
|
+
return [
|
|
823
|
+
...problems,
|
|
824
|
+
...variableProblems(app.variables),
|
|
825
|
+
...classProblems(app),
|
|
826
|
+
...startProblems(app),
|
|
827
|
+
...metricsProblems(app)
|
|
828
|
+
];
|
|
829
|
+
};
|
|
830
|
+
/**
|
|
831
|
+
* This function gives each part of the demand of an app that its live metrics cannot give. A contract whose
|
|
832
|
+
* metrics give its whole demand gives an empty list. The live check of a law reads a demand with no gap.
|
|
833
|
+
*/
|
|
834
|
+
const metricsGaps = (app) => {
|
|
835
|
+
const has = (test) => app.metrics.some(test);
|
|
836
|
+
const gaps = [];
|
|
837
|
+
for (const entry of app.classes) {
|
|
838
|
+
if (!has((series) => "rate" in series.of && series.of.rate === entry.id)) gaps.push({
|
|
839
|
+
need: "rate",
|
|
840
|
+
class: entry.id,
|
|
841
|
+
message: `No counter gives the rate of the class "${entry.id}".`
|
|
842
|
+
});
|
|
843
|
+
for (const name of entry.request) if (!has((series) => "total" in series.of && series.of.total === name && series.of.class === entry.id)) gaps.push({
|
|
844
|
+
need: "total",
|
|
845
|
+
class: entry.id,
|
|
846
|
+
variable: name,
|
|
847
|
+
message: `No counter gives the total of "${name}" over the requests of the class "${entry.id}".`
|
|
848
|
+
});
|
|
849
|
+
}
|
|
850
|
+
for (const variable of app.variables) if (variable.kind === "level" && !has((series) => "level" in series.of && series.of.level === variable.id)) gaps.push({
|
|
851
|
+
need: "level",
|
|
852
|
+
variable: variable.id,
|
|
853
|
+
message: `No gauge gives the level "${variable.id}".`
|
|
854
|
+
});
|
|
855
|
+
return gaps;
|
|
856
|
+
};
|
|
857
|
+
/** This function gives the problem of an amount against the range of its variable: its unit, then its value. */
|
|
858
|
+
const amountProblem = (amount, range, path, owner) => {
|
|
859
|
+
if (!isQuantity(amount)) return void 0;
|
|
860
|
+
if (!convertible(amount.unit, range.unit)) return appProblem("unit", path, `${owner} is in "${amount.unit}", and the app gives it in "${range.unit}".`);
|
|
861
|
+
const value = valueIn(amount, range.unit);
|
|
862
|
+
if (value < range.min || value > range.max) return appProblem("range", path, `${owner} is ${value} ${range.unit}. The app accepts ${range.min} to ${range.max} ${range.unit}.`);
|
|
863
|
+
};
|
|
864
|
+
/**
|
|
865
|
+
* This function examines a workload against the contract of its app, and gives each problem that it finds.
|
|
866
|
+
* The workload is a workload that `checkWorkload` accepts.
|
|
867
|
+
*
|
|
868
|
+
* Each class of the workload is a class of the app: the class that its field `of` names, or else its id. Its
|
|
869
|
+
* rate and each variable of its request are in the range of the app, and it gives each request variable of
|
|
870
|
+
* its class. Each level of the workload is a level variable of the app, in its range. A class or a level that
|
|
871
|
+
* the workload does not give is 0.
|
|
872
|
+
*/
|
|
873
|
+
const appWorkloadProblems = (app, workload) => {
|
|
874
|
+
const problems = [];
|
|
875
|
+
workload.classes.forEach((entry, index) => {
|
|
876
|
+
const path = `classes[${index}]`;
|
|
877
|
+
const id = entry.of ?? entry.id;
|
|
878
|
+
const known = app.classes.find((each) => each.id === id);
|
|
879
|
+
if (known === void 0) {
|
|
880
|
+
problems.push(appProblem("class", entry.of === void 0 ? `${path}.id` : `${path}.of`, `The app "${app.id}" has no class "${id}".`));
|
|
881
|
+
return;
|
|
882
|
+
}
|
|
883
|
+
const rate = amountProblem(entry.rate, known.rate, `${path}.rate`, `The rate of the class "${entry.id}"`);
|
|
884
|
+
if (rate !== void 0) problems.push(rate);
|
|
885
|
+
for (const [name, amount] of Object.entries(entry.request)) {
|
|
886
|
+
const variable = app.variables.find((each) => each.id === name);
|
|
887
|
+
if (variable === void 0 || !known.request.includes(name)) {
|
|
888
|
+
problems.push(appProblem("variable", `${path}.request.${name}`, `"${name}" is not a request variable of the class "${known.id}" of the app.`));
|
|
889
|
+
continue;
|
|
890
|
+
}
|
|
891
|
+
const found = amountProblem(amount, variable, `${path}.request.${name}`, `The variable "${name}" of the class "${entry.id}"`);
|
|
892
|
+
if (found !== void 0) problems.push(found);
|
|
893
|
+
}
|
|
894
|
+
for (const name of known.request) if (!Object.hasOwn(entry.request, name)) problems.push(appProblem("variable", `${path}.request`, `The class "${entry.id}" does not give the request variable "${name}" of the class "${known.id}".`));
|
|
895
|
+
});
|
|
896
|
+
for (const [name, amount] of Object.entries(workload.levels)) {
|
|
897
|
+
const variable = app.variables.find((each) => each.id === name);
|
|
898
|
+
if (variable === void 0 || variable.kind !== "level") {
|
|
899
|
+
problems.push(appProblem("variable", `levels.${name}`, `"${name}" is not a level variable of the app "${app.id}".`));
|
|
900
|
+
continue;
|
|
901
|
+
}
|
|
902
|
+
const found = amountProblem(amount, variable, `levels.${name}`, `The level "${name}"`);
|
|
903
|
+
if (found !== void 0) problems.push(found);
|
|
904
|
+
}
|
|
905
|
+
return problems;
|
|
906
|
+
};
|
|
907
|
+
// src/demand.ts
|
|
908
|
+
/** This function gives the first problem of a demand, or undefined. The text `owner` goes before the path of the problem. */
|
|
909
|
+
const firstProblem = (demand, owner = "") => {
|
|
910
|
+
const found = demandProblems(demand)[0];
|
|
911
|
+
return found === void 0 || owner === "" ? found : {
|
|
912
|
+
...found,
|
|
913
|
+
path: `${owner}.${found.path}`
|
|
914
|
+
};
|
|
915
|
+
};
|
|
916
|
+
/**
|
|
917
|
+
* This function gives a demand with a change of each rate and each level. The variables of the
|
|
918
|
+
* requests stay. The function `change` gets the name of a level, and no name for a rate. It gives
|
|
919
|
+
* undefined for an amount that it cannot change.
|
|
920
|
+
*/
|
|
921
|
+
const changed = (demand, change) => {
|
|
922
|
+
const classes = [];
|
|
923
|
+
for (const [index, entry] of demand.classes.entries()) {
|
|
924
|
+
const rate = change(entry.rate);
|
|
925
|
+
if (rate === void 0) return err(workloadProblem("amount", `classes[${index}].rate`, `The rate of the class "${entry.id}" is not finite after the change.`));
|
|
926
|
+
classes.push({
|
|
927
|
+
id: entry.id,
|
|
928
|
+
rate,
|
|
929
|
+
request: entry.request
|
|
930
|
+
});
|
|
931
|
+
}
|
|
932
|
+
const levels = {};
|
|
933
|
+
for (const [name, amount] of Object.entries(demand.levels)) {
|
|
934
|
+
const level = change(amount, name);
|
|
935
|
+
if (level === void 0) return err(workloadProblem("amount", `levels.${name}`, `The level "${name}" is not finite after the change.`));
|
|
936
|
+
levels[name] = level;
|
|
937
|
+
}
|
|
938
|
+
return ok({
|
|
939
|
+
classes,
|
|
940
|
+
levels
|
|
941
|
+
});
|
|
942
|
+
};
|
|
943
|
+
/**
|
|
944
|
+
* This function gives a demand that is `by` times as large: each rate and each level, times `by`.
|
|
945
|
+
* The variables of the requests stay, because more clients do not make a request larger.
|
|
946
|
+
*
|
|
947
|
+
* It gives a problem for a demand that is not correct, and for a factor that is not a finite number
|
|
948
|
+
* of 0 or more.
|
|
949
|
+
*/
|
|
950
|
+
const scaleDemand = (demand, by) => {
|
|
951
|
+
const bad = firstProblem(demand);
|
|
952
|
+
if (bad !== void 0) return err(bad);
|
|
953
|
+
if (!finite(by) || by < 0) return err(workloadProblem("factor", "by", `The factor ${by} is not a finite number of 0 or more.`));
|
|
954
|
+
return changed(demand, (amount) => {
|
|
955
|
+
const product = scale(amount, by);
|
|
956
|
+
return finite(product.value) ? product : void 0;
|
|
957
|
+
});
|
|
958
|
+
};
|
|
959
|
+
/**
|
|
960
|
+
* This function gives the demand on one instance when `instances` equal instances serve a demand:
|
|
961
|
+
* each rate and each level, divided by the count. The variables of the requests stay.
|
|
962
|
+
*
|
|
963
|
+
* A level in `options.whole` does not divide. For example, each instance of an app that keeps all
|
|
964
|
+
* open incidents in memory has the whole level.
|
|
965
|
+
*
|
|
966
|
+
* It gives a problem for a demand that is not correct. It gives a problem for a count that is not
|
|
967
|
+
* a whole number of 1 or more. It gives a problem for a name in `options.whole` that is not a level.
|
|
968
|
+
*/
|
|
969
|
+
const shareOf = (demand, instances, options = {}) => {
|
|
970
|
+
const bad = firstProblem(demand);
|
|
971
|
+
if (bad !== void 0) return err(bad);
|
|
972
|
+
if (!Number.isSafeInteger(instances) || instances < 1) return err(workloadProblem("factor", "instances", `The count of instances ${instances} is not a whole number of 1 or more.`));
|
|
973
|
+
const whole = options.whole ?? [];
|
|
974
|
+
const unknown = whole.find((name) => !Object.hasOwn(demand.levels, name));
|
|
975
|
+
if (unknown !== void 0) return err(workloadProblem("variable", "whole", `The demand has no level "${unknown}".`));
|
|
976
|
+
return changed(demand, (amount, level) => level !== void 0 && whole.includes(level) ? amount : quantity(amount.value / instances, amount.unit));
|
|
977
|
+
};
|
|
978
|
+
/**
|
|
979
|
+
* This function gives a portion of a demand for a search. Each rate and each level is times
|
|
980
|
+
* `shared`, and each level in `whole` is times `kept`. It does not examine the demand or the names.
|
|
981
|
+
*
|
|
982
|
+
* The demand on one of `n` instances at `s` times the load is the portion with `shared` equal to
|
|
983
|
+
* `s / n` and `kept` equal to `s`.
|
|
984
|
+
*/
|
|
985
|
+
const portionOf = (demand, shared, kept, whole) => changed(demand, (amount, level) => {
|
|
986
|
+
const product = scale(amount, level !== void 0 && whole.includes(level) ? kept : shared);
|
|
987
|
+
return finite(product.value) ? product : void 0;
|
|
988
|
+
});
|
|
989
|
+
/** This function gives the sum of two amounts, or the problem that stops it. */
|
|
990
|
+
const plus = (a, b, path, what) => {
|
|
991
|
+
if (!convertible(a.unit, b.unit)) return err(workloadProblem("unit", path, `${what} do not add: ${whyNotConvertible(a.unit, b.unit) ?? "the units are different"}.`));
|
|
992
|
+
const total = add(a, b);
|
|
993
|
+
return finite(total.value) ? ok(total) : err(workloadProblem("amount", path, `${what} do not have a finite sum.`));
|
|
994
|
+
};
|
|
995
|
+
/** This function tells if two sets of amounts have the same names and equal amounts. */
|
|
996
|
+
const sameAmounts = (a, b) => {
|
|
997
|
+
const names = Object.keys(a);
|
|
998
|
+
return names.length === Object.keys(b).length && names.every((name) => {
|
|
999
|
+
const other = b[name];
|
|
1000
|
+
const amount = a[name];
|
|
1001
|
+
return Object.hasOwn(b, name) && other !== void 0 && convertible(amount.unit, other.unit) && compare(amount, other) === 0;
|
|
1002
|
+
});
|
|
1003
|
+
};
|
|
1004
|
+
const inOrder = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
1005
|
+
/**
|
|
1006
|
+
* This function gives the sum of two demands. A class of one demand stays. A class of the two
|
|
1007
|
+
* demands has the sum of its rates, and its request must be equal in the two. The levels add, and a
|
|
1008
|
+
* level of one demand stays. The classes and the levels of the sum are in the order of their names.
|
|
1009
|
+
*
|
|
1010
|
+
* The sum has the laws of addition: the order of the demands does not change it, and `NO_DEMAND`
|
|
1011
|
+
* is its zero. An amount of the sum is in the unit of the first demand that gives it. Thus the
|
|
1012
|
+
* order can change the unit of an amount, and not the amount.
|
|
1013
|
+
*
|
|
1014
|
+
* It gives a problem for a demand that is not correct, and for amounts that do not add. A path of
|
|
1015
|
+
* a problem starts with `first` or `second`, or with neither for a problem of the sum.
|
|
1016
|
+
*/
|
|
1017
|
+
const mergeDemands = (first, second) => {
|
|
1018
|
+
const bad = firstProblem(first, "first") ?? firstProblem(second, "second");
|
|
1019
|
+
if (bad !== void 0) return err(bad);
|
|
1020
|
+
const classes = new Map(first.classes.map((entry) => [entry.id, entry]));
|
|
1021
|
+
for (const [index, entry] of second.classes.entries()) {
|
|
1022
|
+
const same = classes.get(entry.id);
|
|
1023
|
+
if (same === void 0) {
|
|
1024
|
+
classes.set(entry.id, entry);
|
|
1025
|
+
continue;
|
|
1026
|
+
}
|
|
1027
|
+
const path = `second.classes[${index}]`;
|
|
1028
|
+
if (!sameAmounts(same.request, entry.request)) return err(workloadProblem("class", `${path}.request`, `The class "${entry.id}" has a different request in the two demands.`));
|
|
1029
|
+
const rate = plus(same.rate, entry.rate, `${path}.rate`, `The rates of the class "${entry.id}"`);
|
|
1030
|
+
if (rate.tag === "err") return rate;
|
|
1031
|
+
classes.set(entry.id, {
|
|
1032
|
+
id: same.id,
|
|
1033
|
+
rate: rate.value,
|
|
1034
|
+
request: same.request
|
|
1035
|
+
});
|
|
1036
|
+
}
|
|
1037
|
+
const levels = new Map(Object.entries(first.levels));
|
|
1038
|
+
for (const [name, amount] of Object.entries(second.levels)) {
|
|
1039
|
+
const same = levels.get(name);
|
|
1040
|
+
if (same === void 0) {
|
|
1041
|
+
levels.set(name, amount);
|
|
1042
|
+
continue;
|
|
1043
|
+
}
|
|
1044
|
+
const level = plus(same, amount, `second.levels.${name}`, `The levels "${name}"`);
|
|
1045
|
+
if (level.tag === "err") return level;
|
|
1046
|
+
levels.set(name, level.value);
|
|
1047
|
+
}
|
|
1048
|
+
const total = {
|
|
1049
|
+
classes: [...classes.values()].sort((a, b) => inOrder(a.id, b.id)),
|
|
1050
|
+
levels: Object.fromEntries([...levels].sort(([a], [b]) => inOrder(a, b)))
|
|
1051
|
+
};
|
|
1052
|
+
const together = firstProblem(total);
|
|
1053
|
+
return together === void 0 ? ok(total) : err(together);
|
|
1054
|
+
};
|
|
1055
|
+
/**
|
|
1056
|
+
* This function gives the sum of the rates of the classes of a demand, in `unit`. A demand with no
|
|
1057
|
+
* class has a rate of 0.
|
|
1058
|
+
*
|
|
1059
|
+
* It gives a problem for a demand that is not correct, and for a unit that is not a unit of a
|
|
1060
|
+
* rate. It gives a problem for a class whose rate counts a different thing, for example events.
|
|
1061
|
+
*/
|
|
1062
|
+
const totalRate = (demand, unit) => {
|
|
1063
|
+
const bad = firstProblem(demand);
|
|
1064
|
+
if (bad !== void 0) return err(bad);
|
|
1065
|
+
if (!isCanonicalUnit(unit) || kindOf(unit) !== "frequency") return err(workloadProblem("unit", "unit", `"${unit}" is not a canonical unit of a rate, for example "{request}/s".`));
|
|
1066
|
+
for (const [index, entry] of demand.classes.entries()) if (!convertible(entry.rate.unit, unit)) {
|
|
1067
|
+
const why = whyNotConvertible(entry.rate.unit, unit) ?? "the units are different";
|
|
1068
|
+
return err(workloadProblem("unit", `classes[${index}].rate`, `The rate of the class "${entry.id}" is not in "${unit}": ${why}.`));
|
|
1069
|
+
}
|
|
1070
|
+
let value = 0;
|
|
1071
|
+
for (const entry of demand.classes) value += entry.rate.value * factorOf(entry.rate.unit, unit);
|
|
1072
|
+
const total = quantity(value, unit);
|
|
1073
|
+
return finite(total.value) ? ok(total) : err(workloadProblem("amount", "classes", "The rates do not have a finite sum."));
|
|
1074
|
+
};
|
|
1075
|
+
/** This function gives the point of a law from a rate, the variables of one request, the levels of a demand and `given`. */
|
|
1076
|
+
const pointOf = (demand, law, rate, request, given) => {
|
|
1077
|
+
const point = {};
|
|
1078
|
+
for (const variable of law.variables) {
|
|
1079
|
+
const name = variable.id;
|
|
1080
|
+
const found = [];
|
|
1081
|
+
if (name === "rate") {
|
|
1082
|
+
if (rate === "total") {
|
|
1083
|
+
const total = totalRate(demand, variable.unit);
|
|
1084
|
+
if (total.tag === "err") return err({
|
|
1085
|
+
...total.error,
|
|
1086
|
+
path: name,
|
|
1087
|
+
message: `The variable "${name}" of the law "${law.id}" reads the total rate. ${total.error.message}`
|
|
1088
|
+
});
|
|
1089
|
+
found.push(total.value);
|
|
1090
|
+
} else found.push(rate);
|
|
1091
|
+
}
|
|
1092
|
+
for (const amounts of [
|
|
1093
|
+
request,
|
|
1094
|
+
demand.levels,
|
|
1095
|
+
given
|
|
1096
|
+
]) if (Object.hasOwn(amounts, name)) found.push(amounts[name]);
|
|
1097
|
+
const [amount] = found;
|
|
1098
|
+
if (found.length === 0) return err(workloadProblem("variable", name, `The demand gives no amount for the variable "${name}" of the law "${law.id}".`));
|
|
1099
|
+
if (found.length > 1) return err(workloadProblem("variable", name, `The demand gives the variable "${name}", and the caller gives it also.`));
|
|
1100
|
+
if (!isQuantity(amount)) return err(workloadProblem("amount", name, `The amount of "${name}" from the caller is not a finite amount in a canonical unit.`));
|
|
1101
|
+
if (!isCanonicalUnit(variable.unit)) return err(workloadProblem("unit", name, `The unit "${variable.unit}" of the variable "${name}" of the law "${law.id}" is not canonical.`));
|
|
1102
|
+
if (!convertible(amount.unit, variable.unit)) {
|
|
1103
|
+
const why = whyNotConvertible(amount.unit, variable.unit) ?? "the units are different";
|
|
1104
|
+
return err(workloadProblem("unit", name, `The variable "${name}" of the law "${law.id}" is in "${variable.unit}", not in "${amount.unit}": ${why}.`));
|
|
1105
|
+
}
|
|
1106
|
+
point[name] = amount.value * factorOf(amount.unit, variable.unit);
|
|
1107
|
+
}
|
|
1108
|
+
return ok(point);
|
|
1109
|
+
};
|
|
1110
|
+
/**
|
|
1111
|
+
* This function gives the load point of one class of a demand for a law. The point has one number
|
|
1112
|
+
* for each variable of the law, in the unit of that variable.
|
|
1113
|
+
*
|
|
1114
|
+
* The variable `rate` is the rate of the class, or the total rate with the load `total`. Each other
|
|
1115
|
+
* variable is a variable of the request of the class, a level of the demand, or an amount of
|
|
1116
|
+
* `given`. The amounts of `given` are the state of an instance, for example the size of its heap
|
|
1117
|
+
* and its age. No workload gives that state.
|
|
1118
|
+
*
|
|
1119
|
+
* It gives a problem for a demand that is not correct and for a class that the demand does not
|
|
1120
|
+
* have. It gives a problem for a variable with no amount or with two amounts. It gives a problem
|
|
1121
|
+
* for an amount that does not convert to the unit of the variable.
|
|
1122
|
+
*/
|
|
1123
|
+
const loadPointOf = (demand, id, law, given = {}, options = {}) => {
|
|
1124
|
+
const bad = firstProblem(demand);
|
|
1125
|
+
if (bad !== void 0) return err(bad);
|
|
1126
|
+
const entry = demand.classes.find((candidate) => candidate.id === id);
|
|
1127
|
+
if (entry === void 0) return err(workloadProblem("class", "classes", `The demand has no class "${id}".`));
|
|
1128
|
+
return pointOf(demand, law, (options.load ?? "class") === "class" ? entry.rate : "total", entry.request, given);
|
|
1129
|
+
};
|
|
1130
|
+
/**
|
|
1131
|
+
* This function gives the load point of a whole instance for a law: a point with no class. The
|
|
1132
|
+
* variable `rate` is the total rate of the demand. Each other variable is a level of the demand or
|
|
1133
|
+
* an amount of `given`. A variable of a request has no amount here.
|
|
1134
|
+
*
|
|
1135
|
+
* It gives the problems of `loadPointOf`, with no problem of a class.
|
|
1136
|
+
*/
|
|
1137
|
+
const instancePointOf = (demand, law, given = {}) => {
|
|
1138
|
+
const bad = firstProblem(demand);
|
|
1139
|
+
if (bad !== void 0) return err(bad);
|
|
1140
|
+
return pointOf(demand, law, "total", {}, given);
|
|
1141
|
+
};
|
|
1142
|
+
// src/composition.ts
|
|
1143
|
+
/** This text is the format of a composition file. */
|
|
1144
|
+
const COMPOSITION_FORMAT = "capacity-composition/1";
|
|
1145
|
+
const problem = (code, path, message) => ({
|
|
1146
|
+
code,
|
|
1147
|
+
path,
|
|
1148
|
+
message
|
|
1149
|
+
});
|
|
1150
|
+
/** This function tells if a term names a law. */
|
|
1151
|
+
const isLawTerm = (term) => Object.hasOwn(term, "law");
|
|
1152
|
+
/** This function gives the reason that the values of a unit do not add, or undefined when they add. */
|
|
1153
|
+
const sumUnitProblem = (unit) => {
|
|
1154
|
+
if (!isCanonicalUnit(unit)) return `"${unit}" is not a canonical unit.`;
|
|
1155
|
+
const parsed = parseUnit(unit);
|
|
1156
|
+
if (parsed.log) return `"${unit}" is a level, and levels do not add.`;
|
|
1157
|
+
if (parsed.offset !== void 0) return `"${unit}" is an absolute temperature, and absolute temperatures do not add.`;
|
|
1158
|
+
};
|
|
1159
|
+
const NOT_SLUG = "is not lower-case words joined by \".\", \"-\" or \"/\".";
|
|
1160
|
+
/**
|
|
1161
|
+
* This function examines a composition and gives each problem that it finds. A composition with no
|
|
1162
|
+
* problem gives an empty list.
|
|
1163
|
+
*
|
|
1164
|
+
* It examines the format, the ids, the title and the unit of the measure. It examines that each
|
|
1165
|
+
* term names a law or a composition and has a correct scope. It examines that each factor is a
|
|
1166
|
+
* finite number other than 0. It does not look for the laws: `compositionValue` does that.
|
|
1167
|
+
*/
|
|
1168
|
+
const checkComposition = (composition) => {
|
|
1169
|
+
const problems = [];
|
|
1170
|
+
const add = (code, path, message) => {
|
|
1171
|
+
problems.push(problem(code, path, message));
|
|
1172
|
+
};
|
|
1173
|
+
if (composition.format !== "capacity-composition/1") add("format", "format", `The format is not "${COMPOSITION_FORMAT}".`);
|
|
1174
|
+
for (const [path, id] of [
|
|
1175
|
+
["id", composition.id],
|
|
1176
|
+
["subject", composition.subject],
|
|
1177
|
+
["measure.id", composition.measure.id]
|
|
1178
|
+
]) if (!SLUG.test(id)) add("id", path, `"${id}" ${NOT_SLUG}`);
|
|
1179
|
+
if (composition.title.trim() === "") add("id", "title", "The title is empty.");
|
|
1180
|
+
const unit = sumUnitProblem(composition.measure.unit);
|
|
1181
|
+
if (unit !== void 0) add("unit", "measure.unit", unit);
|
|
1182
|
+
if (composition.terms.length === 0) add("term", "terms", "The composition has no term.");
|
|
1183
|
+
for (const [index, term] of composition.terms.entries()) {
|
|
1184
|
+
const path = `terms[${index}]`;
|
|
1185
|
+
if (term.factor !== void 0 && (!finite(term.factor) || term.factor === 0)) add("factor", `${path}.factor`, `The factor ${term.factor} is not a finite number other than 0.`);
|
|
1186
|
+
if (!isLawTerm(term)) {
|
|
1187
|
+
if (!SLUG.test(term.composition)) add("id", `${path}.composition`, `"${term.composition}" ${NOT_SLUG}`);
|
|
1188
|
+
else if (term.composition === composition.id) add("composition", `${path}.composition`, "The composition names itself.");
|
|
1189
|
+
continue;
|
|
1190
|
+
}
|
|
1191
|
+
if (!SLUG.test(term.law)) add("id", `${path}.law`, `"${term.law}" ${NOT_SLUG}`);
|
|
1192
|
+
if (typeof term.over !== "string") {
|
|
1193
|
+
if (term.over.length === 0) add("class", `${path}.over`, "The list of classes is empty.");
|
|
1194
|
+
for (const [at, id] of term.over.entries()) if (!SLUG.test(id)) add("id", `${path}.over[${at}]`, `"${id}" ${NOT_SLUG}`);
|
|
1195
|
+
const twice = repeated(term.over);
|
|
1196
|
+
if (twice !== void 0) add("class", `${path}.over`, `The list has the class "${twice}" two times.`);
|
|
1197
|
+
} else if (term.over !== "instance" && term.over !== "each") add("term", `${path}.over`, `The scope "${String(term.over)}" is not "instance", "each" or a list of classes.`);
|
|
1198
|
+
if (term.over === "instance" && term.load === "class") add("term", `${path}.load`, "A term over the instance has no class. Its law reads the total rate.");
|
|
1199
|
+
if (term.base !== void 0) {
|
|
1200
|
+
const names = Object.keys(term.base);
|
|
1201
|
+
if (names.length === 0) add("term", `${path}.base`, "The base names no variable.");
|
|
1202
|
+
for (const name of names) {
|
|
1203
|
+
const where = `${path}.base.${name}`;
|
|
1204
|
+
if (!NAME.test(name)) add("variable", where, `"${name}" is not a name: a lower-case letter, then letters and digits.`);
|
|
1205
|
+
else if (name === "rate") add("variable", where, "The rate comes from the demand. A base does not hold it.");
|
|
1206
|
+
else if (!isQuantity(term.base[name])) add("amount", where, `The amount of "${name}" is not a finite amount in a canonical unit.`);
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
return problems;
|
|
1211
|
+
};
|
|
1212
|
+
/**
|
|
1213
|
+
* This function gives the weight that changes a value in `from` to a value in `to`. The quotient
|
|
1214
|
+
* `to / from` decides. A quotient with no dimension needs no rate. A quotient that is a rate needs
|
|
1215
|
+
* the rate of the part, in the unit of the quotient.
|
|
1216
|
+
*/
|
|
1217
|
+
const weightOf = (from, to, factor, path) => {
|
|
1218
|
+
try {
|
|
1219
|
+
const per = divideUnits(to, from);
|
|
1220
|
+
if (convertible(per, "count")) return ok({
|
|
1221
|
+
by: factor * factorOf(from, to),
|
|
1222
|
+
rate: void 0
|
|
1223
|
+
});
|
|
1224
|
+
if (kindOf(per) === "frequency") return ok({
|
|
1225
|
+
by: factor * factorOf(multiplyUnits(from, per), to),
|
|
1226
|
+
rate: per
|
|
1227
|
+
});
|
|
1228
|
+
return err(problem("unit", path, `The law gives "${from}". That unit does not give "${to}", alone or times a rate.`));
|
|
1229
|
+
} catch (error) {
|
|
1230
|
+
if (error instanceof UnitError) return err(problem("unit", path, `The law gives "${from}", and "${to}" does not come from it: ${error.message}`));
|
|
1231
|
+
throw error;
|
|
1232
|
+
}
|
|
1233
|
+
};
|
|
1234
|
+
/** This function gives the number that the rate of a class is in the unit of a weight, or the problem that stops it. */
|
|
1235
|
+
const classRate = (demand, id, unit, path) => {
|
|
1236
|
+
const rate = demand.classes.find((entry) => entry.id === id).rate;
|
|
1237
|
+
if (!convertible(rate.unit, unit)) {
|
|
1238
|
+
const why = whyNotConvertible(rate.unit, unit) ?? "the units are different";
|
|
1239
|
+
return err(problem("unit", `${path}.rate`, `The rate of the class "${id}" is in "${rate.unit}". The law needs a rate in "${unit}": ${why}.`));
|
|
1240
|
+
}
|
|
1241
|
+
return ok(rate.value * factorOf(rate.unit, unit));
|
|
1242
|
+
};
|
|
1243
|
+
/** This function gives the number that the total rate of a demand is in the unit of a weight, or the problem that stops it. */
|
|
1244
|
+
const instanceRate = (demand, unit, path) => {
|
|
1245
|
+
const total = totalRate(demand, unit);
|
|
1246
|
+
return total.tag === "err" ? err(under(total.error, path)) : ok(total.value.value);
|
|
1247
|
+
};
|
|
1248
|
+
/**
|
|
1249
|
+
* This function gives the problem of the base of a term for its law, or undefined. Each name of the
|
|
1250
|
+
* base is a variable of the law, and its amount converts to the unit of that variable.
|
|
1251
|
+
*/
|
|
1252
|
+
const baseProblem = (law, base, path) => {
|
|
1253
|
+
for (const [name, amount] of Object.entries(base)) {
|
|
1254
|
+
const variable = law.variables.find((each) => each.id === name);
|
|
1255
|
+
if (variable === void 0) return problem("variable", `${path}.${name}`, `The law "${law.id}" has no variable "${name}".`);
|
|
1256
|
+
if (!convertible(amount.unit, variable.unit)) {
|
|
1257
|
+
const why = whyNotConvertible(amount.unit, variable.unit) ?? "the units are different";
|
|
1258
|
+
return problem("unit", `${path}.${name}`, `The variable "${name}" of the law "${law.id}" is in "${variable.unit}", not in "${amount.unit}": ${why}.`);
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
};
|
|
1262
|
+
/** This function gives the amounts of a base that no list of amounts gives. The point holds them at the base. */
|
|
1263
|
+
const heldOf = (base, lists) => Object.fromEntries(Object.entries(base).filter(([name]) => !lists.some((amounts) => Object.hasOwn(amounts, name))));
|
|
1264
|
+
/** This function gives the base point of a term: the load point, with the amount of the base for each variable that it names. */
|
|
1265
|
+
const basePointOf = (law, point, base) => {
|
|
1266
|
+
const next = { ...point };
|
|
1267
|
+
for (const [name, amount] of Object.entries(base)) {
|
|
1268
|
+
const variable = law.variables.find((each) => each.id === name);
|
|
1269
|
+
next[name] = amount.value * factorOf(amount.unit, variable.unit);
|
|
1270
|
+
}
|
|
1271
|
+
return next;
|
|
1272
|
+
};
|
|
1273
|
+
/** This function gives a range times a weight. The ends change places for a weight less than 0. */
|
|
1274
|
+
const times = (range, weight) => weight >= 0 ? {
|
|
1275
|
+
low: weight * range.low,
|
|
1276
|
+
high: weight * range.high
|
|
1277
|
+
} : {
|
|
1278
|
+
low: weight * range.high,
|
|
1279
|
+
high: weight * range.low
|
|
1280
|
+
};
|
|
1281
|
+
/** This function gives a problem of a query with the path of its term in front. */
|
|
1282
|
+
const under = (found, path) => ({
|
|
1283
|
+
code: found.code,
|
|
1284
|
+
path: `${path}.${found.path}`,
|
|
1285
|
+
message: found.message
|
|
1286
|
+
});
|
|
1287
|
+
/** This function gives a map of records by id, or the problem of an id that the list has two times. */
|
|
1288
|
+
const byId = (records, what) => {
|
|
1289
|
+
const map = /* @__PURE__ */ new Map();
|
|
1290
|
+
for (const [index, record] of records.entries()) {
|
|
1291
|
+
if (map.has(record.id)) return err(problem(what === "laws" ? "law" : "composition", `${what}[${index}].id`, `The catalog has two ${what} with the id "${record.id}".`));
|
|
1292
|
+
map.set(record.id, record);
|
|
1293
|
+
}
|
|
1294
|
+
return ok(map);
|
|
1295
|
+
};
|
|
1296
|
+
/**
|
|
1297
|
+
* This function examines a composition and its catalog one time, for the queries over many demands.
|
|
1298
|
+
* It gives each problem of `compositionValue` that does not come from a demand.
|
|
1299
|
+
*/
|
|
1300
|
+
const preparedOf = (composition, catalog, level) => {
|
|
1301
|
+
const own = checkComposition(composition)[0];
|
|
1302
|
+
if (own !== void 0) return err(own);
|
|
1303
|
+
const laws = byId(catalog.laws, "laws");
|
|
1304
|
+
if (laws.tag === "err") return laws;
|
|
1305
|
+
const nested = byId(catalog.compositions ?? [], "compositions");
|
|
1306
|
+
if (nested.tag === "err") return nested;
|
|
1307
|
+
const reads = /* @__PURE__ */ new Map();
|
|
1308
|
+
const walked = /* @__PURE__ */ new Set();
|
|
1309
|
+
const walk = (at, prefix, stack) => {
|
|
1310
|
+
for (const [index, term] of at.terms.entries()) {
|
|
1311
|
+
const path = `${prefix}terms[${index}]`;
|
|
1312
|
+
if (isLawTerm(term)) {
|
|
1313
|
+
const law = laws.value.get(term.law);
|
|
1314
|
+
if (law === void 0) return problem("law", `${path}.law`, `The catalog has no law "${term.law}".`);
|
|
1315
|
+
const badBase = baseProblem(law, term.base ?? {}, `${path}.base`);
|
|
1316
|
+
if (badBase !== void 0) return badBase;
|
|
1317
|
+
reads.set(law.id, law);
|
|
1318
|
+
continue;
|
|
1319
|
+
}
|
|
1320
|
+
if (stack.includes(term.composition)) return problem("composition", `${path}.composition`, `The compositions make a cycle: ${[...stack, term.composition].join(" → ")}.`);
|
|
1321
|
+
const next = nested.value.get(term.composition);
|
|
1322
|
+
if (next === void 0) return problem("composition", `${path}.composition`, `The catalog has no composition "${term.composition}".`);
|
|
1323
|
+
if (walked.has(next.id)) continue;
|
|
1324
|
+
const inner = `compositions.${next.id}.`;
|
|
1325
|
+
const bad = checkComposition(next)[0];
|
|
1326
|
+
if (bad !== void 0) return {
|
|
1327
|
+
...bad,
|
|
1328
|
+
path: `${inner}${bad.path}`
|
|
1329
|
+
};
|
|
1330
|
+
const deeper = walk(next, inner, [...stack, next.id]);
|
|
1331
|
+
if (deeper !== void 0) return deeper;
|
|
1332
|
+
walked.add(next.id);
|
|
1333
|
+
}
|
|
1334
|
+
};
|
|
1335
|
+
const broken = walk(composition, "", [composition.id]);
|
|
1336
|
+
if (broken !== void 0) return err(broken);
|
|
1337
|
+
for (const law of reads.values()) {
|
|
1338
|
+
const noLevel = levelProblem(law, level, true);
|
|
1339
|
+
if (noLevel !== void 0) return err(under(noLevel, `laws.${law.id}`));
|
|
1340
|
+
}
|
|
1341
|
+
const rows = Math.min(...[...reads.values()].map((law) => law.replicates.length));
|
|
1342
|
+
return ok({
|
|
1343
|
+
composition,
|
|
1344
|
+
reads,
|
|
1345
|
+
nested: nested.value,
|
|
1346
|
+
rows,
|
|
1347
|
+
level
|
|
1348
|
+
});
|
|
1349
|
+
};
|
|
1350
|
+
/** This function gives the tree of a prepared composition over a demand, or the problem that stops it. It does not examine the demand. */
|
|
1351
|
+
const treeOf = (prepared, demand, given) => {
|
|
1352
|
+
const trees = /* @__PURE__ */ new Map();
|
|
1353
|
+
const build = (at, prefix) => {
|
|
1354
|
+
const unit = at.measure.unit;
|
|
1355
|
+
const nodes = [];
|
|
1356
|
+
for (const [index, term] of at.terms.entries()) {
|
|
1357
|
+
const path = `${prefix}terms[${index}]`;
|
|
1358
|
+
const factor = term.factor ?? 1;
|
|
1359
|
+
if (!isLawTerm(term)) {
|
|
1360
|
+
const inner = prepared.nested.get(term.composition);
|
|
1361
|
+
let tree = trees.get(inner.id);
|
|
1362
|
+
if (tree === void 0) {
|
|
1363
|
+
const found = build(inner, `compositions.${inner.id}.`);
|
|
1364
|
+
if (found.tag === "err") return found;
|
|
1365
|
+
tree = found.value;
|
|
1366
|
+
trees.set(inner.id, tree);
|
|
1367
|
+
}
|
|
1368
|
+
if (!convertible(tree.unit, unit)) {
|
|
1369
|
+
const why = whyNotConvertible(tree.unit, unit) ?? "the units are different";
|
|
1370
|
+
return err(problem("unit", `${path}.composition`, `The composition "${inner.id}" gives "${tree.unit}", not "${unit}": ${why}.`));
|
|
1371
|
+
}
|
|
1372
|
+
nodes.push({
|
|
1373
|
+
kind: "composition",
|
|
1374
|
+
term: index,
|
|
1375
|
+
composition: inner.id,
|
|
1376
|
+
weight: factor * factorOf(tree.unit, unit),
|
|
1377
|
+
tree
|
|
1378
|
+
});
|
|
1379
|
+
continue;
|
|
1380
|
+
}
|
|
1381
|
+
const law = prepared.reads.get(term.law);
|
|
1382
|
+
const weight = weightOf(law.measure.unit, unit, factor, `${path}.law`);
|
|
1383
|
+
if (weight.tag === "err") return weight;
|
|
1384
|
+
const targets = term.over === "instance" ? [null] : term.over === "each" ? demand.classes.map((entry) => entry.id) : term.over;
|
|
1385
|
+
for (const id of targets) {
|
|
1386
|
+
const where = `${path}.${id ?? "instance"}`;
|
|
1387
|
+
const request = id === null ? {} : demand.classes.find((entry) => entry.id === id)?.request ?? {};
|
|
1388
|
+
const state = term.base === void 0 ? given : {
|
|
1389
|
+
...heldOf(term.base, [
|
|
1390
|
+
request,
|
|
1391
|
+
demand.levels,
|
|
1392
|
+
given
|
|
1393
|
+
]),
|
|
1394
|
+
...given
|
|
1395
|
+
};
|
|
1396
|
+
const point = id === null ? instancePointOf(demand, law, state) : loadPointOf(demand, id, law, state, { load: term.load ?? "class" });
|
|
1397
|
+
if (point.tag === "err") return err(under(point.error, where));
|
|
1398
|
+
let by = weight.value.by;
|
|
1399
|
+
const rateUnit = weight.value.rate;
|
|
1400
|
+
if (rateUnit !== void 0) {
|
|
1401
|
+
const rate = id === null ? instanceRate(demand, rateUnit, where) : classRate(demand, id, rateUnit, where);
|
|
1402
|
+
if (rate.tag === "err") return rate;
|
|
1403
|
+
by *= rate.value;
|
|
1404
|
+
}
|
|
1405
|
+
nodes.push({
|
|
1406
|
+
kind: "law",
|
|
1407
|
+
term: index,
|
|
1408
|
+
law,
|
|
1409
|
+
class: id,
|
|
1410
|
+
base: false,
|
|
1411
|
+
weight: by,
|
|
1412
|
+
point: point.value,
|
|
1413
|
+
path: where
|
|
1414
|
+
});
|
|
1415
|
+
if (term.base !== void 0) nodes.push({
|
|
1416
|
+
kind: "law",
|
|
1417
|
+
term: index,
|
|
1418
|
+
law,
|
|
1419
|
+
class: id,
|
|
1420
|
+
base: true,
|
|
1421
|
+
weight: -by,
|
|
1422
|
+
point: basePointOf(law, point.value, term.base),
|
|
1423
|
+
path: `${where}.base`
|
|
1424
|
+
});
|
|
1425
|
+
}
|
|
1426
|
+
}
|
|
1427
|
+
return ok({
|
|
1428
|
+
unit,
|
|
1429
|
+
nodes
|
|
1430
|
+
});
|
|
1431
|
+
};
|
|
1432
|
+
return build(prepared.composition, "");
|
|
1433
|
+
};
|
|
1434
|
+
/** This function gives the sum of the parts of a tree, with the values of its laws. The map `sums` keeps the sum of each nested tree. */
|
|
1435
|
+
const sumOf = (tree, rows, level, sums) => {
|
|
1436
|
+
const known = sums.get(tree);
|
|
1437
|
+
if (known !== void 0) return ok(known);
|
|
1438
|
+
const replicates = new Array(rows).fill(0);
|
|
1439
|
+
const widen = {
|
|
1440
|
+
low: 0,
|
|
1441
|
+
high: 0
|
|
1442
|
+
};
|
|
1443
|
+
const parts = [];
|
|
1444
|
+
let value = 0;
|
|
1445
|
+
let extrapolated = false;
|
|
1446
|
+
const keep = (part, spread, change) => {
|
|
1447
|
+
parts.push(part);
|
|
1448
|
+
value += part.value;
|
|
1449
|
+
for (const [row, draw] of spread.entries()) replicates[row] = replicates[row] + part.weight * draw;
|
|
1450
|
+
const scaled = times(change, part.weight);
|
|
1451
|
+
widen.low += scaled.low;
|
|
1452
|
+
widen.high += scaled.high;
|
|
1453
|
+
};
|
|
1454
|
+
for (const node of tree.nodes) {
|
|
1455
|
+
if (node.kind === "composition") {
|
|
1456
|
+
const found = sumOf(node.tree, rows, level, sums);
|
|
1457
|
+
if (found.tag === "err") return found;
|
|
1458
|
+
const sum = found.value;
|
|
1459
|
+
const { weight } = node;
|
|
1460
|
+
const interval = intervalOf(sum.value, sum.replicates, level);
|
|
1461
|
+
const scaled = times(interval, weight);
|
|
1462
|
+
const change = times(sum.widen, weight);
|
|
1463
|
+
keep({
|
|
1464
|
+
kind: "composition",
|
|
1465
|
+
term: node.term,
|
|
1466
|
+
composition: node.composition,
|
|
1467
|
+
weight,
|
|
1468
|
+
value: weight * sum.value,
|
|
1469
|
+
interval: scaled,
|
|
1470
|
+
run: {
|
|
1471
|
+
low: scaled.low + change.low,
|
|
1472
|
+
high: scaled.high + change.high
|
|
1473
|
+
},
|
|
1474
|
+
parts: sum.parts
|
|
1475
|
+
}, sum.replicates, sum.widen);
|
|
1476
|
+
extrapolated ||= sum.extrapolated;
|
|
1477
|
+
continue;
|
|
1478
|
+
}
|
|
1479
|
+
const evaluated = pointValuesOf(node.law, node.point);
|
|
1480
|
+
if (evaluated.tag === "err") return err(under(evaluated.error, node.path));
|
|
1481
|
+
const by = node.weight;
|
|
1482
|
+
const [estimate, ...all] = evaluated.value.values;
|
|
1483
|
+
const spread = all.slice(0, rows);
|
|
1484
|
+
const interval = intervalOf(estimate, spread, level);
|
|
1485
|
+
const run = runOf(node.law, interval, level);
|
|
1486
|
+
const change = {
|
|
1487
|
+
low: run.low - interval.low,
|
|
1488
|
+
high: run.high - interval.high
|
|
1489
|
+
};
|
|
1490
|
+
const scaled = times(interval, by);
|
|
1491
|
+
const scaledChange = times(change, by);
|
|
1492
|
+
keep({
|
|
1493
|
+
kind: "law",
|
|
1494
|
+
term: node.term,
|
|
1495
|
+
law: node.law.id,
|
|
1496
|
+
class: node.class,
|
|
1497
|
+
base: node.base,
|
|
1498
|
+
weight: by,
|
|
1499
|
+
value: by * estimate,
|
|
1500
|
+
interval: scaled,
|
|
1501
|
+
run: {
|
|
1502
|
+
low: scaled.low + scaledChange.low,
|
|
1503
|
+
high: scaled.high + scaledChange.high
|
|
1504
|
+
},
|
|
1505
|
+
outside: evaluated.value.outside
|
|
1506
|
+
}, spread, change);
|
|
1507
|
+
extrapolated ||= evaluated.value.outside.length > 0;
|
|
1508
|
+
}
|
|
1509
|
+
const sum = {
|
|
1510
|
+
unit: tree.unit,
|
|
1511
|
+
value,
|
|
1512
|
+
replicates,
|
|
1513
|
+
widen,
|
|
1514
|
+
parts,
|
|
1515
|
+
extrapolated
|
|
1516
|
+
};
|
|
1517
|
+
sums.set(tree, sum);
|
|
1518
|
+
return ok(sum);
|
|
1519
|
+
};
|
|
1520
|
+
/**
|
|
1521
|
+
* This function gives the value of a composition over a demand, with its interval. The catalog
|
|
1522
|
+
* gives each law and each nested composition that a term names. It can hold more.
|
|
1523
|
+
*
|
|
1524
|
+
* Each part pairs its replicates by row. Row `i` of each law is one draw of the doubt of that law.
|
|
1525
|
+
* Laws from separate fits have independent rows. Two laws that share points share their doubt
|
|
1526
|
+
* only when their fits use the same resample for row `i`. The count of rows is the least count of
|
|
1527
|
+
* the laws that the composition reads.
|
|
1528
|
+
*
|
|
1529
|
+
* The range `run` adds the change from the interval to the run of each part. That sum is exact
|
|
1530
|
+
* when the noise of each part moves with the noise of each other part. For independent noise, the
|
|
1531
|
+
* sum is wider than the true range. Thus the range is on the safe side for a limit.
|
|
1532
|
+
*
|
|
1533
|
+
* The function gives a problem for a composition that is not correct, for a missing law or
|
|
1534
|
+
* composition, and for a cycle of compositions. It gives the problems of `loadPointOf` and of
|
|
1535
|
+
* `lawValue` for each part, with the path of the term in front.
|
|
1536
|
+
*/
|
|
1537
|
+
const compositionValue = (composition, catalog, demand, options = {}) => {
|
|
1538
|
+
const level = options.level ?? .95;
|
|
1539
|
+
const own = checkComposition(composition)[0];
|
|
1540
|
+
if (own !== void 0) return err(own);
|
|
1541
|
+
const badDemand = demandProblems(demand)[0];
|
|
1542
|
+
if (badDemand !== void 0) return err(under(badDemand, "demand"));
|
|
1543
|
+
const prepared = preparedOf(composition, catalog, level);
|
|
1544
|
+
if (prepared.tag === "err") return prepared;
|
|
1545
|
+
const tree = treeOf(prepared.value, demand, options.given ?? {});
|
|
1546
|
+
if (tree.tag === "err") return tree;
|
|
1547
|
+
const sum = sumOf(tree.value, prepared.value.rows, level, /* @__PURE__ */ new Map());
|
|
1548
|
+
if (sum.tag === "err") return sum;
|
|
1549
|
+
const { value, replicates, widen, parts, extrapolated } = sum.value;
|
|
1550
|
+
const interval = intervalOf(value, replicates, level);
|
|
1551
|
+
return ok({
|
|
1552
|
+
value,
|
|
1553
|
+
unit: composition.measure.unit,
|
|
1554
|
+
level,
|
|
1555
|
+
interval,
|
|
1556
|
+
run: {
|
|
1557
|
+
low: interval.low + widen.low,
|
|
1558
|
+
high: interval.high + widen.high
|
|
1559
|
+
},
|
|
1560
|
+
replicates,
|
|
1561
|
+
parts,
|
|
1562
|
+
extrapolated
|
|
1563
|
+
});
|
|
1564
|
+
};
|
|
1565
|
+
/** This function adds the leaves of a tree to `into`, each times `weight`. */
|
|
1566
|
+
const flatten = (tree, weight, into) => {
|
|
1567
|
+
for (const node of tree.nodes) if (node.kind === "composition") flatten(node.tree, weight * node.weight, into);
|
|
1568
|
+
else into.push({
|
|
1569
|
+
law: node.law,
|
|
1570
|
+
point: node.point,
|
|
1571
|
+
weight: weight * node.weight
|
|
1572
|
+
});
|
|
1573
|
+
};
|
|
1574
|
+
/**
|
|
1575
|
+
* This function gives the flat parts of a prepared composition over a demand, or the problem that
|
|
1576
|
+
* stops them. The value of a row is the sum of the law of each part at its point, times its weight.
|
|
1577
|
+
* The function does not examine the demand.
|
|
1578
|
+
*/
|
|
1579
|
+
const flatPartsOf = (prepared, demand, given = {}) => {
|
|
1580
|
+
const tree = treeOf(prepared, demand, given);
|
|
1581
|
+
if (tree.tag === "err") return tree;
|
|
1582
|
+
const parts = [];
|
|
1583
|
+
flatten(tree.value, 1, parts);
|
|
1584
|
+
return ok(parts);
|
|
1585
|
+
};
|
|
1586
|
+
/**
|
|
1587
|
+
* This function gives the value of one row of some flat parts. Row 0 is the estimate, and row `i`
|
|
1588
|
+
* is replicate `i - 1` of each law. A part with no finite value gives undefined.
|
|
1589
|
+
*/
|
|
1590
|
+
const rowValueOf = (parts, row) => rowValuesOf(parts, row, row)[0];
|
|
1591
|
+
/** This function gives the record that the law of a part reads in one row, or undefined when the law has no such row. */
|
|
1592
|
+
const rowRecordOf = (law, point, row) => {
|
|
1593
|
+
const values = row === 0 ? law.constants.map((constant) => constant.value) : law.replicates[row - 1];
|
|
1594
|
+
return values === void 0 ? void 0 : recordOf(law.constants.map((constant) => constant.id), values, point);
|
|
1595
|
+
};
|
|
1596
|
+
/**
|
|
1597
|
+
* This function gives the value of each row from `first` to `last` of some flat parts, as `rowValueOf` gives it.
|
|
1598
|
+
* It evaluates the law of each part over all the rows in one call. Thus a search that reads each row at one load
|
|
1599
|
+
* evaluates each law one time for that load. Past the range of a variable, each value holds, as heldValue tells.
|
|
1600
|
+
*/
|
|
1601
|
+
const rowValuesOf = (parts, first, last) => {
|
|
1602
|
+
const sums = [];
|
|
1603
|
+
for (let row = first; row <= last; row += 1) sums.push(0);
|
|
1604
|
+
for (const { law, point, weight } of parts) {
|
|
1605
|
+
const edge = edgePointOf(law, point);
|
|
1606
|
+
const atEdge = edge === void 0 ? void 0 : edgeValuesOf(law, edge);
|
|
1607
|
+
const records = [];
|
|
1608
|
+
const slots = [];
|
|
1609
|
+
for (let row = first; row <= last; row += 1) {
|
|
1610
|
+
const record = sums[row - first] === void 0 ? void 0 : rowRecordOf(law, point, row);
|
|
1611
|
+
if (record === void 0) sums[row - first] = void 0;
|
|
1612
|
+
else {
|
|
1613
|
+
records.push(record);
|
|
1614
|
+
slots.push(row - first);
|
|
1615
|
+
}
|
|
1616
|
+
}
|
|
1617
|
+
valuesAt(law.form.program, records).forEach((value, index) => {
|
|
1618
|
+
const slot = slots[index];
|
|
1619
|
+
const found = atEdge === void 0 ? value : heldValue(value, atEdge[slot + first]);
|
|
1620
|
+
sums[slot] = found === void 0 ? void 0 : sums[slot] + weight * found;
|
|
1621
|
+
});
|
|
1622
|
+
}
|
|
1623
|
+
return sums.map((value) => value !== void 0 && Number.isFinite(value) ? value : void 0);
|
|
1624
|
+
};
|
|
1625
|
+
/** This function tells if a flat part reads a variable of its law outside the range of that variable. */
|
|
1626
|
+
const partsOutside = (parts) => parts.some(({ law, point }) => law.variables.some((variable) => point[variable.id] < variable.min || point[variable.id] > variable.max));
|
|
1627
|
+
// src/instance.ts
|
|
1628
|
+
/** This text is the format of an instance file. */
|
|
1629
|
+
const INSTANCE_FORMAT = "capacity-instance/1";
|
|
1630
|
+
/** This text is the format of a limits file. */
|
|
1631
|
+
const LIMITS_FORMAT = "capacity-limits/1";
|
|
1632
|
+
/**
|
|
1633
|
+
* These units are the unit of each resource that has one meaning in each instance. A resource with
|
|
1634
|
+
* one of these names is in a unit that converts to the unit here.
|
|
1635
|
+
*
|
|
1636
|
+
* - `cpu` is the CPU time that the instance gives in a unit of time, in cores.
|
|
1637
|
+
* - `memory` is the memory of the instance.
|
|
1638
|
+
* - `network` is the rate of the network of the instance.
|
|
1639
|
+
*/
|
|
1640
|
+
const RESOURCE_UNITS = {
|
|
1641
|
+
cpu: "{core}",
|
|
1642
|
+
memory: "B",
|
|
1643
|
+
network: "bit/s"
|
|
1644
|
+
};
|
|
1645
|
+
// src/instance-check.ts
|
|
1646
|
+
/** This function makes a problem of an instance. */
|
|
1647
|
+
const instanceProblem = (code, path, message) => ({
|
|
1648
|
+
code,
|
|
1649
|
+
path,
|
|
1650
|
+
message
|
|
1651
|
+
});
|
|
1652
|
+
/** This function makes a problem of a limit. */
|
|
1653
|
+
const limitProblem = (code, path, message) => ({
|
|
1654
|
+
code,
|
|
1655
|
+
path,
|
|
1656
|
+
message
|
|
1657
|
+
});
|
|
1658
|
+
/**
|
|
1659
|
+
* This function examines the resources of an instance and gives each problem that it finds. An
|
|
1660
|
+
* instance with no problem gives an empty list.
|
|
1661
|
+
*
|
|
1662
|
+
* It examines that the instance has a resource. It examines the name and the amount of each
|
|
1663
|
+
* resource. It examines the unit of each resource of `RESOURCE_UNITS`.
|
|
1664
|
+
*/
|
|
1665
|
+
const instanceProblems = (instance) => {
|
|
1666
|
+
const problems = [];
|
|
1667
|
+
const entries = Object.entries(instance.resources);
|
|
1668
|
+
if (entries.length === 0) problems.push(instanceProblem("resource", "resources", "The instance has no resource."));
|
|
1669
|
+
for (const [name, amount] of entries) {
|
|
1670
|
+
const at = `resources.${name}`;
|
|
1671
|
+
if (!NAME.test(name)) problems.push(instanceProblem("id", at, `The name "${name}" is not letters and digits that start with a lower-case letter.`));
|
|
1672
|
+
if (!isQuantity(amount)) {
|
|
1673
|
+
problems.push(instanceProblem("amount", at, `"${name}" is not a finite amount in a canonical unit.`));
|
|
1674
|
+
continue;
|
|
1675
|
+
}
|
|
1676
|
+
if (!(amount.value > 0)) problems.push(instanceProblem("amount", at, `The resource "${name}" is not above 0.`));
|
|
1677
|
+
const unit = Object.hasOwn(RESOURCE_UNITS, name) ? RESOURCE_UNITS[name] : void 0;
|
|
1678
|
+
if (unit !== void 0 && !convertible(amount.unit, unit)) problems.push(instanceProblem("unit", at, `The resource "${name}" is in "${amount.unit}". Its unit converts to "${unit}".`));
|
|
1679
|
+
}
|
|
1680
|
+
return problems;
|
|
1681
|
+
};
|
|
1682
|
+
/**
|
|
1683
|
+
* This function examines an instance type and gives each problem that it finds. An instance type
|
|
1684
|
+
* with no problem gives an empty list.
|
|
1685
|
+
*
|
|
1686
|
+
* It examines the format, the id and the title. It examines the resources with `instanceProblems`.
|
|
1687
|
+
*/
|
|
1688
|
+
const checkInstance = (type) => {
|
|
1689
|
+
const problems = [];
|
|
1690
|
+
if (type.format !== "capacity-instance/1") problems.push(instanceProblem("format", "format", `The format is "${String(type.format)}", not "${INSTANCE_FORMAT}".`));
|
|
1691
|
+
if (!SLUG.test(type.id)) problems.push(instanceProblem("id", "id", `The id "${type.id}" is not lower-case words joined by ".", "-" or "/".`));
|
|
1692
|
+
if (type.title.trim() === "") problems.push(instanceProblem("id", "title", "The title is empty."));
|
|
1693
|
+
problems.push(...instanceProblems(type));
|
|
1694
|
+
return problems;
|
|
1695
|
+
};
|
|
1696
|
+
/**
|
|
1697
|
+
* This function examines one limit and gives each problem that it finds. The text `path` is the
|
|
1698
|
+
* path of the limit, and it goes before the path of each problem.
|
|
1699
|
+
*/
|
|
1700
|
+
const limitProblems = (limit, path = "") => {
|
|
1701
|
+
const problems = [];
|
|
1702
|
+
const at = (field) => path === "" ? field : `${path}.${field}`;
|
|
1703
|
+
if (!SLUG.test(limit.id)) problems.push(limitProblem("id", at("id"), `The id "${limit.id}" is not lower-case words joined by ".", "-" or "/".`));
|
|
1704
|
+
if (!SLUG.test(limit.measure)) problems.push(limitProblem("measure", at("measure"), `The measure "${limit.measure}" is not lower-case words joined by ".", "-" or "/".`));
|
|
1705
|
+
if (!finite(limit.headroom) || limit.headroom < 0 || limit.headroom >= 1) problems.push(limitProblem("headroom", at("headroom"), `The headroom ${limit.headroom} is not a number from 0 to below 1.`));
|
|
1706
|
+
const ceiling = limit.ceiling;
|
|
1707
|
+
if (ceiling.kind === "resource") {
|
|
1708
|
+
if (!NAME.test(ceiling.resource)) problems.push(limitProblem("resource", at("ceiling.resource"), `The name "${ceiling.resource}" is not letters and digits that start with a lower-case letter.`));
|
|
1709
|
+
} else if (!isQuantity(ceiling.amount)) problems.push(limitProblem("amount", at("ceiling.amount"), "The ceiling is not a finite amount in a canonical unit."));
|
|
1710
|
+
else if (!(ceiling.amount.value > 0)) problems.push(limitProblem("amount", at("ceiling.amount"), "The ceiling is not above 0."));
|
|
1711
|
+
return problems;
|
|
1712
|
+
};
|
|
1713
|
+
/**
|
|
1714
|
+
* This function examines a limit set and gives each problem that it finds. A limit set with no
|
|
1715
|
+
* problem gives an empty list.
|
|
1716
|
+
*
|
|
1717
|
+
* It examines the format, the id and the title. It examines each limit with `limitProblems`. It
|
|
1718
|
+
* examines that two limits do not have one id.
|
|
1719
|
+
*/
|
|
1720
|
+
const checkLimits = (set) => {
|
|
1721
|
+
const problems = [];
|
|
1722
|
+
if (set.format !== "capacity-limits/1") problems.push(limitProblem("format", "format", `The format is "${String(set.format)}", not "${LIMITS_FORMAT}".`));
|
|
1723
|
+
if (!SLUG.test(set.id)) problems.push(limitProblem("id", "id", `The id "${set.id}" is not lower-case words joined by ".", "-" or "/".`));
|
|
1724
|
+
if (set.title.trim() === "") problems.push(limitProblem("id", "title", "The title is empty."));
|
|
1725
|
+
set.limits.forEach((limit, index) => problems.push(...limitProblems(limit, `limits[${index}]`)));
|
|
1726
|
+
const twice = repeated(set.limits.map((limit) => limit.id));
|
|
1727
|
+
if (twice !== void 0) problems.push(limitProblem("id", "limits", `Two limits have the id "${twice}".`));
|
|
1728
|
+
return problems;
|
|
1729
|
+
};
|
|
1730
|
+
export { isUtc as $, placeholdersOf as A, WORKLOAD_FORMAT as B, shareOf as C, fillPlaceholders as D, checkApp as E, checkWorkload as F, checkLaw as G, intervalOf as H, demandProblems as I, valuesAt as J, LAW_DOMAINS as K, workloadProblem as L, APP_FILE as M, APP_FORMAT as N, isHttps as O, APP_LINE_KINDS as P, finite as Q, NO_DEMAND as R, scaleDemand as S, appWorkloadProblems as T, lawValue as U, constantIntervals as V, levelProblem as W, NAME as X, valuesOf as Y, SLUG as Z, rowValuesOf as _, limitProblems as a, leastCount as at, mergeDemands as b, RESOURCE_UNITS as c, compositionValue as d, repeated as et, flatPartsOf as f, rowValueOf as g, preparedOf as h, limitProblem as i, bootstrapDraws as it, requestPath as j, metricsGaps as k, COMPOSITION_FORMAT as l, partsOutside as m, checkLimits as n, LAW_FORMAT as nt, INSTANCE_FORMAT as o, leastGroups as ot, isLawTerm as p, recordOf as q, instanceProblems as r, MAX_VARIABLES as rt, LIMITS_FORMAT as s, checkInstance as t, DEFAULT_LEVEL as tt, checkComposition as u, instancePointOf as v, totalRate as w, portionOf as x, loadPointOf as y, RATE as z };
|
|
1731
|
+
|
|
1732
|
+
//# sourceMappingURL=instance-check-Bt-SI8va.js.map
|