-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute.go
127 lines (118 loc) · 3.95 KB
/
execute.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
package bird_data_guessing
import (
"fmt"
"sync"
"time"
"github.com/gbdubs/amass"
)
func (input *Input) Execute() (*Output, error) {
input.VLog("Bird data guessing: starting.\n")
oo := &Output{}
wasMemoizedLatins := make(map[string]bool)
requests := make([]*amass.GetRequest, 0)
requestsDone := 0
var requestsErr error = nil
requestsLock := sync.RWMutex{}
for index, b := range input.Names {
i := index
bird := b
go func() {
isMemoized, birdData, err := readMemoized(bird)
if err != nil {
requestsLock.Lock()
requestsErr = fmt.Errorf("While reading is memoized for %s: %v", bird.English, err)
requestsDone++
requestsLock.Unlock()
return
}
if isMemoized {
requestsLock.Lock()
wasMemoizedLatins[bird.Latin] = true
requestsDone++
input.VLog("[%d/%d] Memoized Read %s\n", requestsDone, len(input.Names), bird.English)
oo.BirdData = append(oo.BirdData, birdData)
requestsLock.Unlock()
} else {
wikipedia := createWikipediaRequests(bird)
audubon := createAudubonRequests(bird)
allAboutBirds := createAllAboutBirdsRequests(bird)
rspb := createRSPBRequests(bird)
// Whatbird is currently down
// whatBird := createWhatBirdRequests(bird)
requestsLock.Lock()
input.VLog("[%d/%d] Created Requests for %s\n", i, len(input.Names), bird.English)
// The compiler doesn't allow mulitple variadic args
requests = append(requests, wikipedia...)
requests = append(requests, audubon...)
requests = append(requests, allAboutBirds...)
requests = append(requests, rspb...)
// requests = append(requests, whatBird...)
requestsDone++
requestsLock.Unlock()
}
}()
}
for requestsDone < len(input.Names) {
if requestsErr != nil {
return oo, requestsErr
}
input.VLog("[%d/%d] Waiting for all requests to be generated.\n", requestsDone, len(input.Names))
time.Sleep(100 * time.Millisecond)
}
amasser := amass.Amasser{
TotalMaxConcurrentRequests: 20,
Verbose: input.VIndent(),
AllowedErrorProportion: 0.10,
}
responses, err := amasser.GetAll(requests)
if err != nil {
return oo, fmt.Errorf("While amassing results: %v", err)
}
latinToWikipedia := reconstructWikipediaResponsesKeyedByLatinName(responses)
englishToAllAboutBirds := reconstructAllAboutBirdsResponsesKeyedByEnglishName(responses)
englishToAudubon := reconstructAudubonResponsesKeyedByEnglishName(responses)
englishToWhatBird := reconstructWhatBirdsResponsesKeyedByEnglishName(responses)
englishToRspb := reconstructRSPBResponsesKeyedByEnglishName(responses)
for i, bird := range input.Names {
input.VLog("[%d/%d] Collecting + merging ", i, len(input.Names))
latin := bird.Latin
english := bird.English
if wasMemoizedLatins[latin] {
input.VLog(" %s - came from memoized.\n", bird.English)
continue
}
allSources := make([]*singleSourceData, 0)
if w, ok := latinToWikipedia[latin]; ok {
allSources = append(allSources, w.propertySearchers().getData(bird))
}
if a, ok := englishToAllAboutBirds[english]; ok {
allSources = append(allSources, a.propertySearchers().getData(bird))
}
if a, ok := englishToAudubon[english]; ok {
allSources = append(allSources, a.propertySearchers().getData(bird))
}
if w, ok := englishToWhatBird[english]; ok {
allSources = append(allSources, w.propertySearchers().getData(bird))
}
if w, ok := englishToRspb[english]; ok {
allSources = append(allSources, w.propertySearchers().getData(bird))
}
if len(allSources) == 0 {
input.VLog(" - EMPTY. Continuing\n")
continue
}
merged, highConfidence := input.mergeSources(allSources)
merged.Name = bird
if highConfidence {
oo.BirdData = append(oo.BirdData, *merged)
err := writeMemoized(*merged)
if err != nil {
return oo, fmt.Errorf("While merging bird %s: %v", bird.English, err)
}
input.VLog(" = merged + memoized %s.\n", bird.English)
} else {
input.VLog(" = LC!, not merged or memoized: %s.\n", bird.English)
}
}
return oo, nil
}