industrial-model 0.11.3 → 0.12.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.
@@ -0,0 +1,250 @@
1
+ import { CogniteClient } from '@cognite/sdk';
2
+ import { D as DataModelId, L as DataModelRetrieveOptions, P as DataModelRetrieveItem, V as ViewDefinition, S as InstancesQueryRequest, T as InstancesQueryResponse, W as InstancesSearchRequest, X as InstancesSearchResponse, Y as InstancesAggregateRequest, Z as InstancesAggregateResponse, _ as InstancesApplyRequest, $ as InstancesApplyResponse, a0 as CogniteDatapointRetrieveOptions, a1 as CogniteDatapointResultItem, a2 as CogniteDatapointLatestItem, a3 as CogniteDatapointInsertItem, a4 as CogniteDatapointDeleteItem, a5 as CogniteFileUploadInfo, a6 as CogniteFileUploadResult, a7 as CogniteFileDownloadUrl, N as NodeId, f as DatapointAggregate } from '../types-CFkHwXW6.js';
3
+
4
+ interface CognitePort {
5
+ retrieveDataModels(ids: DataModelId[], options?: DataModelRetrieveOptions): Promise<{
6
+ items: DataModelRetrieveItem[];
7
+ }>;
8
+ retrieveViews(ids: Array<{
9
+ space: string;
10
+ externalId: string;
11
+ version: string;
12
+ }>): Promise<{
13
+ items: ViewDefinition[];
14
+ }>;
15
+ queryInstances(request: InstancesQueryRequest): Promise<InstancesQueryResponse>;
16
+ searchInstances(request: InstancesSearchRequest): Promise<InstancesSearchResponse>;
17
+ aggregateInstances(request: InstancesAggregateRequest): Promise<InstancesAggregateResponse>;
18
+ applyInstances(request: InstancesApplyRequest): Promise<InstancesApplyResponse>;
19
+ retrieveDatapoints(options: CogniteDatapointRetrieveOptions): Promise<{
20
+ items: CogniteDatapointResultItem[];
21
+ }>;
22
+ retrieveLatestDatapoints(items: CogniteDatapointLatestItem[], options?: {
23
+ ignoreUnknownIds?: boolean;
24
+ }): Promise<{
25
+ items: CogniteDatapointResultItem[];
26
+ }>;
27
+ insertDatapoints(items: CogniteDatapointInsertItem[]): Promise<void>;
28
+ deleteDatapoints(items: CogniteDatapointDeleteItem[]): Promise<void>;
29
+ uploadFile(fileInfo: CogniteFileUploadInfo, content?: unknown): Promise<CogniteFileUploadResult>;
30
+ getFileDownloadUrls(ids: Array<{
31
+ instanceId: {
32
+ space: string;
33
+ externalId: string;
34
+ };
35
+ }>): Promise<CogniteFileDownloadUrl[]>;
36
+ }
37
+
38
+ /** A single computed datapoint of a calculation result. */
39
+ type DataPoint = {
40
+ timestamp: Date;
41
+ value: number;
42
+ };
43
+ /**
44
+ * A time series input to a calculation.
45
+ *
46
+ * When `aggregateType` is set the datapoints are fetched as aggregates and a
47
+ * `granularity` is required; otherwise raw datapoints are used.
48
+ */
49
+ type CalculatorParameter = {
50
+ /** The time series instance to read datapoints from. */
51
+ timeSeries: NodeId;
52
+ /** Optional aggregate to apply; requires `granularity` when set. */
53
+ aggregateType?: DatapointAggregate;
54
+ /** Aggregate granularity (e.g. `"1h"`); required when `aggregateType` is set. */
55
+ granularity?: string;
56
+ /** The placeholder name used to reference this parameter in the formula. */
57
+ alias: string;
58
+ };
59
+ /** A formula plus the parameters its placeholders resolve to. */
60
+ type CalculatorQuery = {
61
+ /** Formula referencing parameters by ``{alias}`` (see `evaluate`). */
62
+ formula: string;
63
+ parameters: CalculatorParameter[];
64
+ };
65
+ /** The datapoints produced by evaluating a `CalculatorQuery`. */
66
+ type CalculationResult = {
67
+ query: CalculatorQuery;
68
+ datapoints: DataPoint[];
69
+ };
70
+
71
+ /**
72
+ * Evaluates formula-based calculations over Cognite time series datapoints.
73
+ *
74
+ * Each {@link CalculatorQuery} pairs a formula with the parameters its
75
+ * placeholders resolve to. The calculator fetches the required datapoints
76
+ * (de-duplicating shared time series), aligns them by position and evaluates
77
+ * the formula element-by-element.
78
+ */
79
+ declare class Calculator {
80
+ private readonly retriever;
81
+ constructor(cognite: CogniteClient | CognitePort);
82
+ /** Evaluate a single query over the given time range. */
83
+ calculate(query: CalculatorQuery, start: Date, end: Date): Promise<CalculationResult>;
84
+ /**
85
+ * Evaluate several queries over the given time range, retrieving every
86
+ * parameter's datapoints in a single de-duplicated round trip.
87
+ */
88
+ calculateMultiples(queries: CalculatorQuery[], start: Date, end: Date): Promise<CalculationResult[]>;
89
+ }
90
+
91
+ /** Binary arithmetic operators supported by the formula grammar. */
92
+ type BinaryOp = "add" | "sub" | "mul" | "div" | "pow" | "mod";
93
+ /** Unary arithmetic operators supported by the formula grammar. */
94
+ type UnaryOp = "pos" | "neg";
95
+ /** Comparison operators supported by the formula grammar. */
96
+ type CompareOp = "eq" | "ne" | "lt" | "le" | "gt" | "ge";
97
+ /** Boolean operators supported by the formula grammar. */
98
+ type BoolOpKind = "and" | "or";
99
+ type NameNode = {
100
+ readonly kind: "name";
101
+ readonly id: string;
102
+ };
103
+ type ConstantNode = {
104
+ readonly kind: "constant";
105
+ readonly value: number;
106
+ };
107
+ type BinOpNode = {
108
+ readonly kind: "binop";
109
+ readonly op: BinaryOp;
110
+ readonly left: ExprNode;
111
+ readonly right: ExprNode;
112
+ };
113
+ type UnaryOpNode = {
114
+ readonly kind: "unaryop";
115
+ readonly op: UnaryOp;
116
+ readonly operand: ExprNode;
117
+ };
118
+ type CompareNode = {
119
+ readonly kind: "compare";
120
+ readonly left: ExprNode;
121
+ readonly ops: readonly CompareOp[];
122
+ readonly comparators: readonly ExprNode[];
123
+ };
124
+ type BoolOpNode = {
125
+ readonly kind: "boolop";
126
+ readonly op: BoolOpKind;
127
+ readonly values: readonly ExprNode[];
128
+ };
129
+ type IfExpNode = {
130
+ readonly kind: "ifexp";
131
+ readonly test: ExprNode;
132
+ readonly body: ExprNode;
133
+ readonly orelse: ExprNode;
134
+ };
135
+ /** Any node of a compiled formula expression tree. */
136
+ type ExprNode = NameNode | ConstantNode | BinOpNode | UnaryOpNode | CompareNode | BoolOpNode | IfExpNode;
137
+
138
+ /** A parsed, validated formula ready to be evaluated over parameter series. */
139
+ type CompiledFormula = {
140
+ /** The whitespace-normalized formula text. */
141
+ readonly raw: string;
142
+ /** The formula text after placeholders were replaced with safe identifiers. */
143
+ readonly expression: string;
144
+ /** The validated expression tree. */
145
+ readonly tree: ExprNode;
146
+ /** Original parameter names in first-appearance order. */
147
+ readonly variables: readonly string[];
148
+ /** Mapping of original parameter name to its safe identifier. */
149
+ readonly nameMap: ReadonlyMap<string, string>;
150
+ /** Whether the formula needs element-by-element (short-circuiting) evaluation. */
151
+ readonly hasConditional: boolean;
152
+ };
153
+ /**
154
+ * Normalize the formula text, then compile it (memoized by normalized text).
155
+ *
156
+ * The public entry point mirrors the Python ``compile_formula`` helper: it
157
+ * exposes {@link clearCache} so callers can inspect or reset the compilation
158
+ * cache.
159
+ */
160
+ declare function compileFormula(formula: string): CompiledFormula;
161
+ /** Clear the memoized formula compilation cache. */
162
+ declare function clearCache(): void;
163
+
164
+ /** A single formula parameter: an aligned sequence of numeric values. */
165
+ type ParameterValue = readonly number[];
166
+ /** The result of evaluating a formula: one value per aligned series element. */
167
+ type EvaluationResult = number[];
168
+ /** Mapping of parameter name to its numeric series. */
169
+ type Parameters = Record<string, ParameterValue>;
170
+
171
+ /**
172
+ * Evaluate a formula over aligned numeric parameter sequences.
173
+ *
174
+ * Placeholders are written as ``{name}`` and each resolves to the matching
175
+ * entry in `parameters`. The following operators are supported:
176
+ *
177
+ * - arithmetic: ``+`` ``-`` ``*`` ``/`` ``**`` ``%`` (binary) and ``+`` ``-`` (unary)
178
+ * - comparisons: ``==`` ``!=`` ``<`` ``<=`` ``>`` ``>=``
179
+ * - boolean: ``and`` ``or``
180
+ * - conditional: ``{A} / {B} if {B} != 0 else 0``
181
+ *
182
+ * Structural problems (bad syntax, unknown identifiers, missing parameters,
183
+ * mismatched lengths, non-numeric values) throw a subclass of `FormulaError`.
184
+ *
185
+ * When every referenced parameter is an empty sequence the result is an empty
186
+ * array — there is nothing to compute over, so this is treated as a valid
187
+ * (empty) result rather than an error. A *mix* of empty and non-empty
188
+ * parameters is still a length mismatch and throws `ParameterLengthError`.
189
+ *
190
+ * Arithmetic failures that depend on the parameter *values* are surfaced as
191
+ * arithmetic errors (subclasses of `ArithmeticError`) rather than
192
+ * `FormulaError`: dividing or taking a modulo by zero throws
193
+ * `ZeroDivisionError` and an overflowing exponentiation throws `OverflowError`.
194
+ *
195
+ * Conditional expressions, comparisons and boolean operators are evaluated
196
+ * element-by-element: for each series element only the selected branch is
197
+ * evaluated, so a division-by-zero (or other value-dependent failure) in the
198
+ * branch that is *not* selected for a given element never throws.
199
+ */
200
+ declare function evaluate(formula: string, parameters?: Parameters): EvaluationResult;
201
+
202
+ /**
203
+ * Structural formula problems (bad syntax, unknown identifiers, missing
204
+ * parameters, mismatched lengths, non-numeric values) are reported as a
205
+ * subclass of {@link FormulaError}.
206
+ *
207
+ * Value-dependent arithmetic failures (division/modulo by zero, overflowing
208
+ * exponentiation) are intentionally *not* {@link FormulaError}s: they mirror
209
+ * the native errors raised by the Python implementation and extend
210
+ * {@link ArithmeticError} instead.
211
+ */
212
+ /** Base error for every structural formula problem. */
213
+ declare class FormulaError extends Error {
214
+ constructor(message: string);
215
+ }
216
+ /** Raised when formula syntax or operations are not supported. */
217
+ declare class InvalidFormulaError extends FormulaError {
218
+ constructor(message: string);
219
+ }
220
+ /** Raised when a formula references parameters that were not provided. */
221
+ declare class MissingParameterError extends FormulaError {
222
+ readonly missing: readonly string[];
223
+ constructor(missing: string[]);
224
+ }
225
+ /** Raised when a parameter value is not a valid numeric sequence. */
226
+ declare class ParameterError extends FormulaError {
227
+ constructor(message: string);
228
+ }
229
+ /** Raised when referenced parameters do not all share the same length. */
230
+ declare class ParameterLengthError extends ParameterError {
231
+ readonly lengths: Readonly<Record<string, number>>;
232
+ constructor(lengths: Record<string, number>);
233
+ }
234
+ /**
235
+ * Base for value-dependent arithmetic failures. Mirrors Python's built-in
236
+ * ``ArithmeticError`` and is deliberately separate from {@link FormulaError}.
237
+ */
238
+ declare class ArithmeticError extends Error {
239
+ constructor(message: string);
240
+ }
241
+ /** Raised when a division or modulo has a zero divisor. */
242
+ declare class ZeroDivisionError extends ArithmeticError {
243
+ constructor(message: string);
244
+ }
245
+ /** Raised when an exponentiation overflows the floating-point range. */
246
+ declare class OverflowError extends ArithmeticError {
247
+ constructor(message: string);
248
+ }
249
+
250
+ export { ArithmeticError, type CalculationResult, Calculator, type CalculatorParameter, type CalculatorQuery, type CompiledFormula, type DataPoint, type EvaluationResult, FormulaError, InvalidFormulaError, MissingParameterError, OverflowError, ParameterError, ParameterLengthError, type ParameterValue, type Parameters, ZeroDivisionError, clearCache, compileFormula, evaluate };