Skip to content

Commit 450e8a4

Browse files
committed
First
Remove idea Create README.md Add recursive parse Add refactored
1 parent 8a584bf commit 450e8a4

File tree

8 files changed

+51
-285
lines changed

8 files changed

+51
-285
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

.idea/libraries/R_User_Library.xml

-6
This file was deleted.

.idea/misc.xml

-6
This file was deleted.

.idea/modules.xml

-8
This file was deleted.

.idea/parsefield.iml

-11
This file was deleted.

.idea/workspace.xml

-247
This file was deleted.

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# parsefields
2+
Tools for parse JSON-like logs for collecting unique fields
3+
4+
## Deploy:
5+
6+
```
7+
docker build . -t parsefield
8+
docker run -d -p 8000:8000 parsefield
9+
```
10+
## Usage:
11+
12+
Example:
13+
14+
### Push new log for parse
15+
16+
```
17+
curl -X POST -d '{"test4": "calc.exe", "process_path":"C:\\windows\\system32"}' 127.0.0.1:8000/v1/json/
18+
```
19+
20+
### Check unique fields
21+
22+
```
23+
curl 127.0.0.1:8000/v1/fields/
24+
```

parse.go

+26-7
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,35 @@ import (
66
"fmt"
77
)
88

9-
func (c *Config) parse(body []byte) {
9+
const separator = "/"
10+
11+
func (c *Config) check (key string, value interface{}) {
12+
//fmt.Println(key, value)
13+
if _, ok := c.Fields.Load(key); !ok {
14+
c.Fields.Store(key, 1)
15+
fmt.Printf("New field: %s \n", key)
16+
}
17+
}
18+
19+
func (c *Config) deep (b map[string]interface{}, prefix string) {
20+
for key, value := range b {
21+
if b, ok := value.(map[string]interface{}); ok {
22+
c.deep(b,fmt.Sprintf("%s%s%s",prefix,separator,key))
23+
continue
24+
}
25+
c.check(fmt.Sprintf("%s%s%s",prefix,separator,key),value)
26+
}
27+
}
28+
29+
func (c *Config) parse (body []byte) {
1030
data := map[string]interface{}{}
1131
dec := json.NewDecoder(bytes.NewBuffer(body))
1232
dec.Decode(&data)
13-
for key, _ := range data {
14-
if _, ok := c.Fields.Load(key); ok {
33+
for key, value := range data {
34+
if b, ok := value.(map[string]interface{}); ok {
35+
c.deep(b,key)
1536
continue
1637
}
17-
c.Fields.Store(key, 1)
18-
fmt.Printf("New field: %s \n", key)
19-
38+
c.check(key,value)
39+
}
2040
}
21-
}

0 commit comments

Comments
 (0)