This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
import_affs.go
1226 lines (1178 loc) · 31.7 KB
/
import_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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"crypto/sha256"
"database/sql"
"fmt"
"os"
"regexp"
"sort"
"strings"
"sync"
"time"
jsoniter "github.com/json-iterator/go"
"github.com/ti-community-infra/devstats/internal/pkg/lib"
yaml "gopkg.in/yaml.v2"
)
// gitHubUsers - list of GitHub user data from cncf/gitdm.
type gitHubUsers []gitHubUser
// gitHubUser - single GitHug user entry from cncf/gitdm `github_users.json` JSON.
type gitHubUser struct {
Login string `json:"login"`
Email string `json:"email"`
Affiliation string `json:"affiliation"`
Source string `json:"source"`
Name string `json:"name"`
CountryID *string `json:"country_id"`
Sex *string `json:"sex"`
Tz *string `json:"tz"`
SexProb *float64 `json:"sex_prob"`
Age *int `json:"age"`
}
// AllAcquisitions contain all company acquisitions data
// Acquisition contains acquired company name regular expression and new company name for it.
type allAcquisitions struct {
Acquisitions [][2]string `yaml:"acquisitions"`
}
// stringSet - set of strings
type stringSet map[string]struct{}
// mapIntSet - this is a map from int to set of string
type mapIntSet map[int]stringSet
// mapStringSet - this is a map from string to Set of strings
type mapStringSet map[string]stringSet
// mapStringIntSet - this is a map from string to map from int to set of string
type mapStringIntSet map[string]mapIntSet
// mapIntArray - this is a map form string to array of ints
type mapIntArray map[string][]int
// mapStringArray - this is a map form string to array of strings
type mapStringArray map[string][]string
// affData - holds single affiliation data
type affData struct {
Login string
Company string
Source string
From time.Time
To time.Time
}
// csData holds country_id, tz, tz_offset, sex, sex_prob, age
type csData struct {
CountryID *string
Sex *string
Tz *string
SexProb *float64
TzOffset *int
Age *int
}
// decode emails with ! instead of @
func emailDecode(line string) string {
re := regexp.MustCompile(`([^\s!]+)!([^\s!]+)`)
return re.ReplaceAllString(line, `$1@$2`)
}
// returns timezone offset in minutes for a given tz string
func tzOffset(db *sql.DB, ctx *lib.Ctx, ptz *string, cache map[string]*int) *int {
if ptz == nil {
return nil
}
tz := *ptz
if tz == "" {
return nil
}
off, ok := cache[tz]
if ok {
return off
}
rows := lib.QuerySQLWithErr(
db,
ctx,
"select extract(epoch from utc_offset) / 60 "+
"from pg_timezone_names where name = "+lib.NValue(1)+
" union select null order by 1 limit 1",
tz,
)
defer func() { lib.FatalOnError(rows.Close()) }()
var offset *int
for rows.Next() {
lib.FatalOnError(rows.Scan(&offset))
}
lib.FatalOnError(rows.Err())
cache[tz] = offset
return offset
}
// Search for given actor using his/her login
// Returns first author found with maximum ID or sets ok=false when not found
func findActor(db *sql.DB, ctx *lib.Ctx, login string, maybeHide func(string) string) (actor lib.Actor, csd csData, ok bool) {
//if login == "kkosaka" {
// defer func() {
// lib.Printf("findActor %s -> (%+v, %+v, %v)\n", login, actor, csd, ok)
// }()
//}
login = maybeHide(login)
rows := lib.QuerySQLWithErr(
db,
ctx,
fmt.Sprintf(
"select id, name, country_id, tz, tz_offset, sex, sex_prob, age from gha_actors where login=%s "+
"union select id, name, country_id, tz, tz_offset, sex, sex_prob, age from gha_actors where lower(login)=%s "+
"order by id desc limit 1",
lib.NValue(1),
lib.NValue(2),
),
login,
strings.ToLower(login),
)
defer func() { lib.FatalOnError(rows.Close()) }()
var name *string
for rows.Next() {
lib.FatalOnError(
rows.Scan(
&actor.ID,
&name,
&csd.CountryID,
&csd.Tz,
&csd.TzOffset,
&csd.Sex,
&csd.SexProb,
&csd.Age,
),
)
actor.Login = login
if name != nil {
actor.Name = *name
}
ok = true
}
lib.FatalOnError(rows.Err())
return
}
// Search for given actor ID(s) using His/Her login
// Return list of actor IDs correlated with that login (downcased) - search deep by lower(login)/id correlations
func findActors(db *sql.DB, ctx *lib.Ctx, login string, maybeHide func(string) string) (actIDs []int, actLogins []string) {
login = maybeHide(login)
//if login == "kkosaka" {
// defer func() {
// lib.Printf("findActors: %s -> (%+v, %+v)\n", login, actIDs, actLogins)
// }()
//}
ids := make(map[int]struct{})
logins := make(map[string]struct{})
logins[login] = struct{}{}
actLogins = append(actLogins, login)
aid := 0
alogin := ""
prevIDs := ""
prevLogins := login
depth := 0
for {
query := "select id from gha_actors where lower(login) in ("
nLogins := len(logins)
args := []interface{}{}
idx := 0
for aLogin := range logins {
idx++
args = append(args, strings.ToLower(aLogin))
query += lib.NValue(idx)
if idx != nLogins {
query += ","
}
}
query += ")"
rows := lib.QuerySQLWithErr(db, ctx, query, args...)
for rows.Next() {
lib.FatalOnError(rows.Scan(&aid))
ids[aid] = struct{}{}
}
if len(ids) == 0 {
return
}
lib.FatalOnError(rows.Err())
lib.FatalOnError(rows.Close())
query = "select login from gha_actors where id in ("
nIDs := len(ids)
args = []interface{}{}
idx = 0
for anID := range ids {
idx++
args = append(args, anID)
query += lib.NValue(idx)
if idx != nIDs {
query += ","
}
}
query += ")"
rows = lib.QuerySQLWithErr(db, ctx, query, args...)
for rows.Next() {
lib.FatalOnError(rows.Scan(&alogin))
alogin = maybeHide(alogin)
logins[alogin] = struct{}{}
}
lib.FatalOnError(rows.Err())
lib.FatalOnError(rows.Close())
currLoginsAry := []string{}
for aLogin := range logins {
currLoginsAry = append(currLoginsAry, aLogin)
}
sort.Strings(currLoginsAry)
currLogins := strings.Join(currLoginsAry, ",")
currIDsAry := []string{}
for aID := range ids {
currIDsAry = append(currIDsAry, fmt.Sprintf("%d", aID))
}
sort.Strings(currIDsAry)
currIDs := strings.Join(currIDsAry, ",")
depth++
if prevLogins == currLogins && prevIDs == currIDs {
break
}
if depth >= 10 {
lib.Printf("Error (non fatal): gone too deep: logins map: %+v, ids map: %+v\n", logins, ids)
lib.Printf("Error (non fatal): gone too deep: Logins: '%s'=='%s', IDs: '%s'=='%s'\n", prevLogins, currLogins, prevIDs, currIDs)
break
}
prevLogins = currLogins
prevIDs = currIDs
}
for aID := range ids {
actIDs = append(actIDs, aID)
}
for aLogin := range logins {
if aLogin == login {
continue
}
actLogins = append(actLogins, aLogin)
}
return
}
// returns first value from stringSet
func firstKey(strMap stringSet) string {
for key := range strMap {
return key
}
return ""
}
// Adds non-existing actor
func addActor(con *sql.DB, ctx *lib.Ctx, login, name string, countryID, sex, tz *string, sexProb *float64, tzOff *int, age *int, maybeHide func(string) string) int {
hlogin := maybeHide(login)
name = maybeHide(name)
aid := lib.HashStrings([]string{login})
lib.ExecSQLWithErr(con, ctx,
"insert into gha_actors(id, login, name, country_id, sex, tz, sex_prob, tz_offset, age) "+lib.NValues(9),
lib.AnyArray{aid, hlogin, lib.TruncToBytes(name, 120), countryID, sex, tz, sexProb, tzOff, age}...,
)
return aid
}
// mapCompanyName: maps company name to possibly new company name (when one was acquired by the another)
// If mapping happens, store it in the cache for speed
// stat:
// --- [no_regexp_match, cache] (unmapped)
// Company_name [match_regexp, match_cache]
func mapCompanyName(comMap map[string][2]string, acqMap map[*regexp.Regexp]string, stat map[string][2]int, company string) string {
res, ok := comMap[company]
if ok {
if res[1] == "m" {
ary := stat[res[0]]
ary[1]++
stat[res[0]] = ary
} else {
ary := stat["---"]
ary[1]++
stat["---"] = ary
}
return res[0]
}
for re, res := range acqMap {
if re.MatchString(company) {
comMap[company] = [2]string{res, "m"}
ary := stat[res]
ary[0]++
stat[res] = ary
return res
}
}
comMap[company] = [2]string{company, "u"}
ary := stat["---"]
ary[0]++
stat["---"] = ary
return company
}
// Check if given file was already imported
func alreadyImported(db *sql.DB, ctx *lib.Ctx, fn string) (imported bool, sha string) {
data, err := lib.ReadFile(ctx, fn)
if err != nil {
lib.FatalOnError(err)
return
}
sha = fmt.Sprintf("%x", sha256.Sum256(data))
rows := lib.QuerySQLWithErr(
db,
ctx,
fmt.Sprintf("select sha from gha_imported_shas where sha=%s", lib.NValue(1)),
sha,
)
defer func() { lib.FatalOnError(rows.Close()) }()
sha2 := ""
for rows.Next() {
lib.FatalOnError(rows.Scan(&sha2))
}
lib.FatalOnError(rows.Err())
return sha2 == sha, sha
}
// Sets given SHA as imported
func setImportedSHA(db *sql.DB, ctx *lib.Ctx, sha string) {
lib.ExecSQLWithErr(db, ctx, "insert into gha_imported_shas(sha) select "+lib.NValue(1)+" on conflict do nothing", sha)
}
func scoreCSD(csd *csData) (score float64) {
if csd.CountryID != nil && *csd.CountryID != "" {
score += 2.0
}
if csd.Tz != nil && *csd.Tz != "" {
score += 1.0
}
if csd.TzOffset != nil {
score += 1.0
}
if csd.Sex != nil && (*csd.Sex == "m" || *csd.Sex == "f" || *csd.Sex == "b") {
score += 1.0
}
if csd.SexProb != nil {
score += *csd.SexProb
}
if csd.Age != nil {
score += 0.5
}
return
}
// Imports given JSON file.
func importAffs(jsonFN string) int {
// Environment context parse
var ctx lib.Ctx
ctx.Init()
// Files path
dataPrefix := ctx.DataDir
if ctx.Local {
dataPrefix = "./"
}
// Handle default file name
if jsonFN == "" {
// Local or cron mode?
jsonFN = dataPrefix + ctx.AffiliationsJSON
}
lib.Printf("Importing %s\n", jsonFN)
// Connect to Postgres DB
con := lib.PgConn(&ctx)
defer func() { lib.FatalOnError(con.Close()) }()
// Check if given file was already imported
currentSHA := ""
currentSHA2 := ""
if ctx.CheckImportedSHA {
imported, sha := alreadyImported(con, &ctx, jsonFN)
if imported {
if ctx.SkipCompanyAcq {
lib.Printf("%s (SHA: %s) was already imported and skip company acquisitions mode is set, exiting\n", jsonFN, sha)
return 3
}
lib.Printf("%s (SHA: %s) was already imported, checking company acquisitions file import status\n", jsonFN, sha)
}
currentSHA = sha
fn := dataPrefix + ctx.CompanyAcqYaml
imported2, sha := alreadyImported(con, &ctx, fn)
if imported2 {
if imported {
lib.Printf("%s (SHA: %s) was already imported, %s (SHA: %s) also imported, exiting\n", fn, sha, jsonFN, currentSHA)
return 3
}
lib.Printf("%s (SHA: %s) was already imported, but %s (SHA: %s) wasn't, continuying\n", fn, sha, jsonFN, currentSHA)
}
currentSHA2 = sha
if ctx.OnlyCheckImportedSHA {
lib.Printf("Returining not-imported state\n")
return 0
}
}
// Read company acquisitions mapping
var (
acqs allAcquisitions
acqMap map[*regexp.Regexp]string
comMap map[string][2]string
stat map[string][2]int
)
if !ctx.SkipCompanyAcq {
data, err := lib.ReadFile(&ctx, dataPrefix+ctx.CompanyAcqYaml)
if err != nil {
lib.Printf("Cannot read company acquisitions mapping '%v', continuying without\n", err)
} else {
lib.FatalOnError(yaml.Unmarshal(data, &acqs))
if ctx.Debug > 0 {
lib.Printf("Acquisitions: %+v\n", acqs)
}
}
var re *regexp.Regexp
acqMap = make(map[*regexp.Regexp]string)
comMap = make(map[string][2]string)
stat = make(map[string][2]int)
srcMap := make(map[string]string)
resMap := make(map[string]struct{})
idxMap := make(map[*regexp.Regexp]int)
for idx, acq := range acqs.Acquisitions {
re = regexp.MustCompile(acq[0])
res, ok := srcMap[acq[0]]
if ok {
lib.Fatalf("Acquisition number %d '%+v' is already present in the mapping and maps into '%s'", idx, acq, res)
}
srcMap[acq[0]] = acq[1]
_, ok = resMap[acq[1]]
if ok {
lib.Fatalf("Acquisition number %d '%+v': some other acquisition already maps into '%s', merge them", idx, acq, acq[1])
}
resMap[acq[1]] = struct{}{}
acqMap[re] = acq[1]
idxMap[re] = idx
}
for re, res := range acqMap {
i := idxMap[re]
for idx, acq := range acqs.Acquisitions {
if re.MatchString(acq[1]) && i != idx {
lib.Fatalf("Acquisition's number %d '%s' result '%s' matches other acquisition number %d '%s' which maps to '%s', simplify it: '%v' -> '%s'", idx, acq[0], acq[1], i, re, res, acq[0], res)
}
if re.MatchString(acq[0]) && res != acq[1] {
lib.Fatalf("Acquisition's number %d '%s' regexp '%s' matches other acquisition number %d '%s' which maps to '%s': result is different '%s'", idx, acq, acq[0], i, re, res, acq[1])
}
}
}
}
// Parse github_users.json
var users gitHubUsers
data, err := lib.ReadFile(&ctx, jsonFN)
if err != nil {
lib.FatalOnError(err)
return 1
}
lib.FatalOnError(jsoniter.Unmarshal(data, &users))
// Process users affiliations
emptyVal := struct{}{}
loginEmails := make(mapStringSet)
loginNames := make(mapStringSet)
loginAffs := make(mapStringIntSet)
loginCSData := make(map[string]csData)
tzCache := make(map[string]*int)
sourceToPrio := map[string]int{"notfound": -20, "domain": -10, "": 0, "config": 10, "manual": 20, "user_manual": 30, "user": 40}
prioToSource := map[int]string{-20: "notfound", -10: "domain", 0: "", 10: "config", 20: "manual", 30: "user_manual", 40: "user"}
eNames, eEmails, eAffs := 0, 0, 0
lib.Printf("Processing %d JSON entries\n", len(users))
for _, user := range users {
// Email decode ! --> @
user.Email = strings.ToLower(emailDecode(user.Email))
login := strings.ToLower(user.Login)
// Affiliation source
source := strings.ToLower(user.Source)
sourcePrio, ok := sourceToPrio[source]
if !ok {
sourcePrio = 0
}
// Email
email := user.Email
if email != "" {
_, ok := loginEmails[login]
if !ok {
loginEmails[login] = stringSet{}
}
loginEmails[login][email] = emptyVal
} else {
eEmails++
}
// Name
name := user.Name
if name != "" {
_, ok := loginNames[login]
if !ok {
loginNames[login] = stringSet{}
}
loginNames[login][name] = emptyVal
} else {
eNames++
}
// Affiliation
aff := user.Affiliation
if aff != "NotFound" && aff != "(Unknown)" && aff != "?" && aff != "-" && aff != "" {
_, ok := loginAffs[login]
if !ok {
loginAffs[login] = mapIntSet{}
}
_, ok = loginAffs[login][sourcePrio]
if !ok {
loginAffs[login][sourcePrio] = stringSet{}
}
loginAffs[login][sourcePrio][aff] = emptyVal
} else {
eAffs++
}
// Country & sex data
newCsd := csData{
CountryID: user.CountryID,
Sex: user.Sex,
Tz: user.Tz,
SexProb: user.SexProb,
TzOffset: tzOffset(con, &ctx, user.Tz, tzCache),
Age: user.Age,
}
csd, ok := loginCSData[login]
if ok {
newScore := scoreCSD(&newCsd)
score := scoreCSD(&csd)
// fmt.Printf("login already has score %f\n", score)
if newScore > score {
// fmt.Printf("Got better score %f > %f for %+v vs %+v\n", newScore, score, newCsd, csd)
loginCSData[login] = newCsd
}
} else {
loginCSData[login] = newCsd
}
}
lib.Printf(
"Processing non-empty: %d name lists, %d email lists, %d affiliations lists, %d objects\n",
len(loginNames), len(loginEmails), len(loginAffs), len(loginCSData),
)
lib.Printf("Empty/Not found: names: %d, emails: %d, affiliations: %d\n", eNames, eEmails, eAffs)
if ctx.DryRun {
lib.Printf("Exiting due to dry-run mode.\n")
return 2
}
// Threads
thrN := lib.GetThreadsNum(&ctx)
var (
mtx *sync.Mutex
hmtx *sync.Mutex
ch chan struct{}
maybeHide func(string) string
lock func()
unlock func()
)
if thrN > 1 {
if thrN > 32 {
thrN = 32
}
ch = make(chan struct{})
mtx = &sync.Mutex{}
hmtx = &sync.Mutex{}
lock = func() {
mtx.Lock()
}
unlock = func() {
mtx.Unlock()
}
maybeHideInternal := lib.MaybeHideFunc(lib.GetHidden(lib.HideCfgFile))
maybeHide = func(arg string) string {
hmtx.Lock()
result := maybeHideInternal(arg)
hmtx.Unlock()
return result
}
} else {
maybeHide = lib.MaybeHideFunc(lib.GetHidden(lib.HideCfgFile))
lock = func() {}
unlock = func() {}
}
// Login - Names should be 1:1 (also handle records without name set)
added, updated, noName, mulNames, notChanged := 0, 0, 0, 0, 0
processLoginCSData := func(ch chan struct{}, login string, csD csData) {
if ch != nil {
defer func() {
ch <- struct{}{}
}()
}
lock()
names, foundName := loginNames[login]
unlock()
name := ""
if foundName {
// Other option would be to join all names via ", " - but it's better to query gha_actors_names then
name = firstKey(names)
if len(names) > 1 {
lock()
mulNames++
unlock()
}
} else {
lock()
noName++
unlock()
}
// Try to find actor by login
actor, csd, ok := findActor(con, &ctx, login, maybeHide)
if !ok {
// If no such actor, add with artificial ID (just like data from pre-2015)
addActor(con, &ctx, login, name, csD.CountryID, csD.Sex, csD.Tz, csD.SexProb, csD.TzOffset, csD.Age, maybeHide)
lock()
added++
unlock()
} else {
if (foundName && name != actor.Name) || !lib.CompareStringPtr(csd.CountryID, csD.CountryID) ||
!lib.CompareStringPtr(csd.Sex, csD.Sex) || !lib.CompareFloat64Ptr(csd.SexProb, csD.SexProb) ||
!lib.CompareStringPtr(csd.Tz, csD.Tz) || !lib.CompareIntPtr(csd.TzOffset, csD.TzOffset) ||
!lib.CompareIntPtr(csd.Age, csD.Age) {
if foundName {
// If actor found, but with different name (actually with name == "" after standard GHA import), update name
// Because there can be the same actor (by id) with different IDs (pre-2015 and post 2015), update His/Her name
// for all records with this login
lib.ExecSQLWithErr(con, &ctx,
"update gha_actors set "+
"name="+lib.NValue(1)+
", country_id="+lib.NValue(2)+
", sex="+lib.NValue(3)+
", tz="+lib.NValue(4)+
", sex_prob="+lib.NValue(5)+
", tz_offset="+lib.NValue(6)+
", age="+lib.NValue(7)+
" where lower(login)="+lib.NValue(8),
lib.AnyArray{
maybeHide(lib.TruncToBytes(name, 120)),
csD.CountryID,
csD.Sex,
csD.Tz,
csD.SexProb,
csD.TzOffset,
csD.Age,
strings.ToLower(maybeHide(login)),
}...,
)
} else {
lib.ExecSQLWithErr(con, &ctx,
"update gha_actors set "+
"country_id="+lib.NValue(1)+
", sex="+lib.NValue(2)+
", tz="+lib.NValue(3)+
", sex_prob="+lib.NValue(4)+
", tz_offset="+lib.NValue(5)+
", age="+lib.NValue(6)+
" where lower(login)="+lib.NValue(7),
lib.AnyArray{
csD.CountryID,
csD.Sex,
csD.Tz,
csD.SexProb,
csD.TzOffset,
csD.Age,
strings.ToLower(maybeHide(login)),
}...,
)
}
lock()
updated++
unlock()
} else {
lock()
notChanged++
unlock()
}
}
//if login == "kkosaka" {
// lib.Printf("processLoginCSData: %s -> (%s, %+v, %v) -> %+v\n", login, name, names, foundName, actor)
//}
}
if thrN > 1 {
lib.Printf("Processing using MT%d version\n", thrN)
nThreads := 0
for login, csD := range loginCSData {
go processLoginCSData(ch, login, csD)
nThreads++
if nThreads == thrN {
<-ch
nThreads--
}
}
lib.Printf("Final threads join\n")
for nThreads > 0 {
<-ch
nThreads--
}
} else {
lib.Printf("Processing using ST version\n")
for login, csD := range loginCSData {
processLoginCSData(nil, login, csD)
}
}
lib.Printf("Added actors: %d, updated actors: %d, empty names: %d, non-unique names: %d, non-changed: %d\n", added, updated, noName, mulNames, notChanged)
// Main caches
cacheActIDs := make(mapIntArray)
cacheActLogins := make(mapStringArray)
// Login - Possible multiple logins, possibly multiple affs
findActorData := func(ch chan struct{}, login string) {
if ch != nil {
defer func() {
ch <- struct{}{}
}()
}
actIDs, actLogins := findActors(con, &ctx, login, maybeHide)
if len(actIDs) < 1 {
lock()
csD := loginCSData[login]
unlock()
aID := addActor(con, &ctx, login, "", csD.CountryID, csD.Sex, csD.Tz, csD.SexProb, csD.TzOffset, csD.Age, maybeHide)
lock()
actIDs = append(actIDs, aID)
added++
unlock()
}
// Store given login's actor IDs in the case
lock()
for _, aLogin := range actLogins {
cacheActIDs[aLogin] = actIDs
cacheActLogins[aLogin] = actLogins
}
unlock()
}
added = 0
if thrN > 1 {
nThreads := 0
for login := range loginAffs {
go findActorData(ch, login)
nThreads++
if nThreads == thrN {
<-ch
nThreads--
}
}
for nThreads > 0 {
<-ch
nThreads--
}
} else {
for login := range loginAffs {
findActorData(nil, login)
}
}
if added > 0 {
lib.Printf("Unexpected: added actors: %d while caching affiliations\n", added)
}
// Handle GitHub login changes
newLogins, copiedAffs, otherPrios := 0, 0, 0
for login, prios := range loginAffs {
actLogins, ok := cacheActLogins[login]
if !ok {
continue
}
for _, otherLogin := range actLogins {
if otherLogin == login {
continue
}
// fmt.Printf("found %s correlated to %s\n", otherLogin, login)
_, ok := loginAffs[otherLogin]
if !ok {
loginAffs[otherLogin] = mapIntSet{}
newLogins++
}
for prio, affs := range prios {
_, ok := loginAffs[otherLogin][prio]
if !ok {
loginAffs[otherLogin][prio] = stringSet{}
otherPrios++
}
for aff := range affs {
_, ok := loginAffs[otherLogin][prio][aff]
if !ok {
// fmt.Printf("other login %s (correlated to %s) has no %s affiliation, adding\n", otherLogin, login, aff)
loginAffs[otherLogin][prio][aff] = emptyVal
copiedAffs++
}
}
}
}
}
lib.Printf("%d new logins added by correlations, copied affiliations: %d (%d different priority)\n", newLogins, copiedAffs, otherPrios)
// Login - Email(s) 1:N
added, allEmails := 0, 0
processEmails := func(ch chan struct{}, login string, emails stringSet) {
if ch != nil {
defer func() {
ch <- struct{}{}
}()
}
actIDs, actLogins := findActors(con, &ctx, login, maybeHide)
if len(actIDs) < 1 {
lock()
csD := loginCSData[login]
unlock()
// Should not happen
aID := addActor(con, &ctx, login, "", csD.CountryID, csD.Sex, csD.Tz, csD.SexProb, csD.TzOffset, csD.Age, maybeHide)
lock()
actIDs = append(actIDs, aID)
added++
unlock()
}
lock()
for _, aLogin := range actLogins {
cacheActIDs[aLogin] = actIDs
cacheActLogins[aLogin] = actLogins
}
unlock()
for email := range emails {
// One actor can have multiple emails but...
// One email can also belong to multiple actors
// This happens when actor was first defined in pre-2015 era (so He/She have negative ID then)
// And then in new API era 2015+ that actor was active too (so He/She will
// have entry with valid GitHub actor_id > 0)
for _, aid := range actIDs {
lib.ExecSQLWithErr(con, &ctx,
lib.InsertIgnore("into gha_actors_emails(actor_id, email) "+lib.NValues(2)),
lib.AnyArray{aid, maybeHide(lib.TruncToBytes(email, 120))}...,
)
lock()
allEmails++
unlock()
}
}
}
if thrN > 1 {
nThreads := 0
for login, emails := range loginEmails {
go processEmails(ch, login, emails)
nThreads++
if nThreads == thrN {
<-ch
nThreads--
}
}
for nThreads > 0 {
<-ch
nThreads--
}
} else {
for login, emails := range loginEmails {
processEmails(nil, login, emails)
}
}
if added > 0 {
lib.Printf("Unexpected: added %d actors while processing emails\n", added)
}
lib.Printf("Added up to %d actors emails\n", allEmails)
// Login - Names(s) 1:N
allNames := 0
processNames := func(ch chan struct{}, login string, names stringSet) {
if ch != nil {
defer func() {
ch <- struct{}{}
}()
}
actIDs, actLogins := findActors(con, &ctx, login, maybeHide)
if len(actIDs) < 1 {
lib.Fatalf("actor login not found %s", login)
}
// Store given login's actor IDs in the case
lock()
for _, aLogin := range actLogins {
cacheActIDs[aLogin] = actIDs
cacheActLogins[aLogin] = actLogins
}
unlock()
for name := range names {
// One actor can have multiple names but...
// One name can also belong to multiple actors
for _, aid := range actIDs {
lib.ExecSQLWithErr(con, &ctx,
lib.InsertIgnore("into gha_actors_names(actor_id, name) "+lib.NValues(2)),
lib.AnyArray{aid, maybeHide(lib.TruncToBytes(name, 120))}...,
)
lock()
allNames++
unlock()
}
}
}
if thrN > 1 {
nThreads := 0
for login, names := range loginNames {
go processNames(ch, login, names)
nThreads++
if nThreads == thrN {
<-ch
nThreads--
}
}
for nThreads > 0 {
<-ch
nThreads--
}
} else {
for login, names := range loginNames {
processNames(nil, login, names)
}
}
lib.Printf("Added up to %d actors names\n", allNames)
// Login - Affiliation should be 1:1, but it is sometimes 1:2 or 1:3
// There are some ambigous affiliations in github_users.json
// For such cases we're picking up the one with top source priority
// If there are still multiple such we're taking one with most entries
// And then if more than 1 with the same number of entries, then pick up first
unique, nonUnique, allAffs, nonUniquePrio := 0, 0, 0, 0
defaultStartDate := time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC)
defaultEndDate := time.Date(2100, 1, 1, 0, 0, 0, 0, time.UTC)
companies := make(stringSet)
var affList []affData
processAffs := func(ch chan struct{}, login string, prios mapIntSet) {
if ch != nil {
defer func() {
ch <- struct{}{}
}()
}
aPrios := []int{}
for prio := range prios {
aPrios = append(aPrios, prio)
}
sort.Ints(aPrios)
nPrios := len(aPrios)
if nPrios > 1 {
lock()
nonUniquePrio++
unlock()
}
maxPrio := aPrios[nPrios-1]
source, _ := prioToSource[maxPrio]
affs, _ := prios[maxPrio]
//fmt.Printf("Prio: %d -> %s\n", maxPrio, source)
var affsAry []string
if len(affs) > 1 {
// This login has different affiliations definitions in the input JSON
// Look for an affiliation that list most companies
maxNum := 1
for aff := range affs {
num := len(strings.Split(aff, ", "))
if num > maxNum {
maxNum = num
}
}
// maxNum holds max number of companies listed in any of affiliations
for aff := range affs {
ary := strings.Split(aff, ", ")
// Just pick first affiliation definition that lists most companies
if len(ary) == maxNum {
affsAry = ary
break
}
}
// Count this as non-unique
lock()
nonUnique++
unlock()
} else {
// This is a good definition, only one list of companies affiliation for this GitHub user login
affsAry = strings.Split(firstKey(affs), ", ")