-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathminer.go
More file actions
159 lines (137 loc) · 3.74 KB
/
miner.go
File metadata and controls
159 lines (137 loc) · 3.74 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
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
package loggingdrain
import "encoding/json"
type TemplateMiner struct {
drain *drain
masker *logMasker
}
type templateMinerMarshalStruct struct {
Drain *drain
Masker *logMasker
}
func (miner *TemplateMiner) MarshalJSON() ([]byte, error) {
return json.Marshal(templateMinerMarshalStruct{
Drain: miner.drain,
Masker: miner.masker,
})
}
func (miner *TemplateMiner) UnmarshalJSON(data []byte) error {
var marshalStruct templateMinerMarshalStruct
err := json.Unmarshal(data, &marshalStruct)
if err != nil {
return err
}
miner.drain = marshalStruct.Drain
miner.masker = marshalStruct.Masker
return nil
}
type LogMessageResponse struct {
ChangeType ClusterUpdateType
Cluster *LogCluster
TemplateMined string
ClusterCount int
}
func NewTemplateMiner(options ...minerOption) (*TemplateMiner, error) {
c := newTemplateMinerConfig(options)
return newTemplateMinerWithConfig(c)
}
func newTemplateMinerWithConfig(config *minerConfig) (*TemplateMiner, error) {
drain := newDrainWithConfig(config.Drain)
masker, err := newLogMaskerWithConfig(config.Mask)
if err != nil {
return nil, err
}
return &TemplateMiner{
drain: drain,
masker: masker,
}, nil
}
func newTemplateMinerConfig(options []minerOption) *minerConfig {
drainConfig := drainConfig{
Depth: default_max_depth,
Similarity: default_sim,
MaxChildren: default_max_children,
MaxCluster: default_max_clusters,
}
maskConfig := maskConfig{
Prefix: default_masking_prefix,
Suffix: default_masking_suffix,
MaskInstructions: make([]maskInstruction, 0),
}
conf := minerConfig{
Mask: maskConfig,
Drain: drainConfig,
}
for _, o := range options {
conf = o.apply(conf)
}
return &conf
}
func (miner *TemplateMiner) AddLogMessage(message string) *LogMessageResponse {
maskedMessage := miner.masker.mask(message)
logCluster, updateType := miner.drain.addLogMessage(maskedMessage)
return &LogMessageResponse{
ChangeType: updateType,
Cluster: logCluster,
TemplateMined: logCluster.getTemplate(),
ClusterCount: len(miner.drain.idToCluster.Keys()),
}
}
func (miner *TemplateMiner) Match(message string) *LogCluster {
maskedMessage := miner.masker.mask(message)
return miner.drain.match(maskedMessage, SEARCH_STRATEGY_NEVER)
}
func WithDrainDepth(depth int) minerOption {
return minerOptionFunc(func(conf minerConfig) minerConfig {
conf.Drain.Depth = depth
return conf
})
}
func WithDrainSim(sim float32) minerOption {
return minerOptionFunc(func(conf minerConfig) minerConfig {
conf.Drain.Similarity = sim
return conf
})
}
func WithDrainMaxChildren(maxChildren int) minerOption {
return minerOptionFunc(func(conf minerConfig) minerConfig {
conf.Drain.MaxChildren = maxChildren
return conf
})
}
func WithDrainMaxCluster(maxCluster int) minerOption {
return minerOptionFunc(func(conf minerConfig) minerConfig {
conf.Drain.MaxCluster = maxCluster
return conf
})
}
func WithMaskPrefix(prefix string) minerOption {
return minerOptionFunc(func(conf minerConfig) minerConfig {
conf.Mask.Prefix = prefix
return conf
})
}
func WithMaskSuffix(suffix string) minerOption {
return minerOptionFunc(func(conf minerConfig) minerConfig {
conf.Mask.Suffix = suffix
return conf
})
}
func WithMaskInsturction(pattern, maskWith string) minerOption {
return minerOptionFunc(func(conf minerConfig) minerConfig {
conf.Mask.MaskInstructions = append(conf.Mask.MaskInstructions, maskInstruction{
Pattern: pattern,
MaskWith: maskWith,
})
return conf
})
}
func (miner *TemplateMiner) Status() string {
return miner.drain.status()
}
type minerOption interface {
apply(minerConfig) minerConfig
}
type minerOptionFunc func(minerConfig) minerConfig
func (o minerOptionFunc) apply(conf minerConfig) minerConfig {
return o(conf)
}