-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate-material-select.js
347 lines (338 loc) · 12.5 KB
/
update-material-select.js
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
// global configuration
const { default: config } = require("../dist/config/config");
const productionMode = config.isProduction;
// topology definition
module.exports = {
general: {
heartbeat: 2000,
pass_binary_messages: true
},
spouts: [
{
name: "input.postgres.materials",
type: "inproc",
working_dir: "./components/spouts",
cmd: "postgres_spout.js",
init: {
pg: config.pg,
sql_statement: `
WITH CURRENT_PROCESS AS (
SELECT
20 - (
SELECT COUNT(*)
FROM material_update_queue
WHERE end_process_time IS NULL
) AS total
),
CURRENT_PROCESS_QUOTA AS (
SELECT
CASE WHEN total < 0 THEN 0
ELSE total
END
FROM CURRENT_PROCESS
),
URLS AS (
SELECT
COALESCE(m.material_id, c.material_id) AS material_id,
COALESCE(m.provider_id, c.provider_id) AS provider_id,
m.url AS material_url,
c.url AS container_url,
(SELECT COUNT(*) FROM user_activities AS ua WHERE ua.url_id = c.id) as u_count
FROM contains
LEFT JOIN urls m ON contains.contains_id = m.id
LEFT JOIN urls c ON contains.container_id = c.id
ORDER BY material_id
),
OERS AS (
SELECT
URLS.material_id,
oer.title,
oer.description,
oer.creation_date,
oer.retrieved_date,
oer.type,
oer.mimetype,
URLS.material_url,
URLS.container_url AS provider_uri,
URLS.u_count,
oer.language,
oer.license
FROM URLS
LEFT JOIN oer_materials oer ON URLS.material_id = oer.id
LEFT JOIN providers p ON URLS.provider_id = p.id
WHERE oer.mimetype NOT LIKE '%video%' AND oer.mimetype NOT LIKE '%audio%' AND oer.mimetype NOT LIKE '%image%'
),
CONTENT AS (
SELECT
material_id,
array_agg(last_updated) AS lu
FROM material_contents
GROUP BY material_id
)
SELECT *
FROM OERS
WHERE material_id IN (
SELECT
material_id
FROM CONTENT
WHERE array_position(lu, NULL) IS NOT NULL
)
AND material_id NOT IN (SELECT material_id FROM material_update_queue)
ORDER BY u_count DESC
LIMIT (SELECT total FROM CURRENT_PROCESS_QUOTA);
`, // TODO: add the SQL statement for checking if the material is already in the queue
// repeat every one day
time_interval: 2 * 60 * 60 * 1000
}
}
],
bolts: [
{
name: "update.material.transform",
working_dir: ".",
type: "sys",
cmd: "transform",
inputs: [
{
source: "input.postgres.materials"
}
],
init: {
output_template: {
material_id: "material_id",
title: "title",
description: "description",
provider_uri: "provider_uri",
material_url: "material_url",
language: "language",
creation_date: "creation_date",
retrieved_date: "retrieved_date",
type: "type",
mimetype: "mimetype",
license: "license",
material_metadata: {
metadata: "material_metadata.metadata",
raw_text: "material_metadata.raw_text",
wikipedia_concepts: {}
}
}
}
},
{
name: "update.material.redirect",
type: "inproc",
working_dir: "./components/bolts",
cmd: "msg_redirect_bolt.js",
inputs: [{ source: "update.material.transform" }],
init: {
last_updated: "2019-08-01"
}
},
// LOGGING STATE OF MATERIAL PROCESS
...(productionMode
? [
{
name: "log.material.process.finished",
type: "inproc",
working_dir: "./components/bolts",
cmd: "pg_logging_bolt.js",
inputs: [
{
source: "update.material.redirect",
stream_id: "updated"
}
],
init: {
pg: config.pg,
postgres_table: "material_update_queue",
postgres_primary_id: "material_id",
message_primary_id: "material_id",
postgres_method: "upsert",
postgres_time_attrs: {
end_process_time: true
},
postgres_literal_attrs: {
status: "material updated"
},
document_error_path: "message",
final_bolt: true
}
}
]
: []),
// LOGGING STATE OF MATERIAL PROCESS
...(productionMode
? [
{
name: "log.material.process.unknown",
type: "inproc",
working_dir: "./components/bolts",
cmd: "pg_logging_bolt.js",
inputs: [
{
source: "update.material.redirect",
stream_id: "unknown"
}
],
init: {
pg: config.pg,
postgres_table: "material_update_queue",
postgres_primary_id: "material_id",
message_primary_id: "material_id",
postgres_method: "upsert",
postgres_time_attrs: {
end_process_time: true
},
postgres_literal_attrs: {
status: "material unknown"
},
document_error_path: "message",
final_bolt: true
}
}
]
: []),
// LOGGING STATE OF MATERIAL PROCESS
...(productionMode
? [
{
name: "log.material.process.text",
type: "inproc",
working_dir: "./components/bolts",
cmd: "pg_logging_bolt.js",
inputs: [
{
source: "update.material.redirect",
stream_id: "text"
}
],
init: {
pg: config.pg,
postgres_table: "material_update_queue",
postgres_primary_id: "material_id",
message_primary_id: "material_id",
postgres_method: "upsert",
postgres_time_attrs: {
start_process_time: true
},
postgres_literal_attrs: {
status: "[TEXT] in queue"
},
document_error_path: "message"
}
}
]
: []),
// LOGGING STATE OF MATERIAL PROCESS
...(productionMode
? [
{
name: "log.material.process.video",
type: "inproc",
working_dir: "./components/bolts",
cmd: "pg_logging_bolt.js",
inputs: [
{
source: "update.material.redirect",
stream_id: "video"
}
],
init: {
pg: config.pg,
postgres_table: "material_update_queue",
postgres_primary_id: "material_id",
message_primary_id: "material_id",
postgres_method: "upsert",
postgres_time_attrs: {
start_process_time: true
},
postgres_literal_attrs: {
status: "[VIDEO] in queue"
},
document_error_path: "message"
}
}
]
: []),
// LOGGING STATE OF MATERIAL PROCESS
...(productionMode
? [
{
name: "log.material.process.audio",
type: "inproc",
working_dir: "./components/bolts",
cmd: "pg_logging_bolt.js",
inputs: [
{
source: "update.material.redirect",
stream_id: "audio"
}
],
init: {
pg: config.pg,
postgres_table: "material_update_queue",
postgres_primary_id: "material_id",
message_primary_id: "material_id",
postgres_method: "upsert",
postgres_time_attrs: {
start_process_time: true
},
postgres_literal_attrs: {
status: "[AUDIO] in queue"
},
document_error_path: "message"
}
}
]
: []),
{
name: "kafka.material.update.text",
type: "inproc",
working_dir: "./components/bolts",
cmd: "kafka_bolt.js",
inputs: [
{
source: productionMode
? "log.material.process.text"
: "update.material.redirect",
stream_id: "text"
}
],
init: {
kafka: {
host: config.kafka.host,
topic: "UPDATE_MATERIAL_TEXT",
clientId: "UPDATE_MATERIAL_TEXT"
}
}
},
{
name: "kafka.material.update.video",
type: "inproc",
working_dir: "./components/bolts",
cmd: "kafka_bolt.js",
inputs: [
{
source: productionMode
? "log.material.process.video"
: "update.material.redirect",
stream_id: "video"
},
{
source: productionMode
? "log.material.process.audio"
: "update.material.redirect",
stream_id: "audio"
}
],
init: {
kafka: {
host: config.kafka.host,
topic: "UPDATE_MATERIAL_VIDEO",
clientId: "UPDATE_MATERIAL_VIDEO"
}
}
}
],
variables: {}
};