-
Notifications
You must be signed in to change notification settings - Fork 2
/
model.go
206 lines (175 loc) · 4.65 KB
/
model.go
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
package tm
import (
"strings"
"github.com/txn2/es/v2"
)
const IdxModel = "models"
// Model
type Model struct {
// MachineName is a lowercase under score delimited uniq id
MachineName string `json:"machine_name" mapstructure:"machine_name"`
// AliasOf is the machine name of a model this model is an alias of.
AliasOf string `json:"alias_of" mapstructure:"alias_of"`
// short human readable display name
DisplayName string `json:"display_name" mapstructure:"display_name"`
// a single sentence description
BriefDescription string `json:"description_brief" mapstructure:"description_brief"`
// full documentation in markdown
Description string `json:"description" mapstructure:"description"`
// default value expressed as a string
DefaultValue string `json:"default_value" mapstructure:"default_value"`
// integer, float, date, binary, text and keyword
DataType string `json:"data_type" mapstructure:"data_type"`
// used for data formats
Format string `json:"format" mapstructure:"format"`
// named parsers
Parsers []string `json:"parsers" mapstructure:"parsers"`
// belongs to a class of models
TypeClass string `json:"type_class" mapstructure:"type_class"`
// groups models
Group string `json:"group" mapstructure:"group"`
// false to ignore inbound parsing
Parse bool `json:"parse" mapstructure:"parse"`
// used by parsers of element ordered inbound data
Index int `json:"index" mapstructure:"index"`
// children of this model
Fields []Model `json:"fields" mapstructure:"fields"`
}
// fieldProps
func fieldProps(fields []Model) map[string]interface{} {
parts := make(map[string]interface{})
for _, field := range fields {
if len(field.Fields) > 0 {
parts[field.MachineName] = es.Obj{
"type": field.DataType,
"properties": fieldProps(field.Fields),
}
continue
}
if field.Format != "" {
parts[field.MachineName] = es.Obj{
"type": field.DataType,
"format": field.Format,
}
continue
}
parts[field.MachineName] = es.Obj{"type": field.DataType}
}
return parts
}
// MakeModelTemplateMapping creates a template for modeled data
// coming in from rxtx.
func MakeModelTemplateMapping(account string, model *Model) es.IndexTemplate {
name := account + "-data-" + model.MachineName
idxPattern := account + "-data-" + model.MachineName + "-*"
// CONVENTION: if the account ends in an underscore "_" then
// it is a system model (SYSTEM_IdxModel)
if strings.HasSuffix(account, "_") {
name = account + IdxModel + "-data-" + model.MachineName
idxPattern = "*-data-" + model.MachineName + "-*"
}
payloadProps := fieldProps(model.Fields)
template := es.Obj{
"index_patterns": []string{idxPattern},
"settings": es.Obj{
"index": es.Obj{
"number_of_shards": 1, // @TODO allow this to be configured
},
},
"mappings": es.Obj{
// _doc is the standard until deprecated, logstash uses "doc"
// messages come into elasticsearch via txn2/rxtx->txn2/rtBeat->logstash
"doc": es.Obj{
"_source": es.Obj{
"enabled": true,
},
"properties": es.Obj{
// txn2/rtbeat sends txn2/rxtx messages as rxtxMsg
"rxtxMsg": es.Obj{
"properties": es.Obj{
"seq": es.Obj{"type": "long"},
"producer": es.Obj{"type": "text"},
"key": es.Obj{"type": "text"},
"uuid": es.Obj{"type": "text"},
"label": es.Obj{"type": "text"},
"payload": es.Obj{
"properties": payloadProps,
},
},
},
},
},
},
}
return es.IndexTemplate{
Name: name,
Template: template,
}
}
// GetModelsTemplateMapping
func GetModelsTemplateMapping() es.IndexTemplate {
properties := es.Obj{
"machine_name": es.Obj{
"type": "keyword",
},
"alias_of": es.Obj{
"type": "keyword",
},
"display_name": es.Obj{
"type": "text",
},
"description_brief": es.Obj{
"type": "text",
},
"description": es.Obj{
"type": "text",
},
"default_value": es.Obj{
"type": "keyword",
},
"data_type": es.Obj{
"type": "keyword",
},
"format": es.Obj{
"type": "text",
},
"parsers": es.Obj{
"type": "text",
},
"type_class": es.Obj{
"type": "keyword",
},
"group": es.Obj{
"type": "keyword",
},
"parse": es.Obj{
"type": "boolean",
},
"index": es.Obj{
"type": "integer",
},
"fields": es.Obj{
"type": "nested",
},
}
template := es.Obj{
"index_patterns": []string{"*-" + IdxModel, "*_" + IdxModel},
"settings": es.Obj{
"index": es.Obj{
"number_of_shards": 1,
},
},
"mappings": es.Obj{
"_doc": es.Obj{
"_source": es.Obj{
"enabled": true,
},
"properties": properties,
},
},
}
return es.IndexTemplate{
Name: IdxModel,
Template: template,
}
}