-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaffs.go
653 lines (633 loc) · 16.7 KB
/
affs.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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
package dads
import (
"database/sql"
"fmt"
"net/url"
"strings"
"sync"
"time"
)
const (
// MultiOrgNames - suffix for multiple orgs affiliation data
MultiOrgNames = "_multi_org_names"
)
var (
// AffsFields - all properties added by affiliations (excluding multi org name)
// AffsFields = []string{"_id", "_uuid", "_name", "_user_name", "_domain", "_gender", "_gender_acc", "_org_name", "_bot"}
AffsFields = []string{"_id", "_uuid", "_name", "_user_name", "_domain", "_org_name", "_bot"}
// RequiredAffsFields - required affs fields
RequiredAffsFields = []string{"_org_name", "_name", "_user_name"}
identityCache = map[string][2]interface{}{}
identityCacheMtx *sync.RWMutex
rollsCache = map[string][]string{}
rollsCacheMtx *sync.RWMutex
i2uCache = map[string]interface{}{}
i2uCacheMtx *sync.RWMutex
)
// EmptyAffsItem - return empty affiliations item for a given role
func EmptyAffsItem(role string, undef bool) map[string]interface{} {
emp := ""
if undef {
emp = "-- UNDEFINED --"
// panic("track empty")
}
return map[string]interface{}{
role + "_id": emp,
role + "_uuid": emp,
role + "_name": emp,
role + "_user_name": emp,
role + "_domain": emp,
//role + "_gender": emp,
//role + "_gender_acc": nil,
role + "_org_name": emp,
role + "_bot": false,
role + MultiOrgNames: []interface{}{},
}
}
// IdentityAffsDomain -return domain for given identity using email if specified
func IdentityAffsDomain(identity map[string]interface{}) (domain interface{}, email string) {
var ok bool
email, ok = identity["email"].(string)
if ok {
ary := strings.Split(email, "@")
if len(ary) > 1 {
domain = strings.TrimSpace(strings.Join(ary[1:], "@"))
}
}
return
}
// FindObject - fetch given fields from object (identities, profiles, uidentities etc.) having key=id
// Assuming that given object has an unique key to gte it
func FindObject(ctx *Ctx, object, key, id string, fields []string) (obj map[string]interface{}, err error) {
var rows *sql.Rows
rows, err = QuerySQL(ctx, nil, fmt.Sprintf("select %s from %s where %s = ? limit 1", strings.Join(fields, ", "), object, key), id)
if err != nil {
return
}
for rows.Next() {
obj = make(map[string]interface{})
data := make([]interface{}, len(fields))
for i := range data {
data[i] = new(interface{})
}
err = rows.Scan(data...)
if err != nil {
return
}
for i, val := range data {
v := *val.(*interface{})
if v == nil {
obj[fields[i]] = v
continue
}
switch cV := v.(type) {
case []byte:
obj[fields[i]] = string(cV)
default:
obj[fields[i]] = cV
}
}
break
}
err = rows.Err()
if err != nil {
return
}
err = rows.Close()
return
}
// GetIdentityUUID - identity's UUID for a given ID
// uses internal cache
func GetIdentityUUID(ctx *Ctx, ds DS, id string) (uuid interface{}) {
if MT {
i2uCacheMtx.RLock()
}
uuid, ok := i2uCache[id]
if MT {
i2uCacheMtx.RUnlock()
}
if ok {
return
}
defer func() {
if MT {
i2uCacheMtx.Lock()
}
i2uCache[id] = uuid
if MT {
i2uCacheMtx.Unlock()
}
}()
i, err := FindObject(ctx, "identities", "id", id, []string{"uuid"})
if err != nil || i == nil {
return
}
uuid = i["uuid"]
return
}
// AffsIdentityIDs - returns affiliations identity id, uuid data
// uses internal cache
func AffsIdentityIDs(ctx *Ctx, ds DS, identity map[string]interface{}) (ids [2]interface{}) {
email, _ := identity["email"]
name, _ := identity["name"]
username, _ := identity["username"]
sEmail, _ := email.(string)
sName, _ := name.(string)
sUsername, _ := username.(string)
sName, sUsername = PostprocessNameUsername(sName, sUsername, sEmail)
if sName != Nil && sName != "" && sName != name {
name = sName
}
if sUsername != Nil && sUsername != "" && sUsername != username {
username = sUsername
}
if email == nil && name == nil && username == nil {
return
}
sEmail, okE := email.(string)
sName, okN := name.(string)
sUsername, okU := username.(string)
k := sEmail + ":" + sName + ":" + sUsername
if MT {
identityCacheMtx.RLock()
}
ids, ok := identityCache[k]
if MT {
identityCacheMtx.RUnlock()
}
if ok {
return
}
defer func() {
if MT {
identityCacheMtx.Lock()
}
identityCache[k] = ids
if MT {
identityCacheMtx.Unlock()
}
}()
if !okE {
sEmail = Nil
}
if !okN {
sName = Nil
}
if !okU {
sUsername = Nil
}
source := ds.Name()
id := UUIDAffs(ctx, source, sEmail, sName, sUsername)
if id == "" {
return
}
identityFound, err := FindObject(ctx, "identities", "id", id, []string{"id", "uuid"})
if err != nil || identityFound == nil {
ids[0] = id
ids[1] = id
return
}
ids[0] = identityFound["id"]
ids[1] = identityFound["uuid"]
return
}
// QueryToStringArray - execute SQL query returning multiple rows each containitg a single string column
func QueryToStringArray(ctx *Ctx, query string, args ...interface{}) (res []string) {
var (
rows *sql.Rows
err error
)
rows, err = QuerySQL(ctx, nil, query, args...)
if err != nil {
return
}
var item string
for rows.Next() {
err = rows.Scan(&item)
if err != nil {
return
}
res = append(res, item)
}
err = rows.Err()
if err != nil {
return
}
_ = rows.Close()
return
}
// QueryToStringIntArrays - execute SQL query returning multiple rows each containitg (string,int64)
func QueryToStringIntArrays(ctx *Ctx, query string, args ...interface{}) (sa []string, ia []int64) {
var (
rows *sql.Rows
err error
)
rows, err = QuerySQL(ctx, nil, query, args...)
if err != nil {
return
}
var (
s string
i int64
)
for rows.Next() {
err = rows.Scan(&s, &i)
if err != nil {
return
}
sa = append(sa, s)
ia = append(ia, i)
}
err = rows.Err()
if err != nil {
return
}
_ = rows.Close()
return
}
// GetEnrollments - returns enrollments for a given uuid in a given date, possibly multiple
// uses cache with date resolution (pslug,uuid,dt.YYYYMMDD)
func GetEnrollments(ctx *Ctx, ds DS, uuid string, dt time.Time, single bool) (orgs []string, e error) {
pSlug := ctx.ProjectSlug
sSep := "m"
if single {
sSep = "s"
}
k := pSlug + uuid + sSep + ToYMDDate(dt)
if MT {
rollsCacheMtx.RLock()
}
orgs, ok := rollsCache[k]
if MT {
rollsCacheMtx.RUnlock()
}
if ok {
return
}
if pSlug == "" {
pSlug = "(empty)"
}
pSlug = url.QueryEscape(pSlug)
api := "single"
if !single {
api = "multi"
}
sdt := url.QueryEscape(ToYMDTHMSZDate(dt))
data, err := ExecuteAffiliationsAPICall(ctx, "GET", fmt.Sprintf("/v1/affiliation/%s/%s/%s/%s", pSlug, api, uuid, sdt), true)
if err != nil {
Printf("GetEnrollments(%s,%s,%s,%s) error: %v\n", pSlug, api, uuid, sdt, err)
e = err
return
}
defer func() {
if MT {
rollsCacheMtx.Lock()
}
rollsCache[k] = orgs
if MT {
rollsCacheMtx.Unlock()
}
}()
if single {
orgs = []string{data["org"].(string)}
return
}
orgsI, _ := data["orgs"].([]interface{})
for _, orgI := range orgsI {
orgs = append(orgs, orgI.(string))
}
return
}
// GetEnrollmentsBoth - returns org name(s) for given uuid and name
// returns data returned by bot GetEnrollmentsSingle and GetEnrollmentsMulti
// by using a single HTTP request when both were not yet called for a given key
func GetEnrollmentsBoth(ctx *Ctx, ds DS, uuid string, dt time.Time) (org string, orgs []string, e error) {
pSlug := ctx.ProjectSlug
kS := pSlug + uuid + "s" + ToYMDDate(dt)
kM := pSlug + uuid + "m" + ToYMDDate(dt)
if MT {
rollsCacheMtx.RLock()
}
orgsS, okS := rollsCache[kS]
orgsM, okM := rollsCache[kM]
if MT {
rollsCacheMtx.RUnlock()
}
if okS {
if len(orgsS) == 0 {
org = Unknown
} else {
org = orgsS[0]
}
}
if okM {
orgs = orgsM
if len(orgs) == 0 {
orgs = append(orgs, Unknown)
}
}
if okS && okM {
//fmt.Printf("cached %s,%s -> %s,%v\n", kS, kM, org, orgs)
return
}
if okS && !okM {
orgs, e = GetEnrollmentsMulti(ctx, ds, uuid, dt)
return
}
if !okS && okM {
org, e = GetEnrollmentsSingle(ctx, ds, uuid, dt)
return
}
if pSlug == "" {
pSlug = "(empty)"
}
//fmt.Printf("missed %s,%s -> %s,%v\n", kS, kM, org, orgs)
pSlug = url.QueryEscape(pSlug)
sdt := url.QueryEscape(ToYMDTHMSZDate(dt))
data, err := ExecuteAffiliationsAPICall(ctx, "GET", fmt.Sprintf("/v1/affiliation/%s/both/%s/%s", pSlug, uuid, sdt), true)
if err != nil {
Printf("GetEnrollmentsBoth(%s,%s,%s) error: %v\n", pSlug, uuid, sdt, err)
e = err
return
}
defer func() {
if MT {
rollsCacheMtx.Lock()
}
rollsCache[kS] = orgsS
rollsCache[kM] = orgsM
if MT {
rollsCacheMtx.Unlock()
}
}()
org, _ = data["org"].(string)
orgsS = []string{org}
orgsI, _ := data["orgs"].([]interface{})
for _, orgI := range orgsI {
orgsM = append(orgsM, orgI.(string))
}
orgs = orgsM
return
}
// GetEnrollmentsSingle - returns org name (or Unknown) for given uuid and date
func GetEnrollmentsSingle(ctx *Ctx, ds DS, uuid string, dt time.Time) (org string, e error) {
var orgs []string
orgs, e = GetEnrollments(ctx, ds, uuid, dt, true)
if len(orgs) == 0 {
org = Unknown
return
}
org = orgs[0]
return
}
// GetEnrollmentsMulti - returns org name(s) for given uuid and name
// Returns 1 or more organizations (all that matches the current date)
// If none matches it returns array [Unknown]
func GetEnrollmentsMulti(ctx *Ctx, ds DS, uuid string, dt time.Time) (orgs []string, e error) {
orgs, e = GetEnrollments(ctx, ds, uuid, dt, false)
if len(orgs) == 0 {
orgs = append(orgs, Unknown)
}
return
}
// CopyAffsRoleData - copy affiliations fields from source role to dest role
func CopyAffsRoleData(dst, src map[string]interface{}, dstRole, srcRole string) {
for _, suff := range AffsFields {
dst[dstRole+suff], _ = Dig(src, []string{srcRole + suff}, false, true)
}
dst[dstRole+MultiOrgNames], _ = Dig(src, []string{srcRole + MultiOrgNames}, false, true)
}
// RedactEmail - possibly redact email from "in"
// If in contains @, replace part after last "@" with suff
// If in doesn't contain "@" then return it or (if forceSuff is set) return in + suff
func RedactEmail(in, suff string, forceSuff bool) string {
ary := strings.Split(strings.Trim(strings.TrimSpace(in), "@"), "@")
n := len(ary)
if n <= 1 {
if forceSuff {
return in + suff
}
return in
}
return strings.TrimSpace(strings.Join(ary[:n-1], "@")) + suff
}
// PostprocessNameUsername - check name field, if it is empty then copy from email (if not empty) or username (if not empty)
// Then check name and username - it cannot contain email addess, if it does - replace a@domain with a-MISSING-NAME
func PostprocessNameUsername(name, username, email string) (outName, outUsername string) {
defer func() {
outName = name
outUsername = username
}()
copiedName := false
if name == "" || name == Nil {
if email != "" && email != Nil {
name = RedactEmail(email, MissingName, true)
copiedName = true
}
}
if name == "" || name == Nil {
if username != "" && username != Nil {
name = RedactEmail(username, MissingName, true)
copiedName = true
}
}
if !copiedName && name != "" && name != Nil {
name = RedactEmail(name, RedactedEmail, false)
}
if username != "" && username != Nil {
username = RedactEmail(username, RedactedEmail, false)
}
return
}
// PostprocessFields - check name field, if it is empty then copy from email (if not empty) or username (if not empty)
// Then check name and username - it cannot contain email addess, if it does - replace a@domain with a-MISSING-NAME
func PostprocessFields(outItem map[string]interface{}, role, email string) {
name, _ := outItem[role+"_name"].(string)
username, _ := outItem[role+"_user_name"].(string)
outItem[role+"_name"], outItem[role+"_user_name"] = PostprocessNameUsername(name, username, email)
}
// IdentityAffsData - add affiliations related data
// identity - full identity
// aid identity ID value (which is uuid), for example from "author_id", "creator_id" etc.
// either identity or aid must be specified
func IdentityAffsData(ctx *Ctx, ds DS, identity map[string]interface{}, aid interface{}, dt time.Time, role string) (outItem map[string]interface{}, empty bool, e error) {
outItem = EmptyAffsItem(role, false)
if ctx.NoAffiliation {
empty = true
return
}
var (
uuid interface{}
email string
)
if identity != nil {
ids := AffsIdentityIDs(ctx, ds, identity)
outItem[role+"_id"] = ids[0]
outItem[role+"_uuid"] = ids[1]
name, _ := identity["name"]
sName, _ := name.(string)
if name == nil || sName == Nil {
outItem[role+"_name"] = ""
} else {
outItem[role+"_name"] = name
}
username, _ := identity["username"]
sUsername, _ := username.(string)
if username == nil || sUsername == Nil {
outItem[role+"_user_name"] = ""
} else {
outItem[role+"_user_name"] = username
}
outItem[role+"_domain"], email = IdentityAffsDomain(identity)
uuid = ids[1]
}
if aid != nil {
outItem[role+"_id"] = aid
uuid = GetIdentityUUID(ctx, ds, aid.(string))
outItem[role+"_uuid"] = uuid
}
suuid, _ := uuid.(string)
if uuid == nil || suuid == "" {
outItem = EmptyAffsItem(role, true)
empty = true
return
}
// profile, err := FindObject(ctx, "profiles", "uuid", suuid, []string{"name", "email", "gender", "gender_acc", "is_bot"})
profile, err := FindObject(ctx, "profiles", "uuid", suuid, []string{"name", "email", "is_bot"})
isBot := 0
if aid != nil && profile == nil {
Printf("warning cannot find profile for identity id %v\n", aid)
}
if err == nil && profile != nil {
pName, _ := profile["name"]
if pName != nil {
outItem[role+"_name"] = pName
}
iEmail, _ := profile["email"]
if iEmail != nil {
email, _ = iEmail.(string)
ary := strings.Split(email, "@")
if len(ary) > 1 {
outItem[role+"_domain"] = strings.TrimSpace(strings.Join(ary[1:], "@"))
}
}
/*
gender, _ := profile["gender"]
if gender != nil {
outItem[role+"_gender"] = gender
} else {
outItem[role+"_gender"] = Unknown
}
genderAcc, _ := profile["gender_acc"]
if genderAcc != nil {
outItem[role+"_gender_acc"] = genderAcc
} else {
outItem[role+"_gender_acc"] = nil
}
*/
bot, ok := profile["is_bot"].(int64)
if ok && bot > 0 {
isBot = 1
}
}
if err != nil {
e = err
empty = true
return
}
/*
gender, ok := outItem[role+"_gender"]
if !ok || gender == nil {
outItem[role+"_gender"] = Unknown
// outItem[role+"_gender_acc"] = 0
outItem[role+"_gender_acc"] = nil
}
*/
if isBot == 0 {
outItem[role+"_bot"] = false
} else {
outItem[role+"_bot"] = true
}
//outItem[role+"_org_name"], e = GetEnrollmentsSingle(ctx, ds, suuid, dt)
//outItem[role+MultiOrgNames], e = GetEnrollmentsMulti(ctx, ds, suuid, dt)
outItem[role+"_org_name"], outItem[role+MultiOrgNames], e = GetEnrollmentsBoth(ctx, ds, suuid, dt)
PostprocessFields(outItem, role, email)
if outItem[role+"_org_name"] == "" {
Printf("warning empty %s_org_name for identity %+v aid %+v dt %+v err %+v outItem %+v\n", role, identity, aid, dt, e, outItem)
}
if outItem[role+"_name"] == "" {
Printf("warning empty %s_name for identity %+v aid %+v dt %+v err %+v outItem %+v\n", role, identity, aid, dt, e, outItem)
}
return
}
// AffsDataForRoles - return affs data for given roles
func AffsDataForRoles(ctx *Ctx, ds DS, rich map[string]interface{}, roles []string) (data map[string]interface{}, e error) {
/*
defer func() {
Printf("AffsDataForRoles: %+v --> %+v,%v\n", roles, data, e)
}()
*/
data = make(map[string]interface{})
authorField := ds.RichAuthorField(ctx)
if len(roles) == 0 {
roles = append(roles, authorField)
}
dateField := ds.DateField(ctx)
idt, ok := rich[dateField]
if !ok {
Printf("cannot read %s from %v\n", dateField, DumpKeys(rich))
return
}
date, err := TimeParseInterfaceString(idt)
if err != nil {
Printf("cannot parse date %v\n", idt)
return
}
var idAuthor interface{}
for _, role := range roles {
roleID := role + "_id"
id, ok := Dig(rich, []string{roleID}, false, true)
if !ok || id == nil {
if ctx.Debug > 1 {
Printf("no %s role in %v (or nil), skipping\n", roleID, DumpKeys(rich))
}
continue
}
if role == authorField {
idAuthor = id
}
affsIdentity, empty, er := IdentityAffsData(ctx, ds, nil, id, date, role)
if er != nil {
Printf("AffsDataForRoles: IdentityAffsData error: %v for %s id %v\n", er, role, id)
if ctx.AffsAPIFailFatal {
e = er
return
}
}
if empty {
Printf("no identity affiliation data for %s id %+v\n", role, id)
continue
}
for prop, value := range affsIdentity {
data[prop] = value
}
}
if idAuthor != nil && authorField != Author {
affsIdentity, empty, er := IdentityAffsData(ctx, ds, nil, idAuthor, date, Author)
if er != nil {
Printf("AffsDataForRoles: IdentityAffsData error: %v\n", er)
if ctx.AffsAPIFailFatal {
e = er
return
}
}
if !empty {
for prop, value := range affsIdentity {
data[prop] = value
}
} else {
Printf("no identity affiliation data for author role id %+v\n", idAuthor)
}
}
return
}