-
Notifications
You must be signed in to change notification settings - Fork 4
/
dsstub.go
493 lines (453 loc) · 11.9 KB
/
dsstub.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
package dads
import (
"fmt"
"sync"
"time"
)
const (
// StubBackendVersion - backend version
StubBackendVersion = "0.0.0"
)
var (
// StubRawMapping - Stub raw index mapping
StubRawMapping = []byte(``)
// StubRichMapping - Stub rich index mapping
StubRichMapping = []byte(``)
// StubCategories - categories defined for Stub
StubCategories = map[string]struct{}{Stub: {}}
)
// DSStub - DS implementation for stub - does nothing at all, just presents a skeleton code
type DSStub struct {
DS string
MultiOrigin bool // can we store multiple endpoints in a single index?
}
// ParseArgs - parse stub specific environment variables
func (j *DSStub) ParseArgs(ctx *Ctx) (err error) {
// IMPL:
j.DS = Stub
return
}
// Validate - is current DS configuration OK?
func (j *DSStub) Validate(ctx *Ctx) (err error) {
// IMPL:
return
}
// Name - return data source name
func (j *DSStub) Name() string {
return j.DS
}
// Info - return DS configuration in a human readable form
func (j DSStub) Info() string {
return fmt.Sprintf("%+v", j)
}
// CustomFetchRaw - is this datasource using custom fetch raw implementation?
func (j *DSStub) CustomFetchRaw() bool {
return false
}
// FetchRaw - implement fetch raw data for stub datasource
func (j *DSStub) FetchRaw(ctx *Ctx) (err error) {
Printf("%s should use generic FetchRaw()\n", j.DS)
return
}
// CustomEnrich - is this datasource using custom enrich implementation?
func (j *DSStub) CustomEnrich() bool {
return false
}
// Enrich - implement enrich data for stub datasource
func (j *DSStub) Enrich(ctx *Ctx) (err error) {
Printf("%s should use generic Enrich()\n", j.DS)
return
}
// FetchItems - implement enrich data for stub datasource
func (j *DSStub) FetchItems(ctx *Ctx) (err error) {
// IMPL:
var messages [][]byte
// Process messages (possibly in threads)
var (
ch chan error
allMsgs []interface{}
allMsgsMtx *sync.Mutex
escha []chan error
eschaMtx *sync.Mutex
)
thrN := GetThreadsNum(ctx)
if thrN > 1 {
ch = make(chan error)
allMsgsMtx = &sync.Mutex{}
eschaMtx = &sync.Mutex{}
}
nThreads := 0
processMsg := func(c chan error, msg []byte) (wch chan error, e error) {
defer func() {
if c != nil {
c <- e
}
}()
// FIXME: Real data processing here
item := map[string]interface{}{"id": time.Now().UnixNano(), "name": "xyz"}
esItem := j.AddMetadata(ctx, item)
if ctx.Project != "" {
item["project"] = ctx.Project
}
esItem["data"] = item
if allMsgsMtx != nil {
allMsgsMtx.Lock()
}
allMsgs = append(allMsgs, esItem)
nMsgs := len(allMsgs)
if nMsgs >= ctx.ESBulkSize {
sendToElastic := func(c chan error) (ee error) {
defer func() {
if c != nil {
c <- ee
}
}()
ee = SendToElastic(ctx, j, true, UUID, allMsgs)
if ee != nil {
Printf("error %v sending %d messages to ElasticSearch\n", ee, len(allMsgs))
}
allMsgs = []interface{}{}
if allMsgsMtx != nil {
allMsgsMtx.Unlock()
}
return
}
if thrN > 1 {
wch = make(chan error)
go func() {
_ = sendToElastic(wch)
}()
} else {
e = sendToElastic(nil)
if e != nil {
return
}
}
} else {
if allMsgsMtx != nil {
allMsgsMtx.Unlock()
}
}
return
}
if thrN > 1 {
for _, message := range messages {
go func(msg []byte) {
var (
e error
esch chan error
)
esch, e = processMsg(ch, msg)
if e != nil {
Printf("process error: %v\n", e)
return
}
if esch != nil {
if eschaMtx != nil {
eschaMtx.Lock()
}
escha = append(escha, esch)
if eschaMtx != nil {
eschaMtx.Unlock()
}
}
}(message)
nThreads++
if nThreads == thrN {
err = <-ch
if err != nil {
return
}
nThreads--
}
}
for nThreads > 0 {
err = <-ch
nThreads--
if err != nil {
return
}
}
} else {
for _, message := range messages {
_, err = processMsg(nil, message)
if err != nil {
return
}
}
}
if eschaMtx != nil {
eschaMtx.Lock()
}
for _, esch := range escha {
err = <-esch
if err != nil {
if eschaMtx != nil {
eschaMtx.Unlock()
}
return
}
}
if eschaMtx != nil {
eschaMtx.Unlock()
}
nMsgs := len(allMsgs)
if ctx.Debug > 0 {
Printf("%d remaining messages to send to ES\n", nMsgs)
}
if nMsgs > 0 {
err = SendToElastic(ctx, j, true, UUID, allMsgs)
if err != nil {
Printf("Error %v sending %d messages to ES\n", err, len(allMsgs))
}
}
return
}
// SupportDateFrom - does DS support resuming from date?
func (j *DSStub) SupportDateFrom() bool {
// IMPL:
return false
}
// SupportOffsetFrom - does DS support resuming from offset?
func (j *DSStub) SupportOffsetFrom() bool {
// IMPL:
return false
}
// DateField - return date field used to detect where to restart from
func (j *DSStub) DateField(*Ctx) string {
return DefaultDateField
}
// RichIDField - return rich ID field name
func (j *DSStub) RichIDField(*Ctx) string {
// IMPL:
// DefaultIDField - will use rich item's id - so mapping can be 1 raw -> N rich items
// you just need to use different IDs for sub-items
// UUID - will use 1:1 raw-rich mapping
return DefaultIDField
}
// RichAuthorField - return rich author field name
func (j *DSStub) RichAuthorField(*Ctx) string {
return DefaultAuthorField
}
// OffsetField - return offset field used to detect where to restart from
func (j *DSStub) OffsetField(*Ctx) string {
return DefaultOffsetField
}
// OriginField - return origin field used to detect where to restart from
func (j *DSStub) OriginField(ctx *Ctx) string {
if ctx.Tag != "" {
return DefaultTagField
}
return DefaultOriginField
}
// Categories - return a set of configured categories
func (j *DSStub) Categories() map[string]struct{} {
return StubCategories
}
// ResumeNeedsOrigin - is origin field needed when resuming
// Origin should be needed when multiple configurations save to the same index
func (j *DSStub) ResumeNeedsOrigin(ctx *Ctx, raw bool) bool {
return j.MultiOrigin
}
// ResumeNeedsCategory - is category field needed when resuming
// Category should be needed when multiple types of categories save to the same index
// or there are multiple types of documents within the same category
func (j *DSStub) ResumeNeedsCategory(ctx *Ctx, raw bool) bool {
return false
}
// Origin - return current origin
func (j *DSStub) Origin(ctx *Ctx) string {
// IMPL: you must change this, for example to j.URL/j.GroupName or somethign like this
return ctx.Tag
}
// ItemID - return unique identifier for an item
func (j *DSStub) ItemID(item interface{}) string {
// IMPL:
return fmt.Sprintf("%d", time.Now().UnixNano())
}
// AddMetadata - add metadata to the item
func (j *DSStub) AddMetadata(ctx *Ctx, item interface{}) (mItem map[string]interface{}) {
// IMPL:
mItem = make(map[string]interface{})
// Change to unique datasource origin
origin := "stub"
tag := ctx.Tag
if tag == "" {
tag = origin
}
itemID := j.ItemID(item)
updatedOn := j.ItemUpdatedOn(item)
uuid := UUIDNonEmpty(ctx, origin, itemID)
timestamp := time.Now()
mItem["backend_name"] = j.DS
mItem["backend_version"] = StubBackendVersion
mItem["timestamp"] = fmt.Sprintf("%.06f", float64(timestamp.UnixNano())/1.0e9)
mItem[UUID] = uuid
mItem[DefaultOriginField] = origin
mItem[DefaultTagField] = tag
mItem[DefaultOffsetField] = float64(updatedOn.Unix())
mItem["category"] = j.ItemCategory(item)
//mItem["search_fields"] = j.GenSearchFields(ctx, issue, uuid)
//mItem["search_fields"] = make(map[string]interface{})
mItem[DefaultDateField] = ToESDate(updatedOn)
mItem[DefaultTimestampField] = ToESDate(timestamp)
mItem[ProjectSlug] = ctx.ProjectSlug
return
}
// ItemUpdatedOn - return updated on date for an item
func (j *DSStub) ItemUpdatedOn(item interface{}) time.Time {
// IMPL:
return time.Now()
}
// ItemCategory - return unique identifier for an item
func (j *DSStub) ItemCategory(item interface{}) string {
// IMPL:
return fmt.Sprintf("%d", time.Now().UnixNano())
}
// ElasticRawMapping - Raw index mapping definition
func (j *DSStub) ElasticRawMapping() []byte {
return StubRawMapping
}
// ElasticRichMapping - Rich index mapping definition
func (j *DSStub) ElasticRichMapping() []byte {
return StubRichMapping
}
// GetItemIdentities return list of item's identities, each one is [3]string
// (name, username, email) tripples, special value Nil "none" means null
// we use string and not *string which allows nil to allow usage as a map key
func (j *DSStub) GetItemIdentities(ctx *Ctx, doc interface{}) (map[[3]string]struct{}, error) {
// IMPL:
return map[[3]string]struct{}{}, nil
}
// StubEnrichItemsFunc - iterate items and enrich them
// items is a current pack of input items
// docs is a pointer to where extracted identities will be stored
func StubEnrichItemsFunc(ctx *Ctx, ds DS, thrN int, items []interface{}, docs *[]interface{}) (err error) {
// IMPL:
if ctx.Debug > 0 {
Printf("stub enrich items %d/%d func\n", len(items), len(*docs))
}
var (
mtx *sync.RWMutex
ch chan error
)
if thrN > 1 {
mtx = &sync.RWMutex{}
ch = make(chan error)
}
dbConfigured := ctx.AffsDBConfigured()
nThreads := 0
procItem := func(c chan error, idx int) (e error) {
if thrN > 1 {
mtx.RLock()
}
item := items[idx]
if thrN > 1 {
mtx.RUnlock()
}
defer func() {
if c != nil {
c <- e
}
}()
src, ok := item.(map[string]interface{})["_source"]
if !ok {
e = fmt.Errorf("Missing _source in item %+v", DumpKeys(item))
return
}
doc, ok := src.(map[string]interface{})
if !ok {
e = fmt.Errorf("Failed to parse document %+v", doc)
return
}
if 1 == 0 {
Printf("%v\n", dbConfigured)
}
// Actual item enrichment
/*
var rich map[string]interface{}
if thrN > 1 {
mtx.Lock()
}
*docs = append(*docs, rich)
if thrN > 1 {
mtx.Unlock()
}
*/
return
}
if thrN > 1 {
for i := range items {
go func(i int) {
_ = procItem(ch, i)
}(i)
nThreads++
if nThreads == thrN {
err = <-ch
if err != nil {
return
}
nThreads--
}
}
for nThreads > 0 {
err = <-ch
nThreads--
if err != nil {
return
}
}
return
}
for i := range items {
err = procItem(nil, i)
if err != nil {
return
}
}
return
}
// EnrichItems - perform the enrichment
func (j *DSStub) EnrichItems(ctx *Ctx) (err error) {
Printf("enriching items\n")
err = ForEachESItem(ctx, j, true, ESBulkUploadFunc, StubEnrichItemsFunc, nil, true)
return
}
// EnrichItem - return rich item from raw item for a given author type
func (j *DSStub) EnrichItem(ctx *Ctx, item map[string]interface{}, author string, affs bool, extra interface{}) (rich map[string]interface{}, err error) {
// IMPL:
rich = item
return
}
// AffsItems - return affiliations data items for given roles and date
func (j *DSStub) AffsItems(ctx *Ctx, rawItem map[string]interface{}, roles []string, date interface{}) (affsItems map[string]interface{}, err error) {
// IMPL:
return
}
// GetRoleIdentity - return identity data for a given role
func (j *DSStub) GetRoleIdentity(ctx *Ctx, item map[string]interface{}, role string) map[string]interface{} {
// IMPL:
return map[string]interface{}{"name": nil, "username": nil, "email": nil}
}
// AllRoles - return all roles defined for the backend
// roles can be static (always the same) or dynamic (per item)
// second return parameter is static mode (true/false)
// dynamic roles will use item to get its roles
func (j *DSStub) AllRoles(ctx *Ctx, item map[string]interface{}) ([]string, bool) {
// IMPL:
return []string{Author}, true
}
// CalculateTimeToReset - calculate time to reset rate limits based on rate limit value and rate limit reset value
func (j *DSStub) CalculateTimeToReset(ctx *Ctx, rateLimit, rateLimitReset int) (seconds int) {
seconds = rateLimitReset
return
}
// HasIdentities - does this data source support identity data
func (j *DSStub) HasIdentities() bool {
// IMPL
return true
}
// UseDefaultMapping - apply MappingNotAnalyzeString for raw/rich (raw=fals/true) index in this DS?
func (j *DSStub) UseDefaultMapping(ctx *Ctx, raw bool) bool {
return true
}