-
Notifications
You must be signed in to change notification settings - Fork 1
/
loader.go
384 lines (332 loc) · 10.4 KB
/
loader.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
package extractor
import (
"context"
"encoding/json"
"errors"
"fmt"
"strconv"
"sync"
"time"
"github.com/olivere/elastic/v7"
"go.uber.org/zap"
)
// CompoundSource is the source where the unichem database extracted that
// compound
type CompoundSource struct {
ID int `json:"id"`
Name string `json:"name"`
LongName string `json:"long_name"`
CompoundID string `json:"compound_id"`
Description string `json:"description"`
BaseURL string `json:"base_url"`
ShortName string `json:"short_name"`
BaseIDURLAvailable bool `json:"base_id_url_available"`
AuxSrc string `json:"aux_src"`
AuxForURL bool `json:"aux_for_url"`
CreatedAt time.Time `json:"created_at"`
LastUpdate time.Time `json:"last_updated,omitempty"`
IsPrivate bool `json:"is_private"`
}
// Compound is an structure describing the information to be indexed
// extracted from Unichem database
type Compound struct {
UCI int `json:"uci"`
Inchi Inchi `json:"inchi"`
Components []Inchi `json:"components,omitempty"`
StandardInchiKey string `json:"standard_inchi_key"`
Smiles string `json:"smiles"`
Sources []CompoundSource `json:"sources,omitempty"`
CreatedAt time.Time `json:"created_at"`
IsSourceless bool `json:"is_sourceless"`
}
// UCICount is the amount of UCI by Sources on UniChem
type UCICount struct {
TotalUCI int `json:"totalUCI"`
Source int `json:"source"`
}
// WorkerResponse contains the result of the BulkRequest to the ElasticSearch index
type WorkerResponse struct {
Succeeded int
Indexed int
Created int
Updated int
Deleted int
Failed int
BulkResponse *elastic.BulkResponse
}
// ElasticManager used for connection and adding compounds to the
// elastic server
type ElasticManager struct {
logger *zap.SugaredLogger
Context context.Context
Client *elastic.Client
IndexName string
TypeName string
Bulklimit int
countBulkRequest int
currentBulkService *elastic.BulkService
Errchan chan error
Respchan chan WorkerResponse
WaitGroup sync.WaitGroup
currentBulkCalls int
MaxBulkCalls int
totalSentJobs int
totalDoneJobs int
}
// Init function initializes an elastic client and pings it to check the provider server is up
func (em *ElasticManager) Init(ctx context.Context, conf *Configuration, logger *zap.SugaredLogger) error {
em.logger = logger
// ctx = context.Background()
em.Context = ctx
if len(conf.ESIndexSettings) <= 0 {
logger.Panic("ES Index Setting can't be empty. PLease provide a valid one on the configuration file")
}
mapping := conf.ESIndexSettings
var err error
em.Client, err = elastic.NewClient(
elastic.SetURL(conf.ElasticHost),
elastic.SetSniff(false),
elastic.SetBasicAuth(conf.ElasticAuth.Username, conf.ElasticAuth.Password),
)
if err != nil {
em.logger.Panic("Error connecting to ElasticSearch ", err)
return err
}
inf, code, err := em.Client.Ping(conf.ElasticHost).Do(ctx)
if err != nil {
em.logger.Panic("Error Pinging elastic client ", err)
return err
}
em.logger.Infof("Succesfully pinged ElasticSearch server with code %d and version %s", code, inf.Version.Number)
ex, err := em.Client.IndexExists(em.IndexName).Do(ctx)
if err != nil {
em.logger.Panic("Error fetching index existence ", err)
return err
}
if !ex {
in, err := em.Client.CreateIndex(em.IndexName).BodyString(mapping).Do(ctx)
em.logger.Infof("Creating index %s", em.IndexName)
if err != nil {
em.logger.Panic("Error creating index ", err)
return err
}
if !in.Acknowledged {
em.logger.Error("Index creation not acknowledged")
return err
}
// Giving ES time to set up the Index
time.Sleep(2 * time.Second)
} else {
em.logger.Infof("Index %s exist, skipping its creation", em.IndexName)
}
em.currentBulkService = em.Client.Bulk()
em.currentBulkCalls = 0
em.countBulkRequest = 0
em.totalSentJobs = 0
em.totalDoneJobs = 0
em.Errchan = make(chan error)
em.Respchan = make(chan WorkerResponse)
return nil
}
// AddToBulk fills a BulkRequest up to the limit set up on the em.Bulklimit property
func (em *ElasticManager) AddToBulk(c Compound) {
em.logger.Debugw(
"Adding to bulk: ",
"UCI",
c.UCI,
"Smiles",
c.Smiles,
"sources",
c.Sources,
"isSouceless",
c.IsSourceless)
if em.countBulkRequest < em.Bulklimit {
em.countBulkRequest++
} else {
em.logger.Debugf("Got %d sending BulkRequest. New Bulk starting from: %d", em.countBulkRequest, c.UCI)
if em.currentBulkCalls < em.MaxBulkCalls {
em.currentBulkCalls++
} else {
// Wait for the MaxBulkCalls threads finish before continuing
em.logger.Debugf("Hitting %d workers to send. Waiting for them to finish. Last UCI: %d", em.currentBulkCalls, c.UCI)
em.WaitGroup.Wait()
em.currentBulkCalls = 0
}
em.totalSentJobs++
em.WaitGroup.Add(1)
em.logger.Debugf("Sending bulk triggered by: %d", c.UCI)
go em.sendBulkRequest(em.Context, em.Errchan, em.Respchan, em.currentBulkService, c.UCI)
em.countBulkRequest = 1
em.currentBulkService = em.Client.Bulk()
}
t := elastic.NewBulkUpdateRequest().Index(em.IndexName).DocAsUpsert(true).Id(strconv.Itoa(c.UCI)).Doc(c)
em.currentBulkService = em.currentBulkService.Add(t)
}
//SendCurrentBulk through a worker, useful for cleaning the requests stored on the BulkService
//regardless the BulkLimit has been reached or not
func (em *ElasticManager) SendCurrentBulk() {
if em.currentBulkService.NumberOfActions() > 0 {
em.totalSentJobs++
br, err := em.currentBulkService.Do(em.Context)
if err != nil {
em.Errchan <- err
return
}
wr := WorkerResponse{
Succeeded: len(br.Succeeded()),
Indexed: len(br.Indexed()),
Created: len(br.Created()),
Updated: len(br.Updated()),
Failed: len(br.Failed()),
BulkResponse: br,
}
em.Respchan <- wr
em.logger.Debug("END last bulk sent")
} else {
em.logger.Warn("No actions on current bulk service, skipping last bulk")
}
}
func (em *ElasticManager) sendBulkRequest(ctx context.Context, ce chan error, cr chan WorkerResponse, b *elastic.BulkService, UCI int) {
defer em.WaitGroup.Done()
em.logger.Debugf("INIT bulk worker %d", UCI)
br, err := b.Do(ctx)
if err != nil {
ce <- err
return
}
wr := WorkerResponse{
Succeeded: len(br.Succeeded()),
Indexed: len(br.Indexed()),
Created: len(br.Created()),
Updated: len(br.Updated()),
Failed: len(br.Failed()),
BulkResponse: br,
}
cr <- wr
em.logger.Debugf("END bulk worker %d", UCI)
}
func (em *ElasticManager) getCount() (int64, error) {
ctx := em.Context
l := em.logger
l.Info("Retrieving the total UCI count")
countResult, err := em.Client.Count().Index(em.IndexName).Do(ctx)
if err != nil {
m := fmt.Sprint("Error getting index total UCI", err)
fmt.Println(m)
l.Fatal(m)
return 0, err
}
l.Info("Elastic count result: ", countResult)
return countResult, err
}
func (em *ElasticManager) getLastIndexedUCI() (int, error) {
ctx := em.Context
l := em.logger
l.Info("Retrieving last UCI indexed")
termQuery := elastic.NewMatchAllQuery()
searchResults, err := em.Client.Search().Index(em.IndexName).Query(termQuery).Sort("uci", false).Size(1).Do(ctx)
if err != nil {
m := fmt.Sprint("Error getting getting last UCI indexed", err)
fmt.Println(m)
l.Fatal(m)
return 0, err
}
var c Compound
if searchResults.Hits.TotalHits.Value > 0 {
for _, hit := range searchResults.Hits.Hits {
err := json.Unmarshal(hit.Source, &c)
if err != nil {
m := fmt.Sprint("Error deserialize", err)
fmt.Println(m)
l.Fatal(m)
return 0, err
}
return c.UCI, nil
}
} else {
return 0, errors.New("no last UCI found")
}
return 0, err
}
func (em *ElasticManager) getLastUpdated() (time.Time, error) {
ctx := em.Context
l := em.logger
l.Info("Retrieving the last updated UCI")
termQuery := elastic.NewMatchAllQuery()
aggLstUp := elastic.NewMaxAggregation().Field("sources.last_updated")
aggCtAt := elastic.NewMaxAggregation().Field("sources.created_at")
searchResults, err := em.Client.Search().Index(em.IndexName).Query(termQuery).Aggregation("max_last_updated", aggLstUp).Aggregation("max_created", aggCtAt).Do(ctx)
if err != nil {
m := fmt.Sprint("Error getting getting last updated UCI", err)
fmt.Println(m)
l.Fatal(m)
return time.Now(), err
}
maxLastUpdated, found := searchResults.Aggregations.MaxBucket("max_last_updated")
if !found {
m := fmt.Sprint("max_last_updated aggregation not found", err)
fmt.Println(m)
l.Fatal(m)
return time.Now(), err
}
l.Debug("Max last updated", maxLastUpdated.ValueAsString)
tm := int64(*maxLastUpdated.Value) / 1000
mu := time.Unix(tm, 0)
maxCreated, found := searchResults.Aggregations.MaxBucket("max_created")
if !found {
m := fmt.Sprint("max_created aggregation not found", err)
fmt.Println(m)
l.Fatal(m)
return time.Now(), err
}
l.Debug("Max Created", maxCreated.ValueAsString)
tm = int64(*maxCreated.Value) / 1000
mc := time.Unix(tm, 0)
oldest := mu
if mc.Before(mu) {
oldest = mc
}
m := fmt.Sprint("Oldest date: ", oldest)
fmt.Println(m)
l.Info(m)
return oldest, err
}
func (em *ElasticManager) getUCICountBySources() (map[int]UCICount, error) {
ctx := em.Context
l := em.logger
l.Info("Retrieving UCI count by sources")
aggUCISou := elastic.NewTermsAggregation().Field("sources.id").Size(3000).OrderByCountDesc()
searchResults, err := em.Client.Search().Index(em.IndexName).Size(0).Aggregation("uci_by_sources_count", aggUCISou).Do(ctx)
if err != nil {
m := fmt.Sprint("Error getting getting last updated UCI", err)
fmt.Println(m)
l.Fatal(m)
return nil, err
}
uciCountAgg, found := searchResults.Aggregations.Terms("uci_by_sources_count")
if !found {
m := fmt.Sprint("uci_by_sources_count aggregation not found", err)
fmt.Println(m)
l.Fatal(m)
return nil, err
}
uca := make(map[int]UCICount)
for _, bu := range uciCountAgg.Buckets {
sid := bu.KeyNumber
s, err := sid.Int64()
if err != nil {
l.Error("Failed to parse the SourceID from the bucket")
return nil, err
}
uca[int(s)] = UCICount{
TotalUCI: int(bu.DocCount),
Source: int(s),
}
}
l.Debug("Map UCI count:", uca)
return uca, err
}
//Close terminates the ElasticSearch Client and BulkProcessor
func (em *ElasticManager) Close() {
em.Client.Stop()
}