-
Notifications
You must be signed in to change notification settings - Fork 4
/
point.go
410 lines (334 loc) · 8.55 KB
/
point.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// Unless explicitly stated otherwise all files in this repository are licensed
// under the MIT License.
// This product includes software developed at Guance Cloud (https://www.guance.com/).
// Copyright 2021-present Guance, Inc.
// Package ptinput impl ppl input interface
package ptinput
import (
"fmt"
"time"
"github.com/GuanceCloud/cliutils/pipeline/ptinput/ipdb"
"github.com/GuanceCloud/cliutils/pipeline/ptinput/plcache"
"github.com/GuanceCloud/cliutils/pipeline/ptinput/plmap"
"github.com/GuanceCloud/cliutils/pipeline/ptinput/ptwindow"
"github.com/GuanceCloud/cliutils/pipeline/ptinput/refertable"
"github.com/GuanceCloud/cliutils/pipeline/ptinput/utils"
"github.com/GuanceCloud/cliutils/point"
"github.com/GuanceCloud/platypus/pkg/ast"
plruntime "github.com/GuanceCloud/platypus/pkg/engine/runtime"
"github.com/spf13/cast"
)
type (
KeyKind uint
PtFlag uint
)
var _ PlInputPt = (*PlPoint)(nil)
type PlInputPt interface {
GetPtName() string
SetPtName(m string)
Get(key string) (any, ast.DType, error)
GetWithIsTag(key string) (any, bool, bool)
Set(key string, value any, dtype ast.DType) error
Delete(key string)
RenameKey(from, to string) error
SetTag(key string, value any, dtype ast.DType) error
PtTime() time.Time
GetAggBuckets() *plmap.AggBuckets
SetAggBuckets(*plmap.AggBuckets)
SetPtWinPool(w *ptwindow.WindowPool)
PtWinRegister(before, after int, k, v []string)
PtWinHit()
CallbackPtWinMove() (result []*point.Point)
AppendSubPoint(PlInputPt)
GetSubPoint() []PlInputPt
Category() point.Category
KeyTime2Time()
MarkDrop(bool)
Dropped() bool
Point() *point.Point
GetIPDB() ipdb.IPdb
SetIPDB(ipdb.IPdb)
GetPlReferTables() refertable.PlReferTables
SetPlReferTables(refertable.PlReferTables)
Tags() map[string]string
Fields() map[string]any
GetCache() *plcache.Cache
SetCache(*plcache.Cache)
}
const (
PtMeasurement PtFlag = iota
PtTag
PtField
PtTFDefaulutOrKeep
PtTime
)
const (
KindPtDefault KeyKind = iota
KindPtTag
)
const Originkey = "message"
type InputWithVarbMapRW interface {
Get(key string) (any, ast.DType, bool)
Set(key string, value any, dtype ast.DType) bool
Delete(key string) bool
}
type InputWithVarbMapR interface {
Get(key string) (any, ast.DType, bool)
}
type InputWithoutVarbMap interface{}
type DoFeedCache func(name, category string, pt *point.Point) error
var DoFeedNOP = func(name, category string, pt *point.Point) error { return nil }
type PlmapManager interface {
// createPtCaheMap(category string, source PtSource) (*fucs.PtCacheMap, bool)
}
type PlPoint struct {
name string
tags map[string]string
fields map[string]any // int, float, bool, string, map, slice, array
time time.Time
aggBuckets *plmap.AggBuckets
ipdb ipdb.IPdb
refTable refertable.PlReferTables
subPlpt []PlInputPt
cache *plcache.Cache
ptWindowPool *ptwindow.WindowPool
winKeyVal [2][]string
ptWindowRegistered bool
drop bool
category point.Category
}
func NewPlPoint(category point.Category, name string,
tags map[string]string, fields map[string]any, ptTime time.Time,
) PlInputPt {
if tags == nil {
tags = map[string]string{}
}
if fields == nil {
fields = map[string]any{}
}
dPt := &PlPoint{
name: name,
tags: tags,
fields: fields,
time: ptTime,
category: category,
}
return dPt
}
func valueDtype(v any) (any, ast.DType) {
switch v := v.(type) {
case int32, int8, int16, int,
uint, uint16, uint32, uint64, uint8:
return cast.ToInt64(v), ast.Int
case int64:
return v, ast.Int
case float32:
return cast.ToFloat64(v), ast.Float
case float64:
return v, ast.Float
case bool:
return v, ast.Bool
case []byte:
return string(v), ast.String
case string:
return v, ast.String
}
// ignore unknown type
return nil, ast.Nil
}
func (pt *PlPoint) GetPtName() string {
return pt.name
}
func (pt *PlPoint) SetPtName(m string) {
pt.name = m
}
func (pt *PlPoint) AppendSubPoint(plpt PlInputPt) {
pt.subPlpt = append(pt.subPlpt, plpt)
}
func (pt *PlPoint) GetSubPoint() []PlInputPt {
return pt.subPlpt
}
func (pt *PlPoint) Category() point.Category {
return pt.category
}
func (pt *PlPoint) Get(key string) (any, ast.DType, error) {
if v, ok := pt.tags[key]; ok {
return v, ast.String, nil
}
if v, ok := pt.fields[key]; ok {
v, dtype := valueDtype(v)
return v, dtype, nil
}
return nil, ast.Invalid, fmt.Errorf("unsupported pt key type")
}
func (pt *PlPoint) GetWithIsTag(key string) (any, bool, bool) {
if v, ok := pt.tags[key]; ok {
return v, true, true
}
if v, ok := pt.fields[key]; ok {
v, _ := valueDtype(v)
return v, false, true
}
return nil, false, false
}
func (pt *PlPoint) Set(key string, value any, dtype ast.DType) error {
if _, ok := pt.tags[key]; ok { // is tag
if dtype == ast.Void || dtype == ast.Invalid {
delete(pt.tags, key)
return nil
}
if v, err := plruntime.Conv2String(value, dtype); err == nil {
pt.tags[key] = v
return nil
} else {
return err
}
} else { // is field
switch dtype { //nolint:exhaustive
case ast.Nil, ast.Void, ast.Invalid:
pt.fields[key] = nil
return nil
case ast.List, ast.Map:
if v, err := plruntime.Conv2String(value, dtype); err == nil {
pt.fields[key] = v
} else {
pt.fields[key] = nil
return nil
}
default:
pt.fields[key] = value
}
}
return nil
}
func (pt *PlPoint) Delete(key string) {
if _, ok := pt.tags[key]; ok {
delete(pt.tags, key)
} else {
delete(pt.fields, key)
}
}
func (pt *PlPoint) RenameKey(from, to string) error {
if v, ok := pt.fields[from]; ok {
pt.fields[to] = v
delete(pt.fields, from)
} else if v, ok := pt.tags[from]; ok {
pt.tags[to] = v
delete(pt.tags, from)
} else {
return fmt.Errorf("key(from) %s not found", from)
}
return nil
}
func (pt *PlPoint) SetTag(key string, value any, dtype ast.DType) error {
delete(pt.fields, key)
if str, err := plruntime.Conv2String(value, dtype); err == nil {
pt.tags[key] = str
return nil
} else {
pt.tags[key] = ""
return err
}
}
func (pt *PlPoint) PtTime() time.Time {
return pt.time
}
func (pt *PlPoint) GetAggBuckets() *plmap.AggBuckets {
return pt.aggBuckets
}
func (pt *PlPoint) SetAggBuckets(buks *plmap.AggBuckets) {
pt.aggBuckets = buks
}
func (pt *PlPoint) SetPlReferTables(refTable refertable.PlReferTables) {
pt.refTable = refTable
}
func (pt *PlPoint) GetPlReferTables() refertable.PlReferTables {
return pt.refTable
}
func (pt *PlPoint) SetPtWinPool(w *ptwindow.WindowPool) {
pt.ptWindowPool = w
}
func (pt *PlPoint) PtWinRegister(before, after int, k, v []string) {
if len(k) != len(v) || len(k) == 0 {
return
}
if pt.ptWindowPool != nil && !pt.ptWindowRegistered {
pt.ptWindowRegistered = true
pt.ptWindowPool.Register(before, after, k, v)
pt.winKeyVal = [2][]string{k, v}
}
}
func (pt *PlPoint) PtWinHit() {
if pt.ptWindowPool != nil && pt.ptWindowRegistered {
if len(pt.winKeyVal[0]) != len(pt.winKeyVal[1]) || len(pt.winKeyVal[0]) == 0 {
return
}
// 不校验 pipeline 中 point_window 函数执行后的 tag 的值的变化
//
if v, ok := pt.ptWindowPool.Get(pt.winKeyVal[0], pt.winKeyVal[1]); ok {
v.Hit()
}
}
}
func (pt *PlPoint) CallbackPtWinMove() (result []*point.Point) {
if pt.ptWindowPool != nil && pt.ptWindowRegistered {
if v, ok := pt.ptWindowPool.Get(pt.winKeyVal[0], pt.winKeyVal[1]); ok {
if pt.Dropped() {
result = v.Move(pt.Point())
} else {
result = v.Move(nil)
}
}
}
return
}
func (pt *PlPoint) SetIPDB(db ipdb.IPdb) {
pt.ipdb = db
}
func (pt *PlPoint) GetIPDB() ipdb.IPdb {
return pt.ipdb
}
func (pt *PlPoint) KeyTime2Time() {
if v, _, err := pt.Get("time"); err == nil {
if nanots, ok := v.(int64); ok {
t := time.Unix(nanots/int64(time.Second),
nanots%int64(time.Second))
if !t.IsZero() {
pt.time = t
}
}
pt.Delete("time")
}
}
func (pt *PlPoint) MarkDrop(drop bool) {
pt.drop = drop
}
func (pt *PlPoint) Dropped() bool {
return pt.drop
}
func (pt *PlPoint) Tags() map[string]string {
return pt.tags
}
func (pt *PlPoint) Fields() map[string]any {
return pt.fields
}
func (pt *PlPoint) Point() *point.Point {
opt := utils.PtCatOption(pt.category)
opt = append(opt, point.WithTime(pt.PtTime()))
fieldsKVS := point.NewTags(pt.tags)
fieldsKVS = append(fieldsKVS, point.NewKVs(pt.fields)...)
return point.NewPointV2(pt.name, fieldsKVS, opt...)
}
func WrapPoint(cat point.Category, pt *point.Point) PlInputPt {
if pt == nil {
return nil
}
return NewPlPoint(cat, pt.Name(),
pt.MapTags(), pt.InfluxFields(), pt.Time())
}
func (pt *PlPoint) GetCache() *plcache.Cache {
return pt.cache
}
func (pt *PlPoint) SetCache(c *plcache.Cache) {
pt.cache = c
}