|
@@ -2,40 +2,79 @@ package es |
|
@@ -2,40 +2,79 @@ package es |
|
2
|
|
2
|
|
|
3
|
import (
|
3
|
import (
|
|
4
|
"context"
|
4
|
"context"
|
|
|
|
5
|
+ "fmt"
|
|
|
|
6
|
+ "strings"
|
|
5
|
"sync"
|
7
|
"sync"
|
|
6
|
"time"
|
8
|
"time"
|
|
7
|
|
9
|
|
|
8
|
"github.com/olivere/elastic"
|
10
|
"github.com/olivere/elastic"
|
|
9
|
"github.com/tal-tech/go-zero/core/fx"
|
11
|
"github.com/tal-tech/go-zero/core/fx"
|
|
|
|
12
|
+ "github.com/tal-tech/go-zero/core/lang"
|
|
10
|
"github.com/tal-tech/go-zero/core/logx"
|
13
|
"github.com/tal-tech/go-zero/core/logx"
|
|
11
|
"github.com/tal-tech/go-zero/core/syncx"
|
14
|
"github.com/tal-tech/go-zero/core/syncx"
|
|
12
|
)
|
15
|
)
|
|
13
|
|
16
|
|
|
14
|
-const sharedCallsKey = "ensureIndex"
|
17
|
+const (
|
|
|
|
18
|
+ sharedCallsKey = "ensureIndex"
|
|
|
|
19
|
+ timestampFormat = "2006-01-02T15:04:05.000Z"
|
|
|
|
20
|
+ timestampKey = "@timestamp"
|
|
|
|
21
|
+)
|
|
|
|
22
|
+
|
|
|
|
23
|
+const (
|
|
|
|
24
|
+ stateNormal = iota
|
|
|
|
25
|
+ stateWrap
|
|
|
|
26
|
+ stateDot
|
|
|
|
27
|
+)
|
|
15
|
|
28
|
|
|
16
|
type (
|
29
|
type (
|
|
17
|
- IndexFormat func(time.Time) string
|
30
|
+ IndexFormat func(m map[string]interface{}) string
|
|
18
|
IndexFunc func() string
|
31
|
IndexFunc func() string
|
|
19
|
|
32
|
|
|
20
|
Index struct {
|
33
|
Index struct {
|
|
21
|
client *elastic.Client
|
34
|
client *elastic.Client
|
|
22
|
indexFormat IndexFormat
|
35
|
indexFormat IndexFormat
|
|
23
|
- index string
|
36
|
+ indices map[string]lang.PlaceholderType
|
|
24
|
lock sync.RWMutex
|
37
|
lock sync.RWMutex
|
|
25
|
sharedCalls syncx.SharedCalls
|
38
|
sharedCalls syncx.SharedCalls
|
|
26
|
}
|
39
|
}
|
|
27
|
)
|
40
|
)
|
|
28
|
|
41
|
|
|
29
|
-func NewIndex(client *elastic.Client, indexFormat IndexFormat) *Index {
|
42
|
+func NewIndex(client *elastic.Client, indexFormat string, loc *time.Location) *Index {
|
|
|
|
43
|
+ var formatter func(map[string]interface{}) string
|
|
|
|
44
|
+ format, attrs := getFormat(indexFormat)
|
|
|
|
45
|
+ if len(attrs) > 0 {
|
|
|
|
46
|
+ formatter = func(m map[string]interface{}) string {
|
|
|
|
47
|
+ var vals []interface{}
|
|
|
|
48
|
+ for _, attr := range attrs {
|
|
|
|
49
|
+ if val, ok := m[attr]; ok {
|
|
|
|
50
|
+ vals = append(vals, val)
|
|
|
|
51
|
+ }
|
|
|
|
52
|
+ }
|
|
|
|
53
|
+ return getTime(m).In(loc).Format(fmt.Sprintf(format, vals...))
|
|
|
|
54
|
+ }
|
|
|
|
55
|
+ } else {
|
|
|
|
56
|
+ formatter = func(m map[string]interface{}) string {
|
|
|
|
57
|
+ return getTime(m).In(loc).Format(format)
|
|
|
|
58
|
+ }
|
|
|
|
59
|
+ }
|
|
|
|
60
|
+
|
|
30
|
return &Index{
|
61
|
return &Index{
|
|
31
|
client: client,
|
62
|
client: client,
|
|
32
|
- indexFormat: indexFormat,
|
63
|
+ indexFormat: formatter,
|
|
|
|
64
|
+ indices: make(map[string]lang.PlaceholderType),
|
|
33
|
sharedCalls: syncx.NewSharedCalls(),
|
65
|
sharedCalls: syncx.NewSharedCalls(),
|
|
34
|
}
|
66
|
}
|
|
35
|
}
|
67
|
}
|
|
36
|
|
68
|
|
|
37
|
-func (idx *Index) GetIndex(t time.Time) string {
|
|
|
|
38
|
- index := idx.indexFormat(t)
|
69
|
+func (idx *Index) GetIndex(m map[string]interface{}) string {
|
|
|
|
70
|
+ index := idx.indexFormat(m)
|
|
|
|
71
|
+ idx.lock.RLock()
|
|
|
|
72
|
+ if _, ok := idx.indices[index]; ok {
|
|
|
|
73
|
+ idx.lock.RUnlock()
|
|
|
|
74
|
+ return index
|
|
|
|
75
|
+ }
|
|
|
|
76
|
+
|
|
|
|
77
|
+ idx.lock.RUnlock()
|
|
39
|
if err := idx.ensureIndex(index); err != nil {
|
78
|
if err := idx.ensureIndex(index); err != nil {
|
|
40
|
logx.Error(err)
|
79
|
logx.Error(err)
|
|
41
|
}
|
80
|
}
|
|
@@ -43,17 +82,14 @@ func (idx *Index) GetIndex(t time.Time) string { |
|
@@ -43,17 +82,14 @@ func (idx *Index) GetIndex(t time.Time) string { |
|
43
|
}
|
82
|
}
|
|
44
|
|
83
|
|
|
45
|
func (idx *Index) ensureIndex(index string) error {
|
84
|
func (idx *Index) ensureIndex(index string) error {
|
|
46
|
- idx.lock.RLock()
|
|
|
|
47
|
- if index == idx.index {
|
|
|
|
48
|
- idx.lock.RUnlock()
|
|
|
|
49
|
- return nil
|
|
|
|
50
|
- }
|
|
|
|
51
|
- idx.lock.RUnlock()
|
|
|
|
52
|
-
|
|
|
|
53
|
_, err := idx.sharedCalls.Do(sharedCallsKey, func() (i interface{}, err error) {
|
85
|
_, err := idx.sharedCalls.Do(sharedCallsKey, func() (i interface{}, err error) {
|
|
54
|
idx.lock.Lock()
|
86
|
idx.lock.Lock()
|
|
55
|
defer idx.lock.Unlock()
|
87
|
defer idx.lock.Unlock()
|
|
56
|
|
88
|
|
|
|
|
89
|
+ if _, ok := idx.indices[index]; ok {
|
|
|
|
90
|
+ return nil, nil
|
|
|
|
91
|
+ }
|
|
|
|
92
|
+
|
|
57
|
existsService := elastic.NewIndicesExistsService(idx.client)
|
93
|
existsService := elastic.NewIndicesExistsService(idx.client)
|
|
58
|
existsService.Index([]string{index})
|
94
|
existsService.Index([]string{index})
|
|
59
|
exist, err := existsService.Do(context.Background())
|
95
|
exist, err := existsService.Do(context.Background())
|
|
@@ -61,7 +97,6 @@ func (idx *Index) ensureIndex(index string) error { |
|
@@ -61,7 +97,6 @@ func (idx *Index) ensureIndex(index string) error { |
|
61
|
return nil, err
|
97
|
return nil, err
|
|
62
|
}
|
98
|
}
|
|
63
|
if exist {
|
99
|
if exist {
|
|
64
|
- idx.index = index
|
|
|
|
65
|
return nil, nil
|
100
|
return nil, nil
|
|
66
|
}
|
101
|
}
|
|
67
|
|
102
|
|
|
@@ -74,8 +109,53 @@ func (idx *Index) ensureIndex(index string) error { |
|
@@ -74,8 +109,53 @@ func (idx *Index) ensureIndex(index string) error { |
|
74
|
return nil, err
|
109
|
return nil, err
|
|
75
|
}
|
110
|
}
|
|
76
|
|
111
|
|
|
77
|
- idx.index = index
|
112
|
+ idx.indices[index] = lang.Placeholder
|
|
78
|
return nil, nil
|
113
|
return nil, nil
|
|
79
|
})
|
114
|
})
|
|
80
|
return err
|
115
|
return err
|
|
81
|
}
|
116
|
}
|
|
|
|
117
|
+
|
|
|
|
118
|
+func getTime(m map[string]interface{}) time.Time {
|
|
|
|
119
|
+ if ti, ok := m[timestampKey]; ok {
|
|
|
|
120
|
+ if ts, ok := ti.(string); ok {
|
|
|
|
121
|
+ if t, err := time.Parse(timestampFormat, ts); err == nil {
|
|
|
|
122
|
+ return t
|
|
|
|
123
|
+ }
|
|
|
|
124
|
+ }
|
|
|
|
125
|
+ }
|
|
|
|
126
|
+
|
|
|
|
127
|
+ return time.Now()
|
|
|
|
128
|
+}
|
|
|
|
129
|
+
|
|
|
|
130
|
+func getFormat(indexFormat string) (format string, attrs []string) {
|
|
|
|
131
|
+ var state = stateNormal
|
|
|
|
132
|
+ var builder strings.Builder
|
|
|
|
133
|
+ var keyBuf strings.Builder
|
|
|
|
134
|
+ for _, ch := range indexFormat {
|
|
|
|
135
|
+ switch ch {
|
|
|
|
136
|
+ case '{':
|
|
|
|
137
|
+ state = stateWrap
|
|
|
|
138
|
+ case '.':
|
|
|
|
139
|
+ if state == stateWrap {
|
|
|
|
140
|
+ state = stateDot
|
|
|
|
141
|
+ } else {
|
|
|
|
142
|
+ builder.WriteRune(ch)
|
|
|
|
143
|
+ }
|
|
|
|
144
|
+ case '}':
|
|
|
|
145
|
+ state = stateNormal
|
|
|
|
146
|
+ if keyBuf.Len() > 0 {
|
|
|
|
147
|
+ attrs = append(attrs, keyBuf.String())
|
|
|
|
148
|
+ builder.WriteString("%s")
|
|
|
|
149
|
+ }
|
|
|
|
150
|
+ default:
|
|
|
|
151
|
+ if state == stateDot {
|
|
|
|
152
|
+ keyBuf.WriteRune(ch)
|
|
|
|
153
|
+ } else {
|
|
|
|
154
|
+ builder.WriteRune(ch)
|
|
|
|
155
|
+ }
|
|
|
|
156
|
+ }
|
|
|
|
157
|
+ }
|
|
|
|
158
|
+
|
|
|
|
159
|
+ format = builder.String()
|
|
|
|
160
|
+ return
|
|
|
|
161
|
+} |