Skip to content

Commit 5541d2d

Browse files
committed
parse last line
1 parent 85cd464 commit 5541d2d

3 files changed

Lines changed: 78 additions & 49 deletions

File tree

libbeat/dashboards/decode.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,31 @@ func DecodeExported(exported []byte) []byte {
4747
line, err := r.ReadBytes('\n')
4848
if err != nil {
4949
if err == io.EOF {
50-
return result
50+
return append(result, decodeLine(line)...)
5151
}
5252
return exported
5353
}
54-
o := common.MapStr{}
55-
err = json.Unmarshal(line, &o)
54+
result = append(result, decodeLine(line)...)
55+
}
56+
}
57+
58+
func decodeLine(line []byte) []byte {
59+
o := common.MapStr{}
60+
err := json.Unmarshal(line, &o)
61+
if err != nil {
62+
return line
63+
}
64+
var result []byte
65+
for _, key := range responseToDecode {
66+
// All fields are optional, so errors are not caught
67+
err := decodeValue(o, key)
5668
if err != nil {
57-
continue
58-
}
59-
for _, key := range responseToDecode {
60-
// All fields are optional, so errors are not caught
61-
err := decodeValue(o, key)
62-
if err != nil {
63-
logger := logp.NewLogger("dashboards")
64-
logger.Debugf("Error while decoding dashboard objects: %+v", err)
65-
}
66-
result = append(result, []byte(o.String())...)
69+
logger := logp.NewLogger("dashboards")
70+
logger.Debugf("Error while decoding dashboard objects: %+v", err)
6771
}
72+
result = append(result, []byte(o.String())...)
6873
}
74+
return result
6975
}
7076

7177
func decodeValue(data common.MapStr, key string) error {

libbeat/dashboards/modify_json.go

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -181,40 +181,49 @@ func ReplaceIndexInDashboardObject(index string, content []byte) []byte {
181181
line, err := r.ReadBytes('\n')
182182
if err != nil {
183183
if err == io.EOF {
184-
return result
184+
return append(result, replaceInNDJSON(logger, index, line)...)
185185
}
186186
logger.Error("Error reading bytes from raw dashboard object: %+v", err)
187187
return content
188188
}
189-
objectMap := make(map[string]interface{}, 0)
190-
err = json.Unmarshal(line, &objectMap)
191-
if err != nil {
192-
result = append(result, append(line, newline...)...)
193-
continue
194-
}
189+
result = append(result, replaceInNDJSON(logger, index, line)...)
190+
}
191+
}
195192

196-
attributes, ok := objectMap["attributes"].(map[string]interface{})
197-
if !ok {
198-
result = append(result, append(line, newline...)...)
199-
continue
200-
}
193+
func replaceInNDJSON(logger *logp.Logger, index string, line []byte) []byte {
194+
if len(line) == 0 {
195+
return line
196+
}
201197

202-
if kibanaSavedObject, ok := attributes["kibanaSavedObjectMeta"].(map[string]interface{}); ok {
203-
attributes["kibanaSavedObjectMeta"] = ReplaceIndexInSavedObject(logger, index, kibanaSavedObject)
204-
}
198+
objectMap := make(map[string]interface{}, 0)
199+
err := json.Unmarshal(line, &objectMap)
200+
if err != nil {
201+
logger.Errorf("Failed to convert bytes to map[string]interface: %+v", err)
202+
return line
203+
}
205204

206-
if visState, ok := attributes["visState"].(string); ok {
207-
attributes["visState"] = ReplaceIndexInVisState(logger, index, visState)
208-
}
205+
attributes, ok := objectMap["attributes"].(map[string]interface{})
206+
if !ok {
207+
logger.Errorf("Object does not have attributes key")
208+
return line
209+
}
209210

210-
b, err := json.Marshal(objectMap)
211-
if err != nil {
212-
logger.Error("Error marshaling modified dashboard: %+v", err)
213-
result = append(result, append(line, newline...)...)
214-
}
211+
if kibanaSavedObject, ok := attributes["kibanaSavedObjectMeta"].(map[string]interface{}); ok {
212+
attributes["kibanaSavedObjectMeta"] = ReplaceIndexInSavedObject(logger, index, kibanaSavedObject)
213+
}
215214

216-
result = append(result, append(b, newline...)...)
215+
if visState, ok := attributes["visState"].(string); ok {
216+
attributes["visState"] = ReplaceIndexInVisState(logger, index, visState)
217217
}
218+
219+
b, err := json.Marshal(objectMap)
220+
if err != nil {
221+
logger.Error("Error marshaling modified dashboard: %+v", err)
222+
return line
223+
}
224+
225+
return append(b, newline...)
226+
218227
}
219228

220229
// ReplaceStringInDashboard replaces a string field in a dashboard

libbeat/kibana/dashboard.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,37 @@ func RemoveIndexPattern(data []byte) ([]byte, error) {
3535
line, err := r.ReadBytes('\n')
3636
if err != nil {
3737
if err == io.EOF {
38-
return result, nil
38+
res, removeErr := removeLineIfIndexPattern(line)
39+
if removeErr != nil {
40+
return data, removeErr
41+
}
42+
return append(result, res...), nil
3943
}
4044
return data, err
4145
}
4246

43-
var r common.MapStr
44-
// Full struct need to not loose any data
45-
err = json.Unmarshal(line, &r)
47+
res, err := removeLineIfIndexPattern(line)
4648
if err != nil {
47-
return nil, err
48-
}
49-
v, err := r.GetValue("type")
50-
if err != nil {
51-
return nil, fmt.Errorf("type key not found or not string")
52-
}
53-
if v != "index-pattern" {
54-
result = append(result, line...)
49+
return data, err
5550
}
51+
result = append(result, res...)
52+
}
53+
}
54+
55+
func removeLineIfIndexPattern(line []byte) ([]byte, error) {
56+
var r common.MapStr
57+
// Full struct need to not loose any data
58+
err := json.Unmarshal(line, &r)
59+
if err != nil {
60+
return nil, err
61+
}
62+
v, err := r.GetValue("type")
63+
if err != nil {
64+
return nil, fmt.Errorf("type key not found or not string")
5665
}
66+
if v != "index-pattern" {
67+
return line, nil
68+
}
69+
70+
return nil, nil
5771
}

0 commit comments

Comments
 (0)