mongoose require mongoose fs require fs async require async Constructo

  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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
mongoose = require 'mongoose'
fs = require 'fs'
async = require 'async'
ConstructorDB = require './../db/index.coffee'
countAdmin = 0
countObject = 0
countChecked = 0
countWithoutCoordinates = 0
class Compare
constructor: (param) ->
new ConstructorDB(param)
@db = require './../db/universal'
@config = require './../config.json'
# @streamDB()
@asyncDB()
streamDB: () ->
self = @
modelDB = 'Object'
stream = @db[modelDB].find().stream()
console.time('time')
stream.on "data", (model) ->
console.log '\t\t\t\t\t\t\t\t\t\t\t\t\t\t object counter', countObject++
# console.log 'Start object title: ', model.title
# stream.pause()
unless model.checked
self.compare modelDB, model, (err) ->
if err
self.errorLog "streamDB compare callback error: #{err}"
# stream.resume()
else
# stream.resume()
stream.on "error", (err) ->
self.errorLog "streamDB error: #{err}"
stream.on "close", ->
console.log '\n---------------------------'
console.log '\nFinished'
console.log '\nViewed objects: ', countObject
console.log '\nObjects checked: ', countChecked
console.log '\nObjects without coordinates: ', countWithoutCoordinates
console.log '\nSave to admin: ', countAdmin
console.timeEnd('time')
process.exit()
asyncDB: () ->
self = @
console.time('time')
modelDB = 'Object'
@db[modelDB].find '*', (err, models) ->
if err
self.errorLog "asyncDB get all objects error: #{err}"
else
allElements = models.length
async.eachSeries models, (model, cb) ->
countObject++
progress = countObject / (allElements/100)
console.log "#{progress.toFixed(2)}%"
self.compare modelDB, model, (err) ->
if err
self.errorLog "asyncDB compare callback error: #{err}"
cb null
, (err) ->
if err
self.errorLog "asyncDB async error: #{err}"
console.log '!!!!!!!!!!!!!!!!!!!!! end of asunc', err
compare: (modelDB, checkModel, callback) ->
self = @
# console.log '\n Start compare', checkModel.title
self.compareCoordinates modelDB, checkModel, (err, resultCoord) ->
unless resultCoord.success
self.compareTitles resultCoord.models, (err, resultTitles) ->
unless resultTitles.success
self.saveToAdmin modelDB, resultTitles.models, (err) ->
if err
self.errorLog "compare saveToAdmin callback error: #{err}"
callback null
else
self.checking modelDB, true, resultCoord.models, callback
else
self.checking modelDB, true, resultCoord.models, callback
checking: (modelDB, flag, models, callback) ->
self = @
async.eachSeries models, (el, cb) ->
self.db[modelDB].findById el._id, (err, model) ->
model.checked = flag
model.single = flag
model.save (err, newData) ->
if err
self.errorLog "checking update model error. #{err}"
cb null
else
countChecked++
# console.log '\n\t checking update model success', el.title
cb null
, (err) ->
if err
self.errorLog "checking async error", err
# console.log '\n\t\tObjects checked: ', countChecked
callback null
compareCoordinates: (modelDB, checkModel, callback) ->
self = @
D = 0.0002
if checkModel.coordinates
lat =
less : parseFloat(checkModel.coordinates.latitude + D)
great : parseFloat(checkModel.coordinates.latitude - D)
lng =
less : parseFloat(checkModel.coordinates.longitude + D)
great : parseFloat(checkModel.coordinates.longitude - D)
# console.log '\tstart compare coordinates:', checkModel.coordinates, ' delta:', D
# console.log '\t\t\tcoordinates Options: '
# console.log '\t\t\t\tcoordinates.longitude:', {$gte: lng.great, $lte: lng.less}
# console.log '\t\t\t\tcoordinates.latitude:' , {$gte: lat.great, $lte: lat.less}
@db[modelDB].find {
'coordinates.longitude': {$gte: lng.great, $lte: lng.less},
'coordinates.latitude' : {$gte: lat.great, $lte: lat.less}
}, (err, objectModel) ->
# console.log '\n\t\tsearch and compare of coordinates complete.\n\t\t\t Found of objects ib DB:', objectModel.length
if err
self.errorLog "failed compare coordinates of object: \n\tdata: #{JSON.stringify(checkModel)} \n\terror: #{err}"
callback err
# duplicates not found
else if !objectModel or objectModel.length is 0
self.errorLog "\tWARNING! compareCoordinates. Object not found itself in DB. This is strange #{checkModel}"
result =
success : true
models : checkModel
callback null, result
# was found itself
else if objectModel.length is 1 and objectModel._id? is checkModel._id
result =
success : true
models : checkModel
callback null, result
else
result =
success : false
models : objectModel
callback null, result
else
logInfo =
if checkModel.title then checkModel.title else 'Untitled'
countWithoutCoordinates++
self.errorLog " this is model haven`t coordinates. Name: #{logInfo}, data: #{checkModel},\n\t count objects without coordinates #{countWithoutCoordinates}"
result =
success : true
models : checkModel
callback null, result
compareTitles: (checkModels, callback) ->
self = @
PERCENT_OF_OVERLAP = 85
overlap = []
array1 = if Array.isArray(checkModels) then checkModels else [checkModels]
array2 = array1
i=0
async.eachSeries array1, (el1, cb1) ->
y=0
async.eachSeries array2, (el2, cb2) ->
self.compareSymbols el1.title, el2.title, (err, result) ->
if !err and result > PERCENT_OF_OVERLAP
if i isnt y
overlap.push el1
y++
cb2()
else
y++
cb2()
else
y++
cb2()
, () ->
i++
cb1()
, (err) ->
# console.log '\t\tcompare of titles complete.\n\t\t\t Objects in group:', overlap.length
if err
self.errorLog "compareTitles async.eachSeries error: #{err}"
callback err
else if overlap.length <= 0
result =
success : true
models : checkModels
callback null, result
else
result =
success : false
models : overlap
callback null, result
saveToAdmin: (modelDB, duplicates, callback) ->
self = @
if duplicates.length is 0
self.errorLog "WARNING. save to admin objects. Duplicates is #{duplicates.length}"
callback null
else
# console.log '\n\tsave to admin objects: ', duplicates.length.length
duplicatesId = []
async.eachSeries duplicates, (el, cb) ->
duplicatesId.push el._id
self.db[modelDB].findById el._id, (err, model) ->
model.checked = true
model.single = false
model.save (err, newData) ->
if err
self.errorLog "saveToAdmin update model error. #{err}"
cb null
else
countChecked++
# console.log '\n\t saveToAdmin update model success', el.title
cb null
, (err) ->
if err
self.errorLog "saveToAdmin async error", err
callback null
else
console.log '\n\t\tObjects checked: ', countChecked
self.db.overlapGroups.findOrCreate {
type : modelDB
models : duplicatesId
}, (err) ->
if err
self.errorLog "saveToAdmin findOrCreate error", err
callback null
else
console.log '\t\tadmin counter:', countAdmin++
callback null
compareSymbols: (first, second, callback) ->
if second.length is 0 or first.length is 0
callback err
matrix = []
# increment along the first column of each row
i = 0
while i <= second.length
matrix[i] = [i]
i++
# increment each column in the first row
j = 0
while j <= first.length
matrix[0][j] = j
j++
# Fill in the rest of the matrix
i = 1
while i <= second.length
j = 1
while j <= first.length
if second.charAt(i - 1) is first.charAt(j - 1)
matrix[i][j] = matrix[i - 1][j - 1]
else
matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, Math.min(matrix[i][j - 1] + 1, matrix[i - 1][j] + 1))
j++
i++
overlapSymbol = matrix[second.length][first.length]
# convert to percent
percent = (first.length + second.length)/100
result = (100 - (overlapSymbol / percent)).toFixed(1)
# console.log "\n\tcompareSymbols \n\t\t__#{second}__ && __#{first}__\n\t\t\tresult: compare #{result}%"
callback null, result
errorLog: (info) ->
console.log "\n\n\t!! Error log: ", info
fs.appendFile 'logs/log_err/logErr_COMPARE', "\n #{'\n\n\t--------\n' + info}", ()->
console.log "\t-------------------------------"
module.exports = Compare