-
Notifications
You must be signed in to change notification settings - Fork 0
/
content_units_index.go
429 lines (396 loc) · 12.6 KB
/
content_units_index.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
package es
import (
"bytes"
"context"
"database/sql"
"encoding/json"
"fmt"
"math"
"os"
"os/exec"
"path"
"strings"
"time"
log "github.com/Sirupsen/logrus"
"github.com/pkg/errors"
"github.com/volatiletech/sqlboiler/queries/qm"
"gopkg.in/olivere/elastic.v5"
"github.com/Bnei-Baruch/archive-backend/consts"
"github.com/Bnei-Baruch/archive-backend/mdb"
"github.com/Bnei-Baruch/archive-backend/mdb/models"
"github.com/Bnei-Baruch/archive-backend/utils"
)
func MakeContentUnitsIndex(namespace string, db *sql.DB, esc *elastic.Client) *ContentUnitsIndex {
cui := new(ContentUnitsIndex)
cui.baseName = consts.ES_UNITS_INDEX
cui.namespace = namespace
cui.db = db
cui.esc = esc
return cui
}
type ContentUnitsIndex struct {
BaseIndex
}
func defaultContentUnit(cu *mdbmodels.ContentUnit) bool {
return cu.Secure == 0 && cu.Published && !utils.Int64InSlice(cu.TypeID, []int64{
mdb.CONTENT_TYPE_REGISTRY.ByName[consts.CT_CLIP].ID,
mdb.CONTENT_TYPE_REGISTRY.ByName[consts.CT_LELO_MIKUD].ID,
mdb.CONTENT_TYPE_REGISTRY.ByName[consts.CT_PUBLICATION].ID,
})
}
func defaultContentUnitSql() string {
return fmt.Sprintf("cu.secure = 0 AND cu.published IS TRUE AND cu.type_id NOT IN (%d, %d, %d)",
mdb.CONTENT_TYPE_REGISTRY.ByName[consts.CT_CLIP].ID,
mdb.CONTENT_TYPE_REGISTRY.ByName[consts.CT_LELO_MIKUD].ID,
mdb.CONTENT_TYPE_REGISTRY.ByName[consts.CT_PUBLICATION].ID,
)
}
func (index *ContentUnitsIndex) ReindexAll() error {
log.Infof("Content Units Index - Reindex all.")
if _, err := index.removeFromIndexQuery(elastic.NewMatchAllQuery()); err != nil {
return err
}
return index.addToIndexSql(defaultContentUnitSql())
}
func (index *ContentUnitsIndex) Add(scope Scope) error {
log.Infof("Content Units Index - Add. Scope: %+v.", scope)
// We only add content units when the scope is content unit, otherwise we need to update.
if scope.ContentUnitUID != "" {
if err := index.addToIndex(Scope{ContentUnitUID: scope.ContentUnitUID}, []string{}); err != nil {
return err
}
scope.ContentUnitUID = ""
}
emptyScope := Scope{}
if scope != emptyScope {
return index.Update(scope)
}
return nil
}
func (index *ContentUnitsIndex) Update(scope Scope) error {
log.Infof("Content Units Index - Update. Scope: %+v.", scope)
removed, err := index.removeFromIndex(scope)
if err != nil {
return err
}
return index.addToIndex(scope, removed)
}
func (index *ContentUnitsIndex) Delete(scope Scope) error {
log.Infof("Content Units Index - Delete. Scope: %+v.", scope)
// We only delete content units when content unit is deleted, otherwise we just update.
if scope.ContentUnitUID != "" {
if _, err := index.removeFromIndex(Scope{ContentUnitUID: scope.ContentUnitUID}); err != nil {
return err
}
scope.ContentUnitUID = ""
}
emptyScope := Scope{}
if scope != emptyScope {
return index.Update(scope)
}
return nil
}
func (index *ContentUnitsIndex) addToIndex(scope Scope, removedUIDs []string) error {
// TODO: Work not done! Missing tags and sources scopes!
sqlScope := defaultContentUnitSql()
uids := removedUIDs
if scope.ContentUnitUID != "" {
uids = append(uids, scope.ContentUnitUID)
}
if scope.FileUID != "" {
moreUIDs, err := contentUnitsScopeByFile(index.db, scope.FileUID)
if err != nil {
return err
}
uids = append(uids, moreUIDs...)
}
if scope.CollectionUID != "" {
moreUIDs, err := contentUnitsScopeByCollection(index.db, scope.CollectionUID)
if err != nil {
return err
}
uids = append(uids, moreUIDs...)
}
if scope.SourceUID != "" {
moreUIDs, err := contentUnitsScopeBySource(index.db, scope.SourceUID)
if err != nil {
return err
}
uids = append(uids, moreUIDs...)
}
if len(uids) == 0 {
return nil
}
quoted := make([]string, len(uids))
for i, uid := range uids {
quoted[i] = fmt.Sprintf("'%s'", uid)
}
sqlScope = fmt.Sprintf("%s AND cu.uid IN (%s)", sqlScope, strings.Join(quoted, ","))
return index.addToIndexSql(sqlScope)
}
func (index *ContentUnitsIndex) removeFromIndex(scope Scope) ([]string, error) {
var typedUIDs []string
if scope.ContentUnitUID != "" {
typedUIDs = append(typedUIDs, uidToTypedUID("content_unit", scope.ContentUnitUID))
}
if scope.FileUID != "" {
typedUIDs = append(typedUIDs, uidToTypedUID("file", scope.FileUID))
}
if scope.CollectionUID != "" {
typedUIDs = append(typedUIDs, uidToTypedUID("collection", scope.CollectionUID))
moreUIDs, err := contentUnitsScopeByCollection(index.db, scope.CollectionUID)
if err != nil {
return []string{}, err
}
typedUIDs = append(typedUIDs, uidsToTypedUIDs("content_unit", moreUIDs)...)
}
if scope.TagUID != "" {
typedUIDs = append(typedUIDs, uidToTypedUID("tag", scope.TagUID))
}
if scope.SourceUID != "" {
typedUIDs = append(typedUIDs, uidToTypedUID("source", scope.SourceUID))
}
if scope.PersonUID != "" {
typedUIDs = append(typedUIDs, uidToTypedUID("person", scope.PersonUID))
}
if scope.PublisherUID != "" {
typedUIDs = append(typedUIDs, uidToTypedUID("publisher", scope.PublisherUID))
}
if len(typedUIDs) > 0 {
typedUIDsI := make([]interface{}, len(typedUIDs))
for i, typedUID := range typedUIDs {
typedUIDsI[i] = typedUID
}
elasticScope := elastic.NewTermsQuery("typed_uids", typedUIDsI...)
return index.removeFromIndexQuery(elasticScope)
} else {
// Nothing to remove.
return []string{}, nil
}
}
func (index *ContentUnitsIndex) addToIndexSql(sqlScope string) error {
var count int64
err := mdbmodels.NewQuery(index.db,
qm.Select("COUNT(1)"),
qm.From("content_units as cu"),
qm.Where(sqlScope)).QueryRow().Scan(&count)
if err != nil {
return err
}
log.Infof("Content Units Index - Adding %d units. Scope: %s", count, sqlScope)
offset := 0
limit := 1000
for offset < int(count) {
var units []*mdbmodels.ContentUnit
err := mdbmodels.NewQuery(index.db,
qm.From("content_units as cu"),
qm.Load("ContentUnitI18ns"),
qm.Load("CollectionsContentUnits"),
qm.Load("CollectionsContentUnits.Collection"),
qm.Where(sqlScope),
qm.Offset(offset),
qm.Limit(limit)).Bind(&units)
if err != nil {
return errors.Wrap(err, "Fetch units from mdb")
}
log.Infof("Adding %d units (offset: %d).", len(units), offset)
indexData, err := MakeIndexData(index.db, sqlScope)
if err != nil {
return err
}
for _, unit := range units {
if err := index.indexUnit(unit, indexData); err != nil {
return err
}
}
offset += limit
}
return nil
}
func (index *ContentUnitsIndex) removeFromIndexQuery(elasticScope elastic.Query) ([]string, error) {
source, err := elasticScope.Source()
if err != nil {
return []string{}, err
}
jsonBytes, err := json.Marshal(source)
if err != nil {
return []string{}, err
}
log.Infof("Content Untis Index - Removing from index. Scope: %s", string(jsonBytes))
removed := make(map[string]bool)
for _, lang := range consts.ALL_KNOWN_LANGS {
indexName := index.indexName(lang)
searchRes, err := index.esc.Search(indexName).Query(elasticScope).Do(context.TODO())
if err != nil {
return []string{}, err
}
for _, h := range searchRes.Hits.Hits {
var cu ContentUnit
err := json.Unmarshal(*h.Source, &cu)
if err != nil {
return []string{}, err
}
removed[cu.MDB_UID] = true
}
delRes, err := index.esc.DeleteByQuery(indexName).
Query(elasticScope).
Do(context.TODO())
if err != nil {
return []string{}, errors.Wrapf(err, "Remove from index %s %+v\n", indexName, elasticScope)
}
if delRes.Deleted > 0 {
fmt.Printf("Deleted %d documents from %s.\n", delRes.Deleted, indexName)
}
}
if len(removed) == 0 {
fmt.Println("Content Units Index - Nothing was delete.")
return []string{}, nil
}
keys := make([]string, len(removed))
for k := range removed {
keys = append(keys, k)
}
return keys, nil
}
func (index *ContentUnitsIndex) parseDocx(uid string) (string, error) {
docxFilename := fmt.Sprintf("%s.docx", uid)
docxPath := path.Join(docFolder, docxFilename)
if _, err := os.Stat(docxPath); os.IsNotExist(err) {
log.Warnf("Could not find file to parse %s", docxPath)
return "", errors.Wrapf(err, "os.Stat %s", docxPath)
}
var cmd *exec.Cmd
if strings.ToLower(operatingSystem) == "windows" {
cmd = exec.Command(pythonPath, parseDocsBin, docxPath)
} else {
cmd = exec.Command(parseDocsBin, docxPath)
}
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
log.Warnf("[%s %s]\nstdout: [%s]\nstderr: [%s]\nError: %+v\n", parseDocsBin, docxPath, stdout.String(), stderr.String(), err)
return "", errors.Wrapf(err, "cmd.Run %s", uid)
}
return stdout.String(), nil
}
func collectionsContentTypes(collectionsContentUnits mdbmodels.CollectionsContentUnitSlice) []string {
ret := make([]string, len(collectionsContentUnits))
for i, ccu := range collectionsContentUnits {
ret[i] = mdb.CONTENT_TYPE_REGISTRY.ByID[ccu.R.Collection.TypeID].Name
}
return ret
}
func collectionsTypedUIDs(collectionsContentUnits mdbmodels.CollectionsContentUnitSlice) []string {
ret := make([]string, len(collectionsContentUnits))
for i, ccu := range collectionsContentUnits {
ret[i] = uidToTypedUID("collection", ccu.R.Collection.UID)
}
return ret
}
func (index *ContentUnitsIndex) indexUnit(cu *mdbmodels.ContentUnit, indexData *IndexData) error {
// Create documents in each language with available translation
i18nMap := make(map[string]ContentUnit)
for _, i18n := range cu.R.ContentUnitI18ns {
if i18n.Name.Valid && i18n.Name.String != "" {
typedUIDs := append([]string{uidToTypedUID("content_unit", cu.UID)},
collectionsTypedUIDs(cu.R.CollectionsContentUnits)...)
unit := ContentUnit{
MDB_UID: cu.UID,
TypedUIDs: typedUIDs,
Name: i18n.Name.String,
ContentType: mdb.CONTENT_TYPE_REGISTRY.ByID[cu.TypeID].Name,
CollectionsContentTypes: collectionsContentTypes(cu.R.CollectionsContentUnits),
}
if i18n.Description.Valid && i18n.Description.String != "" {
unit.Description = i18n.Description.String
}
if cu.Properties.Valid {
var props map[string]interface{}
err := json.Unmarshal(cu.Properties.JSON, &props)
if err != nil {
return errors.Wrapf(err, "json.Unmarshal properties %s", cu.UID)
}
if filmDate, ok := props["film_date"]; ok {
val, err := time.Parse("2006-01-02", filmDate.(string))
if err != nil {
return errors.Wrapf(err, "time.Parse film_date %s", cu.UID)
}
unit.EffectiveDate = &utils.Date{Time: val}
}
if duration, ok := props["duration"]; ok {
unit.Duration = uint64(math.Max(0, duration.(float64)))
}
if originalLanguage, ok := props["original_language"]; ok {
unit.OriginalLanguage = originalLanguage.(string)
}
}
if val, ok := indexData.Sources[cu.UID]; ok {
unit.Sources = val
unit.TypedUIDs = append(unit.TypedUIDs, uidsToTypedUIDs("source", val)...)
}
if val, ok := indexData.Tags[cu.UID]; ok {
unit.Tags = val
unit.TypedUIDs = append(unit.TypedUIDs, uidsToTypedUIDs("tag", val)...)
}
if val, ok := indexData.Persons[cu.UID]; ok {
unit.Persons = val
unit.TypedUIDs = append(unit.TypedUIDs, uidsToTypedUIDs("person", val)...)
}
if val, ok := indexData.Translations[cu.UID]; ok {
unit.Translations = val[1]
unit.TypedUIDs = append(unit.TypedUIDs, uidsToTypedUIDs("file", val[0])...)
}
if byLang, ok := indexData.Transcripts[cu.UID]; ok {
if val, ok := byLang[i18n.Language]; ok {
var err error
fileName, err := LoadDocFilename(index.db, val[0])
if err != nil {
log.Errorf("Error retrieving doc from DB: %s", val[0])
} else {
err = DownloadAndConvert([][]string{{val[0], fileName}})
if err != nil {
log.Errorf("Error downloading or converting doc: %s", val[0])
log.Errorf("Error %+v", err)
} else {
unit.Transcript, err = index.parseDocx(val[0])
if err != nil {
log.Errorf("Error parsing docx: %s", val[0])
} else {
unit.TypedUIDs = append(unit.TypedUIDs, uidToTypedUID("file", val[0]))
}
}
}
}
}
i18nMap[i18n.Language] = unit
}
}
// Index each document in its language index
for k, v := range i18nMap {
name := index.indexName(k)
vCopy := v
if len(vCopy.Transcript) > 30 {
vCopy.Transcript = fmt.Sprintf("%s...", vCopy.Transcript[:30])
}
vBytes, err := json.Marshal(vCopy)
if err != nil {
return err
}
log.Infof("Content Units Index - Add content unit %s to index %s", string(vBytes), name)
resp, err := index.esc.Index().
Index(name).
Type("content_units").
BodyJson(v).
Do(context.TODO())
if err != nil {
return errors.Wrapf(err, "Index unit %s %s", name, cu.UID)
}
if !resp.Created {
return errors.Errorf("Not created: unit %s %s", name, cu.UID)
}
}
return nil
}