-
Notifications
You must be signed in to change notification settings - Fork 0
/
content_units_index.go
661 lines (595 loc) · 17.4 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
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
package es
import (
"bytes"
"context"
"database/sql"
"encoding/json"
"fmt"
"math"
"os"
"os/exec"
"path"
"reflect"
"strings"
"time"
log "github.com/Sirupsen/logrus"
"github.com/lib/pq"
"github.com/pkg/errors"
"github.com/spf13/viper"
"github.com/volatiletech/sqlboiler/queries"
"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) *ContentUnitsIndex {
cui := new(ContentUnitsIndex)
cui.baseName = consts.ES_UNITS_INDEX
cui.namespace = namespace
cui.docFolder = path.Join(viper.GetString("elasticsearch.docx-folder"))
return cui
}
type ContentUnitsIndex struct {
BaseIndex
indexData *IndexData
docFolder string
}
func (index *ContentUnitsIndex) ReindexAll() error {
if _, err := index.removeFromIndexQuery(elastic.NewMatchAllQuery()); err != nil {
return err
}
return index.addToIndexSql("cu.secure = 0 AND cu.published IS TRUE")
}
func contentUnitsScopeByFile(fileUID string) ([]string, error) {
units, err := mdbmodels.ContentUnits(mdb.DB,
qm.Select("content_units.uid"),
qm.InnerJoin("files AS f on f.content_unit_id = content_units.id"),
qm.Where("f.uid = ?", fileUID)).All()
if err != nil {
return nil, err
}
uids := make([]string, len(units))
for i, unit := range units {
uids[i] = unit.UID
}
return uids, nil
}
func is(slice interface{}) []interface{} {
s := reflect.ValueOf(slice)
if s.Kind() != reflect.Slice {
panic("InterfaceSlice() given a non-slice type")
}
ret := make([]interface{}, s.Len())
for i := 0; i < s.Len(); i++ {
ret[i] = s.Index(i).Interface()
}
return ret
}
func pprint(l interface{}) string {
var s []string
for _, i := range is(l) {
s = append(s, fmt.Sprintf("%+v", i))
}
return strings.Join(s, "\n\t")
}
func contentUnitsScopeByCollection(cUID string) ([]string, error) {
units, err := mdbmodels.ContentUnits(mdb.DB,
qm.InnerJoin("collections_content_units AS ccu ON ccu.content_unit_id = content_units.id"),
qm.InnerJoin("collections AS c ON ccu.collection_id = c.id"),
qm.Where("c.uid = ?", cUID)).All()
if err != nil {
return nil, err
}
uids := make([]string, len(units))
for i, unit := range units {
uids[i] = unit.UID
}
return uids, nil
}
func (index *ContentUnitsIndex) Add(scope Scope) error {
// We only add content units when content unit is added, 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 {
removed, err := index.removeFromIndex(scope)
if err != nil {
return err
}
return index.addToIndex(scope, removed)
}
func (index *ContentUnitsIndex) Delete(scope Scope) error {
// 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 := "cu.secure = 0 AND cu.published IS TRUE"
uids := removedUIDs
if scope.ContentUnitUID != "" {
uids = append(uids, scope.ContentUnitUID)
}
if scope.FileUID != "" {
moreUIDs, err := contentUnitsScopeByFile(scope.FileUID)
if err != nil {
return err
}
uids = append(uids, moreUIDs...)
}
if scope.CollectionUID != "" {
moreUIDs, err := contentUnitsScopeByCollection(scope.CollectionUID)
if err != nil {
return err
}
uids = append(uids, moreUIDs...)
}
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, fmt.Sprintf("content_unit:%s", scope.ContentUnitUID))
}
if scope.FileUID != "" {
typedUIDs = append(typedUIDs, fmt.Sprintf("file:%s", scope.FileUID))
}
if scope.CollectionUID != "" {
typedUIDs = append(typedUIDs, fmt.Sprintf("collection:%s", scope.CollectionUID))
moreUIDs, err := contentUnitsScopeByCollection(scope.CollectionUID)
if err != nil {
return []string{}, err
}
typedUIDs = append(typedUIDs, uidsToTypedUIDs("content_unit", moreUIDs)...)
}
if scope.TagUID != "" {
typedUIDs = append(typedUIDs, fmt.Sprintf("tag:%s", scope.TagUID))
}
if scope.SourceUID != "" {
typedUIDs = append(typedUIDs, fmt.Sprintf("source:%s", scope.SourceUID))
}
if scope.PersonUID != "" {
typedUIDs = append(typedUIDs, fmt.Sprintf("person:%s", scope.PersonUID))
}
if scope.PublisherUID != "" {
typedUIDs = append(typedUIDs, fmt.Sprintf("publisher:%s", 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(mdb.DB,
qm.Select("COUNT(1)"),
qm.From("content_units as cu"),
qm.Where(sqlScope)).QueryRow().Scan(&count)
if err != nil {
return err
}
offset := 0
limit := 1000
for offset < int(count) {
var units []*mdbmodels.ContentUnit
err := mdbmodels.NewQuery(mdb.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.", len(units))
index.indexData = new(IndexData)
err = index.indexData.Load(sqlScope)
if err != nil {
return err
}
for _, unit := range units {
if err := index.indexUnit(unit); err != nil {
return err
}
}
offset += limit
}
return nil
}
func (index *ContentUnitsIndex) removeFromIndexQuery(elasticScope elastic.Query) ([]string, error) {
removed := make(map[string]bool)
for _, lang := range consts.ALL_KNOWN_LANGS {
indexName := index.indexName(lang)
searchRes, err := mdb.ESC.Search(indexName).Query(elasticScope).Do(context.TODO())
if err != nil {
return []string{}, nil
}
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 := mdb.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("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(index.docFolder, docxFilename)
if _, err := os.Stat(docxPath); os.IsNotExist(err) {
return "", nil
}
cmd := exec.Command("es/parse_docs.py", docxPath)
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
log.Warnf("parse_docs.py %s\nstdout: %s\nstderr: %s", docxPath, stdout.String(), stderr.String())
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] = fmt.Sprintf("collection:%s", ccu.R.Collection.UID)
}
return ret
}
func uidsToTypedUIDs(t string, uids []string) []string {
ret := make([]string, len(uids))
for i, uid := range uids {
ret[i] = fmt.Sprintf("%s:%s", t, uid)
}
return ret
}
func (index *ContentUnitsIndex) indexUnit(cu *mdbmodels.ContentUnit) error {
// Create documents in each language with available translation
i18nMap := make(map[string]ContentUnit)
for i := range cu.R.ContentUnitI18ns {
i18n := cu.R.ContentUnitI18ns[i]
if i18n.Name.Valid && i18n.Name.String != "" {
typedUIDs := append([]string{fmt.Sprintf("content_unit:%s", 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.FilmDate = &utils.Date{Time: val}
}
if duration, ok := props["duration"]; ok {
unit.Duration = uint16(math.Max(0, duration.(float64)))
}
if originalLanguage, ok := props["original_language"]; ok {
unit.OriginalLanguage = originalLanguage.(string)
}
}
if val, ok := index.indexData.Sources[cu.ID]; ok {
unit.Sources = val
unit.TypedUIDs = append(unit.TypedUIDs, uidsToTypedUIDs("source", val)...)
}
if val, ok := index.indexData.Tags[cu.ID]; ok {
unit.Tags = val
unit.TypedUIDs = append(unit.TypedUIDs, uidsToTypedUIDs("tag", val)...)
}
if val, ok := index.indexData.Persons[cu.ID]; ok {
unit.Persons = val
unit.TypedUIDs = append(unit.TypedUIDs, uidsToTypedUIDs("person", val)...)
}
if val, ok := index.indexData.Translations[cu.ID]; ok {
unit.Translations = val[1]
unit.TypedUIDs = append(unit.TypedUIDs, uidsToTypedUIDs("file:", val[0])...)
}
if byLang, ok := index.indexData.Transcripts[cu.ID]; ok {
if val, ok := byLang[i18n.Language]; ok {
var err error
unit.Transcript, err = index.parseDocx(val[0])
unit.TypedUIDs = append(unit.TypedUIDs, fmt.Sprintf("file:%s", val[0]))
if err != nil {
log.Warnf("Error parsing docx: %s", val[0])
}
// if err == nil && unit.Transcript != "" {
// atomic.AddUint64(&withTranscript, 1)
// }
}
}
i18nMap[i18n.Language] = unit
}
}
// Index each document in its language index
for k, v := range i18nMap {
name := index.indexName(k)
resp, err := mdb.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
}
type IndexData struct {
Sources map[int64][]string
Tags map[int64][]string
Persons map[int64][]string
Translations map[int64][][]string
Transcripts map[int64]map[string][]string
}
func (cm *IndexData) Load(sqlScope string) error {
var err error
cm.Sources, err = cm.loadSources(sqlScope)
if err != nil {
return err
}
cm.Tags, err = cm.loadTags(sqlScope)
if err != nil {
return err
}
cm.Persons, err = cm.loadPersons(sqlScope)
if err != nil {
return err
}
cm.Translations, err = cm.loadTranslations(sqlScope)
if err != nil {
return err
}
cm.Transcripts, err = cm.loadTranscripts(sqlScope)
if err != nil {
return err
}
return nil
}
func (cm *IndexData) loadSources(sqlScope string) (map[int64][]string, error) {
rows, err := queries.Raw(mdb.DB, fmt.Sprintf(`
WITH RECURSIVE rec_sources AS (
SELECT
s.id,
s.uid,
s.position,
ARRAY [a.code, s.uid] "path"
FROM sources s INNER JOIN authors_sources aas ON s.id = aas.source_id
INNER JOIN authors a ON a.id = aas.author_id
UNION
SELECT
s.id,
s.uid,
s.position,
rs.path || s.uid
FROM sources s INNER JOIN rec_sources rs ON s.parent_id = rs.id
)
SELECT
cus.content_unit_id,
array_agg(DISTINCT item)
FROM content_units_sources cus
INNER JOIN rec_sources AS rs ON cus.source_id = rs.id
INNER JOIN content_units AS cu ON cus.content_unit_id = cu.id
, unnest(rs.path) item
WHERE %s
GROUP BY cus.content_unit_id;`, sqlScope)).Query()
if err != nil {
return nil, errors.Wrap(err, "Load sources")
}
defer rows.Close()
return cm.rowsToIdToValues(rows)
}
func (cm *IndexData) loadTags(sqlScope string) (map[int64][]string, error) {
rows, err := queries.Raw(mdb.DB, fmt.Sprintf(`
WITH RECURSIVE rec_tags AS (
SELECT
t.id,
t.uid,
ARRAY [t.uid] :: CHAR(8) [] "path"
FROM tags t
WHERE parent_id IS NULL
UNION
SELECT
t.id,
t.uid,
(rt.path || t.uid) :: CHAR(8) []
FROM tags t INNER JOIN rec_tags rt ON t.parent_id = rt.id
)
SELECT
cut.content_unit_id,
array_agg(DISTINCT item)
FROM content_units_tags cut
INNER JOIN rec_tags AS rt ON cut.tag_id = rt.id
INNER JOIN content_units AS cu ON cut.content_unit_id = cu.id
, unnest(rt.path) item
WHERE %s
GROUP BY cut.content_unit_id;`, sqlScope)).Query()
if err != nil {
return nil, errors.Wrap(err, "Load tags")
}
defer rows.Close()
return cm.rowsToIdToValues(rows)
}
func (cm *IndexData) loadPersons(sqlScope string) (map[int64][]string, error) {
rows, err := queries.Raw(mdb.DB, fmt.Sprintf(`
SELECT
cup.content_unit_id,
array_agg(p.uid)
FROM content_units_persons cup
INNER JOIN persons p ON cup.person_id = p.id
INNER JOIN content_units AS cu ON cup.content_unit_id = cu.id
WHERE %s
GROUP BY cup.content_unit_id;`, sqlScope)).Query()
if err != nil {
return nil, errors.Wrap(err, "Load persons")
}
defer rows.Close()
return cm.rowsToIdToValues(rows)
}
func (cm *IndexData) loadTranslations(sqlScope string) (map[int64][][]string, error) {
rows, err := queries.Raw(mdb.DB, fmt.Sprintf(`
SELECT
files.content_unit_id,
array_agg(DISTINCT files.uid),
array_agg(DISTINCT files.language)
FROM files
INNER JOIN content_units AS cu ON files.content_unit_id = cu.id
WHERE language NOT IN ('zz', 'xx') AND content_unit_id IS NOT NULL AND %s
GROUP BY content_unit_id;`, sqlScope)).Query()
if err != nil {
return nil, errors.Wrap(err, "Load translations")
}
defer rows.Close()
return cm.rowsToIdToUIDsAndValues(rows)
}
func (cm *IndexData) rowsToIdToValues(rows *sql.Rows) (map[int64][]string, error) {
m := make(map[int64][]string)
for rows.Next() {
var cuid int64
var values pq.StringArray
err := rows.Scan(&cuid, &values)
if err != nil {
return nil, errors.Wrap(err, "rows.Scan")
}
m[cuid] = values
}
if err := rows.Err(); err != nil {
return nil, errors.Wrap(err, "rows.Err()")
}
return m, nil
}
func (cm *IndexData) rowsToIdToUIDsAndValues(rows *sql.Rows) (map[int64][][]string, error) {
m := make(map[int64][][]string)
for rows.Next() {
var cuid int64
var values pq.StringArray
var uids pq.StringArray
err := rows.Scan(&cuid, &uids, &values)
if err != nil {
return nil, errors.Wrap(err, "rows.Scan")
}
m[cuid] = [][]string{uids, values}
}
if err := rows.Err(); err != nil {
return nil, errors.Wrap(err, "rows.Err()")
}
return m, nil
}
func (cm *IndexData) loadTranscripts(sqlScope string) (map[int64]map[string][]string, error) {
rows, err := queries.Raw(mdb.DB, fmt.Sprintf(`
SELECT
f.uid,
f.name,
f.language,
cu.id
FROM files AS f
INNER JOIN content_units AS cu ON f.content_unit_id = cu.id
WHERE name ~ '.docx?' AND
f.language NOT IN ('zz', 'xx') AND
f.content_unit_id IS NOT NULL AND
cu.type_id != 31 AND
%s;`, sqlScope)).Query()
if err != nil {
return nil, errors.Wrap(err, "Load transcripts")
}
defer rows.Close()
return loadTranscriptsMap(rows)
}
func loadTranscriptsMap(rows *sql.Rows) (map[int64]map[string][]string, error) {
m := make(map[int64]map[string][]string)
for rows.Next() {
var uid string
var name string
var language string
var cuID int64
err := rows.Scan(&uid, &name, &language, &cuID)
if err != nil {
return nil, errors.Wrap(err, "rows.Scan")
}
if _, ok := m[cuID]; !ok {
m[cuID] = make(map[string][]string)
}
m[cuID][language] = []string{uid, name}
}
if err := rows.Err(); err != nil {
return nil, errors.Wrap(err, "rows.Err()")
}
return m, nil
}