forked from corruptmem/nodejsmodules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04-StripDuplicateGits.coffee
More file actions
72 lines (57 loc) · 1.55 KB
/
04-StripDuplicateGits.coffee
File metadata and controls
72 lines (57 loc) · 1.55 KB
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
require('js-yaml')
mongoose = require('mongoose')
NpmPackage = require('./model/NpmPackage')
config = require('./config')
mongoose.connect(config.mongodb)
score = (doc) =>
return doc.downloads.total
stripDuplicates = (list, callback) =>
console.log(list)
NpmPackage.find({"id": {"$in": list}}).sort("id").exec((error, docs) =>
if(error)
process.nextTick(() => callback(error))
max = -1
maxDoc = null
# find the greatest doc
for doc in docs
thisScore = score(doc)
if thisScore > max
max = thisScore
maxDoc = doc
waiting = docs.length - 1
for doc in docs
if doc != maxDoc
doc.github.exists = false
doc.save((error) =>
if error?
console.error(error)
waiting -= 1
if waiting == 0
callback()
)
)
NpmPackage.aggregate(
[
{ "$match": {"github.url": {"$exists": true, "$ne": null }, "github.exists": true}},
{ "$project": {"github.url": 1, "id": 1} },
{ "$group": {"_id": "$github.url", "count": {"$sum": 1}, "docs": {"$push": "$id"}}},
{ "$match": {"count": {"$gt": 1 }}}
],
(error, docs) ->
if error?
console.error(error)
mongoose.connection.close()
process.exit()
waiting = docs.length
if docs.length == 0
mongoose.connection.close()
for doc in docs
stripDuplicates(doc.docs, (error) =>
if error?
console.error(error)
waiting -= 1
if waiting == 0
console.log("Done")
mongoose.connection.close()
)
)