-
Notifications
You must be signed in to change notification settings - Fork 4
/
unit_mappings.go
369 lines (315 loc) · 9.12 KB
/
unit_mappings.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
package kmedia
import (
"database/sql"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/Bnei-Baruch/mdb/common"
"io/ioutil"
"runtime/debug"
"sync"
"time"
log "github.com/Sirupsen/logrus"
"github.com/pkg/errors"
"github.com/spf13/viper"
"github.com/volatiletech/sqlboiler/boil"
"github.com/volatiletech/sqlboiler/queries"
"github.com/volatiletech/sqlboiler/queries/qm"
"gopkg.in/volatiletech/null.v6"
"github.com/Bnei-Baruch/mdb/api"
"github.com/Bnei-Baruch/mdb/importer/kmedia/kmodels"
"github.com/Bnei-Baruch/mdb/models"
"github.com/Bnei-Baruch/mdb/utils"
)
type FileMappings struct {
Sha1 string
KMediaID int
KMediaContainerID int
MdbExists bool
MdbID int64
MdbUnitID int64
MdbKMediaID int
}
func MapUnits() {
var err error
clock := time.Now()
log.SetFormatter(&log.TextFormatter{FullTimestamp: true})
//log.SetLevel(log.WarnLevel)
log.Info("Starting Kmedia unit mappings")
log.Info("Setting up connection to MDB")
mdb, err = sql.Open("postgres", viper.GetString("mdb.url"))
utils.Must(err)
utils.Must(mdb.Ping())
defer mdb.Close()
boil.SetDB(mdb)
//boil.DebugMode = true
log.Info("Setting up connection to Kmedia")
kmdb, err = sql.Open("postgres", viper.GetString("kmedia.url"))
utils.Must(err)
utils.Must(kmdb.Ping())
defer kmdb.Close()
log.Info("Initializing static data from MDB")
utils.Must(common.InitTypeRegistries(mdb))
log.Info("Initializing static data from Kmedia")
kmediaLessonCT, err = kmodels.ContentTypes(kmdb, qm.Where("name = ?", "Lesson")).One()
utils.Must(err)
serverUrls, err = initServers(kmdb)
utils.Must(err)
log.Info("Loading all containers")
containers, err := kmodels.Containers(kmdb).All()
utils.Must(err)
log.Infof("Got %d containers", len(containers))
stats = NewImportStatistics()
log.Info("Setting up workers")
jobs := make(chan *kmodels.Container, 100)
results := make(chan []*FileMappings, 100)
done := make(chan []*FileMappings)
var workersWG sync.WaitGroup
for w := 1; w <= 5; w++ {
workersWG.Add(1)
go fileMappingsWorker(jobs, results, &workersWG)
}
go mappingsAggregator(results, done)
log.Info("Queueing work")
for _, container := range containers {
jobs <- container
}
log.Info("Closing jobs channel")
close(jobs)
log.Info("Waiting for workers to finish")
workersWG.Wait()
log.Info("Closing results channel")
close(results)
// wait for aggregation results
mappings := <-done
log.Infof("Got %d file mappings", len(mappings))
log.Info("Dumping file mappings")
err = dumpFileMappings(mappings)
utils.Must(err)
log.Info("Fixing unit mappings")
err = fixUnitMappings(mappings)
utils.Must(err)
log.Info("Fixing file mappings")
err = fixFileMappings(mappings)
utils.Must(err)
stats.dump()
log.Info("Success")
log.Infof("Total run time: %s", time.Now().Sub(clock).String())
}
func fileMappingsWorker(jobs <-chan *kmodels.Container, results chan []*FileMappings, wg *sync.WaitGroup) {
for container := range jobs {
stats.ContainersProcessed.Inc(1)
err := container.L.LoadFileAssets(kmdb, true, container)
if err != nil {
log.Error(err)
debug.PrintStack()
continue
}
sha1s := make([][]byte, 0)
bySha1 := make(map[string]*FileMappings, 0)
mappings := make([]*FileMappings, 0)
for _, fileAsset := range container.R.FileAssets {
//log.Infof("Processing file_asset %d", fileAsset.ID)
stats.FileAssetsProcessed.Inc(1)
if !fileAsset.Sha1.Valid {
stats.FileAssetsMissingSHA1.Inc(1)
continue
}
sha1 := fileAsset.Sha1.String
hexSha1, err := hex.DecodeString(sha1)
if err != nil {
log.Error(err)
debug.PrintStack()
continue
}
sha1s = append(sha1s, hexSha1)
fm := &FileMappings{
Sha1: fileAsset.Sha1.String,
KMediaID: fileAsset.ID,
KMediaContainerID: container.ID,
MdbExists: false,
}
bySha1[sha1] = fm
mappings = append(mappings, fm)
}
if len(sha1s) > 0 {
// fetch mdb files
files, err := models.Files(mdb,
qm.WhereIn("sha1 in ?", utils.ConvertArgsBytes(sha1s)...)).
All()
if err != nil {
log.Error(err)
debug.PrintStack()
continue
}
for _, file := range files {
sha1 := hex.EncodeToString(file.Sha1.Bytes)
fm := bySha1[sha1]
fm.MdbExists = true
fm.MdbID = file.ID
if file.ContentUnitID.Valid {
fm.MdbUnitID = file.ContentUnitID.Int64
}
if file.Properties.Valid {
var props map[string]interface{}
err = json.Unmarshal(file.Properties.JSON, &props)
if err != nil {
log.Error(err)
debug.PrintStack()
continue
}
if kmid, ok := props["kmedia_id"]; ok {
fm.MdbKMediaID = int(kmid.(float64))
}
}
}
}
results <- mappings
}
wg.Done()
}
func mappingsAggregator(results <-chan []*FileMappings, done chan []*FileMappings) {
mappings := make([]*FileMappings, 0)
for result := range results {
mappings = append(mappings, result...)
}
done <- mappings
}
func dumpFileMappings(mappings []*FileMappings) error {
f, err := ioutil.TempFile("/tmp", "mdb_kmedia_mappings_report")
if err != nil {
return errors.Wrap(err, "Create temp file")
}
defer f.Close()
log.Infof("Report file: %s", f.Name())
for _, fm := range mappings {
fmt.Fprintf(f, "%s,%d,%d,%d,%d,%t\n",
fm.Sha1, fm.KMediaID, fm.KMediaContainerID, fm.MdbID, fm.MdbUnitID, fm.MdbExists)
}
return nil
}
func fixUnitMappings(mappings []*FileMappings) error {
unitMap := make(map[int]map[int64]bool, stats.ContainersProcessed.Get())
for _, fm := range mappings {
cm, ok := unitMap[fm.KMediaContainerID]
if !ok {
cm = make(map[int64]bool)
}
if fm.MdbUnitID > 25316 {
//if fm.MdbUnitID > 0 {
cm[fm.MdbUnitID] = true
}
unitMap[fm.KMediaContainerID] = cm
}
bySize := make(map[int][]int)
for k, v := range unitMap {
s, ok := bySize[len(v)]
if !ok {
s = make([]int, 0)
}
s = append(s, k)
bySize[len(v)] = s
}
tx, err := mdb.Begin()
utils.Must(err)
unexpectedCT := map[int64]bool{
common.CONTENT_TYPE_REGISTRY.ByName[common.CT_KITEI_MAKOR].ID: true,
common.CONTENT_TYPE_REGISTRY.ByName[common.CT_LELO_MIKUD].ID: true,
common.CONTENT_TYPE_REGISTRY.ByName[common.CT_PUBLICATION].ID: true,
common.CONTENT_TYPE_REGISTRY.ByName[common.CT_RESEARCH_MATERIAL].ID: true,
common.CONTENT_TYPE_REGISTRY.ByName[common.CT_KTAIM_NIVCHARIM].ID: true,
}
for k, v := range bySize {
fmt.Printf("size %d\n", k)
for _, kcid := range v {
for mcuid := range unitMap[kcid] {
//fmt.Printf("%d,%d\n", kcid, mcuid)
u, err := models.FindContentUnit(mdb, mcuid)
if err != nil {
return errors.Wrapf(err, "Load CU from DB %d", mcuid)
}
if _, ok := unexpectedCT[u.TypeID]; ok {
fmt.Printf("!!!%s!!! [%d]\n", common.CONTENT_TYPE_REGISTRY.ByID[u.TypeID].Name, mcuid)
} else {
// check if mapping changed before updating
if u.Properties.Valid {
var props map[string]interface{}
err := u.Properties.Unmarshal(&props)
if err != nil {
return errors.Wrapf(err, "json.Unmarshal unit properties [%d]", u.ID)
}
if kmid, ok := props["kmedia_id"]; ok && int(kmid.(float64)) == kcid {
continue
}
}
log.Infof("Mapping kmedia container %d to mdb unit %d", kcid, u.ID)
err = api.UpdateContentUnitProperties(tx, u, map[string]interface{}{"kmedia_id": kcid})
if err != nil {
utils.Must(tx.Rollback())
return errors.Wrapf(err, "Update CU properties [%d]", u.ID)
}
stats.ContentUnitsUpdated.Inc(1)
}
}
}
}
utils.Must(tx.Commit())
return nil
}
func fixFileMappings(mappings []*FileMappings) error {
// load CU to container mappings
rows, err := queries.Raw(mdb,
"select id, (properties->>'kmedia_id')::int from content_units where properties ? 'kmedia_id';").
Query()
if err != nil {
return errors.Wrap(err, "Load CU to container map")
}
defer rows.Close()
cuMap := make(map[int]int64)
for rows.Next() {
var cuid int64
var kmid int
err = rows.Scan(&cuid, &kmid)
if err != nil {
return errors.Wrap(err, "rows.Scan")
}
cuMap[kmid] = cuid
}
if err = rows.Err(); err != nil {
return errors.Wrap(err, "rows.Err")
}
// fix file mappings:
// 1. "kmedia_id" property
// 2. CU association based on kmedia containers mappings
for _, fm := range mappings {
if !fm.MdbExists {
continue
}
if fm.KMediaID == fm.MdbKMediaID && fm.MdbUnitID != 0 {
continue
}
f, err := models.FindFile(mdb, fm.MdbID)
if err != nil {
return errors.Wrapf(err, "Load file %d", fm.MdbID)
}
if fm.KMediaID != fm.MdbKMediaID {
log.Infof("Updating kmedia_id property for %d to %d", f.ID, fm.KMediaID)
err = api.UpdateFileProperties(mdb, f, map[string]interface{}{"kmedia_id": fm.KMediaID})
if err != nil {
return errors.Wrapf(err, "Update file properties %d", f.ID)
}
}
if !f.ContentUnitID.Valid {
if cuid, ok := cuMap[fm.KMediaContainerID]; ok {
log.Infof("Associating file %d to CU %d [container %d]", f.ID, cuid, fm.KMediaContainerID)
f.ContentUnitID = null.Int64From(cuid)
err = f.Update(mdb, "content_unit_id")
if err != nil {
return errors.Wrapf(err, "Associate CU [file %d]", f.ID)
}
}
}
stats.FilesUpdated.Inc(1)
}
return nil
}