@mark1russell7/capacity 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1108 -2
- package/build/index.d.ts +858 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +1713 -0
- package/build/index.js.map +1 -0
- package/build/instance-0n6WhJzF.d.ts +782 -0
- package/build/instance-0n6WhJzF.d.ts.map +1 -0
- package/build/instance-check-CAuD9L4Z.js +1711 -0
- package/build/instance-check-CAuD9L4Z.js.map +1 -0
- package/build/schema.d.ts +69 -0
- package/build/schema.d.ts.map +1 -0
- package/build/schema.js +315 -0
- package/build/schema.js.map +1 -0
- package/limits/alerts-routes.line.json +78 -0
- package/limits/alerts-scale.limits.json +10 -0
- package/limits/alerts-start-gate.limits.json +1149 -0
- package/limits/alerts-start-routes-limit-home.bracket.json +543 -0
- package/limits/alerts-start-routes-limit-mid.bracket.json +464 -0
- package/limits/alerts-start-routes-limit.bracket.json +579 -0
- package/limits/alerts-tail-gate.limits.json +610 -0
- package/package.json +85 -3
- package/src/app-check.ts +477 -0
- package/src/app-metrics.ts +105 -0
- package/src/app.ts +239 -0
- package/src/check.ts +228 -0
- package/src/composition.ts +680 -0
- package/src/demand.ts +312 -0
- package/src/fit.ts +153 -0
- package/src/forms.ts +358 -0
- package/src/gate.ts +237 -0
- package/src/index.ts +142 -0
- package/src/instance-check.ts +122 -0
- package/src/instance.ts +84 -0
- package/src/knee.ts +606 -0
- package/src/law.ts +215 -0
- package/src/limit.ts +112 -0
- package/src/names.ts +19 -0
- package/src/observe.ts +74 -0
- package/src/program.ts +74 -0
- package/src/scale.ts +617 -0
- package/src/schema.ts +260 -0
- package/src/value.ts +206 -0
- package/src/workload-check.ts +130 -0
- package/src/workload.ts +88 -0
package/src/app.ts
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
// The contract of an app: the one record that tells the lab and the models how to load an app, how to
|
|
2
|
+
// start it, and how to read its live metrics. Each app gives the same contract, from the bare subjects
|
|
3
|
+
// of layer 1 to the apps of layer 3.
|
|
4
|
+
|
|
5
|
+
import type { Problem, Scale } from "./law.ts";
|
|
6
|
+
|
|
7
|
+
/** This text is the format of the contract file of an app. */
|
|
8
|
+
export const APP_FORMAT = "capacity-app/1";
|
|
9
|
+
|
|
10
|
+
/** This name is the contract file of an app, at the root of its repository. */
|
|
11
|
+
export const APP_FILE = "capacity-app.json";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The kind of a load variable tells where the amount is.
|
|
15
|
+
*
|
|
16
|
+
* - `request`: an amount of one request of a class, for example the bytes of its body.
|
|
17
|
+
* - `level`: an amount that stays in the app and does not come with each request, for example the open incidents.
|
|
18
|
+
* - `store`: an amount of a store at the start of the app, for example the bytes of its log.
|
|
19
|
+
* - `run`: an amount that the run of the app gives in its line `amounts`, for example the routes that its configuration makes.
|
|
20
|
+
* No workload and no start case sets it. A start law can read it as its variable.
|
|
21
|
+
*/
|
|
22
|
+
export type VariableKind = "request" | "level" | "store" | "run";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A range tells the amounts of a variable that the app accepts and that a run can measure. The unit
|
|
26
|
+
* is a canonical unit of `@mark1russell7/quantity`. A range on the scale `log` starts above 0.
|
|
27
|
+
*/
|
|
28
|
+
export interface AppRange {
|
|
29
|
+
readonly unit: string;
|
|
30
|
+
readonly min: number;
|
|
31
|
+
readonly max: number;
|
|
32
|
+
readonly scale: Scale;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** A load variable of an app is one amount that a workload or a start case sets, or that the run gives. A program of a law reads it by its id. */
|
|
36
|
+
export interface AppVariable extends AppRange {
|
|
37
|
+
readonly id: string;
|
|
38
|
+
readonly title: string;
|
|
39
|
+
readonly kind: VariableKind;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* A request of HTTP comes from a client of the app. The path is a template: each `{id}` in it is a request
|
|
44
|
+
* variable of the class. The variable `body`, when it is there, gives the bytes of the body of the request.
|
|
45
|
+
*/
|
|
46
|
+
export interface HttpEntry {
|
|
47
|
+
readonly kind: "http";
|
|
48
|
+
readonly method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
49
|
+
readonly path: string;
|
|
50
|
+
readonly body?: string | undefined;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* A service entry tells that the load of a class comes from an outside service that the app calls. The
|
|
55
|
+
* fake of the service reads the workload, and it gives the app the requests of the class at their rates.
|
|
56
|
+
*/
|
|
57
|
+
export interface ServiceEntry {
|
|
58
|
+
readonly kind: "service";
|
|
59
|
+
readonly service: string;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** An entry tells how the requests of a class get to the app: from a client of HTTP, or from an outside service. */
|
|
63
|
+
export type AppEntry = HttpEntry | ServiceEntry;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* A class of an app is one kind of request. `rate` is the range of its rate, in a count in a unit of
|
|
67
|
+
* time. `request` names the request variables of one request, by their ids.
|
|
68
|
+
*/
|
|
69
|
+
export interface AppClass {
|
|
70
|
+
readonly id: string;
|
|
71
|
+
readonly title: string;
|
|
72
|
+
readonly rate: AppRange;
|
|
73
|
+
readonly request: readonly string[];
|
|
74
|
+
readonly entry: AppEntry;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* A command is a program and its arguments, with no shell. `cwd` is the folder of the command, relative to
|
|
79
|
+
* the root of the repository. `env` gives the environment variables that the command adds.
|
|
80
|
+
*
|
|
81
|
+
* Each text of `argv` and of `env` can hold placeholders. The lab puts in their values before the start:
|
|
82
|
+
*
|
|
83
|
+
* - `{port}`: the port of the listener of the process.
|
|
84
|
+
* - `{store}`: the folder of the store of the app.
|
|
85
|
+
* - `{workload}`: the path of the workload file of the run.
|
|
86
|
+
* - `{service.<id>}`: the base URL of the fake of the service `<id>`.
|
|
87
|
+
* - `{variable.<id>}`: the amount of the store or level variable `<id>` at the point, in the unit of the variable.
|
|
88
|
+
*
|
|
89
|
+
* The run holds `{port}` when a class comes from HTTP or the ready signal is `http`. The seed and the run hold
|
|
90
|
+
* `{store}` when the app has a store variable. The seed can hold `{variable.<id>}` of a store or a level variable,
|
|
91
|
+
* for example the routes of a configuration. The fake of each service holds `{port}`, and `{workload}` when a class comes from the service.
|
|
92
|
+
*/
|
|
93
|
+
export interface AppCommand {
|
|
94
|
+
readonly argv: readonly string[];
|
|
95
|
+
readonly cwd: string;
|
|
96
|
+
readonly env: Readonly<Record<string, string>>;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The ready signal tells that a process serves. `line` is a text in a line of the output of the process.
|
|
101
|
+
* `http` is a path of the process that gives a status of 200.
|
|
102
|
+
*/
|
|
103
|
+
export type AppReady = { readonly kind: "line"; readonly text: string } | { readonly kind: "http"; readonly path: string };
|
|
104
|
+
|
|
105
|
+
/** A process is a command that runs until the lab stops it, and its ready signal. */
|
|
106
|
+
export interface AppProcess extends AppCommand {
|
|
107
|
+
readonly ready: AppReady;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** The kinds of the lines of a run that the lab records. The lab knows the fields of each kind. */
|
|
111
|
+
export const APP_LINE_KINDS = ["pass", "write", "readings", "amounts"] as const;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* The kind of a line of a run.
|
|
115
|
+
*
|
|
116
|
+
* - `pass`: the end of one pass of the loop of the app. The lab counts the passes in the watch after ready.
|
|
117
|
+
* - `write`: one write of the snapshot of the start. The fields `longestMs`, `writeMs`, `rowsMs`, `wallMs` and `cpuMs`
|
|
118
|
+
* give its times in milliseconds. The lab keeps the longest value of each time.
|
|
119
|
+
* - `readings`: one collection of the live metrics of the app. The field `readings` is a list of `SeriesReading`. The
|
|
120
|
+
* lab keeps each collection with its time, so the record of a start has the series of the metrics of the app.
|
|
121
|
+
* - `amounts`: the amounts of the run variables. The field `amounts` gives the amount of each run variable by its id, in the
|
|
122
|
+
* unit of the variable. The lab keeps the last line up to the end of the watch.
|
|
123
|
+
*/
|
|
124
|
+
export type AppLineKind = (typeof APP_LINE_KINDS)[number];
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* A line of a run is a JSON line of the output of the run that the lab records. The line is a JSON object, and its
|
|
128
|
+
* field `ev` has the text `ev`. `kind` tells what the lab reads from it.
|
|
129
|
+
*/
|
|
130
|
+
export interface AppLine {
|
|
131
|
+
readonly kind: AppLineKind;
|
|
132
|
+
readonly ev: string;
|
|
133
|
+
readonly title: string;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** The run of an app is its process, and the lines of its output that the lab records. A run with no `lines` gives no pass and no write. */
|
|
137
|
+
export interface AppRun extends AppProcess {
|
|
138
|
+
readonly lines?: readonly AppLine[] | undefined;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* The start of an app tells how to make it run in a tree of its repository.
|
|
143
|
+
*
|
|
144
|
+
* - `install` installs the dependencies of the tree. An app with no dependencies has no install.
|
|
145
|
+
* - `check` examines the tree after the install, for example with a typecheck.
|
|
146
|
+
* - `seed` writes the store of a start case at the amounts of the store variables. An app with no store variable has no seed.
|
|
147
|
+
* The last line of the output of the seed is a JSON record. Its field `amounts` gives the amount of each store variable
|
|
148
|
+
* that the seed wrote, by its id, in the unit of the variable.
|
|
149
|
+
* - `run` is the process of the app, and the lines of its output that the lab records.
|
|
150
|
+
*/
|
|
151
|
+
export interface AppStart {
|
|
152
|
+
readonly install?: AppCommand | undefined;
|
|
153
|
+
readonly check?: AppCommand | undefined;
|
|
154
|
+
readonly seed?: AppCommand | undefined;
|
|
155
|
+
readonly run: AppRun;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* An outside service of an app is a service of others that the app calls, for example a feed or a channel
|
|
160
|
+
* of messages. The lab starts the fake of the service in place of the service, and the app gets its URL.
|
|
161
|
+
*/
|
|
162
|
+
export interface AppService {
|
|
163
|
+
readonly id: string;
|
|
164
|
+
readonly title: string;
|
|
165
|
+
readonly fake: AppProcess;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* The target of a series tells what the series measures.
|
|
170
|
+
*
|
|
171
|
+
* - `rate`: a counter of the requests of a class. Its rate is the rate of the class.
|
|
172
|
+
* - `level`: a gauge of a level variable.
|
|
173
|
+
* - `total`: a counter of the sum of a request variable over the requests of a class. Its increase over
|
|
174
|
+
* the increase of the counter of the class is the mean of the variable.
|
|
175
|
+
* - `measure`: a counter or a gauge of a measure of the laws of the app, for example its CPU time.
|
|
176
|
+
*/
|
|
177
|
+
export type SeriesTarget =
|
|
178
|
+
| { readonly rate: string }
|
|
179
|
+
| { readonly level: string }
|
|
180
|
+
| { readonly total: string; readonly class: string }
|
|
181
|
+
| { readonly measure: string };
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* A series is one live metric of an app. Its `id` is the name of the metric in the readings of the app.
|
|
185
|
+
* A counter only goes up, until the process starts again. A gauge is an amount at the time of its reading.
|
|
186
|
+
*/
|
|
187
|
+
export interface AppSeries {
|
|
188
|
+
readonly id: string;
|
|
189
|
+
readonly kind: "counter" | "gauge";
|
|
190
|
+
readonly unit: string;
|
|
191
|
+
readonly of: SeriesTarget;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* The contract of an app is the record that each app gives to the lab and to the models. A contract file
|
|
196
|
+
* is this record as JSON. The `id` is the subject of the laws of the app.
|
|
197
|
+
*/
|
|
198
|
+
export interface AppContract {
|
|
199
|
+
readonly format: typeof APP_FORMAT;
|
|
200
|
+
readonly id: string;
|
|
201
|
+
readonly title: string;
|
|
202
|
+
/** This URL is the public HTTPS URL of the repository of the app. */
|
|
203
|
+
readonly repository: string;
|
|
204
|
+
readonly variables: readonly AppVariable[];
|
|
205
|
+
readonly classes: readonly AppClass[];
|
|
206
|
+
readonly start: AppStart;
|
|
207
|
+
readonly services: readonly AppService[];
|
|
208
|
+
readonly metrics: readonly AppSeries[];
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** An app problem code names the part of a contract, or of a workload against a contract, that is not correct. */
|
|
212
|
+
export type AppProblemCode =
|
|
213
|
+
| "shape"
|
|
214
|
+
| "format"
|
|
215
|
+
| "id"
|
|
216
|
+
| "range"
|
|
217
|
+
| "unit"
|
|
218
|
+
| "variable"
|
|
219
|
+
| "class"
|
|
220
|
+
| "entry"
|
|
221
|
+
| "command"
|
|
222
|
+
| "placeholder"
|
|
223
|
+
| "service"
|
|
224
|
+
| "series"
|
|
225
|
+
| "workload";
|
|
226
|
+
|
|
227
|
+
/** An app problem tells what is not correct, and where: `path` is the path of the field in the contract or in the workload. */
|
|
228
|
+
export type AppProblem = Problem<AppProblemCode>;
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* A gap is one part of the demand of an app that its live metrics cannot give. `need` names the series that
|
|
232
|
+
* is not there. It is the counter of a class, the gauge of a level, or the counter of a total.
|
|
233
|
+
*/
|
|
234
|
+
export interface MetricsGap {
|
|
235
|
+
readonly need: "rate" | "level" | "total";
|
|
236
|
+
readonly class?: string | undefined;
|
|
237
|
+
readonly variable?: string | undefined;
|
|
238
|
+
readonly message: string;
|
|
239
|
+
}
|
package/src/check.ts
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { isCanonicalUnit, isQuantity } from "@mark1russell7/quantity";
|
|
2
|
+
import { deps, parse } from "@mark1russell7/vex";
|
|
3
|
+
import { DEFAULT_LEVEL, LAW_FORMAT, MAX_VARIABLES, leastCount, type Law, type LawProblem, type Run, type Variable } from "./law.ts";
|
|
4
|
+
import { NAME, SLUG, finite, isUtc, repeated } from "./names.ts";
|
|
5
|
+
import { LAW_DOMAINS, recordOf, valuesOf } from "./program.ts";
|
|
6
|
+
|
|
7
|
+
const COMMIT = /^[0-9a-f]{40}$/;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A column of replicates is flat when its width is this share of its largest size or less. The search of a shape
|
|
11
|
+
* stops at a bracket of `SEPARABLE_SEARCH_TOLERANCE` (1e-10) of its start width. Thus a shape at the same edge of its
|
|
12
|
+
* search in each fit gives a flat column. A bootstrap of measured values gives a much wider column.
|
|
13
|
+
*/
|
|
14
|
+
const FLAT_SHARE = 1e-9;
|
|
15
|
+
|
|
16
|
+
/** This function gives the middle of the range of a variable, on its scale. */
|
|
17
|
+
const middleOf = (variable: Variable): number =>
|
|
18
|
+
variable.scale === "log" ? Math.sqrt(variable.min * variable.max) : (variable.min + variable.max) / 2;
|
|
19
|
+
|
|
20
|
+
/** This function gives each corner of the ranges of some variables, and their middle. */
|
|
21
|
+
const cornersOf = (variables: readonly Variable[]): readonly (readonly number[])[] => {
|
|
22
|
+
const corners: number[][] = [];
|
|
23
|
+
for (let mask = 0; mask < 2 ** variables.length; mask += 1) {
|
|
24
|
+
corners.push(variables.map((variable, index) => ((mask >> index) & 1) === 1 ? variable.max : variable.min));
|
|
25
|
+
}
|
|
26
|
+
return [...corners, variables.map(middleOf)];
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* This function examines a law and gives each problem that it finds. A law with no problem gives
|
|
31
|
+
* an empty list.
|
|
32
|
+
*
|
|
33
|
+
* It examines the ids, the units and the ranges. It examines that the program reads each variable
|
|
34
|
+
* and each constant, and no other name. It examines that the law has a finite value at each corner
|
|
35
|
+
* and at the middle of its ranges. It examines that the replicates and the residuals can show an
|
|
36
|
+
* interval of the level 0.95, and that the evidence names its runs. It examines that the noise
|
|
37
|
+
* has a floor of 0 or more, a CPU model and its runs.
|
|
38
|
+
*
|
|
39
|
+
* A law with points must have one point for each residual. Each point must be in the ranges of the
|
|
40
|
+
* variables and name a run of the evidence.
|
|
41
|
+
*
|
|
42
|
+
* The interval of each constant must have a width above 0. A constant with the same value in each
|
|
43
|
+
* replicate is a bound of a search, not a measured value. For example, a shape at the edge of its
|
|
44
|
+
* search in each fit of the bootstrap gives this.
|
|
45
|
+
*/
|
|
46
|
+
export const checkLaw = (law: Law): readonly LawProblem[] => {
|
|
47
|
+
const problems: LawProblem[] = [];
|
|
48
|
+
const add = (code: LawProblem["code"], path: string, message: string): void => {
|
|
49
|
+
problems.push({ code, path, message });
|
|
50
|
+
};
|
|
51
|
+
const slug = (path: string, value: string): void => {
|
|
52
|
+
if (!SLUG.test(value)) add("id", path, `The id "${value}" is not lower-case words joined by ".", "-" or "/".`);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
if (law.format !== LAW_FORMAT) add("format", "format", `The format is "${String(law.format)}", not "${LAW_FORMAT}".`);
|
|
56
|
+
slug("id", law.id);
|
|
57
|
+
slug("subject", law.subject);
|
|
58
|
+
slug("measure.id", law.measure.id);
|
|
59
|
+
slug("form.id", law.form.id);
|
|
60
|
+
if (law.title.trim() === "") add("id", "title", "The title is empty.");
|
|
61
|
+
if (!isCanonicalUnit(law.measure.unit)) add("unit", "measure.unit", `"${law.measure.unit}" is not a canonical unit.`);
|
|
62
|
+
|
|
63
|
+
// The variables: a name, a unit and a range that moves.
|
|
64
|
+
if (law.variables.length > MAX_VARIABLES) {
|
|
65
|
+
add("range", "variables", `The law has ${law.variables.length} variables. The most is ${MAX_VARIABLES}.`);
|
|
66
|
+
}
|
|
67
|
+
law.variables.forEach((variable, index) => {
|
|
68
|
+
const path = `variables[${index}]`;
|
|
69
|
+
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.`);
|
|
70
|
+
if (!isCanonicalUnit(variable.unit)) add("unit", `${path}.unit`, `"${variable.unit}" is not a canonical unit.`);
|
|
71
|
+
if (!finite(variable.min) || !finite(variable.max) || !(variable.min < variable.max)) {
|
|
72
|
+
add("range", path, `The range of "${variable.id}" does not go from a finite number to a larger finite number.`);
|
|
73
|
+
} else if (variable.scale === "log" && !(variable.min > 0)) {
|
|
74
|
+
add("range", path, `The variable "${variable.id}" has the scale "log", thus its range starts above 0.`);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
const variables = law.variables.map((variable) => variable.id);
|
|
78
|
+
const twoVariables = repeated(variables);
|
|
79
|
+
if (twoVariables !== undefined) add("id", "variables", `Two variables have the name "${twoVariables}".`);
|
|
80
|
+
|
|
81
|
+
// The conditions: each is a name with a plain value or a quantity.
|
|
82
|
+
for (const [name, value] of Object.entries(law.conditions)) {
|
|
83
|
+
slug(`conditions.${name}`, name);
|
|
84
|
+
if (typeof value === "number" && !finite(value)) add("condition", `conditions.${name}`, `The condition "${name}" is not a finite number.`);
|
|
85
|
+
if (typeof value === "object" && !isQuantity(value)) {
|
|
86
|
+
add("condition", `conditions.${name}`, `The condition "${name}" is not a finite amount in a canonical unit.`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// The constants and their replicates.
|
|
91
|
+
const constants = law.constants.map((constant) => constant.id);
|
|
92
|
+
if (constants.length === 0) add("constant", "constants", "The law has no constant.");
|
|
93
|
+
law.constants.forEach((constant, index) => {
|
|
94
|
+
const path = `constants[${index}]`;
|
|
95
|
+
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.`);
|
|
96
|
+
if (!finite(constant.value)) add("constant", `${path}.value`, `The constant "${constant.id}" is not a finite number.`);
|
|
97
|
+
if (variables.includes(constant.id)) add("id", `${path}.id`, `"${constant.id}" names a variable and a constant.`);
|
|
98
|
+
});
|
|
99
|
+
const twoConstants = repeated(constants);
|
|
100
|
+
if (twoConstants !== undefined) add("id", "constants", `Two constants have the name "${twoConstants}".`);
|
|
101
|
+
|
|
102
|
+
const least = leastCount(DEFAULT_LEVEL);
|
|
103
|
+
if (law.replicates.length < least) {
|
|
104
|
+
add("replicates", "replicates", `The law has ${law.replicates.length} replicates. The level ${DEFAULT_LEVEL} needs ${least}.`);
|
|
105
|
+
}
|
|
106
|
+
const badRow = law.replicates.findIndex((row) => row.length !== constants.length || !row.every(finite));
|
|
107
|
+
const rowsAreGood = badRow === -1;
|
|
108
|
+
if (!rowsAreGood) {
|
|
109
|
+
add("replicates", `replicates[${badRow}]`, `The replicate ${badRow} does not have one finite number for each constant.`);
|
|
110
|
+
} else if (law.replicates.length > 0) {
|
|
111
|
+
law.constants.forEach((constant, index) => {
|
|
112
|
+
const column = law.replicates.map((row) => row[index] as number);
|
|
113
|
+
const low = Math.min(...column);
|
|
114
|
+
const high = Math.max(...column);
|
|
115
|
+
if (finite(constant.value) && (constant.value < low || constant.value > high)) {
|
|
116
|
+
add("replicates", `constants[${index}].value`, `The constant "${constant.id}" is outside each of its replicates.`);
|
|
117
|
+
}
|
|
118
|
+
if (high - low <= FLAT_SHARE * Math.max(Math.abs(low), Math.abs(high))) {
|
|
119
|
+
add(
|
|
120
|
+
"flat",
|
|
121
|
+
`constants[${index}]`,
|
|
122
|
+
`Each replicate of the constant "${constant.id}" has the value ${low}, thus its interval has a width of 0. ` +
|
|
123
|
+
"A shape at the edge of its search in each fit gives this.",
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// The program: a Vex expression that reads each variable and each constant of this law, and no other name.
|
|
130
|
+
const text = JSON.stringify(law.form.program);
|
|
131
|
+
const parsed = text === undefined ? undefined : parse(text, LAW_DOMAINS);
|
|
132
|
+
if (parsed === undefined || !parsed.ok) {
|
|
133
|
+
add("program", "form.program", "The program is not a Vex expression.");
|
|
134
|
+
} else {
|
|
135
|
+
const names = [...variables, ...constants];
|
|
136
|
+
const reads = new Set<string>();
|
|
137
|
+
for (const read of deps(parsed.value)) {
|
|
138
|
+
const name = read.path.join(".");
|
|
139
|
+
if (read.at.length > 0 || read.axes.length > 0) {
|
|
140
|
+
add("read", "form.program", `The program reads "${name}" at a different record. A law has one record.`);
|
|
141
|
+
} else if (read.path.length !== 1 || !names.includes(name)) {
|
|
142
|
+
add("read", "form.program", `The program reads "${name}". The law has no variable and no constant with this name.`);
|
|
143
|
+
} else {
|
|
144
|
+
reads.add(name);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
for (const name of names) {
|
|
148
|
+
if (!reads.has(name)) add("read", "form.program", `The program does not read "${name}".`);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// The value: a finite number at each corner and at the middle of the ranges, and for each replicate at the middle.
|
|
152
|
+
const rangesAreGood = !problems.some((found) => found.code === "range" || found.code === "id" || found.code === "constant");
|
|
153
|
+
if (rangesAreGood && rowsAreGood && !problems.some((found) => found.code === "read")) {
|
|
154
|
+
const estimate = law.constants.map((constant) => constant.value);
|
|
155
|
+
const corners = cornersOf(law.variables).map((corner) => recordOf(constants, estimate, recordOf(variables, corner)));
|
|
156
|
+
const atCorners = valuesOf(parsed.value, corners);
|
|
157
|
+
if (atCorners.tag === "err") {
|
|
158
|
+
add("value", "form.program", `The law has no value at a corner or at the middle of its ranges: ${atCorners.error}.`);
|
|
159
|
+
}
|
|
160
|
+
const middle = recordOf(variables, law.variables.map(middleOf));
|
|
161
|
+
const atMiddle = valuesOf(parsed.value, law.replicates.map((row) => recordOf(constants, row, middle)));
|
|
162
|
+
if (atMiddle.tag === "err") {
|
|
163
|
+
add("value", "replicates", `A replicate has no value at the middle of the ranges: ${atMiddle.error}.`);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// The fit: its residuals and each form that lost.
|
|
169
|
+
if (law.fit.method.trim() === "") add("fit", "fit.method", "The method of the fit is empty.");
|
|
170
|
+
if (law.fit.residuals.length < least) {
|
|
171
|
+
add("fit", "fit.residuals", `The fit has ${law.fit.residuals.length} residuals. The level ${DEFAULT_LEVEL} needs ${least}.`);
|
|
172
|
+
}
|
|
173
|
+
if (!law.fit.residuals.every(finite)) add("fit", "fit.residuals", "A residual is not a finite number.");
|
|
174
|
+
law.fit.losers.forEach((loser, index) => {
|
|
175
|
+
const path = `fit.losers[${index}]`;
|
|
176
|
+
slug(`${path}.form`, loser.form);
|
|
177
|
+
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.`);
|
|
178
|
+
const { mae, rmse } = loser.error;
|
|
179
|
+
if (!finite(mae) || !finite(rmse) || mae < 0 || rmse < mae) {
|
|
180
|
+
add("fit", `${path}.error`, `The error of the form "${loser.form}" is not correct: 0 <= mae <= rmse.`);
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
const twoLosers = repeated(law.fit.losers.map((loser) => loser.form));
|
|
184
|
+
if (twoLosers !== undefined) add("fit", "fit.losers", `Two losers have the form "${twoLosers}".`);
|
|
185
|
+
|
|
186
|
+
// The runs of the evidence and of the noise: each one names its address, its commit and its time.
|
|
187
|
+
const runs = (code: "evidence" | "noise", path: string, list: readonly Run[]): void => {
|
|
188
|
+
if (list.length === 0) add(code, path, "The law names no run.");
|
|
189
|
+
list.forEach((run, index) => {
|
|
190
|
+
const at = `${path}[${index}]`;
|
|
191
|
+
if (!run.url.startsWith("https://")) add(code, `${at}.url`, `The address "${run.url}" does not start with "https://".`);
|
|
192
|
+
if (!COMMIT.test(run.commit)) add(code, `${at}.commit`, `The commit "${run.commit}" is not 40 hexadecimal digits.`);
|
|
193
|
+
if (!isUtc(run.date)) add(code, `${at}.date`, `The date "${run.date}" is not a UTC time, for example "2026-11-18T14:00:00Z".`);
|
|
194
|
+
});
|
|
195
|
+
};
|
|
196
|
+
// The evidence: the runs that measured the points.
|
|
197
|
+
runs("evidence", "evidence.runs", law.evidence.runs);
|
|
198
|
+
|
|
199
|
+
// The points of the fit: one for each residual, each in the ranges of the variables, each from a run of the evidence.
|
|
200
|
+
if (law.points !== undefined) {
|
|
201
|
+
if (law.points.length !== law.fit.residuals.length) {
|
|
202
|
+
add("point", "points", `The law has ${law.points.length} points and ${law.fit.residuals.length} residuals. Each point has one residual.`);
|
|
203
|
+
}
|
|
204
|
+
law.points.forEach((point, index) => {
|
|
205
|
+
const path = `points[${index}]`;
|
|
206
|
+
const names = Object.keys(point.load);
|
|
207
|
+
const other = names.find((name) => !variables.includes(name));
|
|
208
|
+
if (other !== undefined) add("point", `${path}.load.${other}`, `The law has no variable "${other}".`);
|
|
209
|
+
for (const variable of law.variables) {
|
|
210
|
+
const load = point.load[variable.id];
|
|
211
|
+
if (load === undefined || !finite(load)) add("point", `${path}.load.${variable.id}`, `The point gives no finite value for the variable "${variable.id}".`);
|
|
212
|
+
else if (load < variable.min || load > variable.max) add("point", `${path}.load.${variable.id}`, `The value ${load} is outside the range of "${variable.id}".`);
|
|
213
|
+
}
|
|
214
|
+
if (!finite(point.value)) add("point", `${path}.value`, "The value is not a finite number.");
|
|
215
|
+
if (!Number.isInteger(point.run) || point.run < 0 || point.run >= law.evidence.runs.length) {
|
|
216
|
+
add("point", `${path}.run`, `The run ${point.run} is not an index of the ${law.evidence.runs.length} runs of the evidence.`);
|
|
217
|
+
}
|
|
218
|
+
if (!Number.isFinite(Date.parse(point.job))) add("point", `${path}.job`, `The job "${point.job}" is not a time.`);
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// The noise: the floor of the measure on one CPU model, and the runs that measured it.
|
|
223
|
+
if (!finite(law.noise.floor) || law.noise.floor < 0) add("noise", "noise.floor", "The noise floor is not a finite number, 0 or more.");
|
|
224
|
+
if (law.noise.cpu.trim() === "") add("noise", "noise.cpu", "The noise names no CPU model.");
|
|
225
|
+
runs("noise", "noise.runs", law.noise.runs);
|
|
226
|
+
|
|
227
|
+
return problems;
|
|
228
|
+
};
|