-
Notifications
You must be signed in to change notification settings - Fork 4
/
update.go
275 lines (234 loc) · 6.19 KB
/
update.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
package kmedia
import (
"database/sql"
"github.com/Bnei-Baruch/mdb/common"
"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/importer/kmedia/kmodels"
"github.com/Bnei-Baruch/mdb/models"
"github.com/Bnei-Baruch/mdb/utils"
)
func UpdateI18ns() {
var err error
clock := time.Now()
log.SetFormatter(&log.TextFormatter{FullTimestamp: true})
//log.SetLevel(log.WarnLevel)
stats = NewImportStatistics()
log.Info("Starting Kmedia unit updates")
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("Updating content units")
utils.Must(doUnits())
//log.Info("Updating collections")
//utils.Must(doCollections())
stats.dump()
log.Info("Success")
log.Infof("Total run time: %s", time.Now().Sub(clock).String())
}
func doUnits() error {
log.Info("Loading all units with kmedia_id")
units, err := models.ContentUnits(mdb,
//qm.Where("created_at > '2018-01-01'"),
qm.Where("properties -> 'kmedia_id' is not null")).
All()
if err != nil {
return errors.Wrap(err, "Load units from mdb")
}
log.Infof("Got %d units", len(units))
log.Info("Setting up workers")
jobs := make(chan *models.ContentUnit, 100)
var workersWG sync.WaitGroup
for w := 1; w <= 5; w++ {
workersWG.Add(1)
go updateUnitWorker(jobs, &workersWG)
}
log.Info("Queueing work")
for _, u := range units {
jobs <- u
}
log.Info("Closing jobs channel")
close(jobs)
log.Info("Waiting for workers to finish")
workersWG.Wait()
return nil
}
func doCollections() error {
log.Info("Loading all collections with kmedia_id")
collections, err := models.Collections(mdb,
qm.Where("properties -> 'kmedia_id' is not null")).
All()
if err != nil {
return errors.Wrap(err, "Load collections from mdb")
}
log.Infof("Got %d collections", len(collections))
log.Info("Setting up workers")
jobs := make(chan *models.Collection, 100)
var workersWG sync.WaitGroup
for w := 1; w <= 5; w++ {
workersWG.Add(1)
go updateCollectionWorker(jobs, &workersWG)
}
log.Info("Queueing work")
for _, u := range collections {
jobs <- u
}
log.Info("Closing jobs channel")
close(jobs)
log.Info("Waiting for workers to finish")
workersWG.Wait()
return nil
}
func updateUnitWorker(jobs <-chan *models.ContentUnit, wg *sync.WaitGroup) {
for u := range jobs {
var props map[string]interface{}
u.Properties.Unmarshal(&props)
kID := props["kmedia_id"]
var lecturerID null.Int
err := queries.Raw(kmdb, "select lecturer_id from containers where id = $1", kID).QueryRow().Scan(&lecturerID)
if err != nil {
if err == sql.ErrNoRows {
log.Warnf("no kmedia container ID: %d", int(kID.(float64)))
} else {
log.Error(err)
debug.PrintStack()
continue
}
}
stats.ContainersVisited.Inc(1)
err = u.L.LoadContentUnitsPersons(mdb, true, u)
if err != nil {
log.Error(err)
debug.PrintStack()
continue
}
hasRav := false
for i := range u.R.ContentUnitsPersons {
if u.R.ContentUnitsPersons[i].PersonID == 1 {
hasRav = true
break
}
}
if lecturerID.IsZero() {
// no rav
if hasRav {
log.Infof("norav has rav: kmID %d mdbID %d", int(kID.(float64)), u.ID)
stats.UnkownCatalogs.Inc("norav => rav", 1)
// TODO: fix manually as there are only few
} else {
// These are good
stats.UnkownCatalogs.Inc("norav => norav", 1)
}
} else {
// rav
if !hasRav {
//log.Infof("Missing Rav: kmID %d mdbID %d", int(kID.(float64)), u.ID)
stats.UnkownCatalogs.Inc("rav => norav", 1)
tx, err := mdb.Begin()
utils.Must(err)
// create association to RAV
cup := models.ContentUnitsPerson{
PersonID: 1, // rav in mdb
ContentUnitID: u.ID,
RoleID: 1, // lecturer
}
cup.Insert(tx)
utils.Must(tx.Commit())
} else {
// These are good
stats.UnkownCatalogs.Inc("rav => rav", 1)
}
}
//descriptions, err := kmodels.ContainerDescriptions(kmdb,
// qm.Where("container_id = ?", kID)).
// All()
//if err != nil {
// log.Error(err)
// debug.PrintStack()
// continue
//}
//
//tx, err := mdb.Begin()
//utils.Must(err)
//
//for _, d := range descriptions {
// if (d.ContainerDesc.Valid && d.ContainerDesc.String != "") ||
// (d.Descr.Valid && d.Descr.String != "") {
// cui18n := models.ContentUnitI18n{
// ContentUnitID: u.ID,
// Language: api.LANG_MAP[d.LangID.String],
// Name: d.ContainerDesc,
// Description: d.Descr,
// }
// err = cui18n.Upsert(tx,
// true,
// []string{"content_unit_id", "language"},
// []string{"name", "description"})
// if err != nil {
// log.Error(err)
// debug.PrintStack()
// utils.Must(tx.Rollback())
// }
// }
//}
//
//utils.Must(tx.Commit())
}
wg.Done()
}
func updateCollectionWorker(jobs <-chan *models.Collection, wg *sync.WaitGroup) {
for u := range jobs {
var props map[string]interface{}
u.Properties.Unmarshal(&props)
kID := props["kmedia_id"]
descriptions, err := kmodels.CatalogDescriptions(kmdb,
qm.Where("catalog_id = ?", kID)).
All()
if err != nil {
log.Error(err)
debug.PrintStack()
continue
}
tx, err := mdb.Begin()
utils.Must(err)
for _, d := range descriptions {
if d.Name.Valid && d.Name.String != "" {
ci18n := models.CollectionI18n{
CollectionID: u.ID,
Language: common.LANG_MAP[d.LangID.String],
Name: d.Name,
}
err = ci18n.Upsert(tx,
true,
[]string{"collection_id", "language"},
[]string{"name"})
if err != nil {
log.Error(err)
debug.PrintStack()
utils.Must(tx.Rollback())
}
}
}
utils.Must(tx.Commit())
}
wg.Done()
}