-
Notifications
You must be signed in to change notification settings - Fork 0
/
Slakefile
68 lines (53 loc) · 1.55 KB
/
Slakefile
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
require! {
fs
child_process
async
mongodb
mongodb.Db
mongodb.Server
\./tasks/product-average
\./tasks/user-average
\./tasks/user-product-rate
handle: \./helper/error-handler
}
server = new Server \localhost, 27017, {native_parser: true}
db = new Db \ibcf, server, {safe: true}
# tasks
task \import:db, "Import data from json files", ->
file-db-mapping = do
favorite: \favorite
product: \product
user: \user
user_rate: \user-rate
(err) <- async.each <[favorite product user user_rate]>, (file, callback) ->
cmd = "mongoimport -d ibcf -c #{file-db-mapping[file]} --drop --jsonArray ./data/#file.json"
(err, stdout, stderr) <- child_process.exec cmd
if err
console.log "[error:#file] #err"
callback err
handle err
task \init:db, "Initialize database", ->
(err, db) <- db.open
handle err
(err) <- async.each <[favorite product user user-rate]>, (collection, callback) ->
db.ensure-index collection, {id: 1}, {unique: true, background: true, dropDups: true}, callback
handle err
<- user-average db
<- product-average db
<- user-product-rate db
db.close!
task \update:user:average, "Calculate each users' average rate", ->
(err, db) <- db.open
handle err
<- user-average db
db.close!
task \update:product:average, "Calculate each products' average rate", ->
(err, db) <- db.open
handle err
<- product-average db
db.close!
task \update:user:product-rate, "Calculate each users' rate on each product", ->
(err, db) <- db.open
handle err
<- user-product-rate db
db.close!