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 | 36x 114x 114x 114x 114x 66x 2x 2x 2x 64x 64x 115x 115x 71x 67x 67x 67x 36x 31x 31x 18x 13x 54x 51x 68x 68x 68x 68x 44x 44x 68x 68x 4x 4x 64x 10x 1x 10x 10x 10x 54x 54x 118x 118x 54x 14x 54x 54x 54x 64x 58x 10x 10x 5x 5x 10x 48x 5x 5x 43x 37x 37x 37x 6x 6x 6x 6x 6x 64x 63x 1x 62x 62x 379x 62x 80x 48x 62x 47x 50x 62x 62x 339x 62x 62x 62x 62x 62x 63x 166x 33x 32x 32x 161x 161x 161x 161x 116x 45x 45x 32x 1x 33x 5x 2x 1x 16x 16x 16x 4x 4x 4x 16x 3x 13x 13x 5x 1x 2x 2x 10x 10x 2x 8x 2x 6x 10x 4x 10x 10x 8x 10x 4x 4x 4x 2x | import { DrainBase } from "./DrainBase.js";
import { LogCluster } from "./LogCluster.js";
import { Node } from "./Node.js";
import { MatchStrategy } from "./types.js";
import type { DrainOptions } from "./types.js";
/**
* JaccardDrain — Drain variant using Jaccard set similarity.
*
* Maps 1:1 to Python `JaccardDrain` class (drain3/jaccard_drain.py).
*
* Key differences from standard Drain:
*
* | Feature | Standard Drain | JaccardDrain |
* |---------|---------------|-------------|
* | Tree root key | Token count | First token |
* | Similarity | Position-wise ratio | Jaccard (intersection / union) |
* | Template merge | Position-wise param replacement | Set intersection + position-wise |
* | Match threshold | 1.0 (exact) | 0.8 (Jaccard is inherently lower) |
* | Variable-length | ✗ must be same | ✓ supports different lengths |
*
* JaccardDrain is particularly useful when messages with the same
* template can have varying token counts (e.g., "session opened for
* user alice by (uid=0)" vs "session closed for user alice").
*
* The Jaccard coefficient is multiplied by 1.3 (gain factor, capped
* at 1.0) to compensate for Jaccard's naturally lower scores.
*
* @example
* ```typescript
* const drain = new JaccardDrain({ depth: 4, simTh: 0.4 });
* drain.addLogMessage("session opened for user alice by (uid=0)");
* drain.addLogMessage("session closed for user alice");
* // Both grouped together via Jaccard similarity
* ```
*/
export class JaccardDrain extends DrainBase {
/**
* @param options - Configuration options forwarded to DrainBase.
*/
constructor(options: DrainOptions = {}) {
super(options);
}
// ============================================================
// treeSearch (maps to Python JaccardDrain.tree_search)
// ============================================================
/**
* Tree search using first token as the root-level key.
*
* Python: JaccardDrain.tree_search(root_node, tokens, sim_th, include_params)
*
* Search strategy:
* 1. First level: lookup by first token (NOT token count)
* 2. Empty tokens: return first cluster directly
* 3. Walk tree for up to maxNodeDepth levels, starting from tokens[1]
* 4. Max depth or "last token" (token_count - 1) → break
* 5. At leaf, run fastMatch
*/
treeSearch(
rootNode: Node,
tokens: readonly string[],
simTh: number,
includeParams: boolean,
): LogCluster | null {
// Step 1: First-level key is the first token
const tokenCount = tokens.length;
const tokenFirst = tokenCount === 0 ? "" : (tokens[0] ?? "");
let curNode = rootNode.keyToChildNode.get(tokenFirst);
if (!curNode) return null;
// Step 2: Empty tokens → return first cluster
if (tokenCount === 0) {
const firstId = curNode.clusterIds[0];
Iif (firstId === undefined) return null;
return this.idToCluster.get(firstId) ?? null;
}
// Step 3: Walk from tokens[1] onward (skip first token)
let curNodeDepth = 1;
for (let i = 1; i < tokens.length; i++) {
const token = tokens[i]!;
// At max depth → break
if (curNodeDepth >= this.maxNodeDepth) break;
// At "last token" (relative to non-first tokens) → break
if (curNodeDepth === tokenCount - 1) break;
const children: Map<string, Node> = curNode.keyToChildNode;
const exactNode: Node | undefined = children.get(token);
if (exactNode) {
curNode = exactNode;
} else {
const paramNode = children.get(this.paramStr);
if (paramNode) {
curNode = paramNode;
} else {
return null;
}
}
curNodeDepth++;
}
return this.fastMatch(curNode!.clusterIds, tokens, simTh, includeParams);
}
// ============================================================
// addSeqToPrefixTree (maps to Python JaccardDrain.add_seq_to_prefix_tree)
// ============================================================
/**
* Inserts a cluster into the prefix tree using first token as root key.
*
* Python: JaccardDrain.add_seq_to_prefix_tree(root_node, cluster)
*
* Structure:
* 1. Root key = first template token (not token count)
* 2. Single-token messages: append directly to first-level node
* 3. Multi-token messages: walk from tokens[1], similar to standard Drain
* 4. Max depth and "last token" bounds use token_count - 1
*/
addSeqToPrefixTree(rootNode: Node, cluster: LogCluster): void {
const tokenCount = cluster.logTemplateTokens.length;
// Step 1: First token as root key
const tokenFirst =
tokenCount === 0 ? "" : (cluster.logTemplateTokens[0] ?? "");
let firstLayerNode = rootNode.keyToChildNode.get(tokenFirst);
if (!firstLayerNode) {
firstLayerNode = new Node();
rootNode.keyToChildNode.set(tokenFirst, firstLayerNode);
}
let curNode = firstLayerNode;
// Step 2: Empty message → assign directly
if (tokenCount === 0) {
curNode.clusterIds = [cluster.clusterId];
return;
}
// Step 3: Single-token message → add to first-level node's clusterIds
if (tokenCount === 1) {
const newClusterIds = curNode.clusterIds.filter((cid) =>
this.idToCluster.has(cid),
);
newClusterIds.push(cluster.clusterId);
curNode.clusterIds = newClusterIds;
return;
}
// Step 4: Walk from tokens[1] (skip first token)
let currentDepth = 1;
for (let i = 1; i < tokenCount; i++) {
const token = cluster.logTemplateTokens[i]!;
// At max depth OR "last token" → add to leaf
if (
currentDepth >= this.maxNodeDepth ||
currentDepth >= tokenCount - 1
) {
const newClusterIds = curNode.clusterIds.filter((cid) =>
this.idToCluster.has(cid),
);
newClusterIds.push(cluster.clusterId);
curNode.clusterIds = newClusterIds;
break;
}
// Token doesn't exist at this level
if (!curNode.keyToChildNode.has(token)) {
if (this.parametrizeNumericTokens && DrainBase.hasNumbers(token)) {
// Numeric token → route through <*> wildcard
let paramNode = curNode.keyToChildNode.get(this.paramStr);
if (!paramNode) {
paramNode = new Node();
curNode.keyToChildNode.set(this.paramStr, paramNode);
}
curNode = paramNode;
} else {
// Non-numeric token → decide based on maxChildren and <*> presence
if (curNode.keyToChildNode.has(this.paramStr)) {
Iif (curNode.keyToChildNode.size < this.maxChildren) {
const newNode = new Node();
curNode.keyToChildNode.set(token, newNode);
curNode = newNode;
} else {
curNode = curNode.keyToChildNode.get(this.paramStr)!;
}
} else {
if (curNode.keyToChildNode.size + 1 < this.maxChildren) {
const newNode = new Node();
curNode.keyToChildNode.set(token, newNode);
curNode = newNode;
} else if (curNode.keyToChildNode.size + 1 === this.maxChildren) {
const newNode = new Node();
curNode.keyToChildNode.set(this.paramStr, newNode);
curNode = newNode;
} else E{
curNode = curNode.keyToChildNode.get(this.paramStr)!;
}
}
}
} else {
// Token exists → follow the path
curNode = curNode.keyToChildNode.get(token)!;
}
currentDepth++;
}
}
// ============================================================
// getSeqDistance (maps to Python JaccardDrain.get_seq_distance)
// ============================================================
/**
* Calculates similarity using the Jaccard index.
*
* Python: JaccardDrain.get_seq_distance(seq1, seq2, include_params)
*
* Jaccard index: `|intersection| / |union|`
*
* Steps:
* 1. Count param tokens in seq1 (template)
* 2. If same length and has params: remove param positions from seq2
* 3. If includeParams: remove param positions from seq1
* 4. Jaccard on remaining sets
* 5. Apply 1.3× gain factor (capped at 1.0)
*
* seq1 is the template (may contain paramStr), seq2 is the log message.
*/
getSeqDistance(
seq1: readonly string[],
seq2: readonly string[],
includeParams: boolean,
): { similarity: number; paramCount: number } {
// Empty sequences → full match
if (seq1.length === 0) {
return { similarity: 1.0, paramCount: 0 };
}
// Count params in template
let paramCount = 0;
for (const token of seq1) {
if (token === this.paramStr) paramCount++;
}
// Build filtered seq2: remove positions where template has paramStr
let filteredSeq2: string[];
if (seq1.length === seq2.length && paramCount > 0) {
filteredSeq2 = seq2.filter((_, i) => seq1[i] !== this.paramStr);
} else {
filteredSeq2 = [...seq2];
}
// Build filtered seq1: remove paramStr if includeParams
let filteredSeq1: string[];
if (includeParams) {
filteredSeq1 = seq1.filter((t) => t !== this.paramStr);
} else {
filteredSeq1 = [...seq1];
}
const set1 = new Set(filteredSeq1);
const set2 = new Set(filteredSeq2);
// Jaccard: intersection / union
const intersection = new Set([...set1].filter((x) => set2.has(x)));
const union = new Set([...set1, ...set2]);
let jaccard = 0;
Eif (union.size > 0) {
jaccard = intersection.size / union.size;
}
// Gain factor: Jaccard naturally produces lower scores
// Multiply by 1.3, capped at 1.0
jaccard = jaccard * 1.3 < 1 ? jaccard * 1.3 : 1;
return { similarity: jaccard, paramCount };
}
// ============================================================
// createTemplate (maps to Python JaccardDrain.create_template)
// ============================================================
/**
* Creates a merged template using set intersection.
*
* Python: JaccardDrain.create_template(seq1, seq2)
*
* Same-length sequences: position-wise comparison with strategy chain.
* Different-length sequences: keep tokens in the intersection set,
* replace all others with paramStr. Uses the LONGER sequence as base.
*
* The strategy chain enables advanced parameterization patterns
* for same-length sequences (affix-preserving, regex-based, etc.)
*
* seq1 = log message tokens, seq2 = template tokens.
*/
createTemplate(
seq1: readonly string[],
seq2: readonly string[],
): readonly string[] {
const interSet = new Set([...seq1].filter((x) => seq2.includes(x)));
if (seq1.length === seq2.length) {
// Same length: use strategy chain for parameterization
const result: string[] = [];
for (let i = 0; i < seq1.length; i++) {
const token1 = seq1[i]!;
const token2 = seq2[i]!;
Iif (this.enableMaskParamGeneralization && this.isMaskedParam(token1)) {
result.push(this.paramStr);
} else if (token1 === token2) {
result.push(token2);
} else {
const paramResult = this.strategyChain.parameterize(
token1,
token2,
this.paramStr,
);
result.push(paramResult.templateToken);
}
}
return Object.freeze(result);
}
// Different lengths: use longer sequence as base, keep intersection tokens
const base = seq1.length > seq2.length ? [...seq1] : [...seq2];
for (let i = 0; i < base.length; i++) {
if (!interSet.has(base[i]!)) {
base[i] = this.paramStr;
}
}
return Object.freeze(base);
}
// ============================================================
// match (maps to Python JaccardDrain.match)
// ============================================================
/**
* Matches a log message against existing clusters (inference mode).
*
* Python: JaccardDrain.match(content, full_search_strategy)
*
* Uses simTh = 0.8 (not 1.0) because Jaccard similarity is
* inherently lower than position-wise matching.
*
* Full search: looks up clusters by first token key.
*/
match(
content: string,
fullSearchStrategy: MatchStrategy = MatchStrategy.Never,
): LogCluster | null {
const REQUIRED_SIM_TH = 0.8;
const contentTokens = this.getContentAsTokens(content);
const fullSearch = (): LogCluster | null => {
const firstToken =
contentTokens.length === 0 ? "" : (contentTokens[0] ?? "");
const allIds = this.getClustersIdsForFirstToken(firstToken);
return this.fastMatch(
allIds,
contentTokens,
REQUIRED_SIM_TH,
true,
);
};
if (fullSearchStrategy === MatchStrategy.Always) {
return fullSearch();
}
const matchCluster = this.treeSearch(
this.rootNode,
contentTokens,
REQUIRED_SIM_TH,
true,
);
if (matchCluster !== null) return matchCluster;
if (fullSearchStrategy === MatchStrategy.Never) return null;
return fullSearch();
}
// ============================================================
// Print tree (debug utility)
// ============================================================
/**
* Prints the prefix tree for debugging.
*/
printTree(stream?: NodeJS.WritableStream, maxClusters: number = 5): void {
const out = stream ?? process.stdout;
const printNode = (token: string, node: Node, depth: number): void => {
const indent = "\t".repeat(depth);
let line: string;
if (depth === 0) {
line = `<${token}>`;
} else if (depth === 1) {
line = `"${token}"`;
} else {
line = `"${token}"`;
}
if (node.clusterIds.length > 0) {
line += ` (cluster_count=${node.clusterIds.length})`;
}
out.write(indent + line + "\n");
for (const [childToken, childNode] of node.keyToChildNode) {
printNode(childToken, childNode, depth + 1);
}
for (const cid of node.clusterIds.slice(0, maxClusters)) {
const cluster = this.idToCluster.get(cid);
Eif (cluster) {
out.write("\t".repeat(depth + 1) + cluster.toString() + "\n");
}
}
};
printNode("root", this.rootNode, 0);
}
}
|