Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 | 272x 272x 272x 3x 269x 2x 267x 267x 267x 267x 267x 267x 267x 267x 267x 267x 267x 2x 265x 1x 264x 267x 272x 267x 1x 266x 266x 266x 1x 266x 6x 266x 266x 16x 11x 11x 21x 4x 11x 6841x 6841x 6832x 9x 9x 5x 1x 1x 238x 1008x 1008x 221x 6715x 6715x 6x 33530x 4x 4x 18x 18x 13x 13x 31x 31x 18x 13x 13x 15x 15x 27x 15x 2x 2x 2x 10x 10x 10x 5x 10x 10x 8x 2x 6447x 6447x 6447x 6447x 6492x 6492x 6486x 6486x 6447x 6447x 6447x 6447x 6664x 6664x 6664x 304x 304x 304x 304x 304x 304x 6360x 6360x 31985x 6248x 112x 112x 6360x 6360x 24x 6664x | import { Node } from "./Node.js";
import { LogCluster } from "./LogCluster.js";
import { LogClusterCache } from "./LogClusterCache.js";
import { ChangeType, type MatchStrategy } from "./types.js";
import type { DrainOptions } from "./types.js";
import type { TemplatePatternStrategyChain } from "./TemplatePatternStrategy.js";
import type { SimilarityStrategyChain } from "./SimilarityStrategy.js";
import {
createDefaultSimilarityChain,
createAELSimilarityChain,
} from "./SimilarityStrategy.js";
import type { ClusterMergePipeline } from "./ClusterMergeStrategy.js";
import {
AffixPreservingStrategy,
ExactMatchStrategy,
FullTokenParameterizationStrategy,
RegexParameterizationStrategy,
TemplatePatternStrategyChain as StrategyChain,
} from "./TemplatePatternStrategy.js";
/**
* Abstract base class for Drain algorithm implementations.
*
* Maps 1:1 to Python `DrainBase` class (drain.py L37-L176 — abstract portions).
*
* Defines the shared state and interface that all Drain variants
* (Drain, JaccardDrain) must implement. Subclasses provide the concrete
* tree search, distance calculation, and template creation logic.
*
* Key invariants:
* - maxNodeDepth = depth - 2 (derived from logClusterDepth)
* - logClusterDepth >= 3 (enforced in constructor)
* - clusterIds are monotonically increasing, starting from 1
* - rootNode is always the entry point for all tree operations
*/
export abstract class DrainBase {
// ============================================================
// Configuration (maps to Python DrainBase.__init__ parameters)
// ============================================================
/** Maximum cluster depth including root and token count layers. Python: self.log_cluster_depth */
readonly logClusterDepth: number;
/** Maximum parse tree node depth = depth - 2. Python: self.max_node_depth */
readonly maxNodeDepth: number;
/** Similarity threshold for creating new clusters. Python: self.sim_th */
readonly simTh: number;
/** Maximum child nodes per tree level. Python: self.max_children */
readonly maxChildren: number;
/** Maximum clusters (null = unlimited). Python: self.max_clusters */
readonly maxClusters: number | null;
/** Additional tokenization delimiters. Python: self.extra_delimiters */
readonly extraDelimiters: readonly string[];
/** String used to replace parameters in templates. Python: self.param_str */
readonly paramStr: string;
/** Whether tokens containing digits are treated as parameters. Python: self.parametrize_numeric_tokens */
readonly parametrizeNumericTokens: boolean;
/** Strategy chain for template token parameterization. */
readonly strategyChain: TemplatePatternStrategyChain;
/** Whether to use (token_count, param_count) as compound root key. */
readonly enableParamBinning: boolean;
/** Whether to generalize masked tokens to paramStr in createTemplate. */
readonly enableMaskParamGeneralization: boolean;
/** Chain for computing token sequence similarity. */
readonly similarityChain: SimilarityStrategyChain;
// ============================================================
// State (maps to Python DrainBase.__init__ state initialization)
// ============================================================
/** Root node of the fixed-depth prefix tree. Python: self.root_node */
readonly rootNode: Node = new Node();
/**
* Cluster ID → LogCluster mapping.
* Uses LogClusterCache (LRU) when maxClusters is set, plain Map otherwise.
*
* Python: self.id_to_cluster = {} if max_clusters is None
* else LogClusterCache(maxsize=max_clusters)
*/
idToCluster: Map<number, LogCluster>;
/**
* Monotonically increasing counter for cluster IDs.
* Incremented before each new cluster creation. Starts at 0.
*
* Python: self.clusters_counter = 0
*/
clustersCounter: number = 0;
// ============================================================
// Constructor (maps to Python DrainBase.__init__)
// ============================================================
constructor({
depth = 4,
simTh = 0.4,
maxChildren = 100,
maxClusters = null,
extraDelimiters = [],
paramStr = "<*>",
parametrizeNumericTokens = true,
templatePatternStrategies,
enableAffixPreserving = false,
minAffixLength = 2,
customRegexPatterns = [],
enableParamBinning = false,
enableMaskParamGeneralization = false,
similarityStrategy,
enableAELSimilarity = false,
maxDiffRatio = 0.3,
}: DrainOptions = {}) {
if (depth < 3) {
throw new Error(`depth must be at least 3, got ${depth}`);
}
if (simTh < 0 || simTh > 1) {
throw new Error(`simTh must be between 0 and 1, got ${simTh}`);
}
this.logClusterDepth = depth;
this.maxNodeDepth = depth - 2;
this.simTh = simTh;
this.maxChildren = maxChildren;
this.maxClusters = maxClusters;
this.extraDelimiters = Object.freeze([...extraDelimiters]);
this.paramStr = paramStr;
this.parametrizeNumericTokens = parametrizeNumericTokens;
this.enableParamBinning = enableParamBinning;
this.enableMaskParamGeneralization = enableMaskParamGeneralization;
// Build similarity chain from options
if (similarityStrategy) {
this.similarityChain = similarityStrategy;
} else if (enableAELSimilarity) {
this.similarityChain = createAELSimilarityChain(maxDiffRatio);
} else {
this.similarityChain = createDefaultSimilarityChain();
}
// Build strategy chain for template parameterization
this.strategyChain = this.buildStrategyChain({
...(templatePatternStrategies !== undefined
? { templatePatternStrategies }
: {}),
enableAffixPreserving,
minAffixLength,
customRegexPatterns,
});
// Python: {} if max_clusters is None else LogClusterCache(maxsize=max_clusters)
this.idToCluster =
maxClusters === null ? new Map() : new LogClusterCache(maxClusters);
}
/**
* Builds the template pattern strategy chain from options.
*
* Priority order:
* 1. Custom strategies (if provided) — use as-is
* 2. Built from options: Exact → [Regex] → [AffixPreserving] → FullToken
*
* Subclasses can override to customize chain construction.
*/
protected buildStrategyChain(options: {
templatePatternStrategies?: readonly import("./TemplatePatternStrategy.js").TemplatePatternStrategy[];
enableAffixPreserving: boolean;
minAffixLength: number;
customRegexPatterns: ReadonlyArray<{
readonly regex: RegExp;
readonly template: string;
readonly confidence?: number;
}>;
}): TemplatePatternStrategyChain {
// If custom strategies provided, use them directly
if (options.templatePatternStrategies) {
return new StrategyChain().registerAll(
options.templatePatternStrategies,
);
}
// Build from configuration options
const chain = new StrategyChain();
// Always register exact match (highest priority)
chain.register(new ExactMatchStrategy());
// Register regex patterns if provided
if (options.customRegexPatterns.length > 0) {
chain.register(
new RegexParameterizationStrategy(options.customRegexPatterns),
);
}
// Register affix-preserving if enabled
if (options.enableAffixPreserving) {
chain.register(new AffixPreservingStrategy(options.minAffixLength));
}
// Always register full-token fallback (lowest priority)
chain.register(new FullTokenParameterizationStrategy());
return chain;
}
// ============================================================
// Properties
// ============================================================
/** All current clusters. Python: DrainBase.clusters property */
get clusters(): LogCluster[] {
return [...this.idToCluster.values()];
}
/**
* Counts parameter tokens (masked values) in a token sequence.
*
* A param token matches the mask pattern: starts with "<" and
* ends with ">" without being the paramStr placeholder itself.
*/
protected countParamTokens(tokens: readonly string[]): number {
let count = 0;
for (const token of tokens) {
if (token.startsWith("<") && token.endsWith(">")) {
count++;
}
}
return count;
}
/**
* Computes the root-level tree key for a token sequence.
*
* When enableParamBinning is true, uses compound key
* "{token_count}#{param_count}" for AEL-style binning.
* Otherwise uses simple "{token_count}" (Drain3-compatible).
*/
protected getRootKey(tokens: readonly string[]): string {
const tc = tokens.length;
if (!this.enableParamBinning) {
return String(tc);
}
const pc = this.countParamTokens(tokens);
return `${tc}#${pc}`;
}
/**
* Returns true if a token is a masked parameter (e.g., "<NUM>", "<IP>").
*
* Masked tokens are recognized by their "<...>" wrapping pattern.
* This is used by getSeqDistance to properly skip parameter positions
* during similarity comparison, rather than treating them as literals.
*/
protected isMaskedParam(token: string): boolean {
return (
token.length > 2 &&
token.startsWith("<") &&
token.endsWith(">") &&
token !== this.paramStr
);
}
/**
* Post-training cluster merge using the configured merge pipeline.
*
* Applies all registered ClusterMergeStrategies iteratively until
* convergence. This is the drain-ts equivalent of AEL's reconcile()
* mechanism, which gives AEL its 0.974 GA on Proxifier.
*
* @param pipeline - Merge pipeline to use (defaults to new empty pipeline)
* @returns Number of merges performed
*/
mergeClusters(pipeline?: ClusterMergePipeline): number {
Iif (!pipeline || pipeline.size === 0) return 0;
return pipeline.merge(this);
}
// ============================================================
// Utility methods (maps to Python DrainBase static/concrete methods)
// ============================================================
/**
* Checks if a string contains any digit characters.
*
* Python: DrainBase.has_numbers(s) → any(char.isdigit() for char in s)
*
* Uses explicit character code comparison for performance (avoids regex).
*/
static hasNumbers(s: string): boolean {
for (let i = 0; i < s.length; i++) {
const code = s.charCodeAt(i);
// '0' = 48, '9' = 57
if (code >= 48 && code <= 57) return true;
}
return false;
}
/**
* Splits a log message into tokens.
*
* Python: DrainBase.get_content_as_tokens(content)
*
* Processing steps (identical to Python):
* 1. strip leading/trailing whitespace
* 2. replace extra delimiters with spaces
* 3. split on whitespace, filtering empty tokens
*/
getContentAsTokens(content: string): string[] {
let processed = content.trim();
for (const delimiter of this.extraDelimiters) {
// Python: content.replace(delimiter, " ")
// Using split+join to match Python's str.replace behavior for plain strings
processed = processed.split(delimiter).join(" ");
}
return processed.split(/\s+/).filter((t) => t.length > 0);
}
/**
* Returns all cluster IDs for a given token sequence length.
*
* Python: DrainBase.get_clusters_ids_for_seq_len(seq_fir)
*
* Recursively traverses the subtree rooted at the token-count node.
*/
getClustersIdsForSeqLen(seqLen: number): number[] {
return this._getClustersIdsForRootKey(String(seqLen));
}
/**
* Returns all cluster IDs for a given root-level tree key.
*
* Used by JaccardDrain which indexes by first token (string)
* instead of token count (number).
*
* Recursively traverses the subtree rooted at the matching node.
*/
getClustersIdsForFirstToken(firstToken: string): number[] {
return this._getClustersIdsForRootKey(firstToken);
}
/**
* Internal: collects cluster IDs from a subtree rooted at a given key.
*/
protected _getClustersIdsForRootKey(key: string): number[] {
const curNode = this.rootNode.keyToChildNode.get(key);
if (!curNode) return [];
const result: number[] = [];
const collectRecursive = (node: Node): void => {
result.push(...node.clusterIds);
for (const child of node.keyToChildNode.values()) {
collectRecursive(child);
}
};
collectRecursive(curNode);
return result;
}
/**
* Total number of log messages across all clusters.
*
* Python: DrainBase.get_total_cluster_size()
*/
getTotalClusterSize(): number {
let size = 0;
for (const c of this.idToCluster.values()) {
size += c.size;
}
return size;
}
/**
* Removes stale cluster IDs from all tree nodes.
*
* When clusters are evicted from the LRU cache (via maxClusters),
* their IDs may remain in Node.clusterIds arrays throughout the
* prefix tree. This method traverses the entire tree and removes
* any cluster ID that is no longer present in idToCluster.
*
* Call this periodically in long-running applications with
* maxClusters enabled, or after bulk LRU eviction.
*
* Complexity: O(n) where n is the number of nodes in the tree.
*/
compactTree(): number {
let removed = 0;
const stack: Node[] = [this.rootNode];
while (stack.length > 0) {
const node = stack.pop()!;
const before = node.clusterIds.length;
node.clusterIds = node.clusterIds.filter((cid) =>
this.idToCluster.has(cid),
);
removed += before - node.clusterIds.length;
for (const child of node.keyToChildNode.values()) {
stack.push(child);
}
}
return removed;
}
// ============================================================
// Fast match (maps to Python DrainBase.fast_match)
// ============================================================
/**
* Finds the best-matching cluster from a candidate list.
*
* Python: DrainBase.fast_match(cluster_ids, tokens, sim_th, include_params)
*
* Key rules:
* - Uses idToCluster.get() (bypasses LRU eviction) for lookups
* - When two clusters have the same similarity score, prefers the one
* with more parameters (more generic template)
* - Returns null if no cluster meets the similarity threshold
*
* Complexity: O(c) where c is the number of candidate clusters
*/
protected fastMatch(
clusterIds: readonly number[],
tokens: readonly string[],
simTh: number,
includeParams: boolean,
): LogCluster | null {
let maxSim = -1;
let maxParamCount = -1;
let bestCluster: LogCluster | null = null;
for (const clusterId of clusterIds) {
// Python: cluster = self.id_to_cluster.get(cluster_id) — bypasses LRU
const cluster = this.idToCluster.get(clusterId);
if (!cluster) continue;
const { similarity, paramCount } = this.getSeqDistance(
cluster.logTemplateTokens,
tokens,
includeParams,
);
// Python: cur_sim > max_sim or (cur_sim == max_sim and param_count > max_param_count)
if (
similarity > maxSim ||
(similarity === maxSim && paramCount > maxParamCount)
) {
maxSim = similarity;
maxParamCount = paramCount;
bestCluster = cluster;
}
}
return maxSim >= simTh ? bestCluster : null;
}
// ============================================================
// addLogMessage — concrete method shared by Drain and JaccardDrain
// (maps to Python DrainBase.add_log_message, drain.py L136-L176)
// ============================================================
/**
* Processes a single log message through the Drain algorithm.
*
* This is the primary entry point for training mode. Each call updates
* the internal state — either by creating a new cluster, updating an
* existing template, or incrementing a cluster's count.
*
* Python: DrainBase.add_log_message(content) → Tuple[LogCluster, str]
*
* Processing flow (identical to Python):
* 1. Tokenize → getContentAsTokens
* 2. Tree search → treeSearch(includeParams=false)
* 3a. No match → create new cluster → changeType = "cluster_created"
* 3b. Match found → merge templates → "cluster_template_changed" or "none"
* 4. Return (cluster, changeType)
*
* @param content - The raw log message to process.
* @returns The assigned cluster and the type of change that occurred.
*/
addLogMessage(content: string): {
cluster: LogCluster;
changeType: typeof ChangeType[keyof typeof ChangeType];
} {
const contentTokens = this.getContentAsTokens(content);
// Phase 1: Tree search
let matchCluster = this.treeSearch(
this.rootNode,
contentTokens,
this.simTh,
false,
);
let changeType: typeof ChangeType[keyof typeof ChangeType];
if (matchCluster === null) {
// Phase 2: Create new cluster
this.clustersCounter += 1;
const clusterId = this.clustersCounter;
matchCluster = new LogCluster(contentTokens, clusterId);
this.idToCluster.set(clusterId, matchCluster);
this.addSeqToPrefixTree(this.rootNode, matchCluster);
changeType = ChangeType.ClusterCreated;
} else {
// Phase 3: Update existing cluster
const newTemplateTokens = this.createTemplate(
contentTokens,
matchCluster.logTemplateTokens,
);
if (
newTemplateTokens.length === matchCluster.logTemplateTokens.length &&
newTemplateTokens.every(
(t, i) => t === matchCluster!.logTemplateTokens[i],
)
) {
changeType = ChangeType.None;
} else {
matchCluster.logTemplateTokens = newTemplateTokens;
changeType = ChangeType.ClusterTemplateChanged;
}
matchCluster.size += 1;
// Trigger LRU access record update
if (this.idToCluster instanceof LogClusterCache) {
this.idToCluster.touch(matchCluster.clusterId);
}
}
return { cluster: matchCluster, changeType };
}
// ============================================================
// Abstract methods (subclasses MUST implement)
// ============================================================
/**
* Searches the prefix tree for the best-matching cluster.
*
* Python: Drain.tree_search(root_node, tokens, sim_th, include_params)
*/
abstract treeSearch(
rootNode: Node,
tokens: readonly string[],
simTh: number,
includeParams: boolean,
): LogCluster | null;
/**
* Inserts a cluster into the prefix tree.
*
* Python: Drain.add_seq_to_prefix_tree(root_node, cluster)
*/
abstract addSeqToPrefixTree(rootNode: Node, cluster: LogCluster): void;
/**
* Calculates similarity and parameter count between two token sequences.
*
* Python: Drain.get_seq_distance(seq1, seq2, include_params)
*/
abstract getSeqDistance(
seq1: readonly string[],
seq2: readonly string[],
includeParams: boolean,
): { similarity: number; paramCount: number };
/**
* Creates a merged template from two token sequences.
*
* Python: Drain.create_template(seq1, seq2)
*/
abstract createTemplate(
seq1: readonly string[],
seq2: readonly string[],
): readonly string[];
/**
* Matches a log message against existing clusters (inference mode).
* Does NOT create new clusters or modify templates.
*
* Python: Drain.match(content, full_search_strategy)
*/
abstract match(
content: string,
fullSearchStrategy?: MatchStrategy,
): LogCluster | null;
}
|