@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-check.ts
ADDED
|
@@ -0,0 +1,477 @@
|
|
|
1
|
+
import { convertible, divideUnits, isCanonicalUnit, isQuantity, kindOf, valueIn, type Quantity } from "@mark1russell7/quantity";
|
|
2
|
+
import {
|
|
3
|
+
APP_FORMAT,
|
|
4
|
+
type AppClass,
|
|
5
|
+
type AppContract,
|
|
6
|
+
type AppProblem,
|
|
7
|
+
type AppSeries,
|
|
8
|
+
type AppVariable,
|
|
9
|
+
type AppCommand,
|
|
10
|
+
type HttpEntry,
|
|
11
|
+
type MetricsGap,
|
|
12
|
+
type AppRange,
|
|
13
|
+
type AppReady,
|
|
14
|
+
type AppLine,
|
|
15
|
+
} from "./app.ts";
|
|
16
|
+
import { NAME, SLUG, finite } from "./names.ts";
|
|
17
|
+
import { RATE, type Workload } from "./workload.ts";
|
|
18
|
+
|
|
19
|
+
/** An HTTPS URL: the scheme `https`, a host, and then a path, a query or a fragment with no space. */
|
|
20
|
+
const HTTPS = /^https:\/\/[^\s/?#@]+(?:[/?#]\S*)?$/i;
|
|
21
|
+
|
|
22
|
+
/** This function tells if a text is an HTTPS URL. The package uses no global of a browser or of Node.js. */
|
|
23
|
+
export const isHttps = (text: string): boolean => HTTPS.test(text);
|
|
24
|
+
|
|
25
|
+
/** This function makes a problem of a contract or of a workload against a contract. */
|
|
26
|
+
const appProblem = (code: AppProblem["code"], path: string, message: string): AppProblem => ({ code, path, message });
|
|
27
|
+
|
|
28
|
+
/** The name of a series: letters, digits, `_`, `.`, `/` and `-`, that start with a letter. */
|
|
29
|
+
const METRIC = /^[A-Za-z][A-Za-z0-9_./-]*$/;
|
|
30
|
+
|
|
31
|
+
/** The name of an environment variable: upper-case letters, digits and `_`, that start with a letter or `_`. */
|
|
32
|
+
const ENV = /^[A-Z_][A-Z0-9_]*$/;
|
|
33
|
+
|
|
34
|
+
const PLACEHOLDER = /\{([^{}]*)\}/g;
|
|
35
|
+
|
|
36
|
+
/** A place is a command of the contract. Each place accepts its own placeholders. */
|
|
37
|
+
type Place = "install" | "check" | "seed" | "run" | "fake";
|
|
38
|
+
|
|
39
|
+
/** These names are the plain placeholders that each place accepts. `service` and `variable` take an id after a `.`. */
|
|
40
|
+
const PLAIN: Readonly<Record<Place, readonly string[]>> = {
|
|
41
|
+
install: [],
|
|
42
|
+
check: [],
|
|
43
|
+
seed: ["store"],
|
|
44
|
+
run: ["port", "store"],
|
|
45
|
+
fake: ["port", "workload"],
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/** This function gives the names of the placeholders in a text, in their order. */
|
|
49
|
+
export const placeholdersOf = (text: string): readonly string[] => [...text.matchAll(PLACEHOLDER)].map((match) => match[1] ?? "");
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* This function puts the value of each placeholder into a text. It gives undefined when the text holds a
|
|
53
|
+
* placeholder that `values` does not give.
|
|
54
|
+
*/
|
|
55
|
+
export const fillPlaceholders = (text: string, values: Readonly<Record<string, string>>): string | undefined => {
|
|
56
|
+
if (placeholdersOf(text).some((name) => !Object.hasOwn(values, name))) return undefined;
|
|
57
|
+
return text.replace(PLACEHOLDER, (_match, name: string) => values[name] ?? "");
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* This function gives the path of a request of a class of HTTP at some amounts. Each amount is a number in
|
|
62
|
+
* the unit of its variable. It gives undefined when `values` does not give each variable of the path.
|
|
63
|
+
*/
|
|
64
|
+
export const requestPath = (entry: HttpEntry, values: Readonly<Record<string, number>>): string | undefined =>
|
|
65
|
+
fillPlaceholders(entry.path, Object.fromEntries(Object.entries(values).map(([name, value]) => [name, encodeURIComponent(String(value))])));
|
|
66
|
+
|
|
67
|
+
/** This function gives the problems of a range: its unit, its two ends and its scale. */
|
|
68
|
+
const rangeProblems = (range: AppRange, path: string, owner: string): readonly AppProblem[] => {
|
|
69
|
+
const problems: AppProblem[] = [];
|
|
70
|
+
if (!isCanonicalUnit(range.unit)) problems.push(appProblem("unit", `${path}.unit`, `The unit "${range.unit}" of ${owner} is not a canonical unit.`));
|
|
71
|
+
if (!finite(range.min) || !finite(range.max) || range.min > range.max) {
|
|
72
|
+
problems.push(appProblem("range", path, `The range of ${owner} does not go from a finite amount to the same or a larger amount.`));
|
|
73
|
+
} else if (range.scale === "log" && !(range.min > 0)) {
|
|
74
|
+
problems.push(appProblem("range", `${path}.min`, `The range of ${owner} is on the scale "log", thus it starts above 0.`));
|
|
75
|
+
}
|
|
76
|
+
return problems;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/** This function gives the problems of the variables: the id, the title and the range of each. */
|
|
80
|
+
const variableProblems = (variables: readonly AppVariable[]): readonly AppProblem[] => {
|
|
81
|
+
const problems: AppProblem[] = [];
|
|
82
|
+
const ids = new Set<string>();
|
|
83
|
+
variables.forEach((variable, index) => {
|
|
84
|
+
const path = `variables[${index}]`;
|
|
85
|
+
if (!NAME.test(variable.id)) {
|
|
86
|
+
problems.push(appProblem("id", `${path}.id`, `The id "${variable.id}" is not letters and digits that start with a lower-case letter.`));
|
|
87
|
+
} else if (variable.id === RATE) {
|
|
88
|
+
problems.push(appProblem("id", `${path}.id`, `The id "${RATE}" is the rate of a class. A variable cannot use it.`));
|
|
89
|
+
} else if (ids.has(variable.id)) {
|
|
90
|
+
problems.push(appProblem("id", `${path}.id`, `Two variables have the id "${variable.id}".`));
|
|
91
|
+
}
|
|
92
|
+
ids.add(variable.id);
|
|
93
|
+
if (variable.title.trim() === "") problems.push(appProblem("id", `${path}.title`, `The title of the variable "${variable.id}" is empty.`));
|
|
94
|
+
problems.push(...rangeProblems(variable, path, `the variable "${variable.id}"`));
|
|
95
|
+
});
|
|
96
|
+
return problems;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/** This function gives the problems of the entry of a class. An entry of HTTP sets each request variable of the class. */
|
|
100
|
+
const entryProblems = (app: AppContract, entry: AppClass, path: string): readonly AppProblem[] => {
|
|
101
|
+
const problems: AppProblem[] = [];
|
|
102
|
+
const at = `${path}.entry`;
|
|
103
|
+
if (entry.entry.kind === "service") {
|
|
104
|
+
const { service } = entry.entry;
|
|
105
|
+
if (!app.services.some((each) => each.id === service)) {
|
|
106
|
+
problems.push(appProblem("service", `${at}.service`, `The class "${entry.id}" names the service "${service}", and the app has no service with that id.`));
|
|
107
|
+
}
|
|
108
|
+
return problems;
|
|
109
|
+
}
|
|
110
|
+
const { path: template, body } = entry.entry;
|
|
111
|
+
if (!template.startsWith("/")) problems.push(appProblem("entry", `${at}.path`, `The path of the class "${entry.id}" does not start with "/".`));
|
|
112
|
+
const named = placeholdersOf(template);
|
|
113
|
+
for (const name of named) {
|
|
114
|
+
if (!entry.request.includes(name)) {
|
|
115
|
+
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.`));
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (body !== undefined) {
|
|
119
|
+
const variable = app.variables.find((each) => each.id === body);
|
|
120
|
+
if (!entry.request.includes(body)) {
|
|
121
|
+
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.`));
|
|
122
|
+
} else if (variable !== undefined && isCanonicalUnit(variable.unit) && !convertible(variable.unit, "B")) {
|
|
123
|
+
problems.push(appProblem("unit", `${at}.body`, `The body of the class "${entry.id}" is the variable "${body}" in "${variable.unit}", not in bytes.`));
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
for (const name of entry.request) {
|
|
127
|
+
if (!named.includes(name) && name !== body) {
|
|
128
|
+
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.`));
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return problems;
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
/** This function gives the problems of the classes: the id, the rate, the request variables and the entry of each. */
|
|
135
|
+
const classProblems = (app: AppContract): readonly AppProblem[] => {
|
|
136
|
+
const problems: AppProblem[] = [];
|
|
137
|
+
const ids = new Set<string>();
|
|
138
|
+
app.classes.forEach((entry, index) => {
|
|
139
|
+
const path = `classes[${index}]`;
|
|
140
|
+
if (!SLUG.test(entry.id)) {
|
|
141
|
+
problems.push(appProblem("id", `${path}.id`, `The id "${entry.id}" is not lower-case words joined by ".", "-" or "/".`));
|
|
142
|
+
} else if (ids.has(entry.id)) {
|
|
143
|
+
problems.push(appProblem("id", `${path}.id`, `Two classes have the id "${entry.id}".`));
|
|
144
|
+
}
|
|
145
|
+
ids.add(entry.id);
|
|
146
|
+
if (entry.title.trim() === "") problems.push(appProblem("id", `${path}.title`, `The title of the class "${entry.id}" is empty.`));
|
|
147
|
+
problems.push(...rangeProblems(entry.rate, `${path}.rate`, `the rate of the class "${entry.id}"`));
|
|
148
|
+
if (isCanonicalUnit(entry.rate.unit) && kindOf(entry.rate.unit) !== "frequency") {
|
|
149
|
+
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".`));
|
|
150
|
+
}
|
|
151
|
+
entry.request.forEach((name, place) => {
|
|
152
|
+
const variable = app.variables.find((each) => each.id === name);
|
|
153
|
+
if (variable === undefined) {
|
|
154
|
+
problems.push(appProblem("variable", `${path}.request[${place}]`, `The class "${entry.id}" names the variable "${name}", and the app has no variable with that id.`));
|
|
155
|
+
} else if (variable.kind !== "request") {
|
|
156
|
+
problems.push(appProblem("variable", `${path}.request[${place}]`, `The variable "${name}" is a ${variable.kind} variable, not a request variable.`));
|
|
157
|
+
} else if (entry.request.indexOf(name) !== place) {
|
|
158
|
+
problems.push(appProblem("variable", `${path}.request[${place}]`, `The class "${entry.id}" names the variable "${name}" two times.`));
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
problems.push(...entryProblems(app, entry, path));
|
|
162
|
+
});
|
|
163
|
+
app.variables.forEach((variable, index) => {
|
|
164
|
+
if (variable.kind === "request" && !app.classes.some((entry) => entry.request.includes(variable.id))) {
|
|
165
|
+
problems.push(appProblem("variable", `variables[${index}]`, `No class has the request variable "${variable.id}".`));
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
return problems;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
/** This function gives the problems of the placeholders of a command at its place in the contract. */
|
|
172
|
+
const placeholderProblems = (app: AppContract, command: AppCommand, path: string, place: Place): readonly AppProblem[] => {
|
|
173
|
+
const problems: AppProblem[] = [];
|
|
174
|
+
const texts: (readonly [string, string])[] = [
|
|
175
|
+
...command.argv.map((text, index) => [`${path}.argv[${index}]`, text] as const),
|
|
176
|
+
...Object.entries(command.env).map(([name, text]) => [`${path}.env.${name}`, text] as const),
|
|
177
|
+
];
|
|
178
|
+
for (const [at, text] of texts) {
|
|
179
|
+
for (const name of placeholdersOf(text)) {
|
|
180
|
+
const [head, ...rest] = name.split(".");
|
|
181
|
+
const id = rest.join(".");
|
|
182
|
+
if (rest.length === 0 && PLAIN[place].includes(name)) continue;
|
|
183
|
+
if (head === "service" && place === "run" && id !== "") {
|
|
184
|
+
if (!app.services.some((each) => each.id === id)) {
|
|
185
|
+
problems.push(appProblem("placeholder", at, `The placeholder "{${name}}" names the service "${id}", and the app has no service with that id.`));
|
|
186
|
+
}
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
if (head === "variable" && place === "seed" && id !== "") {
|
|
190
|
+
if (!app.variables.some((each) => each.id === id && (each.kind === "store" || each.kind === "level"))) {
|
|
191
|
+
problems.push(appProblem("placeholder", at, `The placeholder "{${name}}" names "${id}", and the app has no store or level variable with that id.`));
|
|
192
|
+
}
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
problems.push(appProblem("placeholder", at, `The command ${place} cannot hold the placeholder "{${name}}".`));
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return problems;
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
/** This function gives the problems of a ready signal. */
|
|
202
|
+
const readyProblems = (ready: AppReady, path: string): readonly AppProblem[] => {
|
|
203
|
+
if (ready.kind === "line") return ready.text.trim() === "" ? [appProblem("command", `${path}.text`, "The text of the ready line is empty.")] : [];
|
|
204
|
+
return ready.path.startsWith("/") ? [] : [appProblem("command", `${path}.path`, `The ready path "${ready.path}" does not start with "/".`)];
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* 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
|
|
209
|
+
* have the same kind or the same `ev`.
|
|
210
|
+
*/
|
|
211
|
+
const lineProblems = (lines: readonly AppLine[], path: string): readonly AppProblem[] => {
|
|
212
|
+
const problems: AppProblem[] = [];
|
|
213
|
+
const kinds = new Set<string>();
|
|
214
|
+
const evs = new Set<string>();
|
|
215
|
+
lines.forEach((line, index) => {
|
|
216
|
+
const at = `${path}[${index}]`;
|
|
217
|
+
if (line.ev.trim() === "") problems.push(appProblem("command", `${at}.ev`, "The field ev of the line is empty."));
|
|
218
|
+
if (kinds.has(line.kind)) problems.push(appProblem("command", `${at}.kind`, `Two lines of the run have the kind "${line.kind}".`));
|
|
219
|
+
if (evs.has(line.ev)) problems.push(appProblem("command", `${at}.ev`, `Two lines of the run have the ev "${line.ev}".`));
|
|
220
|
+
kinds.add(line.kind);
|
|
221
|
+
evs.add(line.ev);
|
|
222
|
+
});
|
|
223
|
+
return problems;
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
/** This function gives the problems of a command: its program, its folder, the names of its variables and its placeholders. */
|
|
227
|
+
const commandProblems = (app: AppContract, command: AppCommand, path: string, place: Place): readonly AppProblem[] => {
|
|
228
|
+
const problems: AppProblem[] = [];
|
|
229
|
+
if (command.argv.length === 0 || (command.argv[0] ?? "").trim() === "") {
|
|
230
|
+
problems.push(appProblem("command", `${path}.argv`, `The command ${place} has no program.`));
|
|
231
|
+
}
|
|
232
|
+
const parts = command.cwd.split("/");
|
|
233
|
+
if (command.cwd.startsWith("/") || command.cwd.includes("\\") || parts.some((part) => part === "..") || command.cwd === "") {
|
|
234
|
+
problems.push(appProblem("command", `${path}.cwd`, `The folder "${command.cwd}" is not a path in the repository, with "/" and no "..". The root is ".".`));
|
|
235
|
+
}
|
|
236
|
+
for (const name of Object.keys(command.env)) {
|
|
237
|
+
if (!ENV.test(name)) problems.push(appProblem("command", `${path}.env.${name}`, `The name "${name}" is not an environment variable: upper-case letters, digits and "_".`));
|
|
238
|
+
}
|
|
239
|
+
problems.push(...placeholderProblems(app, command, path, place));
|
|
240
|
+
return problems;
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
/** This function tells if a command holds a placeholder. */
|
|
244
|
+
const holds = (command: AppCommand, placeholder: string): boolean =>
|
|
245
|
+
[...command.argv, ...Object.values(command.env)].some((text) => placeholdersOf(text).includes(placeholder));
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* This function gives the problems of the start and of the outside services. Each process gets its port, the
|
|
249
|
+
* seed and the run get the store, and the run gets the URL of each service. The seed sets each store variable.
|
|
250
|
+
*/
|
|
251
|
+
const startProblems = (app: AppContract): readonly AppProblem[] => {
|
|
252
|
+
const problems: AppProblem[] = [];
|
|
253
|
+
const { install, check, seed, run } = app.start;
|
|
254
|
+
if (install !== undefined) problems.push(...commandProblems(app, install, "start.install", "install"));
|
|
255
|
+
if (check !== undefined) problems.push(...commandProblems(app, check, "start.check", "check"));
|
|
256
|
+
problems.push(...commandProblems(app, run, "start.run", "run"), ...readyProblems(run.ready, "start.run.ready"), ...lineProblems(run.lines ?? [], "start.run.lines"));
|
|
257
|
+
const listens = run.ready.kind === "http" || app.classes.some((entry) => entry.entry.kind === "http");
|
|
258
|
+
if (listens && !holds(run, "port")) problems.push(appProblem("command", "start.run", `The run does not get its port: it does not hold "{port}".`));
|
|
259
|
+
|
|
260
|
+
const measured = app.variables.filter((variable) => variable.kind === "run");
|
|
261
|
+
if (measured.length > 0 && !(run.lines ?? []).some((line) => line.kind === "amounts")) {
|
|
262
|
+
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.`));
|
|
263
|
+
}
|
|
264
|
+
const stores = app.variables.filter((variable) => variable.kind === "store");
|
|
265
|
+
if (stores.length > 0 && !holds(run, "store")) {
|
|
266
|
+
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}".`));
|
|
267
|
+
}
|
|
268
|
+
if (seed === undefined) {
|
|
269
|
+
if (stores.length > 0) problems.push(appProblem("command", "start", `The app has the store variables ${stores.map((each) => `"${each.id}"`).join(", ")}, and no seed.`));
|
|
270
|
+
} else {
|
|
271
|
+
problems.push(...commandProblems(app, seed, "start.seed", "seed"));
|
|
272
|
+
if (stores.length === 0) problems.push(appProblem("command", "start.seed", "The app has a seed, and no store variable for it to set."));
|
|
273
|
+
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}".`));
|
|
274
|
+
for (const variable of stores) {
|
|
275
|
+
if (!holds(seed, `variable.${variable.id}`)) {
|
|
276
|
+
problems.push(appProblem("command", "start.seed", `The seed cannot set the store variable "${variable.id}": it does not hold "{variable.${variable.id}}".`));
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const ids = new Set<string>();
|
|
282
|
+
app.services.forEach((service, index) => {
|
|
283
|
+
const path = `services[${index}]`;
|
|
284
|
+
if (!SLUG.test(service.id)) {
|
|
285
|
+
problems.push(appProblem("id", `${path}.id`, `The id "${service.id}" is not lower-case words joined by ".", "-" or "/".`));
|
|
286
|
+
} else if (ids.has(service.id)) {
|
|
287
|
+
problems.push(appProblem("id", `${path}.id`, `Two services have the id "${service.id}".`));
|
|
288
|
+
}
|
|
289
|
+
ids.add(service.id);
|
|
290
|
+
if (service.title.trim() === "") problems.push(appProblem("id", `${path}.title`, `The title of the service "${service.id}" is empty.`));
|
|
291
|
+
problems.push(...commandProblems(app, service.fake, `${path}.fake`, "fake"), ...readyProblems(service.fake.ready, `${path}.fake.ready`));
|
|
292
|
+
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}".`));
|
|
293
|
+
const feeds = app.classes.some((entry) => entry.entry.kind === "service" && entry.entry.service === service.id);
|
|
294
|
+
if (feeds && !holds(service.fake, "workload")) {
|
|
295
|
+
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}".`));
|
|
296
|
+
}
|
|
297
|
+
if (!holds(run, `service.${service.id}`)) {
|
|
298
|
+
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}}".`));
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
return problems;
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
/** This function gives the key of the target of a series. Two series with one key measure the same thing. */
|
|
305
|
+
const targetKey = (series: AppSeries): string => JSON.stringify(Object.entries(series.of).sort(([a], [b]) => a.localeCompare(b)));
|
|
306
|
+
|
|
307
|
+
/** This function gives the problems of one series: its target, its kind and its unit. */
|
|
308
|
+
const seriesProblems = (app: AppContract, series: AppSeries, path: string): readonly AppProblem[] => {
|
|
309
|
+
const problems: AppProblem[] = [];
|
|
310
|
+
const at = `${path}.of`;
|
|
311
|
+
const target = series.of;
|
|
312
|
+
const kind = (wanted: AppSeries["kind"], what: string): void => {
|
|
313
|
+
if (series.kind !== wanted) problems.push(appProblem("series", `${path}.kind`, `The series "${series.id}" gives ${what}, thus it is a ${wanted}.`));
|
|
314
|
+
};
|
|
315
|
+
const unit = (wanted: string, what: string): void => {
|
|
316
|
+
if (isCanonicalUnit(series.unit) && isCanonicalUnit(wanted) && !convertible(series.unit, wanted)) {
|
|
317
|
+
problems.push(appProblem("unit", `${path}.unit`, `The series "${series.id}" is in "${series.unit}". ${what} is in "${wanted}".`));
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
if ("rate" in target) {
|
|
321
|
+
const entry = app.classes.find((each) => each.id === target.rate);
|
|
322
|
+
kind("counter", "the rate of a class");
|
|
323
|
+
if (entry === undefined) return [...problems, appProblem("series", at, `The series "${series.id}" names the class "${target.rate}", and the app has no class with that id.`)];
|
|
324
|
+
const perSecond = isCanonicalUnit(series.unit) ? divideUnits(series.unit, "s") : undefined;
|
|
325
|
+
if (perSecond !== undefined && isCanonicalUnit(entry.rate.unit) && kindOf(entry.rate.unit) === "frequency" && !convertible(perSecond, entry.rate.unit)) {
|
|
326
|
+
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}".`));
|
|
327
|
+
}
|
|
328
|
+
return problems;
|
|
329
|
+
}
|
|
330
|
+
if ("level" in target) {
|
|
331
|
+
const variable = app.variables.find((each) => each.id === target.level);
|
|
332
|
+
kind("gauge", "a level");
|
|
333
|
+
if (variable === undefined || variable.kind !== "level") {
|
|
334
|
+
return [...problems, appProblem("series", at, `The series "${series.id}" names "${target.level}", and the app has no level variable with that id.`)];
|
|
335
|
+
}
|
|
336
|
+
unit(variable.unit, `The level "${variable.id}"`);
|
|
337
|
+
return problems;
|
|
338
|
+
}
|
|
339
|
+
if ("total" in target) {
|
|
340
|
+
const entry = app.classes.find((each) => each.id === target.class);
|
|
341
|
+
const variable = app.variables.find((each) => each.id === target.total);
|
|
342
|
+
kind("counter", "a total over the requests of a class");
|
|
343
|
+
if (entry === undefined) return [...problems, appProblem("series", at, `The series "${series.id}" names the class "${target.class}", and the app has no class with that id.`)];
|
|
344
|
+
if (variable === undefined || !entry.request.includes(variable.id)) {
|
|
345
|
+
return [...problems, appProblem("series", at, `The series "${series.id}" names "${target.total}", and it is not a request variable of the class "${entry.id}".`)];
|
|
346
|
+
}
|
|
347
|
+
unit(variable.unit, `The variable "${variable.id}"`);
|
|
348
|
+
return problems;
|
|
349
|
+
}
|
|
350
|
+
if (!SLUG.test(target.measure)) problems.push(appProblem("series", `${at}.measure`, `The measure "${target.measure}" is not lower-case words joined by ".", "-" or "/".`));
|
|
351
|
+
return problems;
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
/** This function gives the problems of the live metrics: the id, the unit and the target of each series. */
|
|
355
|
+
const metricsProblems = (app: AppContract): readonly AppProblem[] => {
|
|
356
|
+
const problems: AppProblem[] = [];
|
|
357
|
+
const ids = new Set<string>();
|
|
358
|
+
const targets = new Map<string, string>();
|
|
359
|
+
app.metrics.forEach((series, index) => {
|
|
360
|
+
const path = `metrics[${index}]`;
|
|
361
|
+
if (!METRIC.test(series.id)) {
|
|
362
|
+
problems.push(appProblem("id", `${path}.id`, `The id "${series.id}" is not a name of a metric: letters, digits, "_", ".", "/" and "-".`));
|
|
363
|
+
} else if (ids.has(series.id)) {
|
|
364
|
+
problems.push(appProblem("id", `${path}.id`, `Two series have the id "${series.id}".`));
|
|
365
|
+
}
|
|
366
|
+
ids.add(series.id);
|
|
367
|
+
if (!isCanonicalUnit(series.unit)) problems.push(appProblem("unit", `${path}.unit`, `The unit "${series.unit}" of the series "${series.id}" is not a canonical unit.`));
|
|
368
|
+
const key = targetKey(series);
|
|
369
|
+
const before = targets.get(key);
|
|
370
|
+
if (before !== undefined) problems.push(appProblem("series", `${path}.of`, `The series "${series.id}" and "${before}" measure the same thing.`));
|
|
371
|
+
else targets.set(key, series.id);
|
|
372
|
+
problems.push(...seriesProblems(app, series, path));
|
|
373
|
+
});
|
|
374
|
+
return problems;
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* This function examines the contract of an app and gives each problem that it finds. A contract with no
|
|
379
|
+
* problem gives an empty list.
|
|
380
|
+
*
|
|
381
|
+
* It examines the format, the id, the title and the repository. It examines each variable, each class and
|
|
382
|
+
* its entry, each command and its placeholders, each outside service and each series of the live metrics.
|
|
383
|
+
*/
|
|
384
|
+
export const checkApp = (app: AppContract): readonly AppProblem[] => {
|
|
385
|
+
const problems: AppProblem[] = [];
|
|
386
|
+
if (app.format !== APP_FORMAT) problems.push(appProblem("format", "format", `The format is "${String(app.format)}", not "${APP_FORMAT}".`));
|
|
387
|
+
if (!SLUG.test(app.id)) problems.push(appProblem("id", "id", `The id "${app.id}" is not lower-case words joined by ".", "-" or "/".`));
|
|
388
|
+
if (app.title.trim() === "") problems.push(appProblem("id", "title", "The title is empty."));
|
|
389
|
+
if (!isHttps(app.repository)) {
|
|
390
|
+
problems.push(appProblem("id", "repository", `The repository "${app.repository}" is not an HTTPS URL.`));
|
|
391
|
+
}
|
|
392
|
+
return [...problems, ...variableProblems(app.variables), ...classProblems(app), ...startProblems(app), ...metricsProblems(app)];
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* This function gives each part of the demand of an app that its live metrics cannot give. A contract whose
|
|
397
|
+
* metrics give its whole demand gives an empty list. The live check of a law reads a demand with no gap.
|
|
398
|
+
*/
|
|
399
|
+
export const metricsGaps = (app: AppContract): readonly MetricsGap[] => {
|
|
400
|
+
const has = (test: (series: AppSeries) => boolean): boolean => app.metrics.some(test);
|
|
401
|
+
const gaps: MetricsGap[] = [];
|
|
402
|
+
for (const entry of app.classes) {
|
|
403
|
+
if (!has((series) => "rate" in series.of && series.of.rate === entry.id)) {
|
|
404
|
+
gaps.push({ need: "rate", class: entry.id, message: `No counter gives the rate of the class "${entry.id}".` });
|
|
405
|
+
}
|
|
406
|
+
for (const name of entry.request) {
|
|
407
|
+
if (!has((series) => "total" in series.of && series.of.total === name && series.of.class === entry.id)) {
|
|
408
|
+
gaps.push({ need: "total", class: entry.id, variable: name, message: `No counter gives the total of "${name}" over the requests of the class "${entry.id}".` });
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
for (const variable of app.variables) {
|
|
413
|
+
if (variable.kind === "level" && !has((series) => "level" in series.of && series.of.level === variable.id)) {
|
|
414
|
+
gaps.push({ need: "level", variable: variable.id, message: `No gauge gives the level "${variable.id}".` });
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
return gaps;
|
|
418
|
+
};
|
|
419
|
+
|
|
420
|
+
/** This function gives the problem of an amount against the range of its variable: its unit, then its value. */
|
|
421
|
+
const amountProblem = (amount: Quantity, range: AppRange, path: string, owner: string): AppProblem | undefined => {
|
|
422
|
+
if (!isQuantity(amount)) return undefined;
|
|
423
|
+
if (!convertible(amount.unit, range.unit)) return appProblem("unit", path, `${owner} is in "${amount.unit}", and the app gives it in "${range.unit}".`);
|
|
424
|
+
const value = valueIn(amount, range.unit);
|
|
425
|
+
if (value < range.min || value > range.max) {
|
|
426
|
+
return appProblem("range", path, `${owner} is ${value} ${range.unit}. The app accepts ${range.min} to ${range.max} ${range.unit}.`);
|
|
427
|
+
}
|
|
428
|
+
return undefined;
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* This function examines a workload against the contract of its app, and gives each problem that it finds.
|
|
433
|
+
* The workload is a workload that `checkWorkload` accepts.
|
|
434
|
+
*
|
|
435
|
+
* Each class of the workload is a class of the app: the class that its field `of` names, or else its id. Its
|
|
436
|
+
* rate and each variable of its request are in the range of the app, and it gives each request variable of
|
|
437
|
+
* its class. Each level of the workload is a level variable of the app, in its range. A class or a level that
|
|
438
|
+
* the workload does not give is 0.
|
|
439
|
+
*/
|
|
440
|
+
export const appWorkloadProblems = (app: AppContract, workload: Workload): readonly AppProblem[] => {
|
|
441
|
+
const problems: AppProblem[] = [];
|
|
442
|
+
workload.classes.forEach((entry, index) => {
|
|
443
|
+
const path = `classes[${index}]`;
|
|
444
|
+
const id = entry.of ?? entry.id;
|
|
445
|
+
const known = app.classes.find((each) => each.id === id);
|
|
446
|
+
if (known === undefined) {
|
|
447
|
+
problems.push(appProblem("class", entry.of === undefined ? `${path}.id` : `${path}.of`, `The app "${app.id}" has no class "${id}".`));
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
const rate = amountProblem(entry.rate, known.rate, `${path}.rate`, `The rate of the class "${entry.id}"`);
|
|
451
|
+
if (rate !== undefined) problems.push(rate);
|
|
452
|
+
for (const [name, amount] of Object.entries(entry.request)) {
|
|
453
|
+
const variable = app.variables.find((each) => each.id === name);
|
|
454
|
+
if (variable === undefined || !known.request.includes(name)) {
|
|
455
|
+
problems.push(appProblem("variable", `${path}.request.${name}`, `"${name}" is not a request variable of the class "${known.id}" of the app.`));
|
|
456
|
+
continue;
|
|
457
|
+
}
|
|
458
|
+
const found = amountProblem(amount, variable, `${path}.request.${name}`, `The variable "${name}" of the class "${entry.id}"`);
|
|
459
|
+
if (found !== undefined) problems.push(found);
|
|
460
|
+
}
|
|
461
|
+
for (const name of known.request) {
|
|
462
|
+
if (!Object.hasOwn(entry.request, name)) {
|
|
463
|
+
problems.push(appProblem("variable", `${path}.request`, `The class "${entry.id}" does not give the request variable "${name}" of the class "${known.id}".`));
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
});
|
|
467
|
+
for (const [name, amount] of Object.entries(workload.levels)) {
|
|
468
|
+
const variable = app.variables.find((each) => each.id === name);
|
|
469
|
+
if (variable === undefined || variable.kind !== "level") {
|
|
470
|
+
problems.push(appProblem("variable", `levels.${name}`, `"${name}" is not a level variable of the app "${app.id}".`));
|
|
471
|
+
continue;
|
|
472
|
+
}
|
|
473
|
+
const found = amountProblem(amount, variable, `levels.${name}`, `The level "${name}"`);
|
|
474
|
+
if (found !== undefined) problems.push(found);
|
|
475
|
+
}
|
|
476
|
+
return problems;
|
|
477
|
+
};
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { err, ok, type Result } from "@mark1russell7/optional";
|
|
2
|
+
import { quantity, valueIn, type Quantity } from "@mark1russell7/quantity";
|
|
3
|
+
import { mean } from "@mark1russell7/rigor-statistics";
|
|
4
|
+
import { metricsGaps } from "./app-check.ts";
|
|
5
|
+
import type { AppContract, AppProblem, AppSeries } from "./app.ts";
|
|
6
|
+
import { isUtc } from "./names.ts";
|
|
7
|
+
import type { Observation, ObservedClass } from "./observe.ts";
|
|
8
|
+
|
|
9
|
+
/** A reading is one value of one series of an app at one time. `series` is the id of the series, and `at` is a UTC time. */
|
|
10
|
+
export interface SeriesReading {
|
|
11
|
+
readonly series: string;
|
|
12
|
+
readonly at: string;
|
|
13
|
+
readonly value: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** A window of the live metrics goes from `from` to `to`, two UTC times. Its `origin` names the place of the metrics. */
|
|
17
|
+
export interface MetricsWindow {
|
|
18
|
+
readonly origin: string;
|
|
19
|
+
readonly from: string;
|
|
20
|
+
readonly to: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* This function gives the increase of a counter from its values in the order of their times. A value below
|
|
25
|
+
* the value before it is a new start of the process. The counter starts again at 0, thus the increase adds
|
|
26
|
+
* the whole value.
|
|
27
|
+
*/
|
|
28
|
+
export const counterIncrease = (values: readonly number[]): number =>
|
|
29
|
+
values.reduce((total, value, index) => {
|
|
30
|
+
if (index === 0) return total;
|
|
31
|
+
const before = values[index - 1] ?? 0;
|
|
32
|
+
return total + (value >= before ? value - before : value);
|
|
33
|
+
}, 0);
|
|
34
|
+
|
|
35
|
+
/** This function makes a problem of the readings of an app. */
|
|
36
|
+
const readingProblem = (path: string, message: string): AppProblem => ({ code: "series", path, message });
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* This function gives the observation of an app in a window. The observation has the count of the requests of
|
|
40
|
+
* each class, the mean of each request variable and each level. It reads only the readings in the window.
|
|
41
|
+
*
|
|
42
|
+
* - The count of a class is the increase of its counter. A class with no request in the window is not in the observation.
|
|
43
|
+
* - The mean of a request variable is the increase of the counter of its total, over the count of its class.
|
|
44
|
+
* - A level is the mean of the readings of its gauge, with the same weight for each reading. Item CAP-63 adds a weight for the time of each reading.
|
|
45
|
+
*
|
|
46
|
+
* It gives a problem for each gap of the metrics of the app (`metricsGaps`). It also gives a problem for a
|
|
47
|
+
* counter with fewer than 2 readings, and for a gauge with no reading. `observedWorkload` gives the workload
|
|
48
|
+
* of the observation.
|
|
49
|
+
*/
|
|
50
|
+
export const observationOf = (app: AppContract, window: MetricsWindow, readings: readonly SeriesReading[]): Result<Observation, readonly AppProblem[]> => {
|
|
51
|
+
const { origin, from, to } = window;
|
|
52
|
+
if (!isUtc(from) || !isUtc(to) || !(Date.parse(from) < Date.parse(to))) {
|
|
53
|
+
return err([readingProblem("window", "The window does not go from a UTC time to a later UTC time.")]);
|
|
54
|
+
}
|
|
55
|
+
const problems: AppProblem[] = metricsGaps(app).map((gap) => readingProblem("metrics", gap.message));
|
|
56
|
+
const start = Date.parse(from);
|
|
57
|
+
const end = Date.parse(to);
|
|
58
|
+
|
|
59
|
+
/** This function gives the values of a series in the window, in the order of their times. */
|
|
60
|
+
const valuesOf = (series: AppSeries): readonly number[] =>
|
|
61
|
+
readings
|
|
62
|
+
.filter((reading) => reading.series === series.id && isUtc(reading.at) && Date.parse(reading.at) >= start && Date.parse(reading.at) <= end)
|
|
63
|
+
.sort((a, b) => Date.parse(a.at) - Date.parse(b.at))
|
|
64
|
+
.map((reading) => reading.value)
|
|
65
|
+
.filter((value) => Number.isFinite(value));
|
|
66
|
+
|
|
67
|
+
/** This function gives the increase of a counter in the window, in the unit of the series, or a problem. */
|
|
68
|
+
const increaseOf = (series: AppSeries): number | undefined => {
|
|
69
|
+
const values = valuesOf(series);
|
|
70
|
+
if (values.length >= 2) return counterIncrease(values);
|
|
71
|
+
problems.push(readingProblem(`readings.${series.id}`, `The counter "${series.id}" has ${values.length} readings in the window. Its increase needs 2 or more.`));
|
|
72
|
+
return undefined;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const classes: ObservedClass[] = [];
|
|
76
|
+
for (const entry of app.classes) {
|
|
77
|
+
const counter = app.metrics.find((series) => "rate" in series.of && series.of.rate === entry.id);
|
|
78
|
+
if (counter === undefined) continue;
|
|
79
|
+
const count = increaseOf(counter);
|
|
80
|
+
if (count === undefined || count === 0) continue;
|
|
81
|
+
const request: Record<string, Quantity> = {};
|
|
82
|
+
for (const name of entry.request) {
|
|
83
|
+
const total = app.metrics.find((series) => "total" in series.of && series.of.total === name && series.of.class === entry.id);
|
|
84
|
+
const variable = app.variables.find((each) => each.id === name);
|
|
85
|
+
if (total === undefined || variable === undefined) continue;
|
|
86
|
+
const sum = increaseOf(total);
|
|
87
|
+
if (sum !== undefined) request[name] = quantity(valueIn(quantity(sum, total.unit), variable.unit) / count, variable.unit);
|
|
88
|
+
}
|
|
89
|
+
classes.push({ id: entry.id, count: quantity(count, counter.unit), request });
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const levels: Record<string, Quantity> = {};
|
|
93
|
+
for (const variable of app.variables) {
|
|
94
|
+
if (variable.kind !== "level") continue;
|
|
95
|
+
const gauge = app.metrics.find((series) => "level" in series.of && series.of.level === variable.id);
|
|
96
|
+
if (gauge === undefined) continue;
|
|
97
|
+
const values = valuesOf(gauge);
|
|
98
|
+
if (values.length === 0) {
|
|
99
|
+
problems.push(readingProblem(`readings.${gauge.id}`, `The gauge "${gauge.id}" has no reading in the window.`));
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
levels[variable.id] = quantity(valueIn(quantity(mean(values), gauge.unit), variable.unit), variable.unit);
|
|
103
|
+
}
|
|
104
|
+
return problems.length > 0 ? err(problems) : ok({ origin, from, to, classes, levels });
|
|
105
|
+
};
|