-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Expand file tree
/
Copy pathdraft_grok_expression.ts
More file actions
97 lines (83 loc) · 3.28 KB
/
draft_grok_expression.ts
File metadata and controls
97 lines (83 loc) · 3.28 KB
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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import type { Subscription } from 'rxjs';
import { BehaviorSubject } from 'rxjs';
import type { GrokCollection, GrokFieldUsageSource } from './grok_collection_and_pattern';
import { GrokPattern } from './grok_collection_and_pattern';
export interface DraftGrokExpressionOptions {
patternSlotId?: string | number;
}
export class DraftGrokExpression implements GrokFieldUsageSource {
private expression: string = '';
private grokPattern: GrokPattern;
private expression$: BehaviorSubject<string>;
private customPatternsSubscription: Subscription;
private readonly unregisterFieldUsage: () => void;
private previousFieldNames: Set<string>;
private readonly patternSlotId: string | number | undefined;
constructor(
collection: GrokCollection,
initialExpression?: string,
options?: DraftGrokExpressionOptions
) {
const expression = initialExpression ?? '';
this.expression = expression;
this.patternSlotId = options?.patternSlotId;
this.previousFieldNames = collection.parseFieldNames(expression);
this.unregisterFieldUsage = collection.registerFieldUsageSource(this);
this.grokPattern = new GrokPattern(expression || '', 'DRAFT_GROK_EXPRESSION', collection);
this.grokPattern.resolvePattern();
this.expression$ = new BehaviorSubject<string>(expression);
collection.flushFieldUsage();
this.customPatternsSubscription = collection.customPatternsChanged$.subscribe(() => {
this.grokPattern.resolvePattern(true);
this.expression$.next(this.expression);
});
}
public updateExpression = (expression: string) => {
const collection = this.grokPattern.getParentCollection();
const nextFieldNames = collection.parseFieldNames(expression);
collection.reconcileFieldUsage(this, this.previousFieldNames, nextFieldNames);
this.expression = expression;
this.previousFieldNames = nextFieldNames;
this.grokPattern.updatePattern(this.expression);
this.grokPattern.resolvePattern(true);
collection.flushFieldUsage();
this.expression$.next(this.expression);
};
public parse = (samples: string[]) => {
return this.grokPattern.parse(samples, true);
};
public getRegex = () => {
return this.grokPattern.getRegex();
};
public getRegexPattern = () => {
return this.grokPattern.getRegexPattern();
};
public getFields = () => {
return this.grokPattern.getFields();
};
public getExpression = () => {
return this.expression;
};
public getFieldNames = (): ReadonlySet<string> => {
return this.previousFieldNames;
};
public getPatternSlotId = () => {
return this.patternSlotId;
};
public getExpression$ = () => {
return this.expression$;
};
public destroy = () => {
this.customPatternsSubscription.unsubscribe();
this.unregisterFieldUsage();
this.grokPattern.getParentCollection().flushFieldUsage();
};
}